blob: d165305e052b78879250694f794017084c6f76c7 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010030# ifdef FEAT_EVAL
31 scid_T uc_scriptID; /* SID where the command was defined */
32# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035# endif
36} ucmd_T;
37
38#define UC_BUFFER 1 /* -buffer: local to current buffer */
39
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000040static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
43#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
44
45static void do_ucmd __ARGS((exarg_T *eap));
46static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000047static void ex_delcommand __ARGS((exarg_T *eap));
48# ifdef FEAT_CMDL_COMPL
49static char_u *get_user_command_name __ARGS((int idx));
50# endif
51
52#else
53# define ex_command ex_ni
54# define ex_comclear ex_ni
55# define ex_delcommand ex_ni
56#endif
57
58#ifdef FEAT_EVAL
Bram Moolenaar89d40322006-08-29 15:30:07 +000059static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#else
Bram Moolenaar89d40322006-08-29 15:30:07 +000061static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000062static int if_level = 0; /* depth in :if */
63#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static char_u *find_command __ARGS((exarg_T *eap, int *full));
65
66static void ex_abbreviate __ARGS((exarg_T *eap));
67static void ex_map __ARGS((exarg_T *eap));
68static void ex_unmap __ARGS((exarg_T *eap));
69static void ex_mapclear __ARGS((exarg_T *eap));
70static void ex_abclear __ARGS((exarg_T *eap));
71#ifndef FEAT_MENU
72# define ex_emenu ex_ni
73# define ex_menu ex_ni
74# define ex_menutranslate ex_ni
75#endif
76#ifdef FEAT_AUTOCMD
77static void ex_autocmd __ARGS((exarg_T *eap));
78static void ex_doautocmd __ARGS((exarg_T *eap));
79#else
80# define ex_autocmd ex_ni
81# define ex_doautocmd ex_ni
82# define ex_doautoall ex_ni
83#endif
84#ifdef FEAT_LISTCMDS
85static void ex_bunload __ARGS((exarg_T *eap));
86static void ex_buffer __ARGS((exarg_T *eap));
87static void ex_bmodified __ARGS((exarg_T *eap));
88static void ex_bnext __ARGS((exarg_T *eap));
89static void ex_bprevious __ARGS((exarg_T *eap));
90static void ex_brewind __ARGS((exarg_T *eap));
91static void ex_blast __ARGS((exarg_T *eap));
92#else
93# define ex_bunload ex_ni
94# define ex_buffer ex_ni
95# define ex_bmodified ex_ni
96# define ex_bnext ex_ni
97# define ex_bprevious ex_ni
98# define ex_brewind ex_ni
99# define ex_blast ex_ni
100# define buflist_list ex_ni
101# define ex_checktime ex_ni
102#endif
103#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
104# define ex_buffer_all ex_ni
105#endif
106static char_u *getargcmd __ARGS((char_u **));
107static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
108static int getargopt __ARGS((exarg_T *eap));
109#ifndef FEAT_QUICKFIX
110# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000111# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112# define ex_cc ex_ni
113# define ex_cnext ex_ni
114# define ex_cfile ex_ni
115# define qf_list ex_ni
116# define qf_age ex_ni
117# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000118# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#endif
120#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
121# define ex_cclose ex_ni
122# define ex_copen ex_ni
123# define ex_cwindow ex_ni
124#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000125#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
126# define ex_cexpr ex_ni
127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129static int check_more __ARGS((int, int));
130static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000131static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000133 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000134# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135static void ex_script_ni __ARGS((exarg_T *eap));
136#endif
137static char_u *invalid_range __ARGS((exarg_T *eap));
138static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000139#ifdef FEAT_QUICKFIX
140static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
143static void ex_highlight __ARGS((exarg_T *eap));
144static void ex_colorscheme __ARGS((exarg_T *eap));
145static void ex_quit __ARGS((exarg_T *eap));
146static void ex_cquit __ARGS((exarg_T *eap));
147static void ex_quit_all __ARGS((exarg_T *eap));
148#ifdef FEAT_WINDOWS
149static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000150static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152static void ex_resize __ARGS((exarg_T *eap));
153static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000154static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000155static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000156static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000157static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000158static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#else
160# define ex_close ex_ni
161# define ex_only ex_ni
162# define ex_all ex_ni
163# define ex_resize ex_ni
164# define ex_splitview ex_ni
165# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000166# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000167# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000168# define ex_tabs ex_ni
169# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000170# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#endif
172#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
173static void ex_pclose __ARGS((exarg_T *eap));
174static void ex_ptag __ARGS((exarg_T *eap));
175static void ex_pedit __ARGS((exarg_T *eap));
176#else
177# define ex_pclose ex_ni
178# define ex_ptag ex_ni
179# define ex_pedit ex_ni
180#endif
181static void ex_hide __ARGS((exarg_T *eap));
182static void ex_stop __ARGS((exarg_T *eap));
183static void ex_exit __ARGS((exarg_T *eap));
184static void ex_print __ARGS((exarg_T *eap));
185#ifdef FEAT_BYTEOFF
186static void ex_goto __ARGS((exarg_T *eap));
187#else
188# define ex_goto ex_ni
189#endif
190static void ex_shell __ARGS((exarg_T *eap));
191static void ex_preserve __ARGS((exarg_T *eap));
192static void ex_recover __ARGS((exarg_T *eap));
193#ifndef FEAT_LISTCMDS
194# define ex_argedit ex_ni
195# define ex_argadd ex_ni
196# define ex_argdelete ex_ni
197# define ex_listdo ex_ni
198#endif
199static void ex_mode __ARGS((exarg_T *eap));
200static void ex_wrongmodifier __ARGS((exarg_T *eap));
201static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000202static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203static void ex_edit __ARGS((exarg_T *eap));
204#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
205# define ex_drop ex_ni
206#endif
207#ifndef FEAT_GUI
208# define ex_gui ex_nogui
209static void ex_nogui __ARGS((exarg_T *eap));
210#endif
211#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
212static void ex_tearoff __ARGS((exarg_T *eap));
213#else
214# define ex_tearoff ex_ni
215#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000216#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217static void ex_popup __ARGS((exarg_T *eap));
218#else
219# define ex_popup ex_ni
220#endif
221#ifndef FEAT_GUI_MSWIN
222# define ex_simalt ex_ni
223#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000224#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225# define gui_mch_find_dialog ex_ni
226# define gui_mch_replace_dialog ex_ni
227#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000228#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229# define ex_helpfind ex_ni
230#endif
231#ifndef FEAT_CSCOPE
232# define do_cscope ex_ni
233# define do_scscope ex_ni
234# define do_cstag ex_ni
235#endif
236#ifndef FEAT_SYN_HL
237# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200238# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000239#endif
240#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000241# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000242# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000243# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000244# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000245# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000246#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200247#ifndef FEAT_PERSISTENT_UNDO
248# define ex_rundo ex_ni
249# define ex_wundo ex_ni
250#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000251#ifndef FEAT_MZSCHEME
252# define ex_mzscheme ex_script_ni
253# define ex_mzfile ex_ni
254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255#ifndef FEAT_PERL
256# define ex_perl ex_script_ni
257# define ex_perldo ex_ni
258#endif
259#ifndef FEAT_PYTHON
260# define ex_python ex_script_ni
261# define ex_pyfile ex_ni
262#endif
263#ifndef FEAT_TCL
264# define ex_tcl ex_script_ni
265# define ex_tcldo ex_ni
266# define ex_tclfile ex_ni
267#endif
268#ifndef FEAT_RUBY
269# define ex_ruby ex_script_ni
270# define ex_rubydo ex_ni
271# define ex_rubyfile ex_ni
272#endif
273#ifndef FEAT_SNIFF
274# define ex_sniff ex_ni
275#endif
276#ifndef FEAT_KEYMAP
277# define ex_loadkeymap ex_ni
278#endif
279static void ex_swapname __ARGS((exarg_T *eap));
280static void ex_syncbind __ARGS((exarg_T *eap));
281static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282static void ex_pwd __ARGS((exarg_T *eap));
283static void ex_equal __ARGS((exarg_T *eap));
284static void ex_sleep __ARGS((exarg_T *eap));
285static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
286static void ex_winsize __ARGS((exarg_T *eap));
287#ifdef FEAT_WINDOWS
288static void ex_wincmd __ARGS((exarg_T *eap));
289#else
290# define ex_wincmd ex_ni
291#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000292#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293static void ex_winpos __ARGS((exarg_T *eap));
294#else
295# define ex_winpos ex_ni
296#endif
297static void ex_operators __ARGS((exarg_T *eap));
298static void ex_put __ARGS((exarg_T *eap));
299static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000300static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301static void ex_submagic __ARGS((exarg_T *eap));
302static void ex_join __ARGS((exarg_T *eap));
303static void ex_at __ARGS((exarg_T *eap));
304static void ex_bang __ARGS((exarg_T *eap));
305static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200306#ifdef FEAT_PERSISTENT_UNDO
307static void ex_wundo __ARGS((exarg_T *eap));
308static void ex_rundo __ARGS((exarg_T *eap));
309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000311static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312static void ex_redir __ARGS((exarg_T *eap));
313static void ex_redraw __ARGS((exarg_T *eap));
314static void ex_redrawstatus __ARGS((exarg_T *eap));
315static void close_redir __ARGS((void));
316static void ex_mkrc __ARGS((exarg_T *eap));
317static void ex_mark __ARGS((exarg_T *eap));
318#ifdef FEAT_USR_CMDS
319static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000320static char_u *find_ucmd __ARGS((exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321#endif
322#ifdef FEAT_EX_EXTRA
323static void ex_normal __ARGS((exarg_T *eap));
324static void ex_startinsert __ARGS((exarg_T *eap));
325static void ex_stopinsert __ARGS((exarg_T *eap));
326#else
327# define ex_normal ex_ni
328# define ex_align ex_ni
329# define ex_retab ex_ni
330# define ex_startinsert ex_ni
331# define ex_stopinsert ex_ni
332# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000333# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#endif
335#ifdef FEAT_FIND_ID
336static void ex_checkpath __ARGS((exarg_T *eap));
337static void ex_findpat __ARGS((exarg_T *eap));
338#else
339# define ex_findpat ex_ni
340# define ex_checkpath ex_ni
341#endif
342#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
343static void ex_psearch __ARGS((exarg_T *eap));
344#else
345# define ex_psearch ex_ni
346#endif
347static void ex_tag __ARGS((exarg_T *eap));
348static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
349#ifndef FEAT_EVAL
350# define ex_scriptnames ex_ni
351# define ex_finish ex_ni
352# define ex_echo ex_ni
353# define ex_echohl ex_ni
354# define ex_execute ex_ni
355# define ex_call ex_ni
356# define ex_if ex_ni
357# define ex_endif ex_ni
358# define ex_else ex_ni
359# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000360# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361# define ex_continue ex_ni
362# define ex_break ex_ni
363# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000364# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365# define ex_throw ex_ni
366# define ex_try ex_ni
367# define ex_catch ex_ni
368# define ex_finally ex_ni
369# define ex_endtry ex_ni
370# define ex_endfunction ex_ni
371# define ex_let ex_ni
372# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000373# define ex_lockvar ex_ni
374# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375# define ex_function ex_ni
376# define ex_delfunction ex_ni
377# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000378# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379#endif
380static char_u *arg_all __ARGS((void));
381#ifdef FEAT_SESSION
382static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000383static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static void ex_loadview __ARGS((exarg_T *eap));
385static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000386static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#else
388# define ex_loadview ex_ni
389#endif
390#ifndef FEAT_EVAL
391# define ex_compiler ex_ni
392#endif
393#ifdef FEAT_VIMINFO
394static void ex_viminfo __ARGS((exarg_T *eap));
395#else
396# define ex_viminfo ex_ni
397#endif
398static void ex_behave __ARGS((exarg_T *eap));
399#ifdef FEAT_AUTOCMD
400static void ex_filetype __ARGS((exarg_T *eap));
401static void ex_setfiletype __ARGS((exarg_T *eap));
402#else
403# define ex_filetype ex_ni
404# define ex_setfiletype ex_ni
405#endif
406#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000407# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408# define ex_diffpatch ex_ni
409# define ex_diffgetput ex_ni
410# define ex_diffsplit ex_ni
411# define ex_diffthis ex_ni
412# define ex_diffupdate ex_ni
413#endif
414static void ex_digraphs __ARGS((exarg_T *eap));
415static void ex_set __ARGS((exarg_T *eap));
416#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
417# define ex_options ex_ni
418#endif
419#ifdef FEAT_SEARCH_EXTRA
420static void ex_nohlsearch __ARGS((exarg_T *eap));
421static void ex_match __ARGS((exarg_T *eap));
422#else
423# define ex_nohlsearch ex_ni
424# define ex_match ex_ni
425#endif
426#ifdef FEAT_CRYPT
427static void ex_X __ARGS((exarg_T *eap));
428#else
429# define ex_X ex_ni
430#endif
431#ifdef FEAT_FOLDING
432static void ex_fold __ARGS((exarg_T *eap));
433static void ex_foldopen __ARGS((exarg_T *eap));
434static void ex_folddo __ARGS((exarg_T *eap));
435#else
436# define ex_fold ex_ni
437# define ex_foldopen ex_ni
438# define ex_folddo ex_ni
439#endif
440#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
441 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
442# define ex_language ex_ni
443#endif
444#ifndef FEAT_SIGNS
445# define ex_sign ex_ni
446#endif
447#ifndef FEAT_SUN_WORKSHOP
448# define ex_wsverb ex_ni
449#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000450#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200451# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000452# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200453# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455
456#ifndef FEAT_EVAL
457# define ex_debug ex_ni
458# define ex_breakadd ex_ni
459# define ex_debuggreedy ex_ni
460# define ex_breakdel ex_ni
461# define ex_breaklist ex_ni
462#endif
463
464#ifndef FEAT_CMDHIST
465# define ex_history ex_ni
466#endif
467#ifndef FEAT_JUMPLIST
468# define ex_jumps ex_ni
469# define ex_changes ex_ni
470#endif
471
Bram Moolenaar05159a02005-02-26 23:04:13 +0000472#ifndef FEAT_PROFILE
473# define ex_profile ex_ni
474#endif
475
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476/*
477 * Declare cmdnames[].
478 */
479#define DO_DECLARE_EXCMD
480#include "ex_cmds.h"
481
482/*
483 * Table used to quickly search for a command, based on its first character.
484 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000485static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486{
487 CMD_append,
488 CMD_buffer,
489 CMD_change,
490 CMD_delete,
491 CMD_edit,
492 CMD_file,
493 CMD_global,
494 CMD_help,
495 CMD_insert,
496 CMD_join,
497 CMD_k,
498 CMD_list,
499 CMD_move,
500 CMD_next,
501 CMD_open,
502 CMD_print,
503 CMD_quit,
504 CMD_read,
505 CMD_substitute,
506 CMD_t,
507 CMD_undo,
508 CMD_vglobal,
509 CMD_write,
510 CMD_xit,
511 CMD_yank,
512 CMD_z,
513 CMD_bang
514};
515
516static char_u dollar_command[2] = {'$', 0};
517
518
519#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000520/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521typedef struct
522{
523 char_u *line; /* command line */
524 linenr_T lnum; /* sourcing_lnum of the line */
525} wcmd_T;
526
527/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000528 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000530 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000532struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533{
534 garray_T *lines_gap; /* growarray with line info */
535 int current_line; /* last read line from growarray */
536 int repeating; /* TRUE when looping a second time */
537 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
538 char_u *(*getline) __ARGS((int, void *, int));
539 void *cookie;
540};
541
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000542static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
543static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000545
546/* Struct to save a few things while debugging. Used in do_cmdline() only. */
547struct dbg_stuff
548{
549 int trylevel;
550 int force_abort;
551 except_T *caught_stack;
552 char_u *vv_exception;
553 char_u *vv_throwpoint;
554 int did_emsg;
555 int got_int;
556 int did_throw;
557 int need_rethrow;
558 int check_cstack;
559 except_T *current_exception;
560};
561
562static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
563static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
564
565 static void
566save_dbg_stuff(dsp)
567 struct dbg_stuff *dsp;
568{
569 dsp->trylevel = trylevel; trylevel = 0;
570 dsp->force_abort = force_abort; force_abort = FALSE;
571 dsp->caught_stack = caught_stack; caught_stack = NULL;
572 dsp->vv_exception = v_exception(NULL);
573 dsp->vv_throwpoint = v_throwpoint(NULL);
574
575 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
576 dsp->did_emsg = did_emsg; did_emsg = FALSE;
577 dsp->got_int = got_int; got_int = FALSE;
578 dsp->did_throw = did_throw; did_throw = FALSE;
579 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
580 dsp->check_cstack = check_cstack; check_cstack = FALSE;
581 dsp->current_exception = current_exception; current_exception = NULL;
582}
583
584 static void
585restore_dbg_stuff(dsp)
586 struct dbg_stuff *dsp;
587{
588 suppress_errthrow = FALSE;
589 trylevel = dsp->trylevel;
590 force_abort = dsp->force_abort;
591 caught_stack = dsp->caught_stack;
592 (void)v_exception(dsp->vv_exception);
593 (void)v_throwpoint(dsp->vv_throwpoint);
594 did_emsg = dsp->did_emsg;
595 got_int = dsp->got_int;
596 did_throw = dsp->did_throw;
597 need_rethrow = dsp->need_rethrow;
598 check_cstack = dsp->check_cstack;
599 current_exception = dsp->current_exception;
600}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601#endif
602
603
604/*
605 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
606 * command is given.
607 */
608 void
609do_exmode(improved)
610 int improved; /* TRUE for "improved Ex" mode */
611{
612 int save_msg_scroll;
613 int prev_msg_row;
614 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000615 int changedtick;
616
617 if (improved)
618 exmode_active = EXMODE_VIM;
619 else
620 exmode_active = EXMODE_NORMAL;
621 State = NORMAL;
622
623 /* When using ":global /pat/ visual" and then "Q" we return to continue
624 * the :global command. */
625 if (global_busy)
626 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627
628 save_msg_scroll = msg_scroll;
629 ++RedrawingDisabled; /* don't redisplay the window */
630 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631#ifdef FEAT_GUI
632 /* Ignore scrollbar and mouse events in Ex mode */
633 ++hold_gui_events;
634#endif
635#ifdef FEAT_SNIFF
636 want_sniff_request = 0; /* No K_SNIFF wanted */
637#endif
638
639 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
640 while (exmode_active)
641 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000642#ifdef FEAT_EX_EXTRA
643 /* Check for a ":normal" command and no more characters left. */
644 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
645 {
646 exmode_active = FALSE;
647 break;
648 }
649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 msg_scroll = TRUE;
651 need_wait_return = FALSE;
652 ex_pressedreturn = FALSE;
653 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000654 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 prev_msg_row = msg_row;
656 prev_line = curwin->w_cursor.lnum;
657#ifdef FEAT_SNIFF
658 ProcessSniffRequests();
659#endif
660 if (improved)
661 {
662 cmdline_row = msg_row;
663 do_cmdline(NULL, getexline, NULL, 0);
664 }
665 else
666 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
667 lines_left = Rows - 1;
668
Bram Moolenaardf177f62005-02-22 08:39:57 +0000669 if ((prev_line != curwin->w_cursor.lnum
670 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000672 if (curbuf->b_ml.ml_flags & ML_EMPTY)
673 EMSG(_(e_emptybuf));
674 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000676 if (ex_pressedreturn)
677 {
678 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000679 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000680 msg_row = prev_msg_row;
681 if (prev_msg_row == Rows - 1)
682 msg_row--;
683 }
684 msg_col = 0;
685 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
686 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000689 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
690 {
691 if (curbuf->b_ml.ml_flags & ML_EMPTY)
692 EMSG(_(e_emptybuf));
693 else
694 EMSG(_("E501: At end-of-file"));
695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 }
697
698#ifdef FEAT_GUI
699 --hold_gui_events;
700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 --RedrawingDisabled;
702 --no_wait_return;
703 update_screen(CLEAR);
704 need_wait_return = FALSE;
705 msg_scroll = save_msg_scroll;
706}
707
708/*
709 * Execute a simple command line. Used for translated commands like "*".
710 */
711 int
712do_cmdline_cmd(cmd)
713 char_u *cmd;
714{
715 return do_cmdline(cmd, NULL, NULL,
716 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
717}
718
719/*
720 * do_cmdline(): execute one Ex command line
721 *
722 * 1. Execute "cmdline" when it is not NULL.
723 * If "cmdline" is NULL, or more lines are needed, getline() is used.
724 * 2. Split up in parts separated with '|'.
725 *
726 * This function can be called recursively!
727 *
728 * flags:
729 * DOCMD_VERBOSE - The command will be included in the error message.
730 * DOCMD_NOWAIT - Don't call wait_return() and friends.
731 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
732 * DOCMD_KEYTYPED - Don't reset KeyTyped.
733 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
734 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
735 *
736 * return FAIL if cmdline could not be executed, OK otherwise
737 */
738 int
739do_cmdline(cmdline, getline, cookie, flags)
740 char_u *cmdline;
741 char_u *(*getline) __ARGS((int, void *, int));
742 void *cookie; /* argument for getline() */
743 int flags;
744{
745 char_u *next_cmdline; /* next cmd to execute */
746 char_u *cmdline_copy = NULL; /* copy of cmd line */
747 int used_getline = FALSE; /* used "getline" to obtain command */
748 static int recursive = 0; /* recursive depth */
749 int msg_didout_before_start = 0;
750 int count = 0; /* line number count */
751 int did_inc = FALSE; /* incremented RedrawingDisabled */
752 int retval = OK;
753#ifdef FEAT_EVAL
754 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000755 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 int current_line = 0; /* active line in lines_ga */
757 char_u *fname = NULL; /* function or script name */
758 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
759 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000760 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 int initial_trylevel;
762 struct msglist **saved_msg_list = NULL;
763 struct msglist *private_msg_list;
764
765 /* "getline" and "cookie" passed to do_one_cmd() */
766 char_u *(*cmd_getline) __ARGS((int, void *, int));
767 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000768 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#else
772# define cmd_getline getline
773# define cmd_cookie cookie
774#endif
775 static int call_depth = 0; /* recursiveness */
776
777#ifdef FEAT_EVAL
778 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
779 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000780 * This ensures that the do_errthrow() call in do_one_cmd() does not
781 * combine the messages stored by an earlier invocation of do_one_cmd()
782 * with the command name of the later one. This would happen when
783 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 saved_msg_list = msg_list;
785 msg_list = &private_msg_list;
786 private_msg_list = NULL;
787#endif
788
789 /* It's possible to create an endless loop with ":execute", catch that
790 * here. The value of 200 allows nested function calls, ":source", etc. */
791 if (call_depth == 200)
792 {
793 EMSG(_("E169: Command too recursive"));
794#ifdef FEAT_EVAL
795 /* When converting to an exception, we do not include the command name
796 * since this is not an error of the specific command. */
797 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
798 msg_list = saved_msg_list;
799#endif
800 return FAIL;
801 }
802 ++call_depth;
803
804#ifdef FEAT_EVAL
805 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000806 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 cstack.cs_trylevel = 0;
808 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000809 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
811
812 real_cookie = getline_cookie(getline, cookie);
813
814 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815 getline_is_func = getline_equal(getline, cookie, get_func_line);
816 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 ++ex_nesting_level;
818
819 /* Get the function or script name and the address where the next breakpoint
820 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000821 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 {
823 fname = func_name(real_cookie);
824 breakpoint = func_breakpoint(real_cookie);
825 dbg_tick = func_dbg_tick(real_cookie);
826 }
827 else if (getline_equal(getline, cookie, getsourceline))
828 {
829 fname = sourcing_name;
830 breakpoint = source_breakpoint(real_cookie);
831 dbg_tick = source_dbg_tick(real_cookie);
832 }
833
834 /*
835 * Initialize "force_abort" and "suppress_errthrow" at the top level.
836 */
837 if (!recursive)
838 {
839 force_abort = FALSE;
840 suppress_errthrow = FALSE;
841 }
842
843 /*
844 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000845 * exception handling (used when debugging). Otherwise clear it to avoid
846 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000848 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000849 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000850 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200851 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
853 initial_trylevel = trylevel;
854
855 /*
856 * "did_throw" will be set to TRUE when an exception is being thrown.
857 */
858 did_throw = FALSE;
859#endif
860 /*
861 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000862 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 * If force_abort is set, we cancel everything.
864 */
865 did_emsg = FALSE;
866
867 /*
868 * KeyTyped is only set when calling vgetc(). Reset it here when not
869 * calling vgetc() (sourced command lines).
870 */
871 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
872 KeyTyped = FALSE;
873
874 /*
875 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000876 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 * - for multiple commands on one line, separated with '|'
878 * - when repeating until there are no more lines (for ":source")
879 */
880 next_cmdline = cmdline;
881 do
882 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000883#ifdef FEAT_EVAL
884 getline_is_func = getline_equal(getline, cookie, get_func_line);
885#endif
886
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000887 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 if (next_cmdline == NULL
889#ifdef FEAT_EVAL
890 && !force_abort
891 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000892 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893#endif
894 )
895 did_emsg = FALSE;
896
897 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000898 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 * 2. If no line given: Get an allocated line with getline().
900 * 3. If a line is given: Make a copy, so we can mess with it.
901 */
902
903#ifdef FEAT_EVAL
904 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000905 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 {
907 /* Each '|' separated command is stored separately in lines_ga, to
908 * be able to jump to it. Don't use next_cmdline now. */
909 vim_free(cmdline_copy);
910 cmdline_copy = NULL;
911
912 /* Check if a function has returned or, unless it has an unclosed
913 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000914 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000916# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000917 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000918 func_line_end(real_cookie);
919# endif
920 if (func_has_ended(real_cookie))
921 {
922 retval = FAIL;
923 break;
924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000926#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000927 else if (do_profiling == PROF_YES
Bram Moolenaar05159a02005-02-26 23:04:13 +0000928 && getline_equal(getline, cookie, getsourceline))
929 script_line_end();
930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
932 /* Check if a sourced file hit a ":finish" command. */
933 if (source_finished(getline, cookie))
934 {
935 retval = FAIL;
936 break;
937 }
938
939 /* If breakpoints have been added/deleted need to check for it. */
940 if (breakpoint != NULL && dbg_tick != NULL
941 && *dbg_tick != debug_tick)
942 {
943 *breakpoint = dbg_find_breakpoint(
944 getline_equal(getline, cookie, getsourceline),
945 fname, sourcing_lnum);
946 *dbg_tick = debug_tick;
947 }
948
949 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
950 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
951
952 /* Did we encounter a breakpoint? */
953 if (breakpoint != NULL && *breakpoint != 0
954 && *breakpoint <= sourcing_lnum)
955 {
956 dbg_breakpoint(fname, sourcing_lnum);
957 /* Find next breakpoint. */
958 *breakpoint = dbg_find_breakpoint(
959 getline_equal(getline, cookie, getsourceline),
960 fname, sourcing_lnum);
961 *dbg_tick = debug_tick;
962 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000963# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000964 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000965 {
966 if (getline_is_func)
967 func_line_start(real_cookie);
968 else if (getline_equal(getline, cookie, getsourceline))
969 script_line_start();
970 }
971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 }
973
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000974 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000976 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 * again. Pass a different "getline" function to do_one_cmd()
978 * below, so that it stores lines in or reads them from
979 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000980 * while/for loop. */
981 cmd_getline = get_loop_line;
982 cmd_cookie = (void *)&cmd_loop_cookie;
983 cmd_loop_cookie.lines_gap = &lines_ga;
984 cmd_loop_cookie.current_line = current_line;
985 cmd_loop_cookie.getline = getline;
986 cmd_loop_cookie.cookie = cookie;
987 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 }
989 else
990 {
991 cmd_getline = getline;
992 cmd_cookie = cookie;
993 }
994#endif
995
996 /* 2. If no line given, get an allocated line with getline(). */
997 if (next_cmdline == NULL)
998 {
999 /*
1000 * Need to set msg_didout for the first line after an ":if",
1001 * otherwise the ":if" will be overwritten.
1002 */
1003 if (count == 1 && getline_equal(getline, cookie, getexline))
1004 msg_didout = TRUE;
1005 if (getline == NULL || (next_cmdline = getline(':', cookie,
1006#ifdef FEAT_EVAL
1007 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1008#else
1009 0
1010#endif
1011 )) == NULL)
1012 {
1013 /* Don't call wait_return for aborted command line. The NULL
1014 * returned for the end of a sourced file or executed function
1015 * doesn't do this. */
1016 if (KeyTyped && !(flags & DOCMD_REPEAT))
1017 need_wait_return = FALSE;
1018 retval = FAIL;
1019 break;
1020 }
1021 used_getline = TRUE;
1022
1023 /*
1024 * Keep the first typed line. Clear it when more lines are typed.
1025 */
1026 if (flags & DOCMD_KEEPLINE)
1027 {
1028 vim_free(repeat_cmdline);
1029 if (count == 0)
1030 repeat_cmdline = vim_strsave(next_cmdline);
1031 else
1032 repeat_cmdline = NULL;
1033 }
1034 }
1035
1036 /* 3. Make a copy of the command so we can mess with it. */
1037 else if (cmdline_copy == NULL)
1038 {
1039 next_cmdline = vim_strsave(next_cmdline);
1040 if (next_cmdline == NULL)
1041 {
1042 EMSG(_(e_outofmem));
1043 retval = FAIL;
1044 break;
1045 }
1046 }
1047 cmdline_copy = next_cmdline;
1048
1049#ifdef FEAT_EVAL
1050 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 * Save the current line when inside a ":while" or ":for", and when
1052 * the command looks like a ":while" or ":for", because we may need it
1053 * later. When there is a '|' and another command, it is stored
1054 * separately, because we need to be able to jump back to it from an
1055 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001057 if (current_line == lines_ga.ga_len
1058 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001060 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 {
1062 retval = FAIL;
1063 break;
1064 }
1065 }
1066 did_endif = FALSE;
1067#endif
1068
1069 if (count++ == 0)
1070 {
1071 /*
1072 * All output from the commands is put below each other, without
1073 * waiting for a return. Don't do this when executing commands
1074 * from a script or when being called recursive (e.g. for ":e
1075 * +command file").
1076 */
1077 if (!(flags & DOCMD_NOWAIT) && !recursive)
1078 {
1079 msg_didout_before_start = msg_didout;
1080 msg_didany = FALSE; /* no output yet */
1081 msg_start();
1082 msg_scroll = TRUE; /* put messages below each other */
1083 ++no_wait_return; /* dont wait for return until finished */
1084 ++RedrawingDisabled;
1085 did_inc = TRUE;
1086 }
1087 }
1088
1089 if (p_verbose >= 15 && sourcing_name != NULL)
1090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001092 verbose_enter_scroll();
1093
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 smsg((char_u *)_("line %ld: %s"),
1095 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001096 if (msg_silent == 0)
1097 msg_puts((char_u *)"\n"); /* don't overwrite this */
1098
1099 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 --no_wait_return;
1101 }
1102
1103 /*
1104 * 2. Execute one '|' separated command.
1105 * do_one_cmd() will return NULL if there is no trailing '|'.
1106 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1107 */
1108 ++recursive;
1109 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1110#ifdef FEAT_EVAL
1111 &cstack,
1112#endif
1113 cmd_getline, cmd_cookie);
1114 --recursive;
1115
1116#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001117 if (cmd_cookie == (void *)&cmd_loop_cookie)
1118 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001120 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121#endif
1122
1123 if (next_cmdline == NULL)
1124 {
1125 vim_free(cmdline_copy);
1126 cmdline_copy = NULL;
1127#ifdef FEAT_CMDHIST
1128 /*
1129 * If the command was typed, remember it for the ':' register.
1130 * Do this AFTER executing the command to make :@: work.
1131 */
1132 if (getline_equal(getline, cookie, getexline)
1133 && new_last_cmdline != NULL)
1134 {
1135 vim_free(last_cmdline);
1136 last_cmdline = new_last_cmdline;
1137 new_last_cmdline = NULL;
1138 }
1139#endif
1140 }
1141 else
1142 {
1143 /* need to copy the command after the '|' to cmdline_copy, for the
1144 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001145 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 next_cmdline = cmdline_copy;
1147 }
1148
1149
1150#ifdef FEAT_EVAL
1151 /* reset did_emsg for a function that is not aborted by an error */
1152 if (did_emsg && !force_abort
1153 && getline_equal(getline, cookie, get_func_line)
1154 && !func_has_abort(real_cookie))
1155 did_emsg = FALSE;
1156
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001157 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 {
1159 ++current_line;
1160
1161 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001162 * An ":endwhile", ":endfor" and ":continue" is handled here.
1163 * If we were executing commands, jump back to the ":while" or
1164 * ":for".
1165 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001167 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001169 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001171 /* Jump back to the matching ":while" or ":for". Be careful
1172 * not to use a cs_line[] from an entry that isn't a ":while"
1173 * or ":for": It would make "current_line" invalid and can
1174 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 if (!did_emsg && !got_int && !did_throw
1176 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001177 && (cstack.cs_flags[cstack.cs_idx]
1178 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 && cstack.cs_line[cstack.cs_idx] >= 0
1180 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1181 {
1182 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001183 /* remember we jumped there */
1184 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 line_breakcheck(); /* check if CTRL-C typed */
1186
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001187 /* Check for the next breakpoint at or after the ":while"
1188 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (breakpoint != NULL)
1190 {
1191 *breakpoint = dbg_find_breakpoint(
1192 getline_equal(getline, cookie, getsourceline),
1193 fname,
1194 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1195 *dbg_tick = debug_tick;
1196 }
1197 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001198 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001202 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1203 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205 }
1206
1207 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001208 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001210 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001212 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1214 }
1215 }
1216
1217 /*
1218 * When not inside any ":while" loop, clear remembered lines.
1219 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001220 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 {
1222 if (lines_ga.ga_len > 0)
1223 {
1224 sourcing_lnum =
1225 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1226 free_cmdlines(&lines_ga);
1227 }
1228 current_line = 0;
1229 }
1230
1231 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001232 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1233 * being restored at the ":endtry". Reset them here and set the
1234 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1235 * This includes the case where a missing ":endif", ":endwhile" or
1236 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001238 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001240 cstack.cs_lflags &= ~CSL_HAD_FINA;
1241 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1242 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 did_throw ? (void *)current_exception : NULL);
1244 did_emsg = got_int = did_throw = FALSE;
1245 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1246 }
1247
1248 /* Update global "trylevel" for recursive calls to do_cmdline() from
1249 * within this loop. */
1250 trylevel = initial_trylevel + cstack.cs_trylevel;
1251
1252 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001253 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 * files) is aborted because of an error, an interrupt, or an uncaught
1255 * exception, cancel everything. If it is left normally, reset
1256 * force_abort to get the non-EH compatible abortion behavior for
1257 * the rest of the script.
1258 */
1259 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1260 force_abort = FALSE;
1261
1262 /* Convert an interrupt to an exception if appropriate. */
1263 (void)do_intthrow(&cstack);
1264#endif /* FEAT_EVAL */
1265
1266 }
1267 /*
1268 * Continue executing command lines when:
1269 * - no CTRL-C typed, no aborting error, no exception thrown or try
1270 * conditionals need to be checked for executing finally clauses or
1271 * catching an interrupt exception
1272 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001273 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 * looping for ":source" command or function call.
1275 */
1276 while (!((got_int
1277#ifdef FEAT_EVAL
1278 || (did_emsg && force_abort) || did_throw
1279#endif
1280 )
1281#ifdef FEAT_EVAL
1282 && cstack.cs_trylevel == 0
1283#endif
1284 )
1285 && !(did_emsg && used_getline
1286 && (getline_equal(getline, cookie, getexmodeline)
1287 || getline_equal(getline, cookie, getexline)))
1288 && (next_cmdline != NULL
1289#ifdef FEAT_EVAL
1290 || cstack.cs_idx >= 0
1291#endif
1292 || (flags & DOCMD_REPEAT)));
1293
1294 vim_free(cmdline_copy);
1295#ifdef FEAT_EVAL
1296 free_cmdlines(&lines_ga);
1297 ga_clear(&lines_ga);
1298
1299 if (cstack.cs_idx >= 0)
1300 {
1301 /*
1302 * If a sourced file or executed function ran to its end, report the
1303 * unclosed conditional.
1304 */
1305 if (!got_int && !did_throw
1306 && ((getline_equal(getline, cookie, getsourceline)
1307 && !source_finished(getline, cookie))
1308 || (getline_equal(getline, cookie, get_func_line)
1309 && !func_has_ended(real_cookie))))
1310 {
1311 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1312 EMSG(_(e_endtry));
1313 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1314 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001315 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1316 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 else
1318 EMSG(_(e_endif));
1319 }
1320
1321 /*
1322 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1323 * ":endtry" in a sourced file or executed function. If the try
1324 * conditional is in its finally clause, ignore anything pending.
1325 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001326 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 */
1328 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001329 {
1330 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1331
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001332 if (idx >= 0)
1333 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001334 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1335 &cstack.cs_looplevel);
1336 }
1337 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 trylevel = initial_trylevel;
1339 }
1340
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001341 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1342 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 * exception, do this now after rewinding the cstack. */
1344 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1345 ? (char_u *)"endfunction" : (char_u *)NULL);
1346
1347 if (trylevel == 0)
1348 {
1349 /*
1350 * When an exception is being thrown out of the outermost try
1351 * conditional, discard the uncaught exception, disable the conversion
1352 * of interrupts or errors to exceptions, and ensure that no more
1353 * commands are executed.
1354 */
1355 if (did_throw)
1356 {
1357 void *p = NULL;
1358 char_u *saved_sourcing_name;
1359 int saved_sourcing_lnum;
1360 struct msglist *messages = NULL, *next;
1361
1362 /*
1363 * If the uncaught exception is a user exception, report it as an
1364 * error. If it is an error exception, display the saved error
1365 * message now. For an interrupt exception, do nothing; the
1366 * interrupt message is given elsewhere.
1367 */
1368 switch (current_exception->type)
1369 {
1370 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001371 vim_snprintf((char *)IObuff, IOSIZE,
1372 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 current_exception->value);
1374 p = vim_strsave(IObuff);
1375 break;
1376 case ET_ERROR:
1377 messages = current_exception->messages;
1378 current_exception->messages = NULL;
1379 break;
1380 case ET_INTERRUPT:
1381 break;
1382 default:
1383 p = vim_strsave((char_u *)_(e_internal));
1384 }
1385
1386 saved_sourcing_name = sourcing_name;
1387 saved_sourcing_lnum = sourcing_lnum;
1388 sourcing_name = current_exception->throw_name;
1389 sourcing_lnum = current_exception->throw_lnum;
1390 current_exception->throw_name = NULL;
1391
1392 discard_current_exception(); /* uses IObuff if 'verbose' */
1393 suppress_errthrow = TRUE;
1394 force_abort = TRUE;
1395
1396 if (messages != NULL)
1397 {
1398 do
1399 {
1400 next = messages->next;
1401 emsg(messages->msg);
1402 vim_free(messages->msg);
1403 vim_free(messages);
1404 messages = next;
1405 }
1406 while (messages != NULL);
1407 }
1408 else if (p != NULL)
1409 {
1410 emsg(p);
1411 vim_free(p);
1412 }
1413 vim_free(sourcing_name);
1414 sourcing_name = saved_sourcing_name;
1415 sourcing_lnum = saved_sourcing_lnum;
1416 }
1417
1418 /*
1419 * On an interrupt or an aborting error not converted to an exception,
1420 * disable the conversion of errors to exceptions. (Interrupts are not
1421 * converted any more, here.) This enables also the interrupt message
1422 * when force_abort is set and did_emsg unset in case of an interrupt
1423 * from a finally clause after an error.
1424 */
1425 else if (got_int || (did_emsg && force_abort))
1426 suppress_errthrow = TRUE;
1427 }
1428
1429 /*
1430 * The current cstack will be freed when do_cmdline() returns. An uncaught
1431 * exception will have to be rethrown in the previous cstack. If a function
1432 * has just returned or a script file was just finished and the previous
1433 * cstack belongs to the same function or, respectively, script file, it
1434 * will have to be checked for finally clauses to be executed due to the
1435 * ":return" or ":finish". This is done in do_one_cmd().
1436 */
1437 if (did_throw)
1438 need_rethrow = TRUE;
1439 if ((getline_equal(getline, cookie, getsourceline)
1440 && ex_nesting_level > source_level(real_cookie))
1441 || (getline_equal(getline, cookie, get_func_line)
1442 && ex_nesting_level > func_level(real_cookie) + 1))
1443 {
1444 if (!did_throw)
1445 check_cstack = TRUE;
1446 }
1447 else
1448 {
1449 /* When leaving a function, reduce nesting level. */
1450 if (getline_equal(getline, cookie, get_func_line))
1451 --ex_nesting_level;
1452 /*
1453 * Go to debug mode when returning from a function in which we are
1454 * single-stepping.
1455 */
1456 if ((getline_equal(getline, cookie, getsourceline)
1457 || getline_equal(getline, cookie, get_func_line))
1458 && ex_nesting_level + 1 <= debug_break_level)
1459 do_debug(getline_equal(getline, cookie, getsourceline)
1460 ? (char_u *)_("End of sourced file")
1461 : (char_u *)_("End of function"));
1462 }
1463
1464 /*
1465 * Restore the exception environment (done after returning from the
1466 * debugger).
1467 */
1468 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001469 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470
1471 msg_list = saved_msg_list;
1472#endif /* FEAT_EVAL */
1473
1474 /*
1475 * If there was too much output to fit on the command line, ask the user to
1476 * hit return before redrawing the screen. With the ":global" command we do
1477 * this only once after the command is finished.
1478 */
1479 if (did_inc)
1480 {
1481 --RedrawingDisabled;
1482 --no_wait_return;
1483 msg_scroll = FALSE;
1484
1485 /*
1486 * When just finished an ":if"-":else" which was typed, no need to
1487 * wait for hit-return. Also for an error situation.
1488 */
1489 if (retval == FAIL
1490#ifdef FEAT_EVAL
1491 || (did_endif && KeyTyped && !did_emsg)
1492#endif
1493 )
1494 {
1495 need_wait_return = FALSE;
1496 msg_didany = FALSE; /* don't wait when restarting edit */
1497 }
1498 else if (need_wait_return)
1499 {
1500 /*
1501 * The msg_start() above clears msg_didout. The wait_return we do
1502 * here should not overwrite the command that may be shown before
1503 * doing that.
1504 */
1505 msg_didout |= msg_didout_before_start;
1506 wait_return(FALSE);
1507 }
1508 }
1509
1510#ifndef FEAT_EVAL
1511 /*
1512 * Reset if_level, in case a sourced script file contains more ":if" than
1513 * ":endif" (could be ":if x | foo | endif").
1514 */
1515 if_level = 0;
1516#endif
1517
1518 --call_depth;
1519 return retval;
1520}
1521
1522#ifdef FEAT_EVAL
1523/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001524 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 */
1526 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001527get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 int c;
1529 void *cookie;
1530 int indent;
1531{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001532 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 wcmd_T *wp;
1534 char_u *line;
1535
1536 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1537 {
1538 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001539 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001541 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 if (cp->getline == NULL)
1543 line = getcmdline(c, 0L, indent);
1544 else
1545 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001546 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 ++cp->current_line;
1548
1549 return line;
1550 }
1551
1552 KeyTyped = FALSE;
1553 ++cp->current_line;
1554 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1555 sourcing_lnum = wp->lnum;
1556 return vim_strsave(wp->line);
1557}
1558
1559/*
1560 * Store a line in "gap" so that a ":while" loop can execute it again.
1561 */
1562 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001563store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 garray_T *gap;
1565 char_u *line;
1566{
1567 if (ga_grow(gap, 1) == FAIL)
1568 return FAIL;
1569 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1570 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1571 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 return OK;
1573}
1574
1575/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001576 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 */
1578 static void
1579free_cmdlines(gap)
1580 garray_T *gap;
1581{
1582 while (gap->ga_len > 0)
1583 {
1584 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1585 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 }
1587}
1588#endif
1589
1590/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001591 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1592 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001595getline_equal(fgetline, cookie, func)
1596 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001597 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 char_u *(*func) __ARGS((int, void *, int));
1599{
1600#ifdef FEAT_EVAL
1601 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001602 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603
Bram Moolenaar89d40322006-08-29 15:30:07 +00001604 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001605 * function that's originally used to obtain the lines. This may be
1606 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001607 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001608 cp = (struct loop_cookie *)cookie;
1609 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
1611 gp = cp->getline;
1612 cp = cp->cookie;
1613 }
1614 return gp == func;
1615#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001616 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617#endif
1618}
1619
1620#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1621/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001622 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 * getline function. Otherwise return "cookie".
1624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001626getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001627 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001628 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629{
1630# ifdef FEAT_EVAL
1631 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001632 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
Bram Moolenaar89d40322006-08-29 15:30:07 +00001634 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001635 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001637 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001638 cp = (struct loop_cookie *)cookie;
1639 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 {
1641 gp = cp->getline;
1642 cp = cp->cookie;
1643 }
1644 return cp;
1645# else
1646 return cookie;
1647# endif
1648}
1649#endif
1650
1651/*
1652 * Execute one Ex command.
1653 *
1654 * If 'sourcing' is TRUE, the command will be included in the error message.
1655 *
1656 * 1. skip comment lines and leading space
1657 * 2. handle command modifiers
1658 * 3. parse range
1659 * 4. parse command
1660 * 5. parse arguments
1661 * 6. switch on command name
1662 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001663 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 *
1665 * This function may be called recursively!
1666 */
1667#if (_MSC_VER == 1200)
1668/*
Bram Moolenaared203462004-06-16 11:19:22 +00001669 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001671 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672#endif
1673 static char_u *
1674do_one_cmd(cmdlinep, sourcing,
1675#ifdef FEAT_EVAL
1676 cstack,
1677#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001678 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 char_u **cmdlinep;
1680 int sourcing;
1681#ifdef FEAT_EVAL
1682 struct condstack *cstack;
1683#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001684 char_u *(*fgetline) __ARGS((int, void *, int));
1685 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686{
1687 char_u *p;
1688 linenr_T lnum;
1689 long n;
1690 char_u *errormsg = NULL; /* error message */
1691 exarg_T ea; /* Ex command arguments */
1692 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001693 int save_msg_scroll = msg_scroll;
1694 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001696#ifdef HAVE_SANDBOX
1697 int did_sandbox = FALSE;
1698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 cmdmod_T save_cmdmod;
1700 int ni; /* set when Not Implemented */
1701
1702 vim_memset(&ea, 0, sizeof(ea));
1703 ea.line1 = 1;
1704 ea.line2 = 1;
1705#ifdef FEAT_EVAL
1706 ++ex_nesting_level;
1707#endif
1708
1709 /* when not editing the last file :q has to be typed twice */
1710 if (quitmore
1711#ifdef FEAT_EVAL
1712 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001713 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714#endif
1715 )
1716 --quitmore;
1717
1718 /*
1719 * Reset browse, confirm, etc.. They are restored when returning, for
1720 * recursive calls.
1721 */
1722 save_cmdmod = cmdmod;
1723 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1724
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001725 /* "#!anything" is handled like a comment. */
1726 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1727 goto doend;
1728
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 /*
1730 * Repeat until no more command modifiers are found.
1731 */
1732 ea.cmd = *cmdlinep;
1733 for (;;)
1734 {
1735/*
1736 * 1. skip comment lines and leading white space and colons
1737 */
1738 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1739 ++ea.cmd;
1740
1741 /* in ex mode, an empty line works like :+ */
1742 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001743 && (getline_equal(fgetline, cookie, getexmodeline)
1744 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001745 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 {
1747 ea.cmd = (char_u *)"+";
1748 ex_pressedreturn = TRUE;
1749 }
1750
1751 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001752 if (*ea.cmd == '"')
1753 goto doend;
1754 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001755 {
1756 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
1760/*
1761 * 2. handle command modifiers.
1762 */
1763 p = ea.cmd;
1764 if (VIM_ISDIGIT(*ea.cmd))
1765 p = skipwhite(skipdigits(ea.cmd));
1766 switch (*p)
1767 {
1768 /* When adding an entry, also modify cmd_exists(). */
1769 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1770 break;
1771#ifdef FEAT_WINDOWS
1772 cmdmod.split |= WSP_ABOVE;
1773#endif
1774 continue;
1775
1776 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1777 {
1778#ifdef FEAT_WINDOWS
1779 cmdmod.split |= WSP_BELOW;
1780#endif
1781 continue;
1782 }
1783 if (checkforcmd(&ea.cmd, "browse", 3))
1784 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001785#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 cmdmod.browse = TRUE;
1787#endif
1788 continue;
1789 }
1790 if (!checkforcmd(&ea.cmd, "botright", 2))
1791 break;
1792#ifdef FEAT_WINDOWS
1793 cmdmod.split |= WSP_BOT;
1794#endif
1795 continue;
1796
1797 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1798 break;
1799#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1800 cmdmod.confirm = TRUE;
1801#endif
1802 continue;
1803
1804 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1805 {
1806 cmdmod.keepmarks = TRUE;
1807 continue;
1808 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001809 if (checkforcmd(&ea.cmd, "keepalt", 5))
1810 {
1811 cmdmod.keepalt = TRUE;
1812 continue;
1813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1815 break;
1816 cmdmod.keepjumps = TRUE;
1817 continue;
1818
1819 /* ":hide" and ":hide | cmd" are not modifiers */
1820 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1821 || *p == NUL || ends_excmd(*p))
1822 break;
1823 ea.cmd = p;
1824 cmdmod.hide = TRUE;
1825 continue;
1826
1827 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1828 {
1829 cmdmod.lockmarks = TRUE;
1830 continue;
1831 }
1832
1833 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1834 break;
1835#ifdef FEAT_WINDOWS
1836 cmdmod.split |= WSP_ABOVE;
1837#endif
1838 continue;
1839
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001840 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1841 break;
1842#ifdef FEAT_AUTOCMD
1843 if (cmdmod.save_ei == NULL)
1844 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001845 /* Set 'eventignore' to "all". Restore the
1846 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001847 cmdmod.save_ei = vim_strsave(p_ei);
1848 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001849 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001850 }
1851#endif
1852 continue;
1853
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1855 break;
1856#ifdef FEAT_WINDOWS
1857 cmdmod.split |= WSP_BELOW;
1858#endif
1859 continue;
1860
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001861 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1862 {
1863#ifdef HAVE_SANDBOX
1864 if (!did_sandbox)
1865 ++sandbox;
1866 did_sandbox = TRUE;
1867#endif
1868 continue;
1869 }
1870 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001872 if (save_msg_silent == -1)
1873 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1876 {
1877 /* ":silent!", but not "silent !cmd" */
1878 ea.cmd = skipwhite(ea.cmd + 1);
1879 ++emsg_silent;
1880 ++did_esilent;
1881 }
1882 continue;
1883
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001884 case 't': if (checkforcmd(&p, "tab", 3))
1885 {
1886#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001887 if (vim_isdigit(*ea.cmd))
1888 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1889 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001890 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001891 ea.cmd = p;
1892#endif
1893 continue;
1894 }
1895 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 break;
1897#ifdef FEAT_WINDOWS
1898 cmdmod.split |= WSP_TOP;
1899#endif
1900 continue;
1901
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001902 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1903 break;
1904 if (save_msg_silent == -1)
1905 save_msg_silent = msg_silent;
1906 msg_silent = 0;
1907 continue;
1908
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1910 {
1911#ifdef FEAT_VERTSPLIT
1912 cmdmod.split |= WSP_VERT;
1913#endif
1914 continue;
1915 }
1916 if (!checkforcmd(&p, "verbose", 4))
1917 break;
1918 if (verbose_save < 0)
1919 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001920 if (vim_isdigit(*ea.cmd))
1921 p_verbose = atoi((char *)ea.cmd);
1922 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 p_verbose = 1;
1924 ea.cmd = p;
1925 continue;
1926 }
1927 break;
1928 }
1929
1930#ifdef FEAT_EVAL
1931 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1932 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1933#else
1934 ea.skip = (if_level > 0);
1935#endif
1936
1937#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001938# ifdef FEAT_PROFILE
1939 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001940 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001941 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001942 if (getline_equal(fgetline, cookie, get_func_line))
1943 func_line_exec(getline_cookie(fgetline, cookie));
1944 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001945 script_line_exec();
1946 }
1947#endif
1948
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 /* May go to debug mode. If this happens and the ">quit" debug command is
1950 * used, throw an interrupt exception and skip the next command. */
1951 dbg_check_breakpoint(&ea);
1952 if (!ea.skip && got_int)
1953 {
1954 ea.skip = TRUE;
1955 (void)do_intthrow(cstack);
1956 }
1957#endif
1958
1959/*
1960 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1961 *
1962 * where 'addr' is:
1963 *
1964 * % (entire file)
1965 * $ [+-NUM]
1966 * 'x [+-NUM] (where x denotes a currently defined mark)
1967 * . [+-NUM]
1968 * [+-NUM]..
1969 * NUM
1970 *
1971 * The ea.cmd pointer is updated to point to the first character following the
1972 * range spec. If an initial address is found, but no second, the upper bound
1973 * is equal to the lower.
1974 */
1975
1976 /* repeat for all ',' or ';' separated addresses */
1977 for (;;)
1978 {
1979 ea.line1 = ea.line2;
1980 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1981 ea.cmd = skipwhite(ea.cmd);
1982 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1983 if (ea.cmd == NULL) /* error detected */
1984 goto doend;
1985 if (lnum == MAXLNUM)
1986 {
1987 if (*ea.cmd == '%') /* '%' - all lines */
1988 {
1989 ++ea.cmd;
1990 ea.line1 = 1;
1991 ea.line2 = curbuf->b_ml.ml_line_count;
1992 ++ea.addr_count;
1993 }
1994 /* '*' - visual area */
1995 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1996 {
1997 pos_T *fp;
1998
1999 ++ea.cmd;
2000 if (!ea.skip)
2001 {
2002 fp = getmark('<', FALSE);
2003 if (check_mark(fp) == FAIL)
2004 goto doend;
2005 ea.line1 = fp->lnum;
2006 fp = getmark('>', FALSE);
2007 if (check_mark(fp) == FAIL)
2008 goto doend;
2009 ea.line2 = fp->lnum;
2010 ++ea.addr_count;
2011 }
2012 }
2013 }
2014 else
2015 ea.line2 = lnum;
2016 ea.addr_count++;
2017
2018 if (*ea.cmd == ';')
2019 {
2020 if (!ea.skip)
2021 curwin->w_cursor.lnum = ea.line2;
2022 }
2023 else if (*ea.cmd != ',')
2024 break;
2025 ++ea.cmd;
2026 }
2027
2028 /* One address given: set start and end lines */
2029 if (ea.addr_count == 1)
2030 {
2031 ea.line1 = ea.line2;
2032 /* ... but only implicit: really no address given */
2033 if (lnum == MAXLNUM)
2034 ea.addr_count = 0;
2035 }
2036
2037 /* Don't leave the cursor on an illegal line (caused by ';') */
2038 check_cursor_lnum();
2039
2040/*
2041 * 4. parse command
2042 */
2043
2044 /*
2045 * Skip ':' and any white space
2046 */
2047 ea.cmd = skipwhite(ea.cmd);
2048 while (*ea.cmd == ':')
2049 ea.cmd = skipwhite(ea.cmd + 1);
2050
2051 /*
2052 * If we got a line, but no command, then go to the line.
2053 * If we find a '|' or '\n' we set ea.nextcmd.
2054 */
2055 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2056 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2057 {
2058 /*
2059 * strange vi behaviour:
2060 * ":3" jumps to line 3
2061 * ":3|..." prints line 3
2062 * ":|" prints current line
2063 */
2064 if (ea.skip) /* skip this if inside :if */
2065 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002066 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 {
2068 ea.cmdidx = CMD_print;
2069 ea.argt = RANGE+COUNT+TRLBAR;
2070 if ((errormsg = invalid_range(&ea)) == NULL)
2071 {
2072 correct_range(&ea);
2073 ex_print(&ea);
2074 }
2075 }
2076 else if (ea.addr_count != 0)
2077 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002078 if (ea.line2 > curbuf->b_ml.ml_line_count)
2079 {
2080 /* With '-' in 'cpoptions' a line number past the file is an
2081 * error, otherwise put it at the end of the file. */
2082 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2083 ea.line2 = -1;
2084 else
2085 ea.line2 = curbuf->b_ml.ml_line_count;
2086 }
2087
2088 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002089 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 else
2091 {
2092 if (ea.line2 == 0)
2093 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 else
2095 curwin->w_cursor.lnum = ea.line2;
2096 beginline(BL_SOL | BL_FIX);
2097 }
2098 }
2099 goto doend;
2100 }
2101
2102 /* Find the command and let "p" point to after it. */
2103 p = find_command(&ea, NULL);
2104
2105#ifdef FEAT_USR_CMDS
2106 if (p == NULL)
2107 {
2108 if (!ea.skip)
2109 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2110 goto doend;
2111 }
2112 /* Check for wrong commands. */
2113 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2114 {
2115 errormsg = uc_fun_cmd();
2116 goto doend;
2117 }
2118#endif
2119 if (ea.cmdidx == CMD_SIZE)
2120 {
2121 if (!ea.skip)
2122 {
2123 STRCPY(IObuff, _("E492: Not an editor command"));
2124 if (!sourcing)
2125 {
2126 STRCAT(IObuff, ": ");
2127 STRNCAT(IObuff, *cmdlinep, 40);
2128 }
2129 errormsg = IObuff;
2130 }
2131 goto doend;
2132 }
2133
2134 ni = (
2135#ifdef FEAT_USR_CMDS
2136 !USER_CMDIDX(ea.cmdidx) &&
2137#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002138 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002139#ifdef HAVE_EX_SCRIPT_NI
2140 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2141#endif
2142 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143
2144#ifndef FEAT_EVAL
2145 /*
2146 * When the expression evaluation is disabled, recognize the ":if" and
2147 * ":endif" commands and ignore everything in between it.
2148 */
2149 if (ea.cmdidx == CMD_if)
2150 ++if_level;
2151 if (if_level)
2152 {
2153 if (ea.cmdidx == CMD_endif)
2154 --if_level;
2155 goto doend;
2156 }
2157
2158#endif
2159
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002160 /* forced commands */
2161 if (*p == '!' && ea.cmdidx != CMD_substitute
2162 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 {
2164 ++p;
2165 ea.forceit = TRUE;
2166 }
2167 else
2168 ea.forceit = FALSE;
2169
2170/*
2171 * 5. parse arguments
2172 */
2173#ifdef FEAT_USR_CMDS
2174 if (!USER_CMDIDX(ea.cmdidx))
2175#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002176 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
2178 if (!ea.skip)
2179 {
2180#ifdef HAVE_SANDBOX
2181 if (sandbox != 0 && !(ea.argt & SBOXOK))
2182 {
2183 /* Command not allowed in sandbox. */
2184 errormsg = (char_u *)_(e_sandbox);
2185 goto doend;
2186 }
2187#endif
2188 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2189 {
2190 /* Command not allowed in non-'modifiable' buffer */
2191 errormsg = (char_u *)_(e_modifiable);
2192 goto doend;
2193 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002194
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002195 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196# ifdef FEAT_USR_CMDS
2197 && !USER_CMDIDX(ea.cmdidx)
2198# endif
2199 )
2200 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002201 /* Command not allowed when editing the command line. */
2202#ifdef FEAT_CMDWIN
2203 if (cmdwin_type != 0)
2204 errormsg = (char_u *)_(e_cmdwin);
2205 else
2206#endif
2207 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 goto doend;
2209 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002210#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002211 /* Disallow editing another buffer when "curbuf_lock" is set.
2212 * Do allow ":edit" (check for argument later).
2213 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002214 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002215 && ea.cmdidx != CMD_edit
2216 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002217# ifdef FEAT_USR_CMDS
2218 && !USER_CMDIDX(ea.cmdidx)
2219# endif
2220 && curbuf_locked())
2221 goto doend;
2222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223
2224 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2225 {
2226 /* no range allowed */
2227 errormsg = (char_u *)_(e_norange);
2228 goto doend;
2229 }
2230 }
2231
2232 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2233 {
2234 errormsg = (char_u *)_(e_nobang);
2235 goto doend;
2236 }
2237
2238 /*
2239 * Don't complain about the range if it is not used
2240 * (could happen if line_count is accidentally set to 0).
2241 */
2242 if (!ea.skip && !ni)
2243 {
2244 /*
2245 * If the range is backwards, ask for confirmation and, if given, swap
2246 * ea.line1 & ea.line2 so it's forwards again.
2247 * When global command is busy, don't ask, will fail below.
2248 */
2249 if (!global_busy && ea.line1 > ea.line2)
2250 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002251 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002253 if (sourcing || exmode_active)
2254 {
2255 errormsg = (char_u *)_("E493: Backwards range given");
2256 goto doend;
2257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 if (ask_yesno((char_u *)
2259 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002260 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 }
2262 lnum = ea.line1;
2263 ea.line1 = ea.line2;
2264 ea.line2 = lnum;
2265 }
2266 if ((errormsg = invalid_range(&ea)) != NULL)
2267 goto doend;
2268 }
2269
2270 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2271 ea.line2 = 1;
2272
2273 correct_range(&ea);
2274
2275#ifdef FEAT_FOLDING
2276 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2277 {
2278 /* Put the first line at the start of a closed fold, put the last line
2279 * at the end of a closed fold. */
2280 (void)hasFolding(ea.line1, &ea.line1, NULL);
2281 (void)hasFolding(ea.line2, NULL, &ea.line2);
2282 }
2283#endif
2284
2285#ifdef FEAT_QUICKFIX
2286 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002287 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 * option here, so things like % get expanded.
2289 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002290 p = replace_makeprg(&ea, p, cmdlinep);
2291 if (p == NULL)
2292 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293#endif
2294
2295 /*
2296 * Skip to start of argument.
2297 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2298 */
2299 if (ea.cmdidx == CMD_bang)
2300 ea.arg = p;
2301 else
2302 ea.arg = skipwhite(p);
2303
2304 /*
2305 * Check for "++opt=val" argument.
2306 * Must be first, allow ":w ++enc=utf8 !cmd"
2307 */
2308 if (ea.argt & ARGOPT)
2309 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2310 if (getargopt(&ea) == FAIL && !ni)
2311 {
2312 errormsg = (char_u *)_(e_invarg);
2313 goto doend;
2314 }
2315
2316 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2317 {
2318 if (*ea.arg == '>') /* append */
2319 {
2320 if (*++ea.arg != '>') /* typed wrong */
2321 {
2322 errormsg = (char_u *)_("E494: Use w or w>>");
2323 goto doend;
2324 }
2325 ea.arg = skipwhite(ea.arg + 1);
2326 ea.append = TRUE;
2327 }
2328 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2329 {
2330 ++ea.arg;
2331 ea.usefilter = TRUE;
2332 }
2333 }
2334
2335 if (ea.cmdidx == CMD_read)
2336 {
2337 if (ea.forceit)
2338 {
2339 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2340 ea.forceit = FALSE;
2341 }
2342 else if (*ea.arg == '!') /* :r !filter */
2343 {
2344 ++ea.arg;
2345 ea.usefilter = TRUE;
2346 }
2347 }
2348
2349 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2350 {
2351 ea.amount = 1;
2352 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2353 {
2354 ++ea.arg;
2355 ++ea.amount;
2356 }
2357 ea.arg = skipwhite(ea.arg);
2358 }
2359
2360 /*
2361 * Check for "+command" argument, before checking for next command.
2362 * Don't do this for ":read !cmd" and ":write !cmd".
2363 */
2364 if ((ea.argt & EDITCMD) && !ea.usefilter)
2365 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2366
2367 /*
2368 * Check for '|' to separate commands and '"' to start comments.
2369 * Don't do this for ":read !cmd" and ":write !cmd".
2370 */
2371 if ((ea.argt & TRLBAR) && !ea.usefilter)
2372 separate_nextcmd(&ea);
2373
2374 /*
2375 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002376 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2377 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002379 else if (ea.cmdidx == CMD_bang
2380 || ea.cmdidx == CMD_global
2381 || ea.cmdidx == CMD_vglobal
2382 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
2384 for (p = ea.arg; *p; ++p)
2385 {
2386 /* Remove one backslash before a newline, so that it's possible to
2387 * pass a newline to the shell and also a newline that is preceded
2388 * with a backslash. This makes it impossible to end a shell
2389 * command in a backslash, but that doesn't appear useful.
2390 * Halving the number of backslashes is incompatible with previous
2391 * versions. */
2392 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002393 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 else if (*p == '\n')
2395 {
2396 ea.nextcmd = p + 1;
2397 *p = NUL;
2398 break;
2399 }
2400 }
2401 }
2402
2403 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2404 {
2405 ea.line1 = 1;
2406 ea.line2 = curbuf->b_ml.ml_line_count;
2407 }
2408
2409 /* accept numbered register only when no count allowed (:put) */
2410 if ( (ea.argt & REGSTR)
2411 && *ea.arg != NUL
2412#ifdef FEAT_USR_CMDS
2413 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2414 && USER_CMDIDX(ea.cmdidx)))
2415 /* Do not allow register = for user commands */
2416 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2417#else
2418 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2419#endif
2420 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2421 {
2422 ea.regname = *ea.arg++;
2423#ifdef FEAT_EVAL
2424 /* for '=' register: accept the rest of the line as an expression */
2425 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2426 {
2427 set_expr_line(vim_strsave(ea.arg));
2428 ea.arg += STRLEN(ea.arg);
2429 }
2430#endif
2431 ea.arg = skipwhite(ea.arg);
2432 }
2433
2434 /*
2435 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2436 * count, it's a buffer name.
2437 */
2438 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2439 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2440 || vim_iswhite(*p)))
2441 {
2442 n = getdigits(&ea.arg);
2443 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002444 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
2446 errormsg = (char_u *)_(e_zerocount);
2447 goto doend;
2448 }
2449 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2450 {
2451 ea.line2 = n;
2452 if (ea.addr_count == 0)
2453 ea.addr_count = 1;
2454 }
2455 else
2456 {
2457 ea.line1 = ea.line2;
2458 ea.line2 += n - 1;
2459 ++ea.addr_count;
2460 /*
2461 * Be vi compatible: no error message for out of range.
2462 */
2463 if (ea.line2 > curbuf->b_ml.ml_line_count)
2464 ea.line2 = curbuf->b_ml.ml_line_count;
2465 }
2466 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002467
2468 /*
2469 * Check for flags: 'l', 'p' and '#'.
2470 */
2471 if (ea.argt & EXFLAGS)
2472 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002474 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002475 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 {
2477 errormsg = (char_u *)_(e_trailing);
2478 goto doend;
2479 }
2480
2481 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2482 {
2483 errormsg = (char_u *)_(e_argreq);
2484 goto doend;
2485 }
2486
2487#ifdef FEAT_EVAL
2488 /*
2489 * Skip the command when it's not going to be executed.
2490 * The commands like :if, :endif, etc. always need to be executed.
2491 * Also make an exception for commands that handle a trailing command
2492 * themselves.
2493 */
2494 if (ea.skip)
2495 {
2496 switch (ea.cmdidx)
2497 {
2498 /* commands that need evaluation */
2499 case CMD_while:
2500 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002501 case CMD_for:
2502 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 case CMD_if:
2504 case CMD_elseif:
2505 case CMD_else:
2506 case CMD_endif:
2507 case CMD_try:
2508 case CMD_catch:
2509 case CMD_finally:
2510 case CMD_endtry:
2511 case CMD_function:
2512 break;
2513
2514 /* Commands that handle '|' themselves. Check: A command should
2515 * either have the TRLBAR flag, appear in this list or appear in
2516 * the list at ":help :bar". */
2517 case CMD_aboveleft:
2518 case CMD_and:
2519 case CMD_belowright:
2520 case CMD_botright:
2521 case CMD_browse:
2522 case CMD_call:
2523 case CMD_confirm:
2524 case CMD_delfunction:
2525 case CMD_djump:
2526 case CMD_dlist:
2527 case CMD_dsearch:
2528 case CMD_dsplit:
2529 case CMD_echo:
2530 case CMD_echoerr:
2531 case CMD_echomsg:
2532 case CMD_echon:
2533 case CMD_execute:
2534 case CMD_help:
2535 case CMD_hide:
2536 case CMD_ijump:
2537 case CMD_ilist:
2538 case CMD_isearch:
2539 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002540 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 case CMD_keepjumps:
2542 case CMD_keepmarks:
2543 case CMD_leftabove:
2544 case CMD_let:
2545 case CMD_lockmarks:
2546 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002547 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 case CMD_perl:
2549 case CMD_psearch:
2550 case CMD_python:
2551 case CMD_return:
2552 case CMD_rightbelow:
2553 case CMD_ruby:
2554 case CMD_silent:
2555 case CMD_smagic:
2556 case CMD_snomagic:
2557 case CMD_substitute:
2558 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002559 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 case CMD_tcl:
2561 case CMD_throw:
2562 case CMD_tilde:
2563 case CMD_topleft:
2564 case CMD_unlet:
2565 case CMD_verbose:
2566 case CMD_vertical:
2567 break;
2568
2569 default: goto doend;
2570 }
2571 }
2572#endif
2573
2574 if (ea.argt & XFILE)
2575 {
2576 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2577 goto doend;
2578 }
2579
2580#ifdef FEAT_LISTCMDS
2581 /*
2582 * Accept buffer name. Cannot be used at the same time with a buffer
2583 * number. Don't do this for a user command.
2584 */
2585 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2586# ifdef FEAT_USR_CMDS
2587 && !USER_CMDIDX(ea.cmdidx)
2588# endif
2589 )
2590 {
2591 /*
2592 * :bdelete, :bwipeout and :bunload take several arguments, separated
2593 * by spaces: find next space (skipping over escaped characters).
2594 * The others take one argument: ignore trailing spaces.
2595 */
2596 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2597 || ea.cmdidx == CMD_bunload)
2598 p = skiptowhite_esc(ea.arg);
2599 else
2600 {
2601 p = ea.arg + STRLEN(ea.arg);
2602 while (p > ea.arg && vim_iswhite(p[-1]))
2603 --p;
2604 }
2605 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2606 if (ea.line2 < 0) /* failed */
2607 goto doend;
2608 ea.addr_count = 1;
2609 ea.arg = skipwhite(p);
2610 }
2611#endif
2612
2613/*
2614 * 6. switch on command name
2615 *
2616 * The "ea" structure holds the arguments that can be used.
2617 */
2618 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002619 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 ea.cookie = cookie;
2621#ifdef FEAT_EVAL
2622 ea.cstack = cstack;
2623#endif
2624
2625#ifdef FEAT_USR_CMDS
2626 if (USER_CMDIDX(ea.cmdidx))
2627 {
2628 /*
2629 * Execute a user-defined command.
2630 */
2631 do_ucmd(&ea);
2632 }
2633 else
2634#endif
2635 {
2636 /*
2637 * Call the function to execute the command.
2638 */
2639 ea.errmsg = NULL;
2640 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2641 if (ea.errmsg != NULL)
2642 errormsg = (char_u *)_(ea.errmsg);
2643 }
2644
2645#ifdef FEAT_EVAL
2646 /*
2647 * If the command just executed called do_cmdline(), any throw or ":return"
2648 * or ":finish" encountered there must also check the cstack of the still
2649 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2650 * exception, or reanimate a returned function or finished script file and
2651 * return or finish it again.
2652 */
2653 if (need_rethrow)
2654 do_throw(cstack);
2655 else if (check_cstack)
2656 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002657 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002659 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 && current_func_returned())
2661 do_return(&ea, TRUE, FALSE, NULL);
2662 }
2663 need_rethrow = check_cstack = FALSE;
2664#endif
2665
2666doend:
2667 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2668 curwin->w_cursor.lnum = 1;
2669
2670 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2671 {
2672 if (sourcing)
2673 {
2674 if (errormsg != IObuff)
2675 {
2676 STRCPY(IObuff, errormsg);
2677 errormsg = IObuff;
2678 }
2679 STRCAT(errormsg, ": ");
Bram Moolenaar6c964832007-12-09 18:38:35 +00002680 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 }
2682 emsg(errormsg);
2683 }
2684#ifdef FEAT_EVAL
2685 do_errthrow(cstack,
2686 (ea.cmdidx != CMD_SIZE
2687# ifdef FEAT_USR_CMDS
2688 && !USER_CMDIDX(ea.cmdidx)
2689# endif
2690 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2691#endif
2692
2693 if (verbose_save >= 0)
2694 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002695#ifdef FEAT_AUTOCMD
2696 if (cmdmod.save_ei != NULL)
2697 {
2698 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002699 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2700 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002701 free_string_option(cmdmod.save_ei);
2702 }
2703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
2705 cmdmod = save_cmdmod;
2706
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002707 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
2709 /* messages could be enabled for a serious error, need to check if the
2710 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002711 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002712 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 emsg_silent -= did_esilent;
2714 if (emsg_silent < 0)
2715 emsg_silent = 0;
2716 /* Restore msg_scroll, it's set by file I/O commands, even when no
2717 * message is actually displayed. */
2718 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002719
2720 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2721 * somewhere in the line. Put it back in the first column. */
2722 if (redirecting())
2723 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
2725
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002726#ifdef HAVE_SANDBOX
2727 if (did_sandbox)
2728 --sandbox;
2729#endif
2730
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2732 ea.nextcmd = NULL;
2733
2734#ifdef FEAT_EVAL
2735 --ex_nesting_level;
2736#endif
2737
2738 return ea.nextcmd;
2739}
2740#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002741 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742#endif
2743
2744/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002745 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 * If there is a match advance "pp" to the argument and return TRUE.
2747 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002748 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002750 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 char *cmd; /* name of command */
2752 int len; /* required length */
2753{
2754 int i;
2755
2756 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002757 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 break;
2759 if (i >= len && !isalpha((*pp)[i]))
2760 {
2761 *pp = skipwhite(*pp + i);
2762 return TRUE;
2763 }
2764 return FALSE;
2765}
2766
2767/*
2768 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002769 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002771 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 * Returns NULL for an ambiguous user command.
2773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 static char_u *
2775find_command(eap, full)
2776 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002777 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
2779 int len;
2780 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002781 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
2783 /*
2784 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002785 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 * - the 'k' command can directly be followed by any character.
2787 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2788 * but :sre[wind] is another command, as are :scrip[tnames],
2789 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002790 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 */
2792 p = eap->cmd;
2793 if (*p == 'k')
2794 {
2795 eap->cmdidx = CMD_k;
2796 ++p;
2797 }
2798 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002799 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2800 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 || p[1] == 'g'
2802 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2803 || p[1] == 'I'
2804 || (p[1] == 'r' && p[2] != 'e')))
2805 {
2806 eap->cmdidx = CMD_substitute;
2807 ++p;
2808 }
2809 else
2810 {
2811 while (ASCII_ISALPHA(*p))
2812 ++p;
2813 /* check for non-alpha command */
2814 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2815 ++p;
2816 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002817 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2818 {
2819 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2820 * :delete with the 'l' flag. Same for 'p'. */
2821 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002822 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002823 break;
2824 if (i == len - 1)
2825 {
2826 --len;
2827 if (p[-1] == 'l')
2828 eap->flags |= EXFLAG_LIST;
2829 else
2830 eap->flags |= EXFLAG_PRINT;
2831 }
2832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833
2834 if (ASCII_ISLOWER(*eap->cmd))
2835 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2836 else
2837 eap->cmdidx = cmdidxs[26];
2838
2839 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2840 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2841 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2842 (size_t)len) == 0)
2843 {
2844#ifdef FEAT_EVAL
2845 if (full != NULL
2846 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2847 *full = TRUE;
2848#endif
2849 break;
2850 }
2851
2852#ifdef FEAT_USR_CMDS
2853 /* Look for a user defined command as a last resort */
2854 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2855 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002856 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 while (ASCII_ISALNUM(*p))
2858 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002859 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 }
2861#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002862 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 eap->cmdidx = CMD_SIZE;
2864 }
2865
2866 return p;
2867}
2868
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002869#ifdef FEAT_USR_CMDS
2870/*
2871 * Search for a user command that matches "eap->cmd".
2872 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2873 * Return a pointer to just after the command.
2874 * Return NULL if there is no matching command.
2875 */
2876 static char_u *
2877find_ucmd(eap, p, full, xp, compl)
2878 exarg_T *eap;
2879 char_u *p; /* end of the command (possibly including count) */
2880 int *full; /* set to TRUE for a full match */
2881 expand_T *xp; /* used for completion, NULL otherwise */
2882 int *compl; /* completion flags or NULL */
2883{
2884 int len = (int)(p - eap->cmd);
2885 int j, k, matchlen = 0;
2886 ucmd_T *uc;
2887 int found = FALSE;
2888 int possible = FALSE;
2889 char_u *cp, *np; /* Point into typed cmd and test name */
2890 garray_T *gap;
2891 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2892 only full match global is accepted. */
2893
2894 /*
2895 * Look for buffer-local user commands first, then global ones.
2896 */
2897 gap = &curbuf->b_ucmds;
2898 for (;;)
2899 {
2900 for (j = 0; j < gap->ga_len; ++j)
2901 {
2902 uc = USER_CMD_GA(gap, j);
2903 cp = eap->cmd;
2904 np = uc->uc_name;
2905 k = 0;
2906 while (k < len && *np != NUL && *cp++ == *np++)
2907 k++;
2908 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2909 {
2910 /* If finding a second match, the command is ambiguous. But
2911 * not if a buffer-local command wasn't a full match and a
2912 * global command is a full match. */
2913 if (k == len && found && *np != NUL)
2914 {
2915 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002916 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002917 amb_local = TRUE;
2918 }
2919
2920 if (!found || (k == len && *np == NUL))
2921 {
2922 /* If we matched up to a digit, then there could
2923 * be another command including the digit that we
2924 * should use instead.
2925 */
2926 if (k == len)
2927 found = TRUE;
2928 else
2929 possible = TRUE;
2930
2931 if (gap == &ucmds)
2932 eap->cmdidx = CMD_USER;
2933 else
2934 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002935 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002936 eap->useridx = j;
2937
2938# ifdef FEAT_CMDL_COMPL
2939 if (compl != NULL)
2940 *compl = uc->uc_compl;
2941# ifdef FEAT_EVAL
2942 if (xp != NULL)
2943 {
2944 xp->xp_arg = uc->uc_compl_arg;
2945 xp->xp_scriptID = uc->uc_scriptID;
2946 }
2947# endif
2948# endif
2949 /* Do not search for further abbreviations
2950 * if this is an exact match. */
2951 matchlen = k;
2952 if (k == len && *np == NUL)
2953 {
2954 if (full != NULL)
2955 *full = TRUE;
2956 amb_local = FALSE;
2957 break;
2958 }
2959 }
2960 }
2961 }
2962
2963 /* Stop if we found a full match or searched all. */
2964 if (j < gap->ga_len || gap == &ucmds)
2965 break;
2966 gap = &ucmds;
2967 }
2968
2969 /* Only found ambiguous matches. */
2970 if (amb_local)
2971 {
2972 if (xp != NULL)
2973 xp->xp_context = EXPAND_UNSUCCESSFUL;
2974 return NULL;
2975 }
2976
2977 /* The match we found may be followed immediately by a number. Move "p"
2978 * back to point to it. */
2979 if (found || possible)
2980 return p + (matchlen - len);
2981 return p;
2982}
2983#endif
2984
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00002986static struct cmdmod
2987{
2988 char *name;
2989 int minlen;
2990 int has_count; /* :123verbose :3tab */
2991} cmdmods[] = {
2992 {"aboveleft", 3, FALSE},
2993 {"belowright", 3, FALSE},
2994 {"botright", 2, FALSE},
2995 {"browse", 3, FALSE},
2996 {"confirm", 4, FALSE},
2997 {"hide", 3, FALSE},
2998 {"keepalt", 5, FALSE},
2999 {"keepjumps", 5, FALSE},
3000 {"keepmarks", 3, FALSE},
3001 {"leftabove", 5, FALSE},
3002 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003003 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003004 {"rightbelow", 6, FALSE},
3005 {"sandbox", 3, FALSE},
3006 {"silent", 3, FALSE},
3007 {"tab", 3, TRUE},
3008 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003009 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003010 {"verbose", 4, TRUE},
3011 {"vertical", 4, FALSE},
3012};
3013
3014/*
3015 * Return length of a command modifier (including optional count).
3016 * Return zero when it's not a modifier.
3017 */
3018 int
3019modifier_len(cmd)
3020 char_u *cmd;
3021{
3022 int i, j;
3023 char_u *p = cmd;
3024
3025 if (VIM_ISDIGIT(*cmd))
3026 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003027 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003028 {
3029 for (j = 0; p[j] != NUL; ++j)
3030 if (p[j] != cmdmods[i].name[j])
3031 break;
3032 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3033 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003034 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003035 }
3036 return 0;
3037}
3038
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039/*
3040 * Return > 0 if an Ex command "name" exists.
3041 * Return 2 if there is an exact match.
3042 * Return 3 if there is an ambiguous match.
3043 */
3044 int
3045cmd_exists(name)
3046 char_u *name;
3047{
3048 exarg_T ea;
3049 int full = FALSE;
3050 int i;
3051 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003052 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053
3054 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003055 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 {
3057 for (j = 0; name[j] != NUL; ++j)
3058 if (name[j] != cmdmods[i].name[j])
3059 break;
3060 if (name[j] == NUL && j >= cmdmods[i].minlen)
3061 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3062 }
3063
Bram Moolenaara9587612006-05-04 21:47:50 +00003064 /* Check built-in commands and user defined commands.
3065 * For ":2match" and ":3match" we need to skip the number. */
3066 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003068 p = find_command(&ea, &full);
3069 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003071 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3072 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003073 if (*skipwhite(p) != NUL)
3074 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3076}
3077#endif
3078
3079/*
3080 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3081 * we don't need/want deleted. Maybe this could be done better if we didn't
3082 * repeat all this stuff. The only problem is that they may not stay
3083 * perfectly compatible with each other, but then the command line syntax
3084 * probably won't change that much -- webb.
3085 */
3086 char_u *
3087set_one_cmd_context(xp, buff)
3088 expand_T *xp;
3089 char_u *buff; /* buffer for command string */
3090{
3091 char_u *p;
3092 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003093 int len = 0;
3094 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3096 int compl = EXPAND_NOTHING;
3097#endif
3098#ifdef FEAT_CMDL_COMPL
3099 int delim;
3100#endif
3101 int forceit = FALSE;
3102 int usefilter = FALSE; /* filter instead of file name */
3103
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003104 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 xp->xp_pattern = buff;
3106 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003107 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108
3109/*
3110 * 2. skip comment lines and leading space, colons or bars
3111 */
3112 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3113 ;
3114 xp->xp_pattern = cmd;
3115
3116 if (*cmd == NUL)
3117 return NULL;
3118 if (*cmd == '"') /* ignore comment lines */
3119 {
3120 xp->xp_context = EXPAND_NOTHING;
3121 return NULL;
3122 }
3123
3124/*
3125 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3126 */
3127 cmd = skip_range(cmd, &xp->xp_context);
3128
3129/*
3130 * 4. parse command
3131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 xp->xp_pattern = cmd;
3133 if (*cmd == NUL)
3134 return NULL;
3135 if (*cmd == '"')
3136 {
3137 xp->xp_context = EXPAND_NOTHING;
3138 return NULL;
3139 }
3140
3141 if (*cmd == '|' || *cmd == '\n')
3142 return cmd + 1; /* There's another command */
3143
3144 /*
3145 * Isolate the command and search for it in the command table.
3146 * Exceptions:
3147 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003148 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3150 */
3151 if (*cmd == 'k' && cmd[1] != 'e')
3152 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003153 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 p = cmd + 1;
3155 }
3156 else
3157 {
3158 p = cmd;
3159 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3160 ++p;
3161 /* check for non-alpha command */
3162 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3163 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003164 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003166 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 {
3168 xp->xp_context = EXPAND_UNSUCCESSFUL;
3169 return NULL;
3170 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003171 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003172 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3173 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3174 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 break;
3176
3177#ifdef FEAT_USR_CMDS
3178 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3180 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181#endif
3182 }
3183
3184 /*
3185 * If the cursor is touching the command, and it ends in an alpha-numeric
3186 * character, complete the command name.
3187 */
3188 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3189 return NULL;
3190
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003191 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 {
3193 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3194 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003195 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 p = cmd + 1;
3197 }
3198#ifdef FEAT_USR_CMDS
3199 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3200 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003201 ea.cmd = cmd;
3202 p = find_ucmd(&ea, p, NULL, xp,
3203# if defined(FEAT_CMDL_COMPL)
3204 &compl
3205# else
3206 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003208 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003209 if (p == NULL)
3210 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 }
3212#endif
3213 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003214 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 {
3216 /* Not still touching the command and it was an illegal one */
3217 xp->xp_context = EXPAND_UNSUCCESSFUL;
3218 return NULL;
3219 }
3220
3221 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3222
3223 if (*p == '!') /* forced commands */
3224 {
3225 forceit = TRUE;
3226 ++p;
3227 }
3228
3229/*
3230 * 5. parse arguments
3231 */
3232#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003233 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003235 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
3237 arg = skipwhite(p);
3238
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003239 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 {
3241 if (*arg == '>') /* append */
3242 {
3243 if (*++arg == '>')
3244 ++arg;
3245 arg = skipwhite(arg);
3246 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003247 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 {
3249 ++arg;
3250 usefilter = TRUE;
3251 }
3252 }
3253
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003254 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 {
3256 usefilter = forceit; /* :r! filter if forced */
3257 if (*arg == '!') /* :r !filter */
3258 {
3259 ++arg;
3260 usefilter = TRUE;
3261 }
3262 }
3263
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003264 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 {
3266 while (*arg == *cmd) /* allow any number of '>' or '<' */
3267 ++arg;
3268 arg = skipwhite(arg);
3269 }
3270
3271 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003272 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 {
3274 /* Check if we're in the +command */
3275 p = arg + 1;
3276 arg = skip_cmd_arg(arg, FALSE);
3277
3278 /* Still touching the command after '+'? */
3279 if (*arg == NUL)
3280 return p;
3281
3282 /* Skip space(s) after +command to get to the real argument */
3283 arg = skipwhite(arg);
3284 }
3285
3286 /*
3287 * Check for '|' to separate commands and '"' to start comments.
3288 * Don't do this for ":read !cmd" and ":write !cmd".
3289 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003290 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 {
3292 p = arg;
3293 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003294 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 p += 2;
3296 while (*p)
3297 {
3298 if (*p == Ctrl_V)
3299 {
3300 if (p[1] != NUL)
3301 ++p;
3302 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003303 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 || *p == '|' || *p == '\n')
3305 {
3306 if (*(p - 1) != '\\')
3307 {
3308 if (*p == '|' || *p == '\n')
3309 return p + 1;
3310 return NULL; /* It's a comment */
3311 }
3312 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003313 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 }
3315 }
3316
3317 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003318 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 vim_strchr((char_u *)"|\"", *arg) == NULL)
3320 return NULL;
3321
3322 /* Find start of last argument (argument just before cursor): */
3323 p = buff + STRLEN(buff);
3324 while (p != arg && *p != ' ' && *p != TAB)
3325 p--;
3326 if (*p == ' ' || *p == TAB)
3327 p++;
3328 xp->xp_pattern = p;
3329
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003330 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003332 int c;
3333 int in_quote = FALSE;
3334 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
3336 /*
3337 * Allow spaces within back-quotes to count as part of the argument
3338 * being expanded.
3339 */
3340 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003341 p = xp->xp_pattern;
3342 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003344#ifdef FEAT_MBYTE
3345 if (has_mbyte)
3346 c = mb_ptr2char(p);
3347 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003349 c = *p;
3350 if (c == '\\' && p[1] != NUL)
3351 ++p;
3352 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 {
3354 if (!in_quote)
3355 {
3356 xp->xp_pattern = p;
3357 bow = p + 1;
3358 }
3359 in_quote = !in_quote;
3360 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003361 /* An argument can contain just about everything, except
3362 * characters that end the command and white space. */
3363 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003364#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003365 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003366#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003367 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003368 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003369 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003370 while (*p != NUL)
3371 {
3372#ifdef FEAT_MBYTE
3373 if (has_mbyte)
3374 c = mb_ptr2char(p);
3375 else
3376#endif
3377 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003378 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003379 break;
3380#ifdef FEAT_MBYTE
3381 if (has_mbyte)
3382 len = (*mb_ptr2len)(p);
3383 else
3384#endif
3385 len = 1;
3386 mb_ptr_adv(p);
3387 }
3388 if (in_quote)
3389 bow = p;
3390 else
3391 xp->xp_pattern = p;
3392 p -= len;
3393 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003394 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 }
3396
3397 /*
3398 * If we are still inside the quotes, and we passed a space, just
3399 * expand from there.
3400 */
3401 if (bow != NULL && in_quote)
3402 xp->xp_pattern = bow;
3403 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003404
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003405#ifndef BACKSLASH_IN_FILENAME
3406 /* For a shell command more chars need to be escaped. */
3407 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003408 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003409 xp->xp_shell = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003410
3411 /* When still after the command name expand executables. */
3412 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003413 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003414 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416
3417 /* Check for environment variable */
3418 if (*xp->xp_pattern == '$'
3419#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3420 || *xp->xp_pattern == '%'
3421#endif
3422 )
3423 {
3424 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3425 if (!vim_isIDc(*p))
3426 break;
3427 if (*p == NUL)
3428 {
3429 xp->xp_context = EXPAND_ENV_VARS;
3430 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003431#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3432 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003433 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003434 compl = EXPAND_ENV_VARS;
3435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
3437 }
3438 }
3439
3440/*
3441 * 6. switch on command name
3442 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003443 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 {
3445 case CMD_cd:
3446 case CMD_chdir:
3447 case CMD_lcd:
3448 case CMD_lchdir:
3449 if (xp->xp_context == EXPAND_FILES)
3450 xp->xp_context = EXPAND_DIRECTORIES;
3451 break;
3452 case CMD_help:
3453 xp->xp_context = EXPAND_HELP;
3454 xp->xp_pattern = arg;
3455 break;
3456
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003457 /* Command modifiers: return the argument.
3458 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003460 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 case CMD_belowright:
3462 case CMD_botright:
3463 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003464 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003466 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 case CMD_folddoclosed:
3468 case CMD_folddoopen:
3469 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003470 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 case CMD_keepjumps:
3472 case CMD_keepmarks:
3473 case CMD_leftabove:
3474 case CMD_lockmarks:
3475 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003476 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003478 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 case CMD_topleft:
3480 case CMD_verbose:
3481 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003482 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 return arg;
3484
Bram Moolenaar4f688582007-07-24 12:34:30 +00003485#ifdef FEAT_CMDL_COMPL
3486# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 case CMD_match:
3488 if (*arg == NUL || !ends_excmd(*arg))
3489 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003490 /* also complete "None" */
3491 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 arg = skipwhite(skiptowhite(arg));
3493 if (*arg != NUL)
3494 {
3495 xp->xp_context = EXPAND_NOTHING;
3496 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3497 }
3498 }
3499 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003500# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502/*
3503 * All completion for the +cmdline_compl feature goes here.
3504 */
3505
3506# ifdef FEAT_USR_CMDS
3507 case CMD_command:
3508 /* Check for attributes */
3509 while (*arg == '-')
3510 {
3511 arg++; /* Skip "-" */
3512 p = skiptowhite(arg);
3513 if (*p == NUL)
3514 {
3515 /* Cursor is still in the attribute */
3516 p = vim_strchr(arg, '=');
3517 if (p == NULL)
3518 {
3519 /* No "=", so complete attribute names */
3520 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3521 xp->xp_pattern = arg;
3522 return NULL;
3523 }
3524
3525 /* For the -complete and -nargs attributes, we complete
3526 * their arguments as well.
3527 */
3528 if (STRNICMP(arg, "complete", p - arg) == 0)
3529 {
3530 xp->xp_context = EXPAND_USER_COMPLETE;
3531 xp->xp_pattern = p + 1;
3532 return NULL;
3533 }
3534 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3535 {
3536 xp->xp_context = EXPAND_USER_NARGS;
3537 xp->xp_pattern = p + 1;
3538 return NULL;
3539 }
3540 return NULL;
3541 }
3542 arg = skipwhite(p);
3543 }
3544
3545 /* After the attributes comes the new command name */
3546 p = skiptowhite(arg);
3547 if (*p == NUL)
3548 {
3549 xp->xp_context = EXPAND_USER_COMMANDS;
3550 xp->xp_pattern = arg;
3551 break;
3552 }
3553
3554 /* And finally comes a normal command */
3555 return skipwhite(p);
3556
3557 case CMD_delcommand:
3558 xp->xp_context = EXPAND_USER_COMMANDS;
3559 xp->xp_pattern = arg;
3560 break;
3561# endif
3562
3563 case CMD_global:
3564 case CMD_vglobal:
3565 delim = *arg; /* get the delimiter */
3566 if (delim)
3567 ++arg; /* skip delimiter if there is one */
3568
3569 while (arg[0] != NUL && arg[0] != delim)
3570 {
3571 if (arg[0] == '\\' && arg[1] != NUL)
3572 ++arg;
3573 ++arg;
3574 }
3575 if (arg[0] != NUL)
3576 return arg + 1;
3577 break;
3578 case CMD_and:
3579 case CMD_substitute:
3580 delim = *arg;
3581 if (delim)
3582 {
3583 /* skip "from" part */
3584 ++arg;
3585 arg = skip_regexp(arg, delim, p_magic, NULL);
3586 }
3587 /* skip "to" part */
3588 while (arg[0] != NUL && arg[0] != delim)
3589 {
3590 if (arg[0] == '\\' && arg[1] != NUL)
3591 ++arg;
3592 ++arg;
3593 }
3594 if (arg[0] != NUL) /* skip delimiter */
3595 ++arg;
3596 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3597 ++arg;
3598 if (arg[0] != NUL)
3599 return arg;
3600 break;
3601 case CMD_isearch:
3602 case CMD_dsearch:
3603 case CMD_ilist:
3604 case CMD_dlist:
3605 case CMD_ijump:
3606 case CMD_psearch:
3607 case CMD_djump:
3608 case CMD_isplit:
3609 case CMD_dsplit:
3610 arg = skipwhite(skipdigits(arg)); /* skip count */
3611 if (*arg == '/') /* Match regexp, not just whole words */
3612 {
3613 for (++arg; *arg && *arg != '/'; arg++)
3614 if (*arg == '\\' && arg[1] != NUL)
3615 arg++;
3616 if (*arg)
3617 {
3618 arg = skipwhite(arg + 1);
3619
3620 /* Check for trailing illegal characters */
3621 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3622 xp->xp_context = EXPAND_NOTHING;
3623 else
3624 return arg;
3625 }
3626 }
3627 break;
3628#ifdef FEAT_AUTOCMD
3629 case CMD_autocmd:
3630 return set_context_in_autocmd(xp, arg, FALSE);
3631
3632 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003633 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 return set_context_in_autocmd(xp, arg, TRUE);
3635#endif
3636 case CMD_set:
3637 set_context_in_set_cmd(xp, arg, 0);
3638 break;
3639 case CMD_setglobal:
3640 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3641 break;
3642 case CMD_setlocal:
3643 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3644 break;
3645 case CMD_tag:
3646 case CMD_stag:
3647 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003648 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 case CMD_tselect:
3650 case CMD_stselect:
3651 case CMD_ptselect:
3652 case CMD_tjump:
3653 case CMD_stjump:
3654 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003655 if (*p_wop != NUL)
3656 xp->xp_context = EXPAND_TAGS_LISTFILES;
3657 else
3658 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 xp->xp_pattern = arg;
3660 break;
3661 case CMD_augroup:
3662 xp->xp_context = EXPAND_AUGROUP;
3663 xp->xp_pattern = arg;
3664 break;
3665#ifdef FEAT_SYN_HL
3666 case CMD_syntax:
3667 set_context_in_syntax_cmd(xp, arg);
3668 break;
3669#endif
3670#ifdef FEAT_EVAL
3671 case CMD_let:
3672 case CMD_if:
3673 case CMD_elseif:
3674 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003675 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 case CMD_echo:
3677 case CMD_echon:
3678 case CMD_execute:
3679 case CMD_echomsg:
3680 case CMD_echoerr:
3681 case CMD_call:
3682 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003683 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 break;
3685
3686 case CMD_unlet:
3687 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3688 arg = xp->xp_pattern + 1;
3689 xp->xp_context = EXPAND_USER_VARS;
3690 xp->xp_pattern = arg;
3691 break;
3692
3693 case CMD_function:
3694 case CMD_delfunction:
3695 xp->xp_context = EXPAND_USER_FUNC;
3696 xp->xp_pattern = arg;
3697 break;
3698
3699 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003700 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 break;
3702#endif
3703 case CMD_highlight:
3704 set_context_in_highlight_cmd(xp, arg);
3705 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003706#ifdef FEAT_CSCOPE
3707 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003708 case CMD_lcscope:
3709 case CMD_scscope:
3710 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003711 break;
3712#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003713#ifdef FEAT_SIGNS
3714 case CMD_sign:
3715 set_context_in_sign_cmd(xp, arg);
3716 break;
3717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718#ifdef FEAT_LISTCMDS
3719 case CMD_bdelete:
3720 case CMD_bwipeout:
3721 case CMD_bunload:
3722 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3723 arg = xp->xp_pattern + 1;
3724 /*FALLTHROUGH*/
3725 case CMD_buffer:
3726 case CMD_sbuffer:
3727 case CMD_checktime:
3728 xp->xp_context = EXPAND_BUFFERS;
3729 xp->xp_pattern = arg;
3730 break;
3731#endif
3732#ifdef FEAT_USR_CMDS
3733 case CMD_USER:
3734 case CMD_USER_BUF:
3735 if (compl != EXPAND_NOTHING)
3736 {
3737 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003738 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 {
3740# ifdef FEAT_MENU
3741 if (compl == EXPAND_MENUS)
3742 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3743# endif
3744 if (compl == EXPAND_COMMANDS)
3745 return arg;
3746 if (compl == EXPAND_MAPPINGS)
3747 return set_context_in_map_cmd(xp, (char_u *)"map",
3748 arg, forceit, FALSE, FALSE, CMD_map);
3749 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3750 arg = xp->xp_pattern + 1;
3751 xp->xp_pattern = arg;
3752 }
3753 xp->xp_context = compl;
3754 }
3755 break;
3756#endif
3757 case CMD_map: case CMD_noremap:
3758 case CMD_nmap: case CMD_nnoremap:
3759 case CMD_vmap: case CMD_vnoremap:
3760 case CMD_omap: case CMD_onoremap:
3761 case CMD_imap: case CMD_inoremap:
3762 case CMD_cmap: case CMD_cnoremap:
3763 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003764 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 case CMD_unmap:
3766 case CMD_nunmap:
3767 case CMD_vunmap:
3768 case CMD_ounmap:
3769 case CMD_iunmap:
3770 case CMD_cunmap:
3771 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003772 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 case CMD_abbreviate: case CMD_noreabbrev:
3774 case CMD_cabbrev: case CMD_cnoreabbrev:
3775 case CMD_iabbrev: case CMD_inoreabbrev:
3776 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003777 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 case CMD_unabbreviate:
3779 case CMD_cunabbrev:
3780 case CMD_iunabbrev:
3781 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003782 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783#ifdef FEAT_MENU
3784 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3785 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3786 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3787 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3788 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3789 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3790 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3791 case CMD_tmenu: case CMD_tunmenu:
3792 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3793 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3794#endif
3795
3796 case CMD_colorscheme:
3797 xp->xp_context = EXPAND_COLORS;
3798 xp->xp_pattern = arg;
3799 break;
3800
3801 case CMD_compiler:
3802 xp->xp_context = EXPAND_COMPILER;
3803 xp->xp_pattern = arg;
3804 break;
3805
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003806 case CMD_ownsyntax:
3807 xp->xp_context = EXPAND_FILETYPE;
3808 xp->xp_pattern = arg;
3809 break;
3810
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3812 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3813 case CMD_language:
3814 if (*skiptowhite(arg) == NUL)
3815 {
3816 xp->xp_context = EXPAND_LANGUAGE;
3817 xp->xp_pattern = arg;
3818 }
3819 else
3820 xp->xp_context = EXPAND_NOTHING;
3821 break;
3822#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003823#if defined(FEAT_PROFILE)
3824 case CMD_profile:
3825 set_context_in_profile_cmd(xp, arg);
3826 break;
3827#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003828 case CMD_behave:
3829 xp->xp_context = EXPAND_BEHAVE;
3830 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831
3832#endif /* FEAT_CMDL_COMPL */
3833
3834 default:
3835 break;
3836 }
3837 return NULL;
3838}
3839
3840/*
3841 * skip a range specifier of the form: addr [,addr] [;addr] ..
3842 *
3843 * Backslashed delimiters after / or ? will be skipped, and commands will
3844 * not be expanded between /'s and ?'s or after "'".
3845 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003846 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 * Returns the "cmd" pointer advanced to beyond the range.
3848 */
3849 char_u *
3850skip_range(cmd, ctx)
3851 char_u *cmd;
3852 int *ctx; /* pointer to xp_context or NULL */
3853{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003854 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855
Bram Moolenaardf177f62005-02-22 08:39:57 +00003856 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 {
3858 if (*cmd == '\'')
3859 {
3860 if (*++cmd == NUL && ctx != NULL)
3861 *ctx = EXPAND_NOTHING;
3862 }
3863 else if (*cmd == '/' || *cmd == '?')
3864 {
3865 delim = *cmd++;
3866 while (*cmd != NUL && *cmd != delim)
3867 if (*cmd++ == '\\' && *cmd != NUL)
3868 ++cmd;
3869 if (*cmd == NUL && ctx != NULL)
3870 *ctx = EXPAND_NOTHING;
3871 }
3872 if (*cmd != NUL)
3873 ++cmd;
3874 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003875
3876 /* Skip ":" and white space. */
3877 while (*cmd == ':')
3878 cmd = skipwhite(cmd + 1);
3879
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 return cmd;
3881}
3882
3883/*
3884 * get a single EX address
3885 *
3886 * Set ptr to the next character after the part that was interpreted.
3887 * Set ptr to NULL when an error is encountered.
3888 *
3889 * Return MAXLNUM when no Ex address was found.
3890 */
3891 static linenr_T
3892get_address(ptr, skip, to_other_file)
3893 char_u **ptr;
3894 int skip; /* only skip the address, don't use it */
3895 int to_other_file; /* flag: may jump to other file */
3896{
3897 int c;
3898 int i;
3899 long n;
3900 char_u *cmd;
3901 pos_T pos;
3902 pos_T *fp;
3903 linenr_T lnum;
3904
3905 cmd = skipwhite(*ptr);
3906 lnum = MAXLNUM;
3907 do
3908 {
3909 switch (*cmd)
3910 {
3911 case '.': /* '.' - Cursor position */
3912 ++cmd;
3913 lnum = curwin->w_cursor.lnum;
3914 break;
3915
3916 case '$': /* '$' - last line */
3917 ++cmd;
3918 lnum = curbuf->b_ml.ml_line_count;
3919 break;
3920
3921 case '\'': /* ''' - mark */
3922 if (*++cmd == NUL)
3923 {
3924 cmd = NULL;
3925 goto error;
3926 }
3927 if (skip)
3928 ++cmd;
3929 else
3930 {
3931 /* Only accept a mark in another file when it is
3932 * used by itself: ":'M". */
3933 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3934 ++cmd;
3935 if (fp == (pos_T *)-1)
3936 /* Jumped to another file. */
3937 lnum = curwin->w_cursor.lnum;
3938 else
3939 {
3940 if (check_mark(fp) == FAIL)
3941 {
3942 cmd = NULL;
3943 goto error;
3944 }
3945 lnum = fp->lnum;
3946 }
3947 }
3948 break;
3949
3950 case '/':
3951 case '?': /* '/' or '?' - search */
3952 c = *cmd++;
3953 if (skip) /* skip "/pat/" */
3954 {
3955 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3956 if (*cmd == c)
3957 ++cmd;
3958 }
3959 else
3960 {
3961 pos = curwin->w_cursor; /* save curwin->w_cursor */
3962 /*
3963 * When '/' or '?' follows another address, start
3964 * from there.
3965 */
3966 if (lnum != MAXLNUM)
3967 curwin->w_cursor.lnum = lnum;
3968 /*
3969 * Start a forward search at the end of the line.
3970 * Start a backward search at the start of the line.
3971 * This makes sure we never match in the current
3972 * line, and can match anywhere in the
3973 * next/previous line.
3974 */
3975 if (c == '/')
3976 curwin->w_cursor.col = MAXCOL;
3977 else
3978 curwin->w_cursor.col = 0;
3979 searchcmdlen = 0;
3980 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00003981 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 {
3983 curwin->w_cursor = pos;
3984 cmd = NULL;
3985 goto error;
3986 }
3987 lnum = curwin->w_cursor.lnum;
3988 curwin->w_cursor = pos;
3989 /* adjust command string pointer */
3990 cmd += searchcmdlen;
3991 }
3992 break;
3993
3994 case '\\': /* "\?", "\/" or "\&", repeat search */
3995 ++cmd;
3996 if (*cmd == '&')
3997 i = RE_SUBST;
3998 else if (*cmd == '?' || *cmd == '/')
3999 i = RE_SEARCH;
4000 else
4001 {
4002 EMSG(_(e_backslash));
4003 cmd = NULL;
4004 goto error;
4005 }
4006
4007 if (!skip)
4008 {
4009 /*
4010 * When search follows another address, start from
4011 * there.
4012 */
4013 if (lnum != MAXLNUM)
4014 pos.lnum = lnum;
4015 else
4016 pos.lnum = curwin->w_cursor.lnum;
4017
4018 /*
4019 * Start the search just like for the above
4020 * do_search().
4021 */
4022 if (*cmd != '?')
4023 pos.col = MAXCOL;
4024 else
4025 pos.col = 0;
4026 if (searchit(curwin, curbuf, &pos,
4027 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004028 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004029 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 lnum = pos.lnum;
4031 else
4032 {
4033 cmd = NULL;
4034 goto error;
4035 }
4036 }
4037 ++cmd;
4038 break;
4039
4040 default:
4041 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4042 lnum = getdigits(&cmd);
4043 }
4044
4045 for (;;)
4046 {
4047 cmd = skipwhite(cmd);
4048 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4049 break;
4050
4051 if (lnum == MAXLNUM)
4052 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4053 if (VIM_ISDIGIT(*cmd))
4054 i = '+'; /* "number" is same as "+number" */
4055 else
4056 i = *cmd++;
4057 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4058 n = 1;
4059 else
4060 n = getdigits(&cmd);
4061 if (i == '-')
4062 lnum -= n;
4063 else
4064 lnum += n;
4065 }
4066 } while (*cmd == '/' || *cmd == '?');
4067
4068error:
4069 *ptr = cmd;
4070 return lnum;
4071}
4072
4073/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004074 * Get flags from an Ex command argument.
4075 */
4076 static void
4077get_flags(eap)
4078 exarg_T *eap;
4079{
4080 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4081 {
4082 if (*eap->arg == 'l')
4083 eap->flags |= EXFLAG_LIST;
4084 else if (*eap->arg == 'p')
4085 eap->flags |= EXFLAG_PRINT;
4086 else
4087 eap->flags |= EXFLAG_NR;
4088 eap->arg = skipwhite(eap->arg + 1);
4089 }
4090}
4091
4092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 * Function called for command which is Not Implemented. NI!
4094 */
4095 void
4096ex_ni(eap)
4097 exarg_T *eap;
4098{
4099 if (!eap->skip)
4100 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4101}
4102
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004103#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104/*
4105 * Function called for script command which is Not Implemented. NI!
4106 * Skips over ":perl <<EOF" constructs.
4107 */
4108 static void
4109ex_script_ni(eap)
4110 exarg_T *eap;
4111{
4112 if (!eap->skip)
4113 ex_ni(eap);
4114 else
4115 vim_free(script_get(eap, eap->arg));
4116}
4117#endif
4118
4119/*
4120 * Check range in Ex command for validity.
4121 * Return NULL when valid, error message when invalid.
4122 */
4123 static char_u *
4124invalid_range(eap)
4125 exarg_T *eap;
4126{
4127 if ( eap->line1 < 0
4128 || eap->line2 < 0
4129 || eap->line1 > eap->line2
4130 || ((eap->argt & RANGE)
4131 && !(eap->argt & NOTADR)
4132 && eap->line2 > curbuf->b_ml.ml_line_count
4133#ifdef FEAT_DIFF
4134 + (eap->cmdidx == CMD_diffget)
4135#endif
4136 ))
4137 return (char_u *)_(e_invrange);
4138 return NULL;
4139}
4140
4141/*
4142 * Correct the range for zero line number, if required.
4143 */
4144 static void
4145correct_range(eap)
4146 exarg_T *eap;
4147{
4148 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4149 {
4150 if (eap->line1 == 0)
4151 eap->line1 = 1;
4152 if (eap->line2 == 0)
4153 eap->line2 = 1;
4154 }
4155}
4156
Bram Moolenaar748bf032005-02-02 23:04:36 +00004157#ifdef FEAT_QUICKFIX
4158static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4159
4160/*
4161 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4162 * pattern. Otherwise return eap->arg.
4163 */
4164 static char_u *
4165skip_grep_pat(eap)
4166 exarg_T *eap;
4167{
4168 char_u *p = eap->arg;
4169
Bram Moolenaara37420f2006-02-04 22:37:47 +00004170 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4171 || eap->cmdidx == CMD_vimgrepadd
4172 || eap->cmdidx == CMD_lvimgrepadd
4173 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004174 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004175 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004176 if (p == NULL)
4177 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004178 }
4179 return p;
4180}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004181
4182/*
4183 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4184 * in the command line, so that things like % get expanded.
4185 */
4186 static char_u *
4187replace_makeprg(eap, p, cmdlinep)
4188 exarg_T *eap;
4189 char_u *p;
4190 char_u **cmdlinep;
4191{
4192 char_u *new_cmdline;
4193 char_u *program;
4194 char_u *pos;
4195 char_u *ptr;
4196 int len;
4197 int i;
4198
4199 /*
4200 * Don't do it when ":vimgrep" is used for ":grep".
4201 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004202 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4203 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4204 || eap->cmdidx == CMD_grepadd
4205 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004206 && !grep_internal(eap->cmdidx))
4207 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004208 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4209 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004210 {
4211 if (*curbuf->b_p_gp == NUL)
4212 program = p_gp;
4213 else
4214 program = curbuf->b_p_gp;
4215 }
4216 else
4217 {
4218 if (*curbuf->b_p_mp == NUL)
4219 program = p_mp;
4220 else
4221 program = curbuf->b_p_mp;
4222 }
4223
4224 p = skipwhite(p);
4225
4226 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4227 {
4228 /* replace $* by given arguments */
4229 i = 1;
4230 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4231 ++i;
4232 len = (int)STRLEN(p);
4233 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4234 if (new_cmdline == NULL)
4235 return NULL; /* out of memory */
4236 ptr = new_cmdline;
4237 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4238 {
4239 i = (int)(pos - program);
4240 STRNCPY(ptr, program, i);
4241 STRCPY(ptr += i, p);
4242 ptr += len;
4243 program = pos + 2;
4244 }
4245 STRCPY(ptr, program);
4246 }
4247 else
4248 {
4249 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4250 if (new_cmdline == NULL)
4251 return NULL; /* out of memory */
4252 STRCPY(new_cmdline, program);
4253 STRCAT(new_cmdline, " ");
4254 STRCAT(new_cmdline, p);
4255 }
4256 msg_make(p);
4257
4258 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4259 vim_free(*cmdlinep);
4260 *cmdlinep = new_cmdline;
4261 p = new_cmdline;
4262 }
4263 return p;
4264}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004265#endif
4266
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267/*
4268 * Expand file name in Ex command argument.
4269 * Return FAIL for failure, OK otherwise.
4270 */
4271 int
4272expand_filename(eap, cmdlinep, errormsgp)
4273 exarg_T *eap;
4274 char_u **cmdlinep;
4275 char_u **errormsgp;
4276{
4277 int has_wildcards; /* need to expand wildcards */
4278 char_u *repl;
4279 int srclen;
4280 char_u *p;
4281 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004282 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283
Bram Moolenaar748bf032005-02-02 23:04:36 +00004284#ifdef FEAT_QUICKFIX
4285 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4286 p = skip_grep_pat(eap);
4287#else
4288 p = eap->arg;
4289#endif
4290
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 /*
4292 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4293 * the file name contains a wildcard it should not cause expanding.
4294 * (it will be expanded anyway if there is a wildcard before replacing).
4295 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004296 has_wildcards = mch_has_wildcard(p);
4297 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004299#ifdef FEAT_EVAL
4300 /* Skip over `=expr`, wildcards in it are not expanded. */
4301 if (p[0] == '`' && p[1] == '=')
4302 {
4303 p += 2;
4304 (void)skip_expr(&p);
4305 if (*p == '`')
4306 ++p;
4307 continue;
4308 }
4309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 /*
4311 * Quick check if this cannot be the start of a special string.
4312 * Also removes backslash before '%', '#' and '<'.
4313 */
4314 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4315 {
4316 ++p;
4317 continue;
4318 }
4319
4320 /*
4321 * Try to find a match at this position.
4322 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004323 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4324 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 if (*errormsgp != NULL) /* error detected */
4326 return FAIL;
4327 if (repl == NULL) /* no match found */
4328 {
4329 p += srclen;
4330 continue;
4331 }
4332
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004333 /* Wildcards won't be expanded below, the replacement is taken
4334 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4335 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4336 {
4337 char_u *l = repl;
4338
4339 repl = expand_env_save(repl);
4340 vim_free(l);
4341 }
4342
Bram Moolenaar63b92542007-03-27 14:57:09 +00004343 /* Need to escape white space et al. with a backslash.
4344 * Don't do this for:
4345 * - replacement that already has been escaped: "##"
4346 * - shell commands (may have to use quotes instead).
4347 * - non-unix systems when there is a single argument (spaces don't
4348 * separate arguments then).
4349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004351 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 && eap->cmdidx != CMD_bang
4353 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004354 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004356 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004358 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359#ifndef UNIX
4360 && !(eap->argt & NOSPC)
4361#endif
4362 )
4363 {
4364 char_u *l;
4365#ifdef BACKSLASH_IN_FILENAME
4366 /* Don't escape a backslash here, because rem_backslash() doesn't
4367 * remove it later. */
4368 static char_u *nobslash = (char_u *)" \t\"|";
4369# define ESCAPE_CHARS nobslash
4370#else
4371# define ESCAPE_CHARS escape_chars
4372#endif
4373
4374 for (l = repl; *l; ++l)
4375 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4376 {
4377 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4378 if (l != NULL)
4379 {
4380 vim_free(repl);
4381 repl = l;
4382 }
4383 break;
4384 }
4385 }
4386
4387 /* For a shell command a '!' must be escaped. */
4388 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004389 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 {
4391 char_u *l;
4392
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004393 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 if (l != NULL)
4395 {
4396 vim_free(repl);
4397 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004398 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 if (strstr((char *)p_sh, "sh") != NULL)
4400 {
4401 l = vim_strsave_escaped(repl, (char_u *)"!");
4402 if (l != NULL)
4403 {
4404 vim_free(repl);
4405 repl = l;
4406 }
4407 }
4408 }
4409 }
4410
4411 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4412 vim_free(repl);
4413 if (p == NULL)
4414 return FAIL;
4415 }
4416
4417 /*
4418 * One file argument: Expand wildcards.
4419 * Don't do this with ":r !command" or ":w !command".
4420 */
4421 if ((eap->argt & NOSPC) && !eap->usefilter)
4422 {
4423 /*
4424 * May do this twice:
4425 * 1. Replace environment variables.
4426 * 2. Replace any other wildcards, remove backslashes.
4427 */
4428 for (n = 1; n <= 2; ++n)
4429 {
4430 if (n == 2)
4431 {
4432#ifdef UNIX
4433 /*
4434 * Only for Unix we check for more than one file name.
4435 * For other systems spaces are considered to be part
4436 * of the file name.
4437 * Only check here if there is no wildcard, otherwise
4438 * ExpandOne() will check for errors. This allows
4439 * ":e `ls ve*.c`" on Unix.
4440 */
4441 if (!has_wildcards)
4442 for (p = eap->arg; *p; ++p)
4443 {
4444 /* skip escaped characters */
4445 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4446 ++p;
4447 else if (vim_iswhite(*p))
4448 {
4449 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4450 return FAIL;
4451 }
4452 }
4453#endif
4454
4455 /*
4456 * Halve the number of backslashes (this is Vi compatible).
4457 * For Unix and OS/2, when wildcards are expanded, this is
4458 * done by ExpandOne() below.
4459 */
4460#if defined(UNIX) || defined(OS2)
4461 if (!has_wildcards)
4462#endif
4463 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
4465
4466 if (has_wildcards)
4467 {
4468 if (n == 1)
4469 {
4470 /*
4471 * First loop: May expand environment variables. This
4472 * can be done much faster with expand_env() than with
4473 * something else (e.g., calling a shell).
4474 * After expanding environment variables, check again
4475 * if there are still wildcards present.
4476 */
4477 if (vim_strchr(eap->arg, '$') != NULL
4478 || vim_strchr(eap->arg, '~') != NULL)
4479 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004480 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004481 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 has_wildcards = mch_has_wildcard(NameBuff);
4483 p = NameBuff;
4484 }
4485 else
4486 p = NULL;
4487 }
4488 else /* n == 2 */
4489 {
4490 expand_T xpc;
4491
4492 ExpandInit(&xpc);
4493 xpc.xp_context = EXPAND_FILES;
4494 p = ExpandOne(&xpc, eap->arg, NULL,
4495 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4496 WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 if (p == NULL)
4498 return FAIL;
4499 }
4500 if (p != NULL)
4501 {
4502 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4503 p, cmdlinep);
4504 if (n == 2) /* p came from ExpandOne() */
4505 vim_free(p);
4506 }
4507 }
4508 }
4509 }
4510 return OK;
4511}
4512
4513/*
4514 * Replace part of the command line, keeping eap->cmd, eap->arg and
4515 * eap->nextcmd correct.
4516 * "src" points to the part that is to be replaced, of length "srclen".
4517 * "repl" is the replacement string.
4518 * Returns a pointer to the character after the replaced string.
4519 * Returns NULL for failure.
4520 */
4521 static char_u *
4522repl_cmdline(eap, src, srclen, repl, cmdlinep)
4523 exarg_T *eap;
4524 char_u *src;
4525 int srclen;
4526 char_u *repl;
4527 char_u **cmdlinep;
4528{
4529 int len;
4530 int i;
4531 char_u *new_cmdline;
4532
4533 /*
4534 * The new command line is build in new_cmdline[].
4535 * First allocate it.
4536 * Careful: a "+cmd" argument may have been NUL terminated.
4537 */
4538 len = (int)STRLEN(repl);
4539 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004540 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4542 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4543 return NULL; /* out of memory! */
4544
4545 /*
4546 * Copy the stuff before the expanded part.
4547 * Copy the expanded stuff.
4548 * Copy what came after the expanded part.
4549 * Copy the next commands, if there are any.
4550 */
4551 i = (int)(src - *cmdlinep); /* length of part before match */
4552 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004553
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 mch_memmove(new_cmdline + i, repl, (size_t)len);
4555 i += len; /* remember the end of the string */
4556 STRCPY(new_cmdline + i, src + srclen);
4557 src = new_cmdline + i; /* remember where to continue */
4558
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004559 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 {
4561 i = (int)STRLEN(new_cmdline) + 1;
4562 STRCPY(new_cmdline + i, eap->nextcmd);
4563 eap->nextcmd = new_cmdline + i;
4564 }
4565 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4566 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4567 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4568 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4569 vim_free(*cmdlinep);
4570 *cmdlinep = new_cmdline;
4571
4572 return src;
4573}
4574
4575/*
4576 * Check for '|' to separate commands and '"' to start comments.
4577 */
4578 void
4579separate_nextcmd(eap)
4580 exarg_T *eap;
4581{
4582 char_u *p;
4583
Bram Moolenaar86b68352004-12-27 21:59:20 +00004584#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004585 p = skip_grep_pat(eap);
4586#else
4587 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004588#endif
4589
4590 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 {
4592 if (*p == Ctrl_V)
4593 {
4594 if (eap->argt & (USECTRLV | XFILE))
4595 ++p; /* skip CTRL-V and next char */
4596 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004597 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004598 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 if (*p == NUL) /* stop at NUL after CTRL-V */
4600 break;
4601 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004602
4603#ifdef FEAT_EVAL
4604 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004605 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004606 {
4607 p += 2;
4608 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004609 }
4610#endif
4611
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 /* Check for '"': start of comment or '|': next command */
4613 /* :@" and :*" do not start a comment!
4614 * :redir @" doesn't either. */
4615 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4616 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4617 || p != eap->arg)
4618 && (eap->cmdidx != CMD_redir
4619 || p != eap->arg + 1 || p[-1] != '@'))
4620 || *p == '|' || *p == '\n')
4621 {
4622 /*
4623 * We remove the '\' before the '|', unless USECTRLV is used
4624 * AND 'b' is present in 'cpoptions'.
4625 */
4626 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4627 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4628 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004629 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 --p;
4631 }
4632 else
4633 {
4634 eap->nextcmd = check_nextcmd(p);
4635 *p = NUL;
4636 break;
4637 }
4638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004640
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4642 del_trailing_spaces(eap->arg);
4643}
4644
4645/*
4646 * get + command from ex argument
4647 */
4648 static char_u *
4649getargcmd(argp)
4650 char_u **argp;
4651{
4652 char_u *arg = *argp;
4653 char_u *command = NULL;
4654
4655 if (*arg == '+') /* +[command] */
4656 {
4657 ++arg;
4658 if (vim_isspace(*arg))
4659 command = dollar_command;
4660 else
4661 {
4662 command = arg;
4663 arg = skip_cmd_arg(command, TRUE);
4664 if (*arg != NUL)
4665 *arg++ = NUL; /* terminate command with NUL */
4666 }
4667
4668 arg = skipwhite(arg); /* skip over spaces */
4669 *argp = arg;
4670 }
4671 return command;
4672}
4673
4674/*
4675 * Find end of "+command" argument. Skip over "\ " and "\\".
4676 */
4677 static char_u *
4678skip_cmd_arg(p, rembs)
4679 char_u *p;
4680 int rembs; /* TRUE to halve the number of backslashes */
4681{
4682 while (*p && !vim_isspace(*p))
4683 {
4684 if (*p == '\\' && p[1] != NUL)
4685 {
4686 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004687 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 else
4689 ++p;
4690 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004691 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 }
4693 return p;
4694}
4695
4696/*
4697 * Get "++opt=arg" argument.
4698 * Return FAIL or OK.
4699 */
4700 static int
4701getargopt(eap)
4702 exarg_T *eap;
4703{
4704 char_u *arg = eap->arg + 2;
4705 int *pp = NULL;
4706#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004707 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 char_u *p;
4709#endif
4710
4711 /* ":edit ++[no]bin[ary] file" */
4712 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4713 {
4714 if (*arg == 'n')
4715 {
4716 arg += 2;
4717 eap->force_bin = FORCE_NOBIN;
4718 }
4719 else
4720 eap->force_bin = FORCE_BIN;
4721 if (!checkforcmd(&arg, "binary", 3))
4722 return FAIL;
4723 eap->arg = skipwhite(arg);
4724 return OK;
4725 }
4726
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004727 /* ":read ++edit file" */
4728 if (STRNCMP(arg, "edit", 4) == 0)
4729 {
4730 eap->read_edit = TRUE;
4731 eap->arg = skipwhite(arg + 4);
4732 return OK;
4733 }
4734
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 if (STRNCMP(arg, "ff", 2) == 0)
4736 {
4737 arg += 2;
4738 pp = &eap->force_ff;
4739 }
4740 else if (STRNCMP(arg, "fileformat", 10) == 0)
4741 {
4742 arg += 10;
4743 pp = &eap->force_ff;
4744 }
4745#ifdef FEAT_MBYTE
4746 else if (STRNCMP(arg, "enc", 3) == 0)
4747 {
4748 arg += 3;
4749 pp = &eap->force_enc;
4750 }
4751 else if (STRNCMP(arg, "encoding", 8) == 0)
4752 {
4753 arg += 8;
4754 pp = &eap->force_enc;
4755 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004756 else if (STRNCMP(arg, "bad", 3) == 0)
4757 {
4758 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004759 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761#endif
4762
4763 if (pp == NULL || *arg != '=')
4764 return FAIL;
4765
4766 ++arg;
4767 *pp = (int)(arg - eap->cmd);
4768 arg = skip_cmd_arg(arg, FALSE);
4769 eap->arg = skipwhite(arg);
4770 *arg = NUL;
4771
4772#ifdef FEAT_MBYTE
4773 if (pp == &eap->force_ff)
4774 {
4775#endif
4776 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4777 return FAIL;
4778#ifdef FEAT_MBYTE
4779 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004780 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 {
4782 /* Make 'fileencoding' lower case. */
4783 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4784 *p = TOLOWER_ASC(*p);
4785 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004786 else
4787 {
4788 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4789 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004790 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004791 if (STRICMP(p, "keep") == 0)
4792 eap->bad_char = BAD_KEEP;
4793 else if (STRICMP(p, "drop") == 0)
4794 eap->bad_char = BAD_DROP;
4795 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4796 eap->bad_char = *p;
4797 else
4798 return FAIL;
4799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800#endif
4801
4802 return OK;
4803}
4804
4805/*
4806 * ":abbreviate" and friends.
4807 */
4808 static void
4809ex_abbreviate(eap)
4810 exarg_T *eap;
4811{
4812 do_exmap(eap, TRUE); /* almost the same as mapping */
4813}
4814
4815/*
4816 * ":map" and friends.
4817 */
4818 static void
4819ex_map(eap)
4820 exarg_T *eap;
4821{
4822 /*
4823 * If we are sourcing .exrc or .vimrc in current directory we
4824 * print the mappings for security reasons.
4825 */
4826 if (secure)
4827 {
4828 secure = 2;
4829 msg_outtrans(eap->cmd);
4830 msg_putchar('\n');
4831 }
4832 do_exmap(eap, FALSE);
4833}
4834
4835/*
4836 * ":unmap" and friends.
4837 */
4838 static void
4839ex_unmap(eap)
4840 exarg_T *eap;
4841{
4842 do_exmap(eap, FALSE);
4843}
4844
4845/*
4846 * ":mapclear" and friends.
4847 */
4848 static void
4849ex_mapclear(eap)
4850 exarg_T *eap;
4851{
4852 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4853}
4854
4855/*
4856 * ":abclear" and friends.
4857 */
4858 static void
4859ex_abclear(eap)
4860 exarg_T *eap;
4861{
4862 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4863}
4864
4865#ifdef FEAT_AUTOCMD
4866 static void
4867ex_autocmd(eap)
4868 exarg_T *eap;
4869{
4870 /*
4871 * Disallow auto commands from .exrc and .vimrc in current
4872 * directory for security reasons.
4873 */
4874 if (secure)
4875 {
4876 secure = 2;
4877 eap->errmsg = e_curdir;
4878 }
4879 else if (eap->cmdidx == CMD_autocmd)
4880 do_autocmd(eap->arg, eap->forceit);
4881 else
4882 do_augroup(eap->arg, eap->forceit);
4883}
4884
4885/*
4886 * ":doautocmd": Apply the automatic commands to the current buffer.
4887 */
4888 static void
4889ex_doautocmd(eap)
4890 exarg_T *eap;
4891{
4892 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004893 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894}
4895#endif
4896
4897#ifdef FEAT_LISTCMDS
4898/*
4899 * :[N]bunload[!] [N] [bufname] unload buffer
4900 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4901 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4902 */
4903 static void
4904ex_bunload(eap)
4905 exarg_T *eap;
4906{
4907 eap->errmsg = do_bufdel(
4908 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4909 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4910 : DOBUF_UNLOAD, eap->arg,
4911 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4912}
4913
4914/*
4915 * :[N]buffer [N] to buffer N
4916 * :[N]sbuffer [N] to buffer N
4917 */
4918 static void
4919ex_buffer(eap)
4920 exarg_T *eap;
4921{
4922 if (*eap->arg)
4923 eap->errmsg = e_trailing;
4924 else
4925 {
4926 if (eap->addr_count == 0) /* default is current buffer */
4927 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4928 else
4929 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4930 }
4931}
4932
4933/*
4934 * :[N]bmodified [N] to next mod. buffer
4935 * :[N]sbmodified [N] to next mod. buffer
4936 */
4937 static void
4938ex_bmodified(eap)
4939 exarg_T *eap;
4940{
4941 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4942}
4943
4944/*
4945 * :[N]bnext [N] to next buffer
4946 * :[N]sbnext [N] split and to next buffer
4947 */
4948 static void
4949ex_bnext(eap)
4950 exarg_T *eap;
4951{
4952 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4953}
4954
4955/*
4956 * :[N]bNext [N] to previous buffer
4957 * :[N]bprevious [N] to previous buffer
4958 * :[N]sbNext [N] split and to previous buffer
4959 * :[N]sbprevious [N] split and to previous buffer
4960 */
4961 static void
4962ex_bprevious(eap)
4963 exarg_T *eap;
4964{
4965 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4966}
4967
4968/*
4969 * :brewind to first buffer
4970 * :bfirst to first buffer
4971 * :sbrewind split and to first buffer
4972 * :sbfirst split and to first buffer
4973 */
4974 static void
4975ex_brewind(eap)
4976 exarg_T *eap;
4977{
4978 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4979}
4980
4981/*
4982 * :blast to last buffer
4983 * :sblast split and to last buffer
4984 */
4985 static void
4986ex_blast(eap)
4987 exarg_T *eap;
4988{
4989 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4990}
4991#endif
4992
4993 int
4994ends_excmd(c)
4995 int c;
4996{
4997 return (c == NUL || c == '|' || c == '"' || c == '\n');
4998}
4999
5000#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5001 || defined(PROTO)
5002/*
5003 * Return the next command, after the first '|' or '\n'.
5004 * Return NULL if not found.
5005 */
5006 char_u *
5007find_nextcmd(p)
5008 char_u *p;
5009{
5010 while (*p != '|' && *p != '\n')
5011 {
5012 if (*p == NUL)
5013 return NULL;
5014 ++p;
5015 }
5016 return (p + 1);
5017}
5018#endif
5019
5020/*
5021 * Check if *p is a separator between Ex commands.
5022 * Return NULL if it isn't, (p + 1) if it is.
5023 */
5024 char_u *
5025check_nextcmd(p)
5026 char_u *p;
5027{
5028 p = skipwhite(p);
5029 if (*p == '|' || *p == '\n')
5030 return (p + 1);
5031 else
5032 return NULL;
5033}
5034
5035/*
5036 * - if there are more files to edit
5037 * - and this is the last window
5038 * - and forceit not used
5039 * - and not repeated twice on a row
5040 * return FAIL and give error message if 'message' TRUE
5041 * return OK otherwise
5042 */
5043 static int
5044check_more(message, forceit)
5045 int message; /* when FALSE check only, no messages */
5046 int forceit;
5047{
5048 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5049
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005050 if (!forceit && only_one_window()
5051 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 {
5053 if (message)
5054 {
5055#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5056 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5057 {
5058 char_u buff[IOSIZE];
5059
5060 if (n == 1)
5061 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
5062 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005063 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 _("%d more files to edit. Quit anyway?"), n);
5065 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5066 return OK;
5067 return FAIL;
5068 }
5069#endif
5070 if (n == 1)
5071 EMSG(_("E173: 1 more file to edit"));
5072 else
5073 EMSGN(_("E173: %ld more files to edit"), n);
5074 quitmore = 2; /* next try to quit is allowed */
5075 }
5076 return FAIL;
5077 }
5078 return OK;
5079}
5080
5081#ifdef FEAT_CMDL_COMPL
5082/*
5083 * Function given to ExpandGeneric() to obtain the list of command names.
5084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 char_u *
5086get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005087 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 int idx;
5089{
5090 if (idx >= (int)CMD_SIZE)
5091# ifdef FEAT_USR_CMDS
5092 return get_user_command_name(idx);
5093# else
5094 return NULL;
5095# endif
5096 return cmdnames[idx].cmd_name;
5097}
5098#endif
5099
5100#if defined(FEAT_USR_CMDS) || defined(PROTO)
5101static int uc_add_command __ARGS((char_u *name, size_t name_len, char_u *rep, long argt, long def, int flags, int compl, char_u *compl_arg, int force));
5102static void uc_list __ARGS((char_u *name, size_t name_len));
5103static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5104static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5105static size_t uc_check_code __ARGS((char_u *code, size_t len, char_u *buf, ucmd_T *cmd, exarg_T *eap, char_u **split_buf, size_t *split_len));
5106
5107 static int
5108uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5109 char_u *name;
5110 size_t name_len;
5111 char_u *rep;
5112 long argt;
5113 long def;
5114 int flags;
5115 int compl;
5116 char_u *compl_arg;
5117 int force;
5118{
5119 ucmd_T *cmd = NULL;
5120 char_u *p;
5121 int i;
5122 int cmp = 1;
5123 char_u *rep_buf = NULL;
5124 garray_T *gap;
5125
Bram Moolenaar9c102382006-05-03 21:26:49 +00005126 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 if (rep_buf == NULL)
5128 {
5129 /* Can't replace termcodes - try using the string as is */
5130 rep_buf = vim_strsave(rep);
5131
5132 /* Give up if out of memory */
5133 if (rep_buf == NULL)
5134 return FAIL;
5135 }
5136
5137 /* get address of growarray: global or in curbuf */
5138 if (flags & UC_BUFFER)
5139 {
5140 gap = &curbuf->b_ucmds;
5141 if (gap->ga_itemsize == 0)
5142 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5143 }
5144 else
5145 gap = &ucmds;
5146
5147 /* Search for the command in the already defined commands. */
5148 for (i = 0; i < gap->ga_len; ++i)
5149 {
5150 size_t len;
5151
5152 cmd = USER_CMD_GA(gap, i);
5153 len = STRLEN(cmd->uc_name);
5154 cmp = STRNCMP(name, cmd->uc_name, name_len);
5155 if (cmp == 0)
5156 {
5157 if (name_len < len)
5158 cmp = -1;
5159 else if (name_len > len)
5160 cmp = 1;
5161 }
5162
5163 if (cmp == 0)
5164 {
5165 if (!force)
5166 {
5167 EMSG(_("E174: Command already exists: add ! to replace it"));
5168 goto fail;
5169 }
5170
5171 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005172 cmd->uc_rep = NULL;
5173#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5174 vim_free(cmd->uc_compl_arg);
5175 cmd->uc_compl_arg = NULL;
5176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 break;
5178 }
5179
5180 /* Stop as soon as we pass the name to add */
5181 if (cmp < 0)
5182 break;
5183 }
5184
5185 /* Extend the array unless we're replacing an existing command */
5186 if (cmp != 0)
5187 {
5188 if (ga_grow(gap, 1) != OK)
5189 goto fail;
5190 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5191 goto fail;
5192
5193 cmd = USER_CMD_GA(gap, i);
5194 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5195
5196 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197
5198 cmd->uc_name = p;
5199 }
5200
5201 cmd->uc_rep = rep_buf;
5202 cmd->uc_argt = argt;
5203 cmd->uc_def = def;
5204 cmd->uc_compl = compl;
5205#ifdef FEAT_EVAL
5206 cmd->uc_scriptID = current_SID;
5207# ifdef FEAT_CMDL_COMPL
5208 cmd->uc_compl_arg = compl_arg;
5209# endif
5210#endif
5211
5212 return OK;
5213
5214fail:
5215 vim_free(rep_buf);
5216#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5217 vim_free(compl_arg);
5218#endif
5219 return FAIL;
5220}
5221
5222/*
5223 * List of names for completion for ":command" with the EXPAND_ flag.
5224 * Must be alphabetical for completion.
5225 */
5226static struct
5227{
5228 int expand;
5229 char *name;
5230} command_complete[] =
5231{
5232 {EXPAND_AUGROUP, "augroup"},
5233 {EXPAND_BUFFERS, "buffer"},
5234 {EXPAND_COMMANDS, "command"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005235#if defined(FEAT_CSCOPE)
5236 {EXPAND_CSCOPE, "cscope"},
5237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5239 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005240 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241#endif
5242 {EXPAND_DIRECTORIES, "dir"},
5243 {EXPAND_ENV_VARS, "environment"},
5244 {EXPAND_EVENTS, "event"},
5245 {EXPAND_EXPRESSION, "expression"},
5246 {EXPAND_FILES, "file"},
5247 {EXPAND_FUNCTIONS, "function"},
5248 {EXPAND_HELP, "help"},
5249 {EXPAND_HIGHLIGHT, "highlight"},
5250 {EXPAND_MAPPINGS, "mapping"},
5251 {EXPAND_MENUS, "menu"},
5252 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005253 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005254#if defined(FEAT_SIGNS)
5255 {EXPAND_SIGN, "sign"},
5256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 {EXPAND_TAGS, "tag"},
5258 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5259 {EXPAND_USER_VARS, "var"},
5260 {0, NULL}
5261};
5262
5263 static void
5264uc_list(name, name_len)
5265 char_u *name;
5266 size_t name_len;
5267{
5268 int i, j;
5269 int found = FALSE;
5270 ucmd_T *cmd;
5271 int len;
5272 long a;
5273 garray_T *gap;
5274
5275 gap = &curbuf->b_ucmds;
5276 for (;;)
5277 {
5278 for (i = 0; i < gap->ga_len; ++i)
5279 {
5280 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005281 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282
5283 /* Skip commands which don't match the requested prefix */
5284 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5285 continue;
5286
5287 /* Put out the title first time */
5288 if (!found)
5289 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5290 found = TRUE;
5291 msg_putchar('\n');
5292 if (got_int)
5293 break;
5294
5295 /* Special cases */
5296 msg_putchar(a & BANG ? '!' : ' ');
5297 msg_putchar(a & REGSTR ? '"' : ' ');
5298 msg_putchar(gap != &ucmds ? 'b' : ' ');
5299 msg_putchar(' ');
5300
5301 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5302 len = (int)STRLEN(cmd->uc_name) + 4;
5303
5304 do {
5305 msg_putchar(' ');
5306 ++len;
5307 } while (len < 16);
5308
5309 len = 0;
5310
5311 /* Arguments */
5312 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5313 {
5314 case 0: IObuff[len++] = '0'; break;
5315 case (EXTRA): IObuff[len++] = '*'; break;
5316 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5317 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5318 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5319 }
5320
5321 do {
5322 IObuff[len++] = ' ';
5323 } while (len < 5);
5324
5325 /* Range */
5326 if (a & (RANGE|COUNT))
5327 {
5328 if (a & COUNT)
5329 {
5330 /* -count=N */
5331 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5332 len += (int)STRLEN(IObuff + len);
5333 }
5334 else if (a & DFLALL)
5335 IObuff[len++] = '%';
5336 else if (cmd->uc_def >= 0)
5337 {
5338 /* -range=N */
5339 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5340 len += (int)STRLEN(IObuff + len);
5341 }
5342 else
5343 IObuff[len++] = '.';
5344 }
5345
5346 do {
5347 IObuff[len++] = ' ';
5348 } while (len < 11);
5349
5350 /* Completion */
5351 for (j = 0; command_complete[j].expand != 0; ++j)
5352 if (command_complete[j].expand == cmd->uc_compl)
5353 {
5354 STRCPY(IObuff + len, command_complete[j].name);
5355 len += (int)STRLEN(IObuff + len);
5356 break;
5357 }
5358
5359 do {
5360 IObuff[len++] = ' ';
5361 } while (len < 21);
5362
5363 IObuff[len] = '\0';
5364 msg_outtrans(IObuff);
5365
5366 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005367#ifdef FEAT_EVAL
5368 if (p_verbose > 0)
5369 last_set_msg(cmd->uc_scriptID);
5370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 out_flush();
5372 ui_breakcheck();
5373 if (got_int)
5374 break;
5375 }
5376 if (gap == &ucmds || i < gap->ga_len)
5377 break;
5378 gap = &ucmds;
5379 }
5380
5381 if (!found)
5382 MSG(_("No user-defined commands found"));
5383}
5384
5385 static char_u *
5386uc_fun_cmd()
5387{
5388 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5389 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5390 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5391 0xb9, 0x7f, 0};
5392 int i;
5393
5394 for (i = 0; fcmd[i]; ++i)
5395 IObuff[i] = fcmd[i] - 0x40;
5396 IObuff[i] = 0;
5397 return IObuff;
5398}
5399
5400 static int
5401uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5402 char_u *attr;
5403 size_t len;
5404 long *argt;
5405 long *def;
5406 int *flags;
5407 int *compl;
5408 char_u **compl_arg;
5409{
5410 char_u *p;
5411
5412 if (len == 0)
5413 {
5414 EMSG(_("E175: No attribute specified"));
5415 return FAIL;
5416 }
5417
5418 /* First, try the simple attributes (no arguments) */
5419 if (STRNICMP(attr, "bang", len) == 0)
5420 *argt |= BANG;
5421 else if (STRNICMP(attr, "buffer", len) == 0)
5422 *flags |= UC_BUFFER;
5423 else if (STRNICMP(attr, "register", len) == 0)
5424 *argt |= REGSTR;
5425 else if (STRNICMP(attr, "bar", len) == 0)
5426 *argt |= TRLBAR;
5427 else
5428 {
5429 int i;
5430 char_u *val = NULL;
5431 size_t vallen = 0;
5432 size_t attrlen = len;
5433
5434 /* Look for the attribute name - which is the part before any '=' */
5435 for (i = 0; i < (int)len; ++i)
5436 {
5437 if (attr[i] == '=')
5438 {
5439 val = &attr[i + 1];
5440 vallen = len - i - 1;
5441 attrlen = i;
5442 break;
5443 }
5444 }
5445
5446 if (STRNICMP(attr, "nargs", attrlen) == 0)
5447 {
5448 if (vallen == 1)
5449 {
5450 if (*val == '0')
5451 /* Do nothing - this is the default */;
5452 else if (*val == '1')
5453 *argt |= (EXTRA | NOSPC | NEEDARG);
5454 else if (*val == '*')
5455 *argt |= EXTRA;
5456 else if (*val == '?')
5457 *argt |= (EXTRA | NOSPC);
5458 else if (*val == '+')
5459 *argt |= (EXTRA | NEEDARG);
5460 else
5461 goto wrong_nargs;
5462 }
5463 else
5464 {
5465wrong_nargs:
5466 EMSG(_("E176: Invalid number of arguments"));
5467 return FAIL;
5468 }
5469 }
5470 else if (STRNICMP(attr, "range", attrlen) == 0)
5471 {
5472 *argt |= RANGE;
5473 if (vallen == 1 && *val == '%')
5474 *argt |= DFLALL;
5475 else if (val != NULL)
5476 {
5477 p = val;
5478 if (*def >= 0)
5479 {
5480two_count:
5481 EMSG(_("E177: Count cannot be specified twice"));
5482 return FAIL;
5483 }
5484
5485 *def = getdigits(&p);
5486 *argt |= (ZEROR | NOTADR);
5487
5488 if (p != val + vallen || vallen == 0)
5489 {
5490invalid_count:
5491 EMSG(_("E178: Invalid default value for count"));
5492 return FAIL;
5493 }
5494 }
5495 }
5496 else if (STRNICMP(attr, "count", attrlen) == 0)
5497 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005498 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499
5500 if (val != NULL)
5501 {
5502 p = val;
5503 if (*def >= 0)
5504 goto two_count;
5505
5506 *def = getdigits(&p);
5507
5508 if (p != val + vallen)
5509 goto invalid_count;
5510 }
5511
5512 if (*def < 0)
5513 *def = 0;
5514 }
5515 else if (STRNICMP(attr, "complete", attrlen) == 0)
5516 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 if (val == NULL)
5518 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005519 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 return FAIL;
5521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005523 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5524 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 }
5527 else
5528 {
5529 char_u ch = attr[len];
5530 attr[len] = '\0';
5531 EMSG2(_("E181: Invalid attribute: %s"), attr);
5532 attr[len] = ch;
5533 return FAIL;
5534 }
5535 }
5536
5537 return OK;
5538}
5539
Bram Moolenaara850a712009-01-28 14:42:59 +00005540/*
5541 * ":command ..."
5542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 static void
5544ex_command(eap)
5545 exarg_T *eap;
5546{
5547 char_u *name;
5548 char_u *end;
5549 char_u *p;
5550 long argt = 0;
5551 long def = -1;
5552 int flags = 0;
5553 int compl = EXPAND_NOTHING;
5554 char_u *compl_arg = NULL;
5555 int has_attr = (eap->arg[0] == '-');
5556
5557 p = eap->arg;
5558
5559 /* Check for attributes */
5560 while (*p == '-')
5561 {
5562 ++p;
5563 end = skiptowhite(p);
5564 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5565 == FAIL)
5566 return;
5567 p = skipwhite(end);
5568 }
5569
5570 /* Get the name (if any) and skip to the following argument */
5571 name = p;
5572 if (ASCII_ISALPHA(*p))
5573 while (ASCII_ISALNUM(*p))
5574 ++p;
5575 if (!ends_excmd(*p) && !vim_iswhite(*p))
5576 {
5577 EMSG(_("E182: Invalid command name"));
5578 return;
5579 }
5580 end = p;
5581
5582 /* If there is nothing after the name, and no attributes were specified,
5583 * we are listing commands
5584 */
5585 p = skipwhite(end);
5586 if (!has_attr && ends_excmd(*p))
5587 {
5588 uc_list(name, end - name);
5589 }
5590 else if (!ASCII_ISUPPER(*name))
5591 {
5592 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5593 return;
5594 }
5595 else
5596 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5597 eap->forceit);
5598}
5599
5600/*
5601 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 * Clear all user commands, global and for current buffer.
5603 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005604 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005606 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607{
5608 uc_clear(&ucmds);
5609 uc_clear(&curbuf->b_ucmds);
5610}
5611
5612/*
5613 * Clear all user commands for "gap".
5614 */
5615 void
5616uc_clear(gap)
5617 garray_T *gap;
5618{
5619 int i;
5620 ucmd_T *cmd;
5621
5622 for (i = 0; i < gap->ga_len; ++i)
5623 {
5624 cmd = USER_CMD_GA(gap, i);
5625 vim_free(cmd->uc_name);
5626 vim_free(cmd->uc_rep);
5627# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5628 vim_free(cmd->uc_compl_arg);
5629# endif
5630 }
5631 ga_clear(gap);
5632}
5633
5634 static void
5635ex_delcommand(eap)
5636 exarg_T *eap;
5637{
5638 int i = 0;
5639 ucmd_T *cmd = NULL;
5640 int cmp = -1;
5641 garray_T *gap;
5642
5643 gap = &curbuf->b_ucmds;
5644 for (;;)
5645 {
5646 for (i = 0; i < gap->ga_len; ++i)
5647 {
5648 cmd = USER_CMD_GA(gap, i);
5649 cmp = STRCMP(eap->arg, cmd->uc_name);
5650 if (cmp <= 0)
5651 break;
5652 }
5653 if (gap == &ucmds || cmp == 0)
5654 break;
5655 gap = &ucmds;
5656 }
5657
5658 if (cmp != 0)
5659 {
5660 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5661 return;
5662 }
5663
5664 vim_free(cmd->uc_name);
5665 vim_free(cmd->uc_rep);
5666# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5667 vim_free(cmd->uc_compl_arg);
5668# endif
5669
5670 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671
5672 if (i < gap->ga_len)
5673 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5674}
5675
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005676/*
5677 * split and quote args for <f-args>
5678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 static char_u *
5680uc_split_args(arg, lenp)
5681 char_u *arg;
5682 size_t *lenp;
5683{
5684 char_u *buf;
5685 char_u *p;
5686 char_u *q;
5687 int len;
5688
5689 /* Precalculate length */
5690 p = arg;
5691 len = 2; /* Initial and final quotes */
5692
5693 while (*p)
5694 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005695 if (p[0] == '\\' && p[1] == '\\')
5696 {
5697 len += 2;
5698 p += 2;
5699 }
5700 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
5702 len += 1;
5703 p += 2;
5704 }
5705 else if (*p == '\\' || *p == '"')
5706 {
5707 len += 2;
5708 p += 1;
5709 }
5710 else if (vim_iswhite(*p))
5711 {
5712 p = skipwhite(p);
5713 if (*p == NUL)
5714 break;
5715 len += 3; /* "," */
5716 }
5717 else
5718 {
5719 ++len;
5720 ++p;
5721 }
5722 }
5723
5724 buf = alloc(len + 1);
5725 if (buf == NULL)
5726 {
5727 *lenp = 0;
5728 return buf;
5729 }
5730
5731 p = arg;
5732 q = buf;
5733 *q++ = '"';
5734 while (*p)
5735 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005736 if (p[0] == '\\' && p[1] == '\\')
5737 {
5738 *q++ = '\\';
5739 *q++ = '\\';
5740 p += 2;
5741 }
5742 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 {
5744 *q++ = p[1];
5745 p += 2;
5746 }
5747 else if (*p == '\\' || *p == '"')
5748 {
5749 *q++ = '\\';
5750 *q++ = *p++;
5751 }
5752 else if (vim_iswhite(*p))
5753 {
5754 p = skipwhite(p);
5755 if (*p == NUL)
5756 break;
5757 *q++ = '"';
5758 *q++ = ',';
5759 *q++ = '"';
5760 }
5761 else
5762 {
5763 *q++ = *p++;
5764 }
5765 }
5766 *q++ = '"';
5767 *q = 0;
5768
5769 *lenp = len;
5770 return buf;
5771}
5772
5773/*
5774 * Check for a <> code in a user command.
5775 * "code" points to the '<'. "len" the length of the <> (inclusive).
5776 * "buf" is where the result is to be added.
5777 * "split_buf" points to a buffer used for splitting, caller should free it.
5778 * "split_len" is the length of what "split_buf" contains.
5779 * Returns the length of the replacement, which has been added to "buf".
5780 * Returns -1 if there was no match, and only the "<" has been copied.
5781 */
5782 static size_t
5783uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5784 char_u *code;
5785 size_t len;
5786 char_u *buf;
5787 ucmd_T *cmd; /* the user command we're expanding */
5788 exarg_T *eap; /* ex arguments */
5789 char_u **split_buf;
5790 size_t *split_len;
5791{
5792 size_t result = 0;
5793 char_u *p = code + 1;
5794 size_t l = len - 2;
5795 int quote = 0;
5796 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5797 ct_LT, ct_NONE } type = ct_NONE;
5798
5799 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5800 {
5801 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5802 p += 2;
5803 l -= 2;
5804 }
5805
Bram Moolenaar371d5402006-03-20 21:47:49 +00005806 ++l;
5807 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005809 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005811 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005813 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005815 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005817 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005819 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005821 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 type = ct_REGISTER;
5823
5824 switch (type)
5825 {
5826 case ct_ARGS:
5827 /* Simple case first */
5828 if (*eap->arg == NUL)
5829 {
5830 if (quote == 1)
5831 {
5832 result = 2;
5833 if (buf != NULL)
5834 STRCPY(buf, "''");
5835 }
5836 else
5837 result = 0;
5838 break;
5839 }
5840
5841 /* When specified there is a single argument don't split it.
5842 * Works for ":Cmd %" when % is "a b c". */
5843 if ((eap->argt & NOSPC) && quote == 2)
5844 quote = 1;
5845
5846 switch (quote)
5847 {
5848 case 0: /* No quoting, no splitting */
5849 result = STRLEN(eap->arg);
5850 if (buf != NULL)
5851 STRCPY(buf, eap->arg);
5852 break;
5853 case 1: /* Quote, but don't split */
5854 result = STRLEN(eap->arg) + 2;
5855 for (p = eap->arg; *p; ++p)
5856 {
5857 if (*p == '\\' || *p == '"')
5858 ++result;
5859 }
5860
5861 if (buf != NULL)
5862 {
5863 *buf++ = '"';
5864 for (p = eap->arg; *p; ++p)
5865 {
5866 if (*p == '\\' || *p == '"')
5867 *buf++ = '\\';
5868 *buf++ = *p;
5869 }
5870 *buf = '"';
5871 }
5872
5873 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005874 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 /* This is hard, so only do it once, and cache the result */
5876 if (*split_buf == NULL)
5877 *split_buf = uc_split_args(eap->arg, split_len);
5878
5879 result = *split_len;
5880 if (buf != NULL && result != 0)
5881 STRCPY(buf, *split_buf);
5882
5883 break;
5884 }
5885 break;
5886
5887 case ct_BANG:
5888 result = eap->forceit ? 1 : 0;
5889 if (quote)
5890 result += 2;
5891 if (buf != NULL)
5892 {
5893 if (quote)
5894 *buf++ = '"';
5895 if (eap->forceit)
5896 *buf++ = '!';
5897 if (quote)
5898 *buf = '"';
5899 }
5900 break;
5901
5902 case ct_LINE1:
5903 case ct_LINE2:
5904 case ct_COUNT:
5905 {
5906 char num_buf[20];
5907 long num = (type == ct_LINE1) ? eap->line1 :
5908 (type == ct_LINE2) ? eap->line2 :
5909 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5910 size_t num_len;
5911
5912 sprintf(num_buf, "%ld", num);
5913 num_len = STRLEN(num_buf);
5914 result = num_len;
5915
5916 if (quote)
5917 result += 2;
5918
5919 if (buf != NULL)
5920 {
5921 if (quote)
5922 *buf++ = '"';
5923 STRCPY(buf, num_buf);
5924 buf += num_len;
5925 if (quote)
5926 *buf = '"';
5927 }
5928
5929 break;
5930 }
5931
5932 case ct_REGISTER:
5933 result = eap->regname ? 1 : 0;
5934 if (quote)
5935 result += 2;
5936 if (buf != NULL)
5937 {
5938 if (quote)
5939 *buf++ = '\'';
5940 if (eap->regname)
5941 *buf++ = eap->regname;
5942 if (quote)
5943 *buf = '\'';
5944 }
5945 break;
5946
5947 case ct_LT:
5948 result = 1;
5949 if (buf != NULL)
5950 *buf = '<';
5951 break;
5952
5953 default:
5954 /* Not recognized: just copy the '<' and return -1. */
5955 result = (size_t)-1;
5956 if (buf != NULL)
5957 *buf = '<';
5958 break;
5959 }
5960
5961 return result;
5962}
5963
5964 static void
5965do_ucmd(eap)
5966 exarg_T *eap;
5967{
5968 char_u *buf;
5969 char_u *p;
5970 char_u *q;
5971
5972 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00005973 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00005974 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 size_t len, totlen;
5976
5977 size_t split_len = 0;
5978 char_u *split_buf = NULL;
5979 ucmd_T *cmd;
5980#ifdef FEAT_EVAL
5981 scid_T save_current_SID = current_SID;
5982#endif
5983
5984 if (eap->cmdidx == CMD_USER)
5985 cmd = USER_CMD(eap->useridx);
5986 else
5987 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5988
5989 /*
5990 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00005991 * First round: "buf" is NULL, compute length, allocate "buf".
5992 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 */
5994 buf = NULL;
5995 for (;;)
5996 {
Bram Moolenaara850a712009-01-28 14:42:59 +00005997 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005998 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006000
6001 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006003 start = vim_strchr(p, '<');
6004 if (start != NULL)
6005 end = vim_strchr(start + 1, '>');
6006 if (buf != NULL)
6007 {
6008 ksp = vim_strchr(p, K_SPECIAL);
6009 if (ksp != NULL && (start == NULL || ksp < start || end == NULL)
6010 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6011# ifdef FEAT_GUI
6012 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6013# endif
6014 ))
6015 {
6016 /* K_SPECIAL han been put in the buffer as K_SPECIAL
6017 * KS_SPECIAL KE_FILLER, like for mappings, but
6018 * do_cmdline() doesn't handle that, so convert it back.
6019 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6020 len = ksp - p;
6021 if (len > 0)
6022 {
6023 mch_memmove(q, p, len);
6024 q += len;
6025 }
6026 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6027 p = ksp + 3;
6028 continue;
6029 }
6030 }
6031
6032 /* break if there no <item> is found */
6033 if (start == NULL || end == NULL)
6034 break;
6035
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 /* Include the '>' */
6037 ++end;
6038
6039 /* Take everything up to the '<' */
6040 len = start - p;
6041 if (buf == NULL)
6042 totlen += len;
6043 else
6044 {
6045 mch_memmove(q, p, len);
6046 q += len;
6047 }
6048
6049 len = uc_check_code(start, end - start, q, cmd, eap,
6050 &split_buf, &split_len);
6051 if (len == (size_t)-1)
6052 {
6053 /* no match, continue after '<' */
6054 p = start + 1;
6055 len = 1;
6056 }
6057 else
6058 p = end;
6059 if (buf == NULL)
6060 totlen += len;
6061 else
6062 q += len;
6063 }
6064 if (buf != NULL) /* second time here, finished */
6065 {
6066 STRCPY(q, p);
6067 break;
6068 }
6069
6070 totlen += STRLEN(p); /* Add on the trailing characters */
6071 buf = alloc((unsigned)(totlen + 1));
6072 if (buf == NULL)
6073 {
6074 vim_free(split_buf);
6075 return;
6076 }
6077 }
6078
6079#ifdef FEAT_EVAL
6080 current_SID = cmd->uc_scriptID;
6081#endif
6082 (void)do_cmdline(buf, eap->getline, eap->cookie,
6083 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6084#ifdef FEAT_EVAL
6085 current_SID = save_current_SID;
6086#endif
6087 vim_free(buf);
6088 vim_free(split_buf);
6089}
6090
6091# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6092 static char_u *
6093get_user_command_name(idx)
6094 int idx;
6095{
6096 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6097}
6098
6099/*
6100 * Function given to ExpandGeneric() to obtain the list of user command names.
6101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 char_u *
6103get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006104 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 int idx;
6106{
6107 if (idx < curbuf->b_ucmds.ga_len)
6108 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6109 idx -= curbuf->b_ucmds.ga_len;
6110 if (idx < ucmds.ga_len)
6111 return USER_CMD(idx)->uc_name;
6112 return NULL;
6113}
6114
6115/*
6116 * Function given to ExpandGeneric() to obtain the list of user command
6117 * attributes.
6118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 char_u *
6120get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006121 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 int idx;
6123{
6124 static char *user_cmd_flags[] =
6125 {"bang", "bar", "buffer", "complete", "count",
6126 "nargs", "range", "register"};
6127
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006128 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 return NULL;
6130 return (char_u *)user_cmd_flags[idx];
6131}
6132
6133/*
6134 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 char_u *
6137get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006138 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 int idx;
6140{
6141 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6142
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006143 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 return NULL;
6145 return (char_u *)user_cmd_nargs[idx];
6146}
6147
6148/*
6149 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 char_u *
6152get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006153 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 int idx;
6155{
6156 return (char_u *)command_complete[idx].name;
6157}
6158# endif /* FEAT_CMDL_COMPL */
6159
6160#endif /* FEAT_USR_CMDS */
6161
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006162#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6163/*
6164 * Parse a completion argument "value[vallen]".
6165 * The detected completion goes in "*complp", argument type in "*argt".
6166 * When there is an argument, for function and user defined completion, it's
6167 * copied to allocated memory and stored in "*compl_arg".
6168 * Returns FAIL if something is wrong.
6169 */
6170 int
6171parse_compl_arg(value, vallen, complp, argt, compl_arg)
6172 char_u *value;
6173 int vallen;
6174 int *complp;
6175 long *argt;
6176 char_u **compl_arg;
6177{
6178 char_u *arg = NULL;
6179 size_t arglen = 0;
6180 int i;
6181 int valend = vallen;
6182
6183 /* Look for any argument part - which is the part after any ',' */
6184 for (i = 0; i < vallen; ++i)
6185 {
6186 if (value[i] == ',')
6187 {
6188 arg = &value[i + 1];
6189 arglen = vallen - i - 1;
6190 valend = i;
6191 break;
6192 }
6193 }
6194
6195 for (i = 0; command_complete[i].expand != 0; ++i)
6196 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006197 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006198 && STRNCMP(value, command_complete[i].name, valend) == 0)
6199 {
6200 *complp = command_complete[i].expand;
6201 if (command_complete[i].expand == EXPAND_BUFFERS)
6202 *argt |= BUFNAME;
6203 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6204 || command_complete[i].expand == EXPAND_FILES)
6205 *argt |= XFILE;
6206 break;
6207 }
6208 }
6209
6210 if (command_complete[i].expand == 0)
6211 {
6212 EMSG2(_("E180: Invalid complete value: %s"), value);
6213 return FAIL;
6214 }
6215
6216# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6217 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6218 && arg != NULL)
6219# else
6220 if (arg != NULL)
6221# endif
6222 {
6223 EMSG(_("E468: Completion argument only allowed for custom completion"));
6224 return FAIL;
6225 }
6226
6227# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6228 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6229 && arg == NULL)
6230 {
6231 EMSG(_("E467: Custom completion requires a function argument"));
6232 return FAIL;
6233 }
6234
6235 if (arg != NULL)
6236 *compl_arg = vim_strnsave(arg, (int)arglen);
6237# endif
6238 return OK;
6239}
6240#endif
6241
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 static void
6243ex_colorscheme(eap)
6244 exarg_T *eap;
6245{
Bram Moolenaare6850792010-05-14 15:28:44 +02006246 if (*eap->arg == NUL)
6247 {
6248#ifdef FEAT_EVAL
6249 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6250 char_u *p = NULL;
6251
6252 if (expr != NULL)
6253 {
6254 ++emsg_off;
6255 p = eval_to_string(expr, NULL, FALSE);
6256 --emsg_off;
6257 vim_free(expr);
6258 }
6259 if (p != NULL)
6260 {
6261 MSG(p);
6262 vim_free(p);
6263 }
6264 else
6265 MSG("default");
6266#else
6267 MSG(_("unknown"));
6268#endif
6269 }
6270 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6272}
6273
6274 static void
6275ex_highlight(eap)
6276 exarg_T *eap;
6277{
6278 if (*eap->arg == NUL && eap->cmd[2] == '!')
6279 MSG(_("Greetings, Vim user!"));
6280 do_highlight(eap->arg, eap->forceit, FALSE);
6281}
6282
6283
6284/*
6285 * Call this function if we thought we were going to exit, but we won't
6286 * (because of an error). May need to restore the terminal mode.
6287 */
6288 void
6289not_exiting()
6290{
6291 exiting = FALSE;
6292 settmode(TMODE_RAW);
6293}
6294
6295/*
6296 * ":quit": quit current window, quit Vim if closed the last window.
6297 */
6298 static void
6299ex_quit(eap)
6300 exarg_T *eap;
6301{
6302#ifdef FEAT_CMDWIN
6303 if (cmdwin_type != 0)
6304 {
6305 cmdwin_result = Ctrl_C;
6306 return;
6307 }
6308#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006309 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006310 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006311 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006312 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006313 return;
6314 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006315#ifdef FEAT_AUTOCMD
6316 if (curbuf_locked())
6317 return;
6318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319
6320#ifdef FEAT_NETBEANS_INTG
6321 netbeansForcedQuit = eap->forceit;
6322#endif
6323
6324 /*
6325 * If there are more files or windows we won't exit.
6326 */
6327 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6328 exiting = TRUE;
6329 if ((!P_HID(curbuf)
6330 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6331 || check_more(TRUE, eap->forceit) == FAIL
6332 || (only_one_window() && check_changed_any(eap->forceit)))
6333 {
6334 not_exiting();
6335 }
6336 else
6337 {
6338#ifdef FEAT_WINDOWS
6339 if (only_one_window()) /* quit last window */
6340#endif
6341 getout(0);
6342#ifdef FEAT_WINDOWS
6343# ifdef FEAT_GUI
6344 need_mouse_correct = TRUE;
6345# endif
6346 /* close window; may free buffer */
6347 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6348#endif
6349 }
6350}
6351
6352/*
6353 * ":cquit".
6354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 static void
6356ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006357 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
6359 getout(1); /* this does not always pass on the exit code to the Manx
6360 compiler. why? */
6361}
6362
6363/*
6364 * ":qall": try to quit all windows
6365 */
6366 static void
6367ex_quit_all(eap)
6368 exarg_T *eap;
6369{
6370# ifdef FEAT_CMDWIN
6371 if (cmdwin_type != 0)
6372 {
6373 if (eap->forceit)
6374 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6375 else
6376 cmdwin_result = K_XF2;
6377 return;
6378 }
6379# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006380
6381 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006382 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006383 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006384 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006385 return;
6386 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006387#ifdef FEAT_AUTOCMD
6388 if (curbuf_locked())
6389 return;
6390#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006391
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 exiting = TRUE;
6393 if (eap->forceit || !check_changed_any(FALSE))
6394 getout(0);
6395 not_exiting();
6396}
6397
Bram Moolenaard9967712006-03-11 21:18:15 +00006398#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399/*
6400 * ":close": close current window, unless it is the last one
6401 */
6402 static void
6403ex_close(eap)
6404 exarg_T *eap;
6405{
6406# ifdef FEAT_CMDWIN
6407 if (cmdwin_type != 0)
6408 cmdwin_result = K_IGNORE;
6409 else
6410# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006411 if (!text_locked()
6412#ifdef FEAT_AUTOCMD
6413 && !curbuf_locked()
6414#endif
6415 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006416 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417}
6418
Bram Moolenaard9967712006-03-11 21:18:15 +00006419# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006420/*
6421 * ":pclose": Close any preview window.
6422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006424ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006426{
6427 win_T *win;
6428
6429 for (win = firstwin; win != NULL; win = win->w_next)
6430 if (win->w_p_pvw)
6431 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006432 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006433 break;
6434 }
6435}
Bram Moolenaard9967712006-03-11 21:18:15 +00006436# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006437
Bram Moolenaarf740b292006-02-16 22:11:02 +00006438/*
6439 * Close window "win" and take care of handling closing the last window for a
6440 * modified buffer.
6441 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006442 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006443ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006444 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006446 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447{
6448 int need_hide;
6449 buf_T *buf = win->w_buffer;
6450
6451 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006452 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006454# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 if ((p_confirm || cmdmod.confirm) && p_write)
6456 {
6457 dialog_changed(buf, FALSE);
6458 if (buf_valid(buf) && bufIsChanged(buf))
6459 return;
6460 need_hide = FALSE;
6461 }
6462 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006463# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 {
6465 EMSG(_(e_nowrtmsg));
6466 return;
6467 }
6468 }
6469
Bram Moolenaard9967712006-03-11 21:18:15 +00006470# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006472# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006473
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006475 if (tp == NULL)
6476 win_close(win, !need_hide && !P_HID(buf));
6477 else
6478 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479}
6480
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006482 * ":tabclose": close current tab page, unless it is the last one.
6483 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 */
6485 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006486ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 exarg_T *eap;
6488{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006489 tabpage_T *tp;
6490
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006491# ifdef FEAT_CMDWIN
6492 if (cmdwin_type != 0)
6493 cmdwin_result = K_IGNORE;
6494 else
6495# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006496 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006497 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006498 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006500 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006501 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006502 tp = find_tabpage((int)eap->line2);
6503 if (tp == NULL)
6504 {
6505 beep_flush();
6506 return;
6507 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006508 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006509 {
6510 tabpage_close_other(tp, eap->forceit);
6511 return;
6512 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006513 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006514 if (!text_locked()
6515#ifdef FEAT_AUTOCMD
6516 && !curbuf_locked()
6517#endif
6518 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006519 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006521}
6522
6523/*
6524 * ":tabonly": close all tab pages except the current one
6525 */
6526 static void
6527ex_tabonly(eap)
6528 exarg_T *eap;
6529{
6530 tabpage_T *tp;
6531 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006532
6533# ifdef FEAT_CMDWIN
6534 if (cmdwin_type != 0)
6535 cmdwin_result = K_IGNORE;
6536 else
6537# endif
6538 if (first_tabpage->tp_next == NULL)
6539 MSG(_("Already only one tab page"));
6540 else
6541 {
6542 /* Repeat this up to a 1000 times, because autocommands may mess
6543 * up the lists. */
6544 for (done = 0; done < 1000; ++done)
6545 {
6546 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6547 if (tp->tp_topframe != topframe)
6548 {
6549 tabpage_close_other(tp, eap->forceit);
6550 /* if we failed to close it quit */
6551 if (valid_tabpage(tp))
6552 done = 1000;
6553 /* start over, "tp" is now invalid */
6554 break;
6555 }
6556 if (first_tabpage->tp_next == NULL)
6557 break;
6558 }
6559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561
6562/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006563 * Close the current tab page.
6564 */
6565 void
6566tabpage_close(forceit)
6567 int forceit;
6568{
6569 /* First close all the windows but the current one. If that worked then
6570 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006571 if (lastwin != firstwin)
6572 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006573 if (lastwin == firstwin)
6574 ex_win_close(forceit, curwin, NULL);
6575# ifdef FEAT_GUI
6576 need_mouse_correct = TRUE;
6577# endif
6578}
6579
6580/*
6581 * Close tab page "tp", which is not the current tab page.
6582 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006583 * Also takes care of the tab pages line disappearing when closing the
6584 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006585 */
6586 void
6587tabpage_close_other(tp, forceit)
6588 tabpage_T *tp;
6589 int forceit;
6590{
6591 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006592 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006593 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006594
6595 /* Limit to 1000 windows, autocommands may add a window while we close
6596 * one. OK, so I'm paranoid... */
6597 while (++done < 1000)
6598 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006599 wp = tp->tp_firstwin;
6600 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006601
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006602 /* Autocommands may delete the tab page under our fingers and we may
6603 * fail to close a window with a modified buffer. */
6604 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006605 break;
6606 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006607
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006608 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006609 if (h != tabline_height())
6610 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006611}
6612
6613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 * ":only".
6615 */
6616 static void
6617ex_only(eap)
6618 exarg_T *eap;
6619{
6620# ifdef FEAT_GUI
6621 need_mouse_correct = TRUE;
6622# endif
6623 close_others(TRUE, eap->forceit);
6624}
6625
6626/*
6627 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006628 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006630 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631ex_all(eap)
6632 exarg_T *eap;
6633{
6634 if (eap->addr_count == 0)
6635 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006636 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637}
6638#endif /* FEAT_WINDOWS */
6639
6640 static void
6641ex_hide(eap)
6642 exarg_T *eap;
6643{
6644 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6645 eap->errmsg = e_invarg;
6646 else
6647 {
6648 /* ":hide" or ":hide | cmd": hide current window */
6649 eap->nextcmd = check_nextcmd(eap->arg);
6650#ifdef FEAT_WINDOWS
6651 if (!eap->skip)
6652 {
6653# ifdef FEAT_GUI
6654 need_mouse_correct = TRUE;
6655# endif
6656 win_close(curwin, FALSE); /* don't free buffer */
6657 }
6658#endif
6659 }
6660}
6661
6662/*
6663 * ":stop" and ":suspend": Suspend Vim.
6664 */
6665 static void
6666ex_stop(eap)
6667 exarg_T *eap;
6668{
6669 /*
6670 * Disallow suspending for "rvim".
6671 */
6672 if (!check_restricted()
6673#ifdef WIN3264
6674 /*
6675 * Check if external commands are allowed now.
6676 */
6677 && can_end_termcap_mode(TRUE)
6678#endif
6679 )
6680 {
6681 if (!eap->forceit)
6682 autowrite_all();
6683 windgoto((int)Rows - 1, 0);
6684 out_char('\n');
6685 out_flush();
6686 stoptermcap();
6687 out_flush(); /* needed for SUN to restore xterm buffer */
6688#ifdef FEAT_TITLE
6689 mch_restore_title(3); /* restore window titles */
6690#endif
6691 ui_suspend(); /* call machine specific function */
6692#ifdef FEAT_TITLE
6693 maketitle();
6694 resettitle(); /* force updating the title */
6695#endif
6696 starttermcap();
6697 scroll_start(); /* scroll screen before redrawing */
6698 redraw_later_clear();
6699 shell_resized(); /* may have resized window */
6700 }
6701}
6702
6703/*
6704 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6705 */
6706 static void
6707ex_exit(eap)
6708 exarg_T *eap;
6709{
6710#ifdef FEAT_CMDWIN
6711 if (cmdwin_type != 0)
6712 {
6713 cmdwin_result = Ctrl_C;
6714 return;
6715 }
6716#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006717 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006718 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006719 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006720 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006721 return;
6722 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006723#ifdef FEAT_AUTOCMD
6724 if (curbuf_locked())
6725 return;
6726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727
6728 /*
6729 * if more files or windows we won't exit
6730 */
6731 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6732 exiting = TRUE;
6733 if ( ((eap->cmdidx == CMD_wq
6734 || curbufIsChanged())
6735 && do_write(eap) == FAIL)
6736 || check_more(TRUE, eap->forceit) == FAIL
6737 || (only_one_window() && check_changed_any(eap->forceit)))
6738 {
6739 not_exiting();
6740 }
6741 else
6742 {
6743#ifdef FEAT_WINDOWS
6744 if (only_one_window()) /* quit last window, exit Vim */
6745#endif
6746 getout(0);
6747#ifdef FEAT_WINDOWS
6748# ifdef FEAT_GUI
6749 need_mouse_correct = TRUE;
6750# endif
6751 /* quit current window, may free buffer */
6752 win_close(curwin, !P_HID(curwin->w_buffer));
6753#endif
6754 }
6755}
6756
6757/*
6758 * ":print", ":list", ":number".
6759 */
6760 static void
6761ex_print(eap)
6762 exarg_T *eap;
6763{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006764 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6765 EMSG(_(e_emptybuf));
6766 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006768 for ( ;!got_int; ui_breakcheck())
6769 {
6770 print_line(eap->line1,
6771 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6772 || (eap->flags & EXFLAG_NR)),
6773 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6774 if (++eap->line1 > eap->line2)
6775 break;
6776 out_flush(); /* show one line at a time */
6777 }
6778 setpcmark();
6779 /* put cursor at last line */
6780 curwin->w_cursor.lnum = eap->line2;
6781 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 }
6783
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785}
6786
6787#ifdef FEAT_BYTEOFF
6788 static void
6789ex_goto(eap)
6790 exarg_T *eap;
6791{
6792 goto_byte(eap->line2);
6793}
6794#endif
6795
6796/*
6797 * ":shell".
6798 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 static void
6800ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006801 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802{
6803 do_shell(NULL, 0);
6804}
6805
6806#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6807 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006808 || defined(FEAT_GUI_MSWIN) \
6809 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 || defined(PROTO)
6811
6812/*
6813 * Handle a file drop. The code is here because a drop is *nearly* like an
6814 * :args command, but not quite (we have a list of exact filenames, so we
6815 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6816 * code is very similar to :args and hence needs access to a lot of the static
6817 * functions in this file.
6818 *
6819 * The list should be allocated using alloc(), as should each item in the
6820 * list. This function takes over responsibility for freeing the list.
6821 *
Bram Moolenaar81870892007-11-11 18:17:28 +00006822 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6824 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6825 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6826 * file functionality is (currently) not in EMX this is not presently a
6827 * problem.
6828 */
6829 void
6830handle_drop(filec, filev, split)
6831 int filec; /* the number of files dropped */
6832 char_u **filev; /* the list of files dropped */
6833 int split; /* force splitting the window */
6834{
6835 exarg_T ea;
6836 int save_msg_scroll = msg_scroll;
6837
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006838 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006839 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006841#ifdef FEAT_AUTOCMD
6842 if (curbuf_locked())
6843 return;
6844#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00006845 /* When the screen is being updated we should not change buffers and
6846 * windows structures, it may cause freed memory to be used. */
6847 if (updating_screen)
6848 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849
6850 /* Check whether the current buffer is changed. If so, we will need
6851 * to split the current window or data could be lost.
6852 * We don't need to check if the 'hidden' option is set, as in this
6853 * case the buffer won't be lost.
6854 */
6855 if (!P_HID(curbuf) && !split)
6856 {
6857 ++emsg_off;
6858 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6859 --emsg_off;
6860 }
6861 if (split)
6862 {
6863# ifdef FEAT_WINDOWS
6864 if (win_split(0, 0) == FAIL)
6865 return;
6866# ifdef FEAT_SCROLLBIND
6867 curwin->w_p_scb = FALSE;
6868# endif
6869
6870 /* When splitting the window, create a new alist. Otherwise the
6871 * existing one is overwritten. */
6872 alist_unlink(curwin->w_alist);
6873 alist_new();
6874# else
6875 return; /* can't split, always fail */
6876# endif
6877 }
6878
6879 /*
6880 * Set up the new argument list.
6881 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006882 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883
6884 /*
6885 * Move to the first file.
6886 */
6887 /* Fake up a minimal "next" command for do_argfile() */
6888 vim_memset(&ea, 0, sizeof(ea));
6889 ea.cmd = (char_u *)"next";
6890 do_argfile(&ea, 0);
6891
6892 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6893 * mode that is not needed here. */
6894 need_start_insertmode = FALSE;
6895
6896 /* Restore msg_scroll, otherwise a following command may cause scrolling
6897 * unexpectedly. The screen will be redrawn by the caller, thus
6898 * msg_scroll being set by displaying a message is irrelevant. */
6899 msg_scroll = save_msg_scroll;
6900}
6901#endif
6902
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903/*
6904 * Clear an argument list: free all file names and reset it to zero entries.
6905 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006906 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907alist_clear(al)
6908 alist_T *al;
6909{
6910 while (--al->al_ga.ga_len >= 0)
6911 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6912 ga_clear(&al->al_ga);
6913}
6914
6915/*
6916 * Init an argument list.
6917 */
6918 void
6919alist_init(al)
6920 alist_T *al;
6921{
6922 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6923}
6924
6925#if defined(FEAT_WINDOWS) || defined(PROTO)
6926
6927/*
6928 * Remove a reference from an argument list.
6929 * Ignored when the argument list is the global one.
6930 * If the argument list is no longer used by any window, free it.
6931 */
6932 void
6933alist_unlink(al)
6934 alist_T *al;
6935{
6936 if (al != &global_alist && --al->al_refcount <= 0)
6937 {
6938 alist_clear(al);
6939 vim_free(al);
6940 }
6941}
6942
6943# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6944/*
6945 * Create a new argument list and use it for the current window.
6946 */
6947 void
6948alist_new()
6949{
6950 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6951 if (curwin->w_alist == NULL)
6952 {
6953 curwin->w_alist = &global_alist;
6954 ++global_alist.al_refcount;
6955 }
6956 else
6957 {
6958 curwin->w_alist->al_refcount = 1;
6959 alist_init(curwin->w_alist);
6960 }
6961}
6962# endif
6963#endif
6964
6965#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6966/*
6967 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006968 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6969 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 */
6971 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006972alist_expand(fnum_list, fnum_len)
6973 int *fnum_list;
6974 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975{
6976 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006977 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 char_u **new_arg_files;
6979 int new_arg_file_count;
6980 char_u *save_p_su = p_su;
6981 int i;
6982
6983 /* Don't use 'suffixes' here. This should work like the shell did the
6984 * expansion. Also, the vimrc file isn't read yet, thus the user
6985 * can't set the options. */
6986 p_su = empty_option;
6987 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6988 if (old_arg_files != NULL)
6989 {
6990 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006991 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6992 old_arg_count = GARGCOUNT;
6993 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 &new_arg_file_count, &new_arg_files,
6995 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6996 && new_arg_file_count > 0)
6997 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006998 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6999 TRUE, fnum_list, fnum_len);
7000 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 }
7003 p_su = save_p_su;
7004}
7005#endif
7006
7007/*
7008 * Set the argument list for the current window.
7009 * Takes over the allocated files[] and the allocated fnames in it.
7010 */
7011 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007012alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 alist_T *al;
7014 int count;
7015 char_u **files;
7016 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007017 int *fnum_list;
7018 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019{
7020 int i;
7021
7022 alist_clear(al);
7023 if (ga_grow(&al->al_ga, count) == OK)
7024 {
7025 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007026 {
7027 if (got_int)
7028 {
7029 /* When adding many buffers this can take a long time. Allow
7030 * interrupting here. */
7031 while (i < count)
7032 vim_free(files[i++]);
7033 break;
7034 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007035
7036 /* May set buffer name of a buffer previously used for the
7037 * argument list, so that it's re-used by alist_add. */
7038 if (fnum_list != NULL && i < fnum_len)
7039 buf_set_name(fnum_list[i], files[i]);
7040
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007042 ui_breakcheck();
7043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 vim_free(files);
7045 }
7046 else
7047 FreeWild(count, files);
7048#ifdef FEAT_WINDOWS
7049 if (al == &global_alist)
7050#endif
7051 arg_had_last = FALSE;
7052}
7053
7054/*
7055 * Add file "fname" to argument list "al".
7056 * "fname" must have been allocated and "al" must have been checked for room.
7057 */
7058 void
7059alist_add(al, fname, set_fnum)
7060 alist_T *al;
7061 char_u *fname;
7062 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7063{
7064 if (fname == NULL) /* don't add NULL file names */
7065 return;
7066#ifdef BACKSLASH_IN_FILENAME
7067 slash_adjust(fname);
7068#endif
7069 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7070 if (set_fnum > 0)
7071 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7072 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7073 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074}
7075
7076#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7077/*
7078 * Adjust slashes in file names. Called after 'shellslash' was set.
7079 */
7080 void
7081alist_slash_adjust()
7082{
7083 int i;
7084# ifdef FEAT_WINDOWS
7085 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007086 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087# endif
7088
7089 for (i = 0; i < GARGCOUNT; ++i)
7090 if (GARGLIST[i].ae_fname != NULL)
7091 slash_adjust(GARGLIST[i].ae_fname);
7092# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007093 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 if (wp->w_alist != &global_alist)
7095 for (i = 0; i < WARGCOUNT(wp); ++i)
7096 if (WARGLIST(wp)[i].ae_fname != NULL)
7097 slash_adjust(WARGLIST(wp)[i].ae_fname);
7098# endif
7099}
7100#endif
7101
7102/*
7103 * ":preserve".
7104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 static void
7106ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007107 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007109 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 ml_preserve(curbuf, TRUE);
7111}
7112
7113/*
7114 * ":recover".
7115 */
7116 static void
7117ex_recover(eap)
7118 exarg_T *eap;
7119{
7120 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7121 recoverymode = TRUE;
7122 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7123 && (*eap->arg == NUL
7124 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7125 ml_recover();
7126 recoverymode = FALSE;
7127}
7128
7129/*
7130 * Command modifier used in a wrong way.
7131 */
7132 static void
7133ex_wrongmodifier(eap)
7134 exarg_T *eap;
7135{
7136 eap->errmsg = e_invcmd;
7137}
7138
7139#ifdef FEAT_WINDOWS
7140/*
7141 * :sview [+command] file split window with new file, read-only
7142 * :split [[+command] file] split window with current or new file
7143 * :vsplit [[+command] file] split window vertically with current or new file
7144 * :new [[+command] file] split window with no or new file
7145 * :vnew [[+command] file] split vertically window with no or new file
7146 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007147 *
7148 * :tabedit open new Tab page with empty window
7149 * :tabedit [+command] file open new Tab page and edit "file"
7150 * :tabnew [[+command] file] just like :tabedit
7151 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 */
7153 void
7154ex_splitview(eap)
7155 exarg_T *eap;
7156{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007157 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007158# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007160# endif
7161# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007163# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007165# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7167 {
7168 ex_ni(eap);
7169 return;
7170 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007171# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007173# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007175# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007177# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007179 * quickfix windows. But it's OK when doing ":tab split". */
7180 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 {
7182 if (eap->cmdidx == CMD_split)
7183 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007184# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 if (eap->cmdidx == CMD_vsplit)
7186 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007187# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007189# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007191# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007192 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 {
7194 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7195 FNAME_MESS, TRUE, curbuf->b_ffname);
7196 if (fname == NULL)
7197 goto theend;
7198 eap->arg = fname;
7199 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007200# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007202# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007204# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007206# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007208# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 && eap->cmdidx != CMD_new)
7210 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007211# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007212 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007213# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007214 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007215# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007216 au_has_group((char_u *)"FileExplorer"))
7217 {
7218 /* No browsing supported but we do have the file explorer:
7219 * Edit the directory. */
7220 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7221 eap->arg = (char_u *)".";
7222 }
7223 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007224# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007225 {
7226 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007228 if (fname == NULL)
7229 goto theend;
7230 eap->arg = fname;
7231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 }
7233 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007234# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007236 /*
7237 * Either open new tab page or split the window.
7238 */
7239 if (eap->cmdidx == CMD_tabedit
7240 || eap->cmdidx == CMD_tabfind
7241 || eap->cmdidx == CMD_tabnew)
7242 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007243 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7244 : eap->addr_count == 0 ? 0
7245 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007246 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007247 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007248
7249 /* set the alternate buffer for the window we came from */
7250 if (curwin != old_curwin
7251 && win_valid(old_curwin)
7252 && old_curwin->w_buffer != curbuf
7253 && !cmdmod.keepalt)
7254 old_curwin->w_alt_fnum = curbuf->b_fnum;
7255 }
7256 }
7257 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7259 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007260# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 /* Reset 'scrollbind' when editing another file, but keep it when
7262 * doing ":split" without arguments. */
7263 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007264# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 )
7268 curwin->w_p_scb = FALSE;
7269 else
7270 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 do_exedit(eap, old_curwin);
7273 }
7274
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007275# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007277# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007279# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280theend:
7281 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007282# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007284
7285/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007286 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007287 */
7288 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007289tabpage_new()
7290{
7291 exarg_T ea;
7292
7293 vim_memset(&ea, 0, sizeof(ea));
7294 ea.cmdidx = CMD_tabnew;
7295 ea.cmd = (char_u *)"tabn";
7296 ea.arg = (char_u *)"";
7297 ex_splitview(&ea);
7298}
7299
7300/*
7301 * :tabnext command
7302 */
7303 static void
7304ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007305 exarg_T *eap;
7306{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007307 switch (eap->cmdidx)
7308 {
7309 case CMD_tabfirst:
7310 case CMD_tabrewind:
7311 goto_tabpage(1);
7312 break;
7313 case CMD_tablast:
7314 goto_tabpage(9999);
7315 break;
7316 case CMD_tabprevious:
7317 case CMD_tabNext:
7318 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7319 break;
7320 default: /* CMD_tabnext */
7321 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7322 break;
7323 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007324}
7325
7326/*
7327 * :tabmove command
7328 */
7329 static void
7330ex_tabmove(eap)
7331 exarg_T *eap;
7332{
7333 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007334}
7335
7336/*
7337 * :tabs command: List tabs and their contents.
7338 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007339 static void
7340ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007341 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007342{
7343 tabpage_T *tp;
7344 win_T *wp;
7345 int tabcount = 1;
7346
7347 msg_start();
7348 msg_scroll = TRUE;
7349 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7350 {
7351 msg_putchar('\n');
7352 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7353 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7354 out_flush(); /* output one line at a time */
7355 ui_breakcheck();
7356
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007357 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007358 wp = firstwin;
7359 else
7360 wp = tp->tp_firstwin;
7361 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7362 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007363 msg_putchar('\n');
7364 msg_putchar(wp == curwin ? '>' : ' ');
7365 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007366 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7367 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007368 if (buf_spname(wp->w_buffer) != NULL)
7369 STRCPY(IObuff, buf_spname(wp->w_buffer));
7370 else
7371 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7372 IObuff, IOSIZE, TRUE);
7373 msg_outtrans(IObuff);
7374 out_flush(); /* output one line at a time */
7375 ui_breakcheck();
7376 }
7377 }
7378}
7379
7380#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381
7382/*
7383 * ":mode": Set screen mode.
7384 * If no argument given, just get the screen size and redraw.
7385 */
7386 static void
7387ex_mode(eap)
7388 exarg_T *eap;
7389{
7390 if (*eap->arg == NUL)
7391 shell_resized();
7392 else
7393 mch_screenmode(eap->arg);
7394}
7395
7396#ifdef FEAT_WINDOWS
7397/*
7398 * ":resize".
7399 * set, increment or decrement current window height
7400 */
7401 static void
7402ex_resize(eap)
7403 exarg_T *eap;
7404{
7405 int n;
7406 win_T *wp = curwin;
7407
7408 if (eap->addr_count > 0)
7409 {
7410 n = eap->line2;
7411 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7412 ;
7413 }
7414
7415#ifdef FEAT_GUI
7416 need_mouse_correct = TRUE;
7417#endif
7418 n = atol((char *)eap->arg);
7419#ifdef FEAT_VERTSPLIT
7420 if (cmdmod.split & WSP_VERT)
7421 {
7422 if (*eap->arg == '-' || *eap->arg == '+')
7423 n += W_WIDTH(curwin);
7424 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7425 n = 9999;
7426 win_setwidth_win((int)n, wp);
7427 }
7428 else
7429#endif
7430 {
7431 if (*eap->arg == '-' || *eap->arg == '+')
7432 n += curwin->w_height;
7433 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7434 n = 9999;
7435 win_setheight_win((int)n, wp);
7436 }
7437}
7438#endif
7439
7440/*
7441 * ":find [+command] <file>" command.
7442 */
7443 static void
7444ex_find(eap)
7445 exarg_T *eap;
7446{
7447#ifdef FEAT_SEARCHPATH
7448 char_u *fname;
7449 int count;
7450
7451 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7452 TRUE, curbuf->b_ffname);
7453 if (eap->addr_count > 0)
7454 {
7455 /* Repeat finding the file "count" times. This matters when it
7456 * appears several times in the path. */
7457 count = eap->line2;
7458 while (fname != NULL && --count > 0)
7459 {
7460 vim_free(fname);
7461 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7462 FALSE, curbuf->b_ffname);
7463 }
7464 }
7465
7466 if (fname != NULL)
7467 {
7468 eap->arg = fname;
7469#endif
7470 do_exedit(eap, NULL);
7471#ifdef FEAT_SEARCHPATH
7472 vim_free(fname);
7473 }
7474#endif
7475}
7476
7477/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007478 * ":open" simulation: for now just work like ":visual".
7479 */
7480 static void
7481ex_open(eap)
7482 exarg_T *eap;
7483{
7484 regmatch_T regmatch;
7485 char_u *p;
7486
7487 curwin->w_cursor.lnum = eap->line2;
7488 beginline(BL_SOL | BL_FIX);
7489 if (*eap->arg == '/')
7490 {
7491 /* ":open /pattern/": put cursor in column found with pattern */
7492 ++eap->arg;
7493 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7494 *p = NUL;
7495 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7496 if (regmatch.regprog != NULL)
7497 {
7498 regmatch.rm_ic = p_ic;
7499 p = ml_get_curline();
7500 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007501 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007502 else
7503 EMSG(_(e_nomatch));
7504 vim_free(regmatch.regprog);
7505 }
7506 /* Move to the NUL, ignore any other arguments. */
7507 eap->arg += STRLEN(eap->arg);
7508 }
7509 check_cursor();
7510
7511 eap->cmdidx = CMD_visual;
7512 do_exedit(eap, NULL);
7513}
7514
7515/*
7516 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 */
7518 static void
7519ex_edit(eap)
7520 exarg_T *eap;
7521{
7522 do_exedit(eap, NULL);
7523}
7524
7525/*
7526 * ":edit <file>" command and alikes.
7527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 void
7529do_exedit(eap, old_curwin)
7530 exarg_T *eap;
7531 win_T *old_curwin; /* curwin before doing a split or NULL */
7532{
7533 int n;
7534#ifdef FEAT_WINDOWS
7535 int need_hide;
7536#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007537 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538
7539 /*
7540 * ":vi" command ends Ex mode.
7541 */
7542 if (exmode_active && (eap->cmdidx == CMD_visual
7543 || eap->cmdidx == CMD_view))
7544 {
7545 exmode_active = FALSE;
7546 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007547 {
7548 /* Special case: ":global/pat/visual\NLvi-commands" */
7549 if (global_busy)
7550 {
7551 int rd = RedrawingDisabled;
7552 int nwr = no_wait_return;
7553 int ms = msg_scroll;
7554#ifdef FEAT_GUI
7555 int he = hold_gui_events;
7556#endif
7557
7558 if (eap->nextcmd != NULL)
7559 {
7560 stuffReadbuff(eap->nextcmd);
7561 eap->nextcmd = NULL;
7562 }
7563
7564 if (exmode_was != EXMODE_VIM)
7565 settmode(TMODE_RAW);
7566 RedrawingDisabled = 0;
7567 no_wait_return = 0;
7568 need_wait_return = FALSE;
7569 msg_scroll = 0;
7570#ifdef FEAT_GUI
7571 hold_gui_events = 0;
7572#endif
7573 must_redraw = CLEAR;
7574
7575 main_loop(FALSE, TRUE);
7576
7577 RedrawingDisabled = rd;
7578 no_wait_return = nwr;
7579 msg_scroll = ms;
7580#ifdef FEAT_GUI
7581 hold_gui_events = he;
7582#endif
7583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 }
7587
7588 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007589 || eap->cmdidx == CMD_tabnew
7590 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591#ifdef FEAT_VERTSPLIT
7592 || eap->cmdidx == CMD_vnew
7593#endif
7594 ) && *eap->arg == NUL)
7595 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007596 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 setpcmark();
7598 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007599 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7600 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 }
7602 else if ((eap->cmdidx != CMD_split
7603#ifdef FEAT_VERTSPLIT
7604 && eap->cmdidx != CMD_vsplit
7605#endif
7606 )
7607 || *eap->arg != NUL
7608#ifdef FEAT_BROWSE
7609 || cmdmod.browse
7610#endif
7611 )
7612 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007613#ifdef FEAT_AUTOCMD
7614 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7615 * can bring us here, others are stopped earlier. */
7616 if (*eap->arg != NUL && curbuf_locked())
7617 return;
7618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 n = readonlymode;
7620 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7621 readonlymode = TRUE;
7622 else if (eap->cmdidx == CMD_enew)
7623 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7624 empty buffer */
7625 setpcmark();
7626 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7627 NULL, eap,
7628 /* ":edit" goes to first line if Vi compatible */
7629 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7630 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7631 ? ECMD_ONE : eap->do_ecmd_lnum,
7632 (P_HID(curbuf) ? ECMD_HIDE : 0)
7633 + (eap->forceit ? ECMD_FORCEIT : 0)
7634#ifdef FEAT_LISTCMDS
7635 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7636#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007637 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 {
7639 /* Editing the file failed. If the window was split, close it. */
7640#ifdef FEAT_WINDOWS
7641 if (old_curwin != NULL)
7642 {
7643 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7644 if (!need_hide || P_HID(curbuf))
7645 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007646# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7647 cleanup_T cs;
7648
7649 /* Reset the error/interrupt/exception state here so that
7650 * aborting() returns FALSE when closing a window. */
7651 enter_cleanup(&cs);
7652# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653# ifdef FEAT_GUI
7654 need_mouse_correct = TRUE;
7655# endif
7656 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007657
7658# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7659 /* Restore the error/interrupt/exception state if not
7660 * discarded by a new aborting error, interrupt, or
7661 * uncaught exception. */
7662 leave_cleanup(&cs);
7663# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 }
7665 }
7666#endif
7667 }
7668 else if (readonlymode && curbuf->b_nwindows == 1)
7669 {
7670 /* When editing an already visited buffer, 'readonly' won't be set
7671 * but the previous value is kept. With ":view" and ":sview" we
7672 * want the file to be readonly, except when another window is
7673 * editing the same buffer. */
7674 curbuf->b_p_ro = TRUE;
7675 }
7676 readonlymode = n;
7677 }
7678 else
7679 {
7680 if (eap->do_ecmd_cmd != NULL)
7681 do_cmdline_cmd(eap->do_ecmd_cmd);
7682#ifdef FEAT_TITLE
7683 n = curwin->w_arg_idx_invalid;
7684#endif
7685 check_arg_idx(curwin);
7686#ifdef FEAT_TITLE
7687 if (n != curwin->w_arg_idx_invalid)
7688 maketitle();
7689#endif
7690 }
7691
7692#ifdef FEAT_WINDOWS
7693 /*
7694 * if ":split file" worked, set alternate file name in old window to new
7695 * file
7696 */
7697 if (old_curwin != NULL
7698 && *eap->arg != NUL
7699 && curwin != old_curwin
7700 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007701 && old_curwin->w_buffer != curbuf
7702 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 old_curwin->w_alt_fnum = curbuf->b_fnum;
7704#endif
7705
7706 ex_no_reprint = TRUE;
7707}
7708
7709#ifndef FEAT_GUI
7710/*
7711 * ":gui" and ":gvim" when there is no GUI.
7712 */
7713 static void
7714ex_nogui(eap)
7715 exarg_T *eap;
7716{
7717 eap->errmsg = e_nogvim;
7718}
7719#endif
7720
7721#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7722 static void
7723ex_tearoff(eap)
7724 exarg_T *eap;
7725{
7726 gui_make_tearoff(eap->arg);
7727}
7728#endif
7729
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007730#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 static void
7732ex_popup(eap)
7733 exarg_T *eap;
7734{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007735 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736}
7737#endif
7738
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 static void
7740ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007741 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742{
7743 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7744 MSG(_("No swap file"));
7745 else
7746 msg(curbuf->b_ml.ml_mfp->mf_fname);
7747}
7748
7749/*
7750 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7751 * offset.
7752 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 static void
7755ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007756 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757{
7758#ifdef FEAT_SCROLLBIND
7759 win_T *wp;
7760 long topline;
7761 long y;
7762 linenr_T old_linenr = curwin->w_cursor.lnum;
7763
7764 setpcmark();
7765
7766 /*
7767 * determine max topline
7768 */
7769 if (curwin->w_p_scb)
7770 {
7771 topline = curwin->w_topline;
7772 for (wp = firstwin; wp; wp = wp->w_next)
7773 {
7774 if (wp->w_p_scb && wp->w_buffer)
7775 {
7776 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7777 if (topline > y)
7778 topline = y;
7779 }
7780 }
7781 if (topline < 1)
7782 topline = 1;
7783 }
7784 else
7785 {
7786 topline = 1;
7787 }
7788
7789
7790 /*
7791 * set all scrollbind windows to the same topline
7792 */
7793 wp = curwin;
7794 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7795 {
7796 if (curwin->w_p_scb)
7797 {
7798 y = topline - curwin->w_topline;
7799 if (y > 0)
7800 scrollup(y, TRUE);
7801 else
7802 scrolldown(-y, TRUE);
7803 curwin->w_scbind_pos = topline;
7804 redraw_later(VALID);
7805 cursor_correct();
7806#ifdef FEAT_WINDOWS
7807 curwin->w_redr_status = TRUE;
7808#endif
7809 }
7810 }
7811 curwin = wp;
7812 if (curwin->w_p_scb)
7813 {
7814 did_syncbind = TRUE;
7815 checkpcmark();
7816 if (old_linenr != curwin->w_cursor.lnum)
7817 {
7818 char_u ctrl_o[2];
7819
7820 ctrl_o[0] = Ctrl_O;
7821 ctrl_o[1] = 0;
7822 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7823 }
7824 }
7825#endif
7826}
7827
7828
7829 static void
7830ex_read(eap)
7831 exarg_T *eap;
7832{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007833 int i;
7834 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7835 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836
7837 if (eap->usefilter) /* :r!cmd */
7838 do_bang(1, eap, FALSE, FALSE, TRUE);
7839 else
7840 {
7841 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7842 return;
7843
7844#ifdef FEAT_BROWSE
7845 if (cmdmod.browse)
7846 {
7847 char_u *browseFile;
7848
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007849 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 NULL, NULL, NULL, curbuf);
7851 if (browseFile != NULL)
7852 {
7853 i = readfile(browseFile, NULL,
7854 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7855 vim_free(browseFile);
7856 }
7857 else
7858 i = OK;
7859 }
7860 else
7861#endif
7862 if (*eap->arg == NUL)
7863 {
7864 if (check_fname() == FAIL) /* check for no file name */
7865 return;
7866 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7867 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7868 }
7869 else
7870 {
7871 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7872 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7873 i = readfile(eap->arg, NULL,
7874 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7875
7876 }
7877 if (i == FAIL)
7878 {
7879#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7880 if (!aborting())
7881#endif
7882 EMSG2(_(e_notopen), eap->arg);
7883 }
7884 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007885 {
7886 if (empty && exmode_active)
7887 {
7888 /* Delete the empty line that remains. Historically ex does
7889 * this but vi doesn't. */
7890 if (eap->line2 == 0)
7891 lnum = curbuf->b_ml.ml_line_count;
7892 else
7893 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007894 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007895 {
7896 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007897 if (curwin->w_cursor.lnum > 1
7898 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007899 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007900 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007901 }
7902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 }
7906}
7907
Bram Moolenaarea408852005-06-25 22:49:46 +00007908static char_u *prev_dir = NULL;
7909
7910#if defined(EXITFREE) || defined(PROTO)
7911 void
7912free_cd_dir()
7913{
7914 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00007915 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00007916
7917 vim_free(globaldir);
7918 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00007919}
7920#endif
7921
7922
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923/*
7924 * ":cd", ":lcd", ":chdir" and ":lchdir".
7925 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007926 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927ex_cd(eap)
7928 exarg_T *eap;
7929{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 char_u *new_dir;
7931 char_u *tofree;
7932
7933 new_dir = eap->arg;
7934#if !defined(UNIX) && !defined(VMS)
7935 /* for non-UNIX ":cd" means: print current directory */
7936 if (*new_dir == NUL)
7937 ex_pwd(NULL);
7938 else
7939#endif
7940 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00007941#ifdef FEAT_AUTOCMD
7942 if (allbuf_locked())
7943 return;
7944#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007945 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7946 && !eap->forceit)
7947 {
Bram Moolenaar81870892007-11-11 18:17:28 +00007948 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007949 return;
7950 }
7951
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 /* ":cd -": Change to previous directory */
7953 if (STRCMP(new_dir, "-") == 0)
7954 {
7955 if (prev_dir == NULL)
7956 {
7957 EMSG(_("E186: No previous directory"));
7958 return;
7959 }
7960 new_dir = prev_dir;
7961 }
7962
7963 /* Save current directory for next ":cd -" */
7964 tofree = prev_dir;
7965 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7966 prev_dir = vim_strsave(NameBuff);
7967 else
7968 prev_dir = NULL;
7969
7970#if defined(UNIX) || defined(VMS)
7971 /* for UNIX ":cd" means: go to home directory */
7972 if (*new_dir == NUL)
7973 {
7974 /* use NameBuff for home directory name */
7975# ifdef VMS
7976 char_u *p;
7977
7978 p = mch_getenv((char_u *)"SYS$LOGIN");
7979 if (p == NULL || *p == NUL) /* empty is the same as not set */
7980 NameBuff[0] = NUL;
7981 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007982 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983# else
7984 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7985# endif
7986 new_dir = NameBuff;
7987 }
7988#endif
7989 if (new_dir == NULL || vim_chdir(new_dir))
7990 EMSG(_(e_failed));
7991 else
7992 {
7993 vim_free(curwin->w_localdir);
7994 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7995 {
7996 /* If still in global directory, need to remember current
7997 * directory as global directory. */
7998 if (globaldir == NULL && prev_dir != NULL)
7999 globaldir = vim_strsave(prev_dir);
8000 /* Remember this local directory for the window. */
8001 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8002 curwin->w_localdir = vim_strsave(NameBuff);
8003 }
8004 else
8005 {
8006 /* We are now in the global directory, no need to remember its
8007 * name. */
8008 vim_free(globaldir);
8009 globaldir = NULL;
8010 curwin->w_localdir = NULL;
8011 }
8012
8013 shorten_fnames(TRUE);
8014
8015 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008016 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 ex_pwd(eap);
8018 }
8019 vim_free(tofree);
8020 }
8021}
8022
8023/*
8024 * ":pwd".
8025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 static void
8027ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008028 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029{
8030 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8031 {
8032#ifdef BACKSLASH_IN_FILENAME
8033 slash_adjust(NameBuff);
8034#endif
8035 msg(NameBuff);
8036 }
8037 else
8038 EMSG(_("E187: Unknown"));
8039}
8040
8041/*
8042 * ":=".
8043 */
8044 static void
8045ex_equal(eap)
8046 exarg_T *eap;
8047{
8048 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008049 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050}
8051
8052 static void
8053ex_sleep(eap)
8054 exarg_T *eap;
8055{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008056 int n;
8057 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058
8059 if (cursor_valid())
8060 {
8061 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8062 if (n >= 0)
8063 windgoto((int)n, curwin->w_wcol);
8064 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008065
8066 len = eap->line2;
8067 switch (*eap->arg)
8068 {
8069 case 'm': break;
8070 case NUL: len *= 1000L; break;
8071 default: EMSG2(_(e_invarg2), eap->arg); return;
8072 }
8073 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074}
8075
8076/*
8077 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8078 */
8079 void
8080do_sleep(msec)
8081 long msec;
8082{
8083 long done;
8084
8085 cursor_on();
8086 out_flush();
8087 for (done = 0; !got_int && done < msec; done += 1000L)
8088 {
8089 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8090 ui_breakcheck();
8091 }
8092}
8093
8094 static void
8095do_exmap(eap, isabbrev)
8096 exarg_T *eap;
8097 int isabbrev;
8098{
8099 int mode;
8100 char_u *cmdp;
8101
8102 cmdp = eap->cmd;
8103 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8104
8105 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8106 eap->arg, mode, isabbrev))
8107 {
8108 case 1: EMSG(_(e_invarg));
8109 break;
8110 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8111 break;
8112 }
8113}
8114
8115/*
8116 * ":winsize" command (obsolete).
8117 */
8118 static void
8119ex_winsize(eap)
8120 exarg_T *eap;
8121{
8122 int w, h;
8123 char_u *arg = eap->arg;
8124 char_u *p;
8125
8126 w = getdigits(&arg);
8127 arg = skipwhite(arg);
8128 p = arg;
8129 h = getdigits(&arg);
8130 if (*p != NUL && *arg == NUL)
8131 set_shellsize(w, h, TRUE);
8132 else
8133 EMSG(_("E465: :winsize requires two number arguments"));
8134}
8135
8136#ifdef FEAT_WINDOWS
8137 static void
8138ex_wincmd(eap)
8139 exarg_T *eap;
8140{
8141 int xchar = NUL;
8142 char_u *p;
8143
8144 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8145 {
8146 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8147 if (eap->arg[1] == NUL)
8148 {
8149 EMSG(_(e_invarg));
8150 return;
8151 }
8152 xchar = eap->arg[1];
8153 p = eap->arg + 2;
8154 }
8155 else
8156 p = eap->arg + 1;
8157
8158 eap->nextcmd = check_nextcmd(p);
8159 p = skipwhite(p);
8160 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8161 EMSG(_(e_invarg));
8162 else
8163 {
8164 /* Pass flags on for ":vertical wincmd ]". */
8165 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008166 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8168 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008169 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 }
8171}
8172#endif
8173
Bram Moolenaar843ee412004-06-30 16:16:41 +00008174#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175/*
8176 * ":winpos".
8177 */
8178 static void
8179ex_winpos(eap)
8180 exarg_T *eap;
8181{
8182 int x, y;
8183 char_u *arg = eap->arg;
8184 char_u *p;
8185
8186 if (*arg == NUL)
8187 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008188# if defined(FEAT_GUI) || defined(MSWIN)
8189# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008191# else
8192 if (mch_get_winpos(&x, &y) != FAIL)
8193# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {
8195 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8196 msg(IObuff);
8197 }
8198 else
8199# endif
8200 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8201 }
8202 else
8203 {
8204 x = getdigits(&arg);
8205 arg = skipwhite(arg);
8206 p = arg;
8207 y = getdigits(&arg);
8208 if (*p == NUL || *arg != NUL)
8209 {
8210 EMSG(_("E466: :winpos requires two number arguments"));
8211 return;
8212 }
8213# ifdef FEAT_GUI
8214 if (gui.in_use)
8215 gui_mch_set_winpos(x, y);
8216 else if (gui.starting)
8217 {
8218 /* Remember the coordinates for when the window is opened. */
8219 gui_win_x = x;
8220 gui_win_y = y;
8221 }
8222# ifdef HAVE_TGETENT
8223 else
8224# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008225# else
8226# ifdef MSWIN
8227 mch_set_winpos(x, y);
8228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229# endif
8230# ifdef HAVE_TGETENT
8231 if (*T_CWP)
8232 term_set_winpos(x, y);
8233# endif
8234 }
8235}
8236#endif
8237
8238/*
8239 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8240 */
8241 static void
8242ex_operators(eap)
8243 exarg_T *eap;
8244{
8245 oparg_T oa;
8246
8247 clear_oparg(&oa);
8248 oa.regname = eap->regname;
8249 oa.start.lnum = eap->line1;
8250 oa.end.lnum = eap->line2;
8251 oa.line_count = eap->line2 - eap->line1 + 1;
8252 oa.motion_type = MLINE;
8253#ifdef FEAT_VIRTUALEDIT
8254 virtual_op = FALSE;
8255#endif
8256 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8257 {
8258 setpcmark();
8259 curwin->w_cursor.lnum = eap->line1;
8260 beginline(BL_SOL | BL_FIX);
8261 }
8262
8263 switch (eap->cmdidx)
8264 {
8265 case CMD_delete:
8266 oa.op_type = OP_DELETE;
8267 op_delete(&oa);
8268 break;
8269
8270 case CMD_yank:
8271 oa.op_type = OP_YANK;
8272 (void)op_yank(&oa, FALSE, TRUE);
8273 break;
8274
8275 default: /* CMD_rshift or CMD_lshift */
8276 if ((eap->cmdidx == CMD_rshift)
8277#ifdef FEAT_RIGHTLEFT
8278 ^ curwin->w_p_rl
8279#endif
8280 )
8281 oa.op_type = OP_RSHIFT;
8282 else
8283 oa.op_type = OP_LSHIFT;
8284 op_shift(&oa, FALSE, eap->amount);
8285 break;
8286 }
8287#ifdef FEAT_VIRTUALEDIT
8288 virtual_op = MAYBE;
8289#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008290 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291}
8292
8293/*
8294 * ":put".
8295 */
8296 static void
8297ex_put(eap)
8298 exarg_T *eap;
8299{
8300 /* ":0put" works like ":1put!". */
8301 if (eap->line2 == 0)
8302 {
8303 eap->line2 = 1;
8304 eap->forceit = TRUE;
8305 }
8306 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008307 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8308 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309}
8310
8311/*
8312 * Handle ":copy" and ":move".
8313 */
8314 static void
8315ex_copymove(eap)
8316 exarg_T *eap;
8317{
8318 long n;
8319
8320 n = get_address(&eap->arg, FALSE, FALSE);
8321 if (eap->arg == NULL) /* error detected */
8322 {
8323 eap->nextcmd = NULL;
8324 return;
8325 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008326 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327
8328 /*
8329 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8330 */
8331 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8332 {
8333 EMSG(_(e_invaddr));
8334 return;
8335 }
8336
8337 if (eap->cmdidx == CMD_move)
8338 {
8339 if (do_move(eap->line1, eap->line2, n) == FAIL)
8340 return;
8341 }
8342 else
8343 ex_copy(eap->line1, eap->line2, n);
8344 u_clearline();
8345 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008346 ex_may_print(eap);
8347}
8348
8349/*
8350 * Print the current line if flags were given to the Ex command.
8351 */
8352 static void
8353ex_may_print(eap)
8354 exarg_T *eap;
8355{
8356 if (eap->flags != 0)
8357 {
8358 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8359 (eap->flags & EXFLAG_LIST));
8360 ex_no_reprint = TRUE;
8361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362}
8363
8364/*
8365 * ":smagic" and ":snomagic".
8366 */
8367 static void
8368ex_submagic(eap)
8369 exarg_T *eap;
8370{
8371 int magic_save = p_magic;
8372
8373 p_magic = (eap->cmdidx == CMD_smagic);
8374 do_sub(eap);
8375 p_magic = magic_save;
8376}
8377
8378/*
8379 * ":join".
8380 */
8381 static void
8382ex_join(eap)
8383 exarg_T *eap;
8384{
8385 curwin->w_cursor.lnum = eap->line1;
8386 if (eap->line1 == eap->line2)
8387 {
8388 if (eap->addr_count >= 2) /* :2,2join does nothing */
8389 return;
8390 if (eap->line2 == curbuf->b_ml.ml_line_count)
8391 {
8392 beep_flush();
8393 return;
8394 }
8395 ++eap->line2;
8396 }
8397 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
8398 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008399 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400}
8401
8402/*
8403 * ":[addr]@r" or ":[addr]*r": execute register
8404 */
8405 static void
8406ex_at(eap)
8407 exarg_T *eap;
8408{
8409 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008410 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411
8412 curwin->w_cursor.lnum = eap->line2;
8413
8414#ifdef USE_ON_FLY_SCROLL
8415 dont_scroll = TRUE; /* disallow scrolling here */
8416#endif
8417
8418 /* get the register name. No name means to use the previous one */
8419 c = *eap->arg;
8420 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8421 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008422 /* Put the register in the typeahead buffer with the "silent" flag. */
8423 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8424 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008425 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 else
8429 {
8430 int save_efr = exec_from_reg;
8431
8432 exec_from_reg = TRUE;
8433
8434 /*
8435 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008436 * Continue until the stuff buffer is empty and all added characters
8437 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008439 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8441
8442 exec_from_reg = save_efr;
8443 }
8444}
8445
8446/*
8447 * ":!".
8448 */
8449 static void
8450ex_bang(eap)
8451 exarg_T *eap;
8452{
8453 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8454}
8455
8456/*
8457 * ":undo".
8458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 static void
8460ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008461 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008463 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008464 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008465 else
8466 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467}
8468
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008469#ifdef FEAT_PERSISTENT_UNDO
8470 void
8471ex_wundo(eap)
8472 exarg_T *eap;
8473{
8474 char_u hash[UNDO_HASH_SIZE];
8475
8476 u_compute_hash(hash);
8477 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8478}
8479
8480 void
8481ex_rundo(eap)
8482 exarg_T *eap;
8483{
8484 char_u hash[UNDO_HASH_SIZE];
8485
8486 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008487 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008488}
8489#endif
8490
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491/*
8492 * ":redo".
8493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 static void
8495ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008496 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497{
8498 u_redo(1);
8499}
8500
8501/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008502 * ":earlier" and ":later".
8503 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008504 static void
8505ex_later(eap)
8506 exarg_T *eap;
8507{
8508 long count = 0;
8509 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008510 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008511 char_u *p = eap->arg;
8512
8513 if (*p == NUL)
8514 count = 1;
8515 else if (isdigit(*p))
8516 {
8517 count = getdigits(&p);
8518 switch (*p)
8519 {
8520 case 's': ++p; sec = TRUE; break;
8521 case 'm': ++p; sec = TRUE; count *= 60; break;
8522 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008523 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8524 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008525 }
8526 }
8527
8528 if (*p != NUL)
8529 EMSG2(_(e_invarg2), eap->arg);
8530 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008531 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8532 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008533}
8534
8535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 * ":redir": start/stop redirection.
8537 */
8538 static void
8539ex_redir(eap)
8540 exarg_T *eap;
8541{
8542 char *mode;
8543 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008544 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545
8546 if (STRICMP(eap->arg, "END") == 0)
8547 close_redir();
8548 else
8549 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008550 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008552 ++arg;
8553 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008555 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 mode = "a";
8557 }
8558 else
8559 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008560 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561
8562 close_redir();
8563
8564 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008565 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 if (fname == NULL)
8567 return;
8568#ifdef FEAT_BROWSE
8569 if (cmdmod.browse)
8570 {
8571 char_u *browseFile;
8572
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008573 browseFile = do_browse(BROWSE_SAVE,
8574 (char_u *)_("Save Redirection"),
8575 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 if (browseFile == NULL)
8577 return; /* operation cancelled */
8578 vim_free(fname);
8579 fname = browseFile;
8580 eap->forceit = TRUE; /* since dialog already asked */
8581 }
8582#endif
8583
8584 redir_fd = open_exfile(fname, eap->forceit, mode);
8585 vim_free(fname);
8586 }
8587#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008588 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 {
8590 /* redirect to a register a-z (resp. A-Z for appending) */
8591 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008592 ++arg;
8593 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008595 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008596 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008598 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008600 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008601 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008602 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008603 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008605 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008606 if (*arg == '>')
8607 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008608 /* Make register empty when not using @A-@Z and the
8609 * command is valid. */
8610 if (*arg == NUL && !isupper(redir_reg))
8611 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008613 }
8614 if (*arg != NUL)
8615 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008616 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008617 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008618 }
8619 }
8620 else if (*arg == '=' && arg[1] == '>')
8621 {
8622 int append;
8623
8624 /* redirect to a variable */
8625 close_redir();
8626 arg += 2;
8627
8628 if (*arg == '>')
8629 {
8630 ++arg;
8631 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 }
8633 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008634 append = FALSE;
8635
8636 if (var_redir_start(skipwhite(arg), append) == OK)
8637 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 }
8639#endif
8640
8641 /* TODO: redirect to a buffer */
8642
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008644 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008646
8647 /* Make sure redirection is not off. Can happen for cmdline completion
8648 * that indirectly invokes a command to catch its output. */
8649 if (redir_fd != NULL
8650#ifdef FEAT_EVAL
8651 || redir_reg || redir_vname
8652#endif
8653 )
8654 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655}
8656
8657/*
8658 * ":redraw": force redraw
8659 */
8660 static void
8661ex_redraw(eap)
8662 exarg_T *eap;
8663{
8664 int r = RedrawingDisabled;
8665 int p = p_lz;
8666
8667 RedrawingDisabled = 0;
8668 p_lz = FALSE;
8669 update_topline();
8670 update_screen(eap->forceit ? CLEAR :
8671#ifdef FEAT_VISUAL
8672 VIsual_active ? INVERTED :
8673#endif
8674 0);
8675#ifdef FEAT_TITLE
8676 if (need_maketitle)
8677 maketitle();
8678#endif
8679 RedrawingDisabled = r;
8680 p_lz = p;
8681
8682 /* Reset msg_didout, so that a message that's there is overwritten. */
8683 msg_didout = FALSE;
8684 msg_col = 0;
8685
8686 out_flush();
8687}
8688
8689/*
8690 * ":redrawstatus": force redraw of status line(s)
8691 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 static void
8693ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008694 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695{
8696#if defined(FEAT_WINDOWS)
8697 int r = RedrawingDisabled;
8698 int p = p_lz;
8699
8700 RedrawingDisabled = 0;
8701 p_lz = FALSE;
8702 if (eap->forceit)
8703 status_redraw_all();
8704 else
8705 status_redraw_curbuf();
8706 update_screen(
8707# ifdef FEAT_VISUAL
8708 VIsual_active ? INVERTED :
8709# endif
8710 0);
8711 RedrawingDisabled = r;
8712 p_lz = p;
8713 out_flush();
8714#endif
8715}
8716
8717 static void
8718close_redir()
8719{
8720 if (redir_fd != NULL)
8721 {
8722 fclose(redir_fd);
8723 redir_fd = NULL;
8724 }
8725#ifdef FEAT_EVAL
8726 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008727 if (redir_vname)
8728 {
8729 var_redir_stop();
8730 redir_vname = 0;
8731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732#endif
8733}
8734
8735#if defined(FEAT_SESSION) && defined(USE_CRNL)
8736# define MKSESSION_NL
8737static int mksession_nl = FALSE; /* use NL only in put_eol() */
8738#endif
8739
8740/*
8741 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8742 */
8743 static void
8744ex_mkrc(eap)
8745 exarg_T *eap;
8746{
8747 FILE *fd;
8748 int failed = FALSE;
8749 char_u *fname;
8750#ifdef FEAT_BROWSE
8751 char_u *browseFile = NULL;
8752#endif
8753#ifdef FEAT_SESSION
8754 int view_session = FALSE;
8755 int using_vdir = FALSE; /* using 'viewdir'? */
8756 char_u *viewFile = NULL;
8757 unsigned *flagp;
8758#endif
8759
8760 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8761 {
8762#ifdef FEAT_SESSION
8763 view_session = TRUE;
8764#else
8765 ex_ni(eap);
8766 return;
8767#endif
8768 }
8769
8770#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008771 /* Use the short file name until ":lcd" is used. We also don't use the
8772 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008773 did_lcd = FALSE;
8774
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8776 if (eap->cmdidx == CMD_mkview
8777 && (*eap->arg == NUL
8778 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8779 {
8780 eap->forceit = TRUE;
8781 fname = get_view_file(*eap->arg);
8782 if (fname == NULL)
8783 return;
8784 viewFile = fname;
8785 using_vdir = TRUE;
8786 }
8787 else
8788#endif
8789 if (*eap->arg != NUL)
8790 fname = eap->arg;
8791 else if (eap->cmdidx == CMD_mkvimrc)
8792 fname = (char_u *)VIMRC_FILE;
8793#ifdef FEAT_SESSION
8794 else if (eap->cmdidx == CMD_mksession)
8795 fname = (char_u *)SESSION_FILE;
8796#endif
8797 else
8798 fname = (char_u *)EXRC_FILE;
8799
8800#ifdef FEAT_BROWSE
8801 if (cmdmod.browse)
8802 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008803 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804# ifdef FEAT_SESSION
8805 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8806 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8807# endif
8808 (char_u *)_("Save Setup"),
8809 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8810 if (browseFile == NULL)
8811 goto theend;
8812 fname = browseFile;
8813 eap->forceit = TRUE; /* since dialog already asked */
8814 }
8815#endif
8816
8817#if defined(FEAT_SESSION) && defined(vim_mkdir)
8818 /* When using 'viewdir' may have to create the directory. */
8819 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008820 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821#endif
8822
8823 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8824 if (fd != NULL)
8825 {
8826#ifdef FEAT_SESSION
8827 if (eap->cmdidx == CMD_mkview)
8828 flagp = &vop_flags;
8829 else
8830 flagp = &ssop_flags;
8831#endif
8832
8833#ifdef MKSESSION_NL
8834 /* "unix" in 'sessionoptions': use NL line separator */
8835 if (view_session && (*flagp & SSOP_UNIX))
8836 mksession_nl = TRUE;
8837#endif
8838
8839 /* Write the version command for :mkvimrc */
8840 if (eap->cmdidx == CMD_mkvimrc)
8841 (void)put_line(fd, "version 6.0");
8842
8843#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008844 if (eap->cmdidx == CMD_mksession)
8845 {
8846 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8847 failed = TRUE;
8848 }
8849
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 if (eap->cmdidx != CMD_mkview)
8851#endif
8852 {
8853 /* Write setting 'compatible' first, because it has side effects.
8854 * For that same reason only do it when needed. */
8855 if (p_cp)
8856 (void)put_line(fd, "if !&cp | set cp | endif");
8857 else
8858 (void)put_line(fd, "if &cp | set nocp | endif");
8859 }
8860
8861#ifdef FEAT_SESSION
8862 if (!view_session
8863 || (eap->cmdidx == CMD_mksession
8864 && (*flagp & SSOP_OPTIONS)))
8865#endif
8866 failed |= (makemap(fd, NULL) == FAIL
8867 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8868
8869#ifdef FEAT_SESSION
8870 if (!failed && view_session)
8871 {
8872 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8873 failed = TRUE;
8874 if (eap->cmdidx == CMD_mksession)
8875 {
8876 char_u dirnow[MAXPATHL]; /* current directory */
8877
8878 /*
8879 * Change to session file's dir.
8880 */
8881 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8882 || mch_chdir((char *)dirnow) != 0)
8883 *dirnow = NUL;
8884 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8885 {
8886 if (vim_chdirfile(fname) == OK)
8887 shorten_fnames(TRUE);
8888 }
8889 else if (*dirnow != NUL
8890 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8891 {
Bram Moolenaarc0b35652009-03-01 01:45:35 +00008892 if (mch_chdir((char *)globaldir) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00008893 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 }
8895
8896 failed |= (makeopens(fd, dirnow) == FAIL);
8897
8898 /* restore original dir */
8899 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8900 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8901 {
8902 if (mch_chdir((char *)dirnow) != 0)
8903 EMSG(_(e_prev_dir));
8904 shorten_fnames(TRUE);
8905 }
8906 }
8907 else
8908 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008909 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8910 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 }
8912 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8913 == FAIL)
8914 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008915 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8916 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008917 if (eap->cmdidx == CMD_mksession)
8918 {
8919 if (put_line(fd, "unlet SessionLoad") == FAIL)
8920 failed = TRUE;
8921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 }
8923#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008924 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8925 failed = TRUE;
8926
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 failed |= fclose(fd);
8928
8929 if (failed)
8930 EMSG(_(e_write));
8931#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8932 else if (eap->cmdidx == CMD_mksession)
8933 {
8934 /* successful session write - set this_session var */
8935 char_u tbuf[MAXPATHL];
8936
8937 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8938 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8939 }
8940#endif
8941#ifdef MKSESSION_NL
8942 mksession_nl = FALSE;
8943#endif
8944 }
8945
8946#ifdef FEAT_BROWSE
8947theend:
8948 vim_free(browseFile);
8949#endif
8950#ifdef FEAT_SESSION
8951 vim_free(viewFile);
8952#endif
8953}
8954
Bram Moolenaardf177f62005-02-22 08:39:57 +00008955#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8956 || defined(PROTO)
8957 int
8958vim_mkdir_emsg(name, prot)
8959 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00008960 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008961{
8962 if (vim_mkdir(name, prot) != 0)
8963 {
8964 EMSG2(_("E739: Cannot create directory: %s"), name);
8965 return FAIL;
8966 }
8967 return OK;
8968}
8969#endif
8970
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971/*
8972 * Open a file for writing for an Ex command, with some checks.
8973 * Return file descriptor, or NULL on failure.
8974 */
8975 FILE *
8976open_exfile(fname, forceit, mode)
8977 char_u *fname;
8978 int forceit;
8979 char *mode; /* "w" for create new file or "a" for append */
8980{
8981 FILE *fd;
8982
8983#ifdef UNIX
8984 /* with Unix it is possible to open a directory */
8985 if (mch_isdir(fname))
8986 {
8987 EMSG2(_(e_isadir2), fname);
8988 return NULL;
8989 }
8990#endif
8991 if (!forceit && *mode != 'a' && vim_fexists(fname))
8992 {
8993 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8994 return NULL;
8995 }
8996
8997 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8998 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8999
9000 return fd;
9001}
9002
9003/*
9004 * ":mark" and ":k".
9005 */
9006 static void
9007ex_mark(eap)
9008 exarg_T *eap;
9009{
9010 pos_T pos;
9011
9012 if (*eap->arg == NUL) /* No argument? */
9013 EMSG(_(e_argreq));
9014 else if (eap->arg[1] != NUL) /* more than one character? */
9015 EMSG(_(e_trailing));
9016 else
9017 {
9018 pos = curwin->w_cursor; /* save curwin->w_cursor */
9019 curwin->w_cursor.lnum = eap->line2;
9020 beginline(BL_WHITE | BL_FIX);
9021 if (setmark(*eap->arg) == FAIL) /* set mark */
9022 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9023 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9024 }
9025}
9026
9027/*
9028 * Update w_topline, w_leftcol and the cursor position.
9029 */
9030 void
9031update_topline_cursor()
9032{
9033 check_cursor(); /* put cursor on valid line */
9034 update_topline();
9035 if (!curwin->w_p_wrap)
9036 validate_cursor();
9037 update_curswant();
9038}
9039
9040#ifdef FEAT_EX_EXTRA
9041/*
9042 * ":normal[!] {commands}": Execute normal mode commands.
9043 */
9044 static void
9045ex_normal(eap)
9046 exarg_T *eap;
9047{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 int save_msg_scroll = msg_scroll;
9049 int save_restart_edit = restart_edit;
9050 int save_msg_didout = msg_didout;
9051 int save_State = State;
9052 tasave_T tabuf;
9053 int save_insertmode = p_im;
9054 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009055 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056#ifdef FEAT_MBYTE
9057 char_u *arg = NULL;
9058 int l;
9059 char_u *p;
9060#endif
9061
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009062 if (ex_normal_lock > 0)
9063 {
9064 EMSG(_(e_secure));
9065 return;
9066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 if (ex_normal_busy >= p_mmd)
9068 {
9069 EMSG(_("E192: Recursive use of :normal too deep"));
9070 return;
9071 }
9072 ++ex_normal_busy;
9073
9074 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9075 restart_edit = 0; /* don't go to Insert mode */
9076 p_im = FALSE; /* don't use 'insertmode' */
9077
9078#ifdef FEAT_MBYTE
9079 /*
9080 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9081 * this for the K_SPECIAL leading byte, otherwise special keys will not
9082 * work.
9083 */
9084 if (has_mbyte)
9085 {
9086 int len = 0;
9087
9088 /* Count the number of characters to be escaped. */
9089 for (p = eap->arg; *p != NUL; ++p)
9090 {
9091# ifdef FEAT_GUI
9092 if (*p == CSI) /* leadbyte CSI */
9093 len += 2;
9094# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009095 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9097# ifdef FEAT_GUI
9098 || *p == CSI
9099# endif
9100 )
9101 len += 2;
9102 }
9103 if (len > 0)
9104 {
9105 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9106 if (arg != NULL)
9107 {
9108 len = 0;
9109 for (p = eap->arg; *p != NUL; ++p)
9110 {
9111 arg[len++] = *p;
9112# ifdef FEAT_GUI
9113 if (*p == CSI)
9114 {
9115 arg[len++] = KS_EXTRA;
9116 arg[len++] = (int)KE_CSI;
9117 }
9118# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009119 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 {
9121 arg[len++] = *++p;
9122 if (*p == K_SPECIAL)
9123 {
9124 arg[len++] = KS_SPECIAL;
9125 arg[len++] = KE_FILLER;
9126 }
9127# ifdef FEAT_GUI
9128 else if (*p == CSI)
9129 {
9130 arg[len++] = KS_EXTRA;
9131 arg[len++] = (int)KE_CSI;
9132 }
9133# endif
9134 }
9135 arg[len] = NUL;
9136 }
9137 }
9138 }
9139 }
9140#endif
9141
9142 /*
9143 * Save the current typeahead. This is required to allow using ":normal"
9144 * from an event handler and makes sure we don't hang when the argument
9145 * ends with half a command.
9146 */
9147 save_typeahead(&tabuf);
9148 if (tabuf.typebuf_valid)
9149 {
9150 /*
9151 * Repeat the :normal command for each line in the range. When no
9152 * range given, execute it just once, without positioning the cursor
9153 * first.
9154 */
9155 do
9156 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 if (eap->addr_count != 0)
9158 {
9159 curwin->w_cursor.lnum = eap->line1++;
9160 curwin->w_cursor.col = 0;
9161 }
9162
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009163 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164#ifdef FEAT_MBYTE
9165 arg != NULL ? arg :
9166#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009167 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 }
9169 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9170 }
9171
9172 /* Might not return to the main loop when in an event handler. */
9173 update_topline_cursor();
9174
9175 /* Restore the previous typeahead. */
9176 restore_typeahead(&tabuf);
9177
9178 --ex_normal_busy;
9179 msg_scroll = save_msg_scroll;
9180 restart_edit = save_restart_edit;
9181 p_im = save_insertmode;
9182 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009183 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9185
9186 /* Restore the state (needed when called from a function executed for
9187 * 'indentexpr'). */
9188 State = save_State;
9189#ifdef FEAT_MBYTE
9190 vim_free(arg);
9191#endif
9192}
9193
9194/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009195 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 */
9197 static void
9198ex_startinsert(eap)
9199 exarg_T *eap;
9200{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009201 if (eap->forceit)
9202 {
9203 coladvance((colnr_T)MAXCOL);
9204 curwin->w_curswant = MAXCOL;
9205 curwin->w_set_curswant = FALSE;
9206 }
9207
Bram Moolenaara40c5002005-01-09 21:16:21 +00009208 /* Ignore the command when already in Insert mode. Inserting an
9209 * expression register that invokes a function can do this. */
9210 if (State & INSERT)
9211 return;
9212
Bram Moolenaar64969662005-12-14 21:59:55 +00009213 if (eap->cmdidx == CMD_startinsert)
9214 restart_edit = 'a';
9215 else if (eap->cmdidx == CMD_startreplace)
9216 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009218 restart_edit = 'V';
9219
9220 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009222 if (eap->cmdidx == CMD_startinsert)
9223 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 curwin->w_curswant = 0; /* avoid MAXCOL */
9225 }
9226}
9227
9228/*
9229 * ":stopinsert"
9230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 static void
9232ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009233 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234{
9235 restart_edit = 0;
9236 stop_insert_mode = TRUE;
9237}
9238#endif
9239
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009240#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9241/*
9242 * Execute normal mode command "cmd".
9243 * "remap" can be REMAP_NONE or REMAP_YES.
9244 */
9245 void
9246exec_normal_cmd(cmd, remap, silent)
9247 char_u *cmd;
9248 int remap;
9249 int silent;
9250{
9251 oparg_T oa;
9252
9253 /*
9254 * Stuff the argument into the typeahead buffer.
9255 * Execute normal_cmd() until there is no typeahead left.
9256 */
9257 clear_oparg(&oa);
9258 finish_op = FALSE;
9259 ins_typebuf(cmd, remap, 0, TRUE, silent);
9260 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9261 && !got_int)
9262 {
9263 update_topline_cursor();
9264 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
9265 }
9266}
9267#endif
9268
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269#ifdef FEAT_FIND_ID
9270 static void
9271ex_checkpath(eap)
9272 exarg_T *eap;
9273{
9274 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9275 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9276 (linenr_T)1, (linenr_T)MAXLNUM);
9277}
9278
9279#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9280/*
9281 * ":psearch"
9282 */
9283 static void
9284ex_psearch(eap)
9285 exarg_T *eap;
9286{
9287 g_do_tagpreview = p_pvh;
9288 ex_findpat(eap);
9289 g_do_tagpreview = 0;
9290}
9291#endif
9292
9293 static void
9294ex_findpat(eap)
9295 exarg_T *eap;
9296{
9297 int whole = TRUE;
9298 long n;
9299 char_u *p;
9300 int action;
9301
9302 switch (cmdnames[eap->cmdidx].cmd_name[2])
9303 {
9304 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9305 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9306 action = ACTION_GOTO;
9307 else
9308 action = ACTION_SHOW;
9309 break;
9310 case 'i': /* ":ilist" and ":dlist" */
9311 action = ACTION_SHOW_ALL;
9312 break;
9313 case 'u': /* ":ijump" and ":djump" */
9314 action = ACTION_GOTO;
9315 break;
9316 default: /* ":isplit" and ":dsplit" */
9317 action = ACTION_SPLIT;
9318 break;
9319 }
9320
9321 n = 1;
9322 if (vim_isdigit(*eap->arg)) /* get count */
9323 {
9324 n = getdigits(&eap->arg);
9325 eap->arg = skipwhite(eap->arg);
9326 }
9327 if (*eap->arg == '/') /* Match regexp, not just whole words */
9328 {
9329 whole = FALSE;
9330 ++eap->arg;
9331 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9332 if (*p)
9333 {
9334 *p++ = NUL;
9335 p = skipwhite(p);
9336
9337 /* Check for trailing illegal characters */
9338 if (!ends_excmd(*p))
9339 eap->errmsg = e_trailing;
9340 else
9341 eap->nextcmd = check_nextcmd(p);
9342 }
9343 }
9344 if (!eap->skip)
9345 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9346 whole, !eap->forceit,
9347 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9348 n, action, eap->line1, eap->line2);
9349}
9350#endif
9351
9352#ifdef FEAT_WINDOWS
9353
9354# ifdef FEAT_QUICKFIX
9355/*
9356 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9357 */
9358 static void
9359ex_ptag(eap)
9360 exarg_T *eap;
9361{
9362 g_do_tagpreview = p_pvh;
9363 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9364}
9365
9366/*
9367 * ":pedit"
9368 */
9369 static void
9370ex_pedit(eap)
9371 exarg_T *eap;
9372{
9373 win_T *curwin_save = curwin;
9374
9375 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009376 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 keep_help_flag = curwin_save->w_buffer->b_help;
9378 do_exedit(eap, NULL);
9379 keep_help_flag = FALSE;
9380 if (curwin != curwin_save && win_valid(curwin_save))
9381 {
9382 /* Return cursor to where we were */
9383 validate_cursor();
9384 redraw_later(VALID);
9385 win_enter(curwin_save, TRUE);
9386 }
9387 g_do_tagpreview = 0;
9388}
9389# endif
9390
9391/*
9392 * ":stag", ":stselect" and ":stjump".
9393 */
9394 static void
9395ex_stag(eap)
9396 exarg_T *eap;
9397{
9398 postponed_split = -1;
9399 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009400 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9402 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009403 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404}
9405#endif
9406
9407/*
9408 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9409 */
9410 static void
9411ex_tag(eap)
9412 exarg_T *eap;
9413{
9414 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9415}
9416
9417 static void
9418ex_tag_cmd(eap, name)
9419 exarg_T *eap;
9420 char_u *name;
9421{
9422 int cmd;
9423
9424 switch (name[1])
9425 {
9426 case 'j': cmd = DT_JUMP; /* ":tjump" */
9427 break;
9428 case 's': cmd = DT_SELECT; /* ":tselect" */
9429 break;
9430 case 'p': cmd = DT_PREV; /* ":tprevious" */
9431 break;
9432 case 'N': cmd = DT_PREV; /* ":tNext" */
9433 break;
9434 case 'n': cmd = DT_NEXT; /* ":tnext" */
9435 break;
9436 case 'o': cmd = DT_POP; /* ":pop" */
9437 break;
9438 case 'f': /* ":tfirst" */
9439 case 'r': cmd = DT_FIRST; /* ":trewind" */
9440 break;
9441 case 'l': cmd = DT_LAST; /* ":tlast" */
9442 break;
9443 default: /* ":tag" */
9444#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009445 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 {
9447 do_cstag(eap);
9448 return;
9449 }
9450#endif
9451 cmd = DT_TAG;
9452 break;
9453 }
9454
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009455 if (name[0] == 'l')
9456 {
9457#ifndef FEAT_QUICKFIX
9458 ex_ni(eap);
9459 return;
9460#else
9461 cmd = DT_LTAG;
9462#endif
9463 }
9464
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9466 eap->forceit, TRUE);
9467}
9468
9469/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009470 * Check "str" for starting with a special cmdline variable.
9471 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9472 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9473 */
9474 int
9475find_cmdline_var(src, usedlen)
9476 char_u *src;
9477 int *usedlen;
9478{
9479 int len;
9480 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009481 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009482 "%",
9483#define SPEC_PERC 0
9484 "#",
9485#define SPEC_HASH 1
9486 "<cword>", /* cursor word */
9487#define SPEC_CWORD 2
9488 "<cWORD>", /* cursor WORD */
9489#define SPEC_CCWORD 3
9490 "<cfile>", /* cursor path name */
9491#define SPEC_CFILE 4
9492 "<sfile>", /* ":so" file name */
9493#define SPEC_SFILE 5
9494#ifdef FEAT_AUTOCMD
9495 "<afile>", /* autocommand file name */
9496# define SPEC_AFILE 6
9497 "<abuf>", /* autocommand buffer number */
9498# define SPEC_ABUF 7
9499 "<amatch>", /* autocommand match name */
9500# define SPEC_AMATCH 8
9501#endif
9502#ifdef FEAT_CLIENTSERVER
9503 "<client>"
9504# define SPEC_CLIENT 9
9505#endif
9506 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009507
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009508 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009509 {
9510 len = (int)STRLEN(spec_str[i]);
9511 if (STRNCMP(src, spec_str[i], len) == 0)
9512 {
9513 *usedlen = len;
9514 return i;
9515 }
9516 }
9517 return -1;
9518}
9519
9520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 * Evaluate cmdline variables.
9522 *
9523 * change '%' to curbuf->b_ffname
9524 * '#' to curwin->w_altfile
9525 * '<cword>' to word under the cursor
9526 * '<cWORD>' to WORD under the cursor
9527 * '<cfile>' to path name under the cursor
9528 * '<sfile>' to sourced file name
9529 * '<afile>' to file name for autocommand
9530 * '<abuf>' to buffer number for autocommand
9531 * '<amatch>' to matching name for autocommand
9532 *
9533 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9534 * "" for error without a message) and NULL is returned.
9535 * Returns an allocated string if a valid match was found.
9536 * Returns NULL if no match was found. "usedlen" then still contains the
9537 * number of characters to skip.
9538 */
9539 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009540eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009542 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 int *usedlen; /* characters after src that are used */
9544 linenr_T *lnump; /* line number for :e command, or NULL */
9545 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009546 int *escaped; /* return value has escaped white space (can
9547 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548{
9549 int i;
9550 char_u *s;
9551 char_u *result;
9552 char_u *resultbuf = NULL;
9553 int resultlen;
9554 buf_T *buf;
9555 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9556 int spec_idx;
9557#ifdef FEAT_MODIFY_FNAME
9558 int skip_mod = FALSE;
9559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560
9561#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9562 char_u strbuf[30];
9563#endif
9564
9565 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009566 if (escaped != NULL)
9567 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568
9569 /*
9570 * Check if there is something to do.
9571 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009572 spec_idx = find_cmdline_var(src, usedlen);
9573 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 {
9575 *usedlen = 1;
9576 return NULL;
9577 }
9578
9579 /*
9580 * Skip when preceded with a backslash "\%" and "\#".
9581 * Note: In "\\%" the % is also not recognized!
9582 */
9583 if (src > srcstart && src[-1] == '\\')
9584 {
9585 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009586 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 return NULL;
9588 }
9589
9590 /*
9591 * word or WORD under cursor
9592 */
9593 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9594 {
9595 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9596 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9597 if (resultlen == 0)
9598 {
9599 *errormsg = (char_u *)"";
9600 return NULL;
9601 }
9602 }
9603
9604 /*
9605 * '#': Alternate file name
9606 * '%': Current file name
9607 * File name under the cursor
9608 * File name for autocommand
9609 * and following modifiers
9610 */
9611 else
9612 {
9613 switch (spec_idx)
9614 {
9615 case SPEC_PERC: /* '%': current file */
9616 if (curbuf->b_fname == NULL)
9617 {
9618 result = (char_u *)"";
9619 valid = 0; /* Must have ":p:h" to be valid */
9620 }
9621 else
9622#ifdef RISCOS
9623 /* Always use the full path for RISC OS if possible. */
9624 result = curbuf->b_ffname;
9625 if (result == NULL)
9626 result = curbuf->b_fname;
9627#else
9628 result = curbuf->b_fname;
9629#endif
9630 break;
9631
9632 case SPEC_HASH: /* '#' or "#99": alternate file */
9633 if (src[1] == '#') /* "##": the argument list */
9634 {
9635 result = arg_all();
9636 resultbuf = result;
9637 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009638 if (escaped != NULL)
9639 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640#ifdef FEAT_MODIFY_FNAME
9641 skip_mod = TRUE;
9642#endif
9643 break;
9644 }
9645 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009646 if (*s == '<') /* "#<99" uses v:oldfiles */
9647 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 i = (int)getdigits(&s);
9649 *usedlen = (int)(s - src); /* length of what we expand */
9650
Bram Moolenaard812df62008-11-09 12:46:09 +00009651 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009653 if (*usedlen < 2)
9654 {
9655 /* Should we give an error message for #<text? */
9656 *usedlen = 1;
9657 return NULL;
9658 }
9659#ifdef FEAT_EVAL
9660 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9661 (long)i);
9662 if (result == NULL)
9663 {
9664 *errormsg = (char_u *)"";
9665 return NULL;
9666 }
9667#else
9668 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 }
9672 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009673 {
9674 buf = buflist_findnr(i);
9675 if (buf == NULL)
9676 {
9677 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9678 return NULL;
9679 }
9680 if (lnump != NULL)
9681 *lnump = ECMD_LAST;
9682 if (buf->b_fname == NULL)
9683 {
9684 result = (char_u *)"";
9685 valid = 0; /* Must have ":p:h" to be valid */
9686 }
9687 else
9688 result = buf->b_fname;
9689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 break;
9691
9692#ifdef FEAT_SEARCHPATH
9693 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009694 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 if (result == NULL)
9696 {
9697 *errormsg = (char_u *)"";
9698 return NULL;
9699 }
9700 resultbuf = result; /* remember allocated string */
9701 break;
9702#endif
9703
9704#ifdef FEAT_AUTOCMD
9705 case SPEC_AFILE: /* file name for autocommand */
9706 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009707 if (result != NULL && !autocmd_fname_full)
9708 {
9709 /* Still need to turn the fname into a full path. It is
9710 * postponed to avoid a delay when <afile> is not used. */
9711 autocmd_fname_full = TRUE;
9712 result = FullName_save(autocmd_fname, FALSE);
9713 vim_free(autocmd_fname);
9714 autocmd_fname = result;
9715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 if (result == NULL)
9717 {
9718 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9719 return NULL;
9720 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009721 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 break;
9723
9724 case SPEC_ABUF: /* buffer number for autocommand */
9725 if (autocmd_bufnr <= 0)
9726 {
9727 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9728 return NULL;
9729 }
9730 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9731 result = strbuf;
9732 break;
9733
9734 case SPEC_AMATCH: /* match name for autocommand */
9735 result = autocmd_match;
9736 if (result == NULL)
9737 {
9738 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9739 return NULL;
9740 }
9741 break;
9742
9743#endif
9744 case SPEC_SFILE: /* file name for ":so" command */
9745 result = sourcing_name;
9746 if (result == NULL)
9747 {
9748 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9749 return NULL;
9750 }
9751 break;
9752#if defined(FEAT_CLIENTSERVER)
9753 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009754 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9755 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 result = strbuf;
9757 break;
9758#endif
9759 }
9760
9761 resultlen = (int)STRLEN(result); /* length of new string */
9762 if (src[*usedlen] == '<') /* remove the file name extension */
9763 {
9764 ++*usedlen;
9765#ifdef RISCOS
9766 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9767#else
9768 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9769#endif
9770 resultlen = (int)(s - result);
9771 }
9772#ifdef FEAT_MODIFY_FNAME
9773 else if (!skip_mod)
9774 {
9775 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9776 &resultlen);
9777 if (result == NULL)
9778 {
9779 *errormsg = (char_u *)"";
9780 return NULL;
9781 }
9782 }
9783#endif
9784 }
9785
9786 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9787 {
9788 if (valid != VALID_HEAD + VALID_PATH)
9789 /* xgettext:no-c-format */
9790 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9791 else
9792 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9793 result = NULL;
9794 }
9795 else
9796 result = vim_strnsave(result, resultlen);
9797 vim_free(resultbuf);
9798 return result;
9799}
9800
9801/*
9802 * Concatenate all files in the argument list, separated by spaces, and return
9803 * it in one allocated string.
9804 * Spaces and backslashes in the file names are escaped with a backslash.
9805 * Returns NULL when out of memory.
9806 */
9807 static char_u *
9808arg_all()
9809{
9810 int len;
9811 int idx;
9812 char_u *retval = NULL;
9813 char_u *p;
9814
9815 /*
9816 * Do this loop two times:
9817 * first time: compute the total length
9818 * second time: concatenate the names
9819 */
9820 for (;;)
9821 {
9822 len = 0;
9823 for (idx = 0; idx < ARGCOUNT; ++idx)
9824 {
9825 p = alist_name(&ARGLIST[idx]);
9826 if (p != NULL)
9827 {
9828 if (len > 0)
9829 {
9830 /* insert a space in between names */
9831 if (retval != NULL)
9832 retval[len] = ' ';
9833 ++len;
9834 }
9835 for ( ; *p != NUL; ++p)
9836 {
9837 if (*p == ' ' || *p == '\\')
9838 {
9839 /* insert a backslash */
9840 if (retval != NULL)
9841 retval[len] = '\\';
9842 ++len;
9843 }
9844 if (retval != NULL)
9845 retval[len] = *p;
9846 ++len;
9847 }
9848 }
9849 }
9850
9851 /* second time: break here */
9852 if (retval != NULL)
9853 {
9854 retval[len] = NUL;
9855 break;
9856 }
9857
9858 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009859 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 if (retval == NULL)
9861 break;
9862 }
9863
9864 return retval;
9865}
9866
9867#if defined(FEAT_AUTOCMD) || defined(PROTO)
9868/*
9869 * Expand the <sfile> string in "arg".
9870 *
9871 * Returns an allocated string, or NULL for any error.
9872 */
9873 char_u *
9874expand_sfile(arg)
9875 char_u *arg;
9876{
9877 char_u *errormsg;
9878 int len;
9879 char_u *result;
9880 char_u *newres;
9881 char_u *repl;
9882 int srclen;
9883 char_u *p;
9884
9885 result = vim_strsave(arg);
9886 if (result == NULL)
9887 return NULL;
9888
9889 for (p = result; *p; )
9890 {
9891 if (STRNCMP(p, "<sfile>", 7) != 0)
9892 ++p;
9893 else
9894 {
9895 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009896 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 if (errormsg != NULL)
9898 {
9899 if (*errormsg)
9900 emsg(errormsg);
9901 vim_free(result);
9902 return NULL;
9903 }
9904 if (repl == NULL) /* no match (cannot happen) */
9905 {
9906 p += srclen;
9907 continue;
9908 }
9909 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9910 newres = alloc(len);
9911 if (newres == NULL)
9912 {
9913 vim_free(repl);
9914 vim_free(result);
9915 return NULL;
9916 }
9917 mch_memmove(newres, result, (size_t)(p - result));
9918 STRCPY(newres + (p - result), repl);
9919 len = (int)STRLEN(newres);
9920 STRCAT(newres, p + srclen);
9921 vim_free(repl);
9922 vim_free(result);
9923 result = newres;
9924 p = newres + len; /* continue after the match */
9925 }
9926 }
9927
9928 return result;
9929}
9930#endif
9931
9932#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009933static int ses_winsizes __ARGS((FILE *fd, int restore_size,
9934 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9936static frame_T *ses_skipframe __ARGS((frame_T *fr));
9937static int ses_do_frame __ARGS((frame_T *fr));
9938static int ses_do_win __ARGS((win_T *wp));
9939static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9940static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9941static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9942
9943/*
9944 * Write openfile commands for the current buffers to an .exrc file.
9945 * Return FAIL on error, OK otherwise.
9946 */
9947 static int
9948makeopens(fd, dirnow)
9949 FILE *fd;
9950 char_u *dirnow; /* Current directory name */
9951{
9952 buf_T *buf;
9953 int only_save_windows = TRUE;
9954 int nr;
9955 int cnr = 1;
9956 int restore_size = TRUE;
9957 win_T *wp;
9958 char_u *sname;
9959 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009960 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009961 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009962 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009963 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009964 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965
9966 if (ssop_flags & SSOP_BUFFERS)
9967 only_save_windows = FALSE; /* Save ALL buffers */
9968
9969 /*
9970 * Begin by setting the this_session variable, and then other
9971 * sessionable variables.
9972 */
9973#ifdef FEAT_EVAL
9974 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9975 return FAIL;
9976 if (ssop_flags & SSOP_GLOBALS)
9977 if (store_session_globals(fd) == FAIL)
9978 return FAIL;
9979#endif
9980
9981 /*
9982 * Close all windows but one.
9983 */
9984 if (put_line(fd, "silent only") == FAIL)
9985 return FAIL;
9986
9987 /*
9988 * Now a :cd command to the session directory or the current directory
9989 */
9990 if (ssop_flags & SSOP_SESDIR)
9991 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009992 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9993 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 return FAIL;
9995 }
9996 else if (ssop_flags & SSOP_CURDIR)
9997 {
9998 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9999 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010000 || fputs("cd ", fd) < 0
10001 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10002 || put_eol(fd) == FAIL)
10003 {
10004 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 vim_free(sname);
10008 }
10009
10010 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010011 * If there is an empty, unnamed buffer we will wipe it out later.
10012 * Remember the buffer number.
10013 */
10014 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10015 return FAIL;
10016 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10017 return FAIL;
10018 if (put_line(fd, "endif") == FAIL)
10019 return FAIL;
10020
10021 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 * Now save the current files, current buffer first.
10023 */
10024 if (put_line(fd, "set shortmess=aoO") == FAIL)
10025 return FAIL;
10026
10027 /* Now put the other buffers into the buffer list */
10028 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10029 {
10030 if (!(only_save_windows && buf->b_nwindows == 0)
10031 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10032 && buf->b_fname != NULL
10033 && buf->b_p_bl)
10034 {
10035 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10036 : buf->b_wininfo->wi_fpos.lnum) < 0
10037 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10038 return FAIL;
10039 }
10040 }
10041
10042 /* the global argument list */
10043 if (ses_arglist(fd, "args", &global_alist.al_ga,
10044 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10045 return FAIL;
10046
10047 if (ssop_flags & SSOP_RESIZE)
10048 {
10049 /* Note: after the restore we still check it worked!*/
10050 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10051 || put_eol(fd) == FAIL)
10052 return FAIL;
10053 }
10054
10055#ifdef FEAT_GUI
10056 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10057 {
10058 int x, y;
10059
10060 if (gui_mch_get_winpos(&x, &y) == OK)
10061 {
10062 /* Note: after the restore we still check it worked!*/
10063 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10064 return FAIL;
10065 }
10066 }
10067#endif
10068
10069 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010070 * May repeat putting Windows for each tab, when "tabpages" is in
10071 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010072 * Don't use goto_tabpage(), it may change directory and trigger
10073 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010075 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010076 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010077 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010079 int need_tabnew = FALSE;
10080
Bram Moolenaar18144c82006-04-12 21:52:12 +000010081 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010083 tabpage_T *tp = find_tabpage(tabnr);
10084
10085 if (tp == NULL)
10086 break; /* done all tab pages */
10087 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010088 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010089 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010090 tab_topframe = topframe;
10091 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010092 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010093 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010094 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010095 tab_topframe = tp->tp_topframe;
10096 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010097 if (tabnr > 1)
10098 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100
Bram Moolenaar18144c82006-04-12 21:52:12 +000010101 /*
10102 * Before creating the window layout, try loading one file. If this
10103 * is aborted we don't end up with a number of useless windows.
10104 * This may have side effects! (e.g., compressed or network file).
10105 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010106 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010107 {
10108 if (ses_do_win(wp)
10109 && wp->w_buffer->b_ffname != NULL
10110 && !wp->w_buffer->b_help
10111#ifdef FEAT_QUICKFIX
10112 && !bt_nofile(wp->w_buffer)
10113#endif
10114 )
10115 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010116 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010117 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10118 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010119 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010120 if (!wp->w_arg_idx_invalid)
10121 edited_win = wp;
10122 break;
10123 }
10124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010126 /* If no file got edited create an empty tab page. */
10127 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10128 return FAIL;
10129
Bram Moolenaar18144c82006-04-12 21:52:12 +000010130 /*
10131 * Save current window layout.
10132 */
10133 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010135 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010137 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10138 return FAIL;
10139 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10140 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141
Bram Moolenaar18144c82006-04-12 21:52:12 +000010142 /*
10143 * Check if window sizes can be restored (no windows omitted).
10144 * Remember the window number of the current window after restoring.
10145 */
10146 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010147 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010148 {
10149 if (ses_do_win(wp))
10150 ++nr;
10151 else
10152 restore_size = FALSE;
10153 if (curwin == wp)
10154 cnr = nr;
10155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156
Bram Moolenaar18144c82006-04-12 21:52:12 +000010157 /* Go to the first window. */
10158 if (put_line(fd, "wincmd t") == FAIL)
10159 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010160
Bram Moolenaar18144c82006-04-12 21:52:12 +000010161 /*
10162 * If more than one window, see if sizes can be restored.
10163 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10164 * resized when moving between windows.
10165 * Do this before restoring the view, so that the topline and the
10166 * cursor can be set. This is done again below.
10167 */
10168 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10169 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010170 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010171 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172
Bram Moolenaar18144c82006-04-12 21:52:12 +000010173 /*
10174 * Restore the view of the window (options, file, cursor, etc.).
10175 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010176 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010177 {
10178 if (!ses_do_win(wp))
10179 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010180 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10181 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010182 return FAIL;
10183 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10184 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010185 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010186 }
10187
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010188 /* The argument index in the first tab page is zero, need to set it in
10189 * each window. For further tab pages it's the window where we do
10190 * "tabedit". */
10191 cur_arg_idx = next_arg_idx;
10192
Bram Moolenaar18144c82006-04-12 21:52:12 +000010193 /*
10194 * Restore cursor to the current window if it's not the first one.
10195 */
10196 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10197 || put_eol(fd) == FAIL))
10198 return FAIL;
10199
10200 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010201 * Restore window sizes again after jumping around in windows, because
10202 * the current window has a minimum size while others may not.
10203 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010204 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010205 return FAIL;
10206
Bram Moolenaar18144c82006-04-12 21:52:12 +000010207 /* Don't continue in another tab page when doing only the current one
10208 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010209 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010210 break;
10211 }
10212
10213 if (ssop_flags & SSOP_TABPAGES)
10214 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010215 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10216 || put_eol(fd) == FAIL)
10217 return FAIL;
10218 }
10219
Bram Moolenaar9c102382006-05-03 21:26:49 +000010220 /*
10221 * Wipe out an empty unnamed buffer we started in.
10222 */
10223 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10224 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010225 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010226 return FAIL;
10227 if (put_line(fd, "endif") == FAIL)
10228 return FAIL;
10229 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10230 return FAIL;
10231
10232 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10233 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10234 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10235 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236
10237 /*
10238 * Lastly, execute the x.vim file if it exists.
10239 */
10240 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10241 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010242 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 || put_line(fd, "endif") == FAIL)
10244 return FAIL;
10245
10246 return OK;
10247}
10248
10249 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010250ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 FILE *fd;
10252 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010253 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254{
10255 int n = 0;
10256 win_T *wp;
10257
10258 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10259 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010260 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 {
10262 if (!ses_do_win(wp))
10263 continue;
10264 ++n;
10265
10266 /* restore height when not full height */
10267 if (wp->w_height + wp->w_status_height < topframe->fr_height
10268 && (fprintf(fd,
10269 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10270 n, (long)wp->w_height, Rows / 2, Rows) < 0
10271 || put_eol(fd) == FAIL))
10272 return FAIL;
10273
10274 /* restore width when not full width */
10275 if (wp->w_width < Columns && (fprintf(fd,
10276 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10277 n, (long)wp->w_width, Columns / 2, Columns) < 0
10278 || put_eol(fd) == FAIL))
10279 return FAIL;
10280 }
10281 }
10282 else
10283 {
10284 /* Just equalise window sizes */
10285 if (put_line(fd, "wincmd =") == FAIL)
10286 return FAIL;
10287 }
10288 return OK;
10289}
10290
10291/*
10292 * Write commands to "fd" to recursively create windows for frame "fr",
10293 * horizontally and vertically split.
10294 * After the commands the last window in the frame is the current window.
10295 * Returns FAIL when writing the commands to "fd" fails.
10296 */
10297 static int
10298ses_win_rec(fd, fr)
10299 FILE *fd;
10300 frame_T *fr;
10301{
10302 frame_T *frc;
10303 int count = 0;
10304
10305 if (fr->fr_layout != FR_LEAF)
10306 {
10307 /* Find first frame that's not skipped and then create a window for
10308 * each following one (first frame is already there). */
10309 frc = ses_skipframe(fr->fr_child);
10310 if (frc != NULL)
10311 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10312 {
10313 /* Make window as big as possible so that we have lots of room
10314 * to split. */
10315 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10316 || put_line(fd, fr->fr_layout == FR_COL
10317 ? "split" : "vsplit") == FAIL)
10318 return FAIL;
10319 ++count;
10320 }
10321
10322 /* Go back to the first window. */
10323 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10324 ? "%dwincmd k" : "%dwincmd h", count) < 0
10325 || put_eol(fd) == FAIL))
10326 return FAIL;
10327
10328 /* Recursively create frames/windows in each window of this column or
10329 * row. */
10330 frc = ses_skipframe(fr->fr_child);
10331 while (frc != NULL)
10332 {
10333 ses_win_rec(fd, frc);
10334 frc = ses_skipframe(frc->fr_next);
10335 /* Go to next window. */
10336 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10337 return FAIL;
10338 }
10339 }
10340 return OK;
10341}
10342
10343/*
10344 * Skip frames that don't contain windows we want to save in the Session.
10345 * Returns NULL when there none.
10346 */
10347 static frame_T *
10348ses_skipframe(fr)
10349 frame_T *fr;
10350{
10351 frame_T *frc;
10352
10353 for (frc = fr; frc != NULL; frc = frc->fr_next)
10354 if (ses_do_frame(frc))
10355 break;
10356 return frc;
10357}
10358
10359/*
10360 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10361 * the Session.
10362 */
10363 static int
10364ses_do_frame(fr)
10365 frame_T *fr;
10366{
10367 frame_T *frc;
10368
10369 if (fr->fr_layout == FR_LEAF)
10370 return ses_do_win(fr->fr_win);
10371 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10372 if (ses_do_frame(frc))
10373 return TRUE;
10374 return FALSE;
10375}
10376
10377/*
10378 * Return non-zero if window "wp" is to be stored in the Session.
10379 */
10380 static int
10381ses_do_win(wp)
10382 win_T *wp;
10383{
10384 if (wp->w_buffer->b_fname == NULL
10385#ifdef FEAT_QUICKFIX
10386 /* When 'buftype' is "nofile" can't restore the window contents. */
10387 || bt_nofile(wp->w_buffer)
10388#endif
10389 )
10390 return (ssop_flags & SSOP_BLANK);
10391 if (wp->w_buffer->b_help)
10392 return (ssop_flags & SSOP_HELP);
10393 return TRUE;
10394}
10395
10396/*
10397 * Write commands to "fd" to restore the view of a window.
10398 * Caller must make sure 'scrolloff' is zero.
10399 */
10400 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010401put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 FILE *fd;
10403 win_T *wp;
10404 int add_edit; /* add ":edit" command to view */
10405 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010406 int current_arg_idx; /* current argument index of the window, use
10407 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408{
10409 win_T *save_curwin;
10410 int f;
10411 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010412 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413
10414 /* Always restore cursor position for ":mksession". For ":mkview" only
10415 * when 'viewoptions' contains "cursor". */
10416 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10417
10418 /*
10419 * Local argument list.
10420 */
10421 if (wp->w_alist == &global_alist)
10422 {
10423 if (put_line(fd, "argglobal") == FAIL)
10424 return FAIL;
10425 }
10426 else
10427 {
10428 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10429 flagp == &vop_flags
10430 || !(*flagp & SSOP_CURDIR)
10431 || wp->w_localdir != NULL, flagp) == FAIL)
10432 return FAIL;
10433 }
10434
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010435 /* Only when part of a session: restore the argument index. Some
10436 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010437 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010438 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010440 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 || put_eol(fd) == FAIL)
10442 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010443 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 }
10445
10446 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010447 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 {
10449 /*
10450 * Load the file.
10451 */
10452 if (wp->w_buffer->b_ffname != NULL
10453#ifdef FEAT_QUICKFIX
10454 && !bt_nofile(wp->w_buffer)
10455#endif
10456 )
10457 {
10458 /*
10459 * Editing a file in this buffer: use ":edit file".
10460 * This may have side effects! (e.g., compressed or network file).
10461 */
10462 if (fputs("edit ", fd) < 0
10463 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10464 return FAIL;
10465 }
10466 else
10467 {
10468 /* No file in this buffer, just make it empty. */
10469 if (put_line(fd, "enew") == FAIL)
10470 return FAIL;
10471#ifdef FEAT_QUICKFIX
10472 if (wp->w_buffer->b_ffname != NULL)
10473 {
10474 /* The buffer does have a name, but it's not a file name. */
10475 if (fputs("file ", fd) < 0
10476 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10477 return FAIL;
10478 }
10479#endif
10480 do_cursor = FALSE;
10481 }
10482 }
10483
10484 /*
10485 * Local mappings and abbreviations.
10486 */
10487 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10488 && makemap(fd, wp->w_buffer) == FAIL)
10489 return FAIL;
10490
10491 /*
10492 * Local options. Need to go to the window temporarily.
10493 * Store only local values when using ":mkview" and when ":mksession" is
10494 * used and 'sessionoptions' doesn't include "options".
10495 * Some folding options are always stored when "folds" is included,
10496 * otherwise the folds would not be restored correctly.
10497 */
10498 save_curwin = curwin;
10499 curwin = wp;
10500 curbuf = curwin->w_buffer;
10501 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10502 f = makeset(fd, OPT_LOCAL,
10503 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10504#ifdef FEAT_FOLDING
10505 else if (*flagp & SSOP_FOLDS)
10506 f = makefoldset(fd);
10507#endif
10508 else
10509 f = OK;
10510 curwin = save_curwin;
10511 curbuf = curwin->w_buffer;
10512 if (f == FAIL)
10513 return FAIL;
10514
10515#ifdef FEAT_FOLDING
10516 /*
10517 * Save Folds when 'buftype' is empty and for help files.
10518 */
10519 if ((*flagp & SSOP_FOLDS)
10520 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010521# ifdef FEAT_QUICKFIX
10522 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10523# endif
10524 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 {
10526 if (put_folds(fd, wp) == FAIL)
10527 return FAIL;
10528 }
10529#endif
10530
10531 /*
10532 * Set the cursor after creating folds, since that moves the cursor.
10533 */
10534 if (do_cursor)
10535 {
10536
10537 /* Restore the cursor line in the file and relatively in the
10538 * window. Don't use "G", it changes the jumplist. */
10539 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10540 (long)wp->w_cursor.lnum,
10541 (long)(wp->w_cursor.lnum - wp->w_topline),
10542 (long)wp->w_height / 2, (long)wp->w_height) < 0
10543 || put_eol(fd) == FAIL
10544 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10545 || put_line(fd, "exe s:l") == FAIL
10546 || put_line(fd, "normal! zt") == FAIL
10547 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10548 || put_eol(fd) == FAIL)
10549 return FAIL;
10550 /* Restore the cursor column and left offset when not wrapping. */
10551 if (wp->w_cursor.col == 0)
10552 {
10553 if (put_line(fd, "normal! 0") == FAIL)
10554 return FAIL;
10555 }
10556 else
10557 {
10558 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10559 {
10560 if (fprintf(fd,
10561 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10562 (long)wp->w_cursor.col,
10563 (long)(wp->w_cursor.col - wp->w_leftcol),
10564 (long)wp->w_width / 2, (long)wp->w_width) < 0
10565 || put_eol(fd) == FAIL
10566 || put_line(fd, "if s:c > 0") == FAIL
10567 || fprintf(fd,
10568 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10569 (long)wp->w_cursor.col) < 0
10570 || put_eol(fd) == FAIL
10571 || put_line(fd, "else") == FAIL
10572 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10573 || put_eol(fd) == FAIL
10574 || put_line(fd, "endif") == FAIL)
10575 return FAIL;
10576 }
10577 else
10578 {
10579 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10580 || put_eol(fd) == FAIL)
10581 return FAIL;
10582 }
10583 }
10584 }
10585
10586 /*
10587 * Local directory.
10588 */
10589 if (wp->w_localdir != NULL)
10590 {
10591 if (fputs("lcd ", fd) < 0
10592 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10593 || put_eol(fd) == FAIL)
10594 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010595 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 }
10597
10598 return OK;
10599}
10600
10601/*
10602 * Write an argument list to the session file.
10603 * Returns FAIL if writing fails.
10604 */
10605 static int
10606ses_arglist(fd, cmd, gap, fullname, flagp)
10607 FILE *fd;
10608 char *cmd;
10609 garray_T *gap;
10610 int fullname; /* TRUE: use full path name */
10611 unsigned *flagp;
10612{
10613 int i;
10614 char_u buf[MAXPATHL];
10615 char_u *s;
10616
10617 if (gap->ga_len == 0)
10618 return put_line(fd, "silent! argdel *");
10619 if (fputs(cmd, fd) < 0)
10620 return FAIL;
10621 for (i = 0; i < gap->ga_len; ++i)
10622 {
10623 /* NULL file names are skipped (only happens when out of memory). */
10624 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10625 if (s != NULL)
10626 {
10627 if (fullname)
10628 {
10629 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10630 s = buf;
10631 }
10632 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10633 return FAIL;
10634 }
10635 }
10636 return put_eol(fd);
10637}
10638
10639/*
10640 * Write a buffer name to the session file.
10641 * Also ends the line.
10642 * Returns FAIL if writing fails.
10643 */
10644 static int
10645ses_fname(fd, buf, flagp)
10646 FILE *fd;
10647 buf_T *buf;
10648 unsigned *flagp;
10649{
10650 char_u *name;
10651
10652 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010653 * the session file will be sourced.
10654 * Don't do this for ":mkview", we don't know the current directory.
10655 * Don't do this after ":lcd", we don't keep track of what the current
10656 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 if (buf->b_sfname != NULL
10658 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010659 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010660#ifdef FEAT_AUTOCHDIR
10661 && !p_acd
10662#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010663 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 name = buf->b_sfname;
10665 else
10666 name = buf->b_ffname;
10667 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10668 return FAIL;
10669 return OK;
10670}
10671
10672/*
10673 * Write a file name to the session file.
10674 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10675 * characters.
10676 * Returns FAIL if writing fails.
10677 */
10678 static int
10679ses_put_fname(fd, name, flagp)
10680 FILE *fd;
10681 char_u *name;
10682 unsigned *flagp;
10683{
10684 char_u *sname;
10685 int retval = OK;
10686 int c;
10687
10688 sname = home_replace_save(NULL, name);
10689 if (sname != NULL)
10690 name = sname;
10691 while (*name != NUL)
10692 {
10693#ifdef FEAT_MBYTE
10694 {
10695 int l;
10696
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010697 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698 {
10699 /* copy a multibyte char */
10700 while (--l >= 0)
10701 {
10702 if (putc(*name, fd) != *name)
10703 retval = FAIL;
10704 ++name;
10705 }
10706 continue;
10707 }
10708 }
10709#endif
10710 c = *name++;
10711 if (c == '\\' && (*flagp & SSOP_SLASH))
10712 /* change a backslash to a forward slash */
10713 c = '/';
10714 else if ((vim_strchr(escape_chars, c) != NULL
10715#ifdef BACKSLASH_IN_FILENAME
10716 && c != '\\'
10717#endif
10718 ) || c == '#' || c == '%')
10719 {
10720 /* escape a special character with a backslash */
10721 if (putc('\\', fd) != '\\')
10722 retval = FAIL;
10723 }
10724 if (putc(c, fd) != c)
10725 retval = FAIL;
10726 }
10727 vim_free(sname);
10728 return retval;
10729}
10730
10731/*
10732 * ":loadview [nr]"
10733 */
10734 static void
10735ex_loadview(eap)
10736 exarg_T *eap;
10737{
10738 char_u *fname;
10739
10740 fname = get_view_file(*eap->arg);
10741 if (fname != NULL)
10742 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010743 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 vim_free(fname);
10745 }
10746}
10747
10748/*
10749 * Get the name of the view file for the current buffer.
10750 */
10751 static char_u *
10752get_view_file(c)
10753 int c;
10754{
10755 int len = 0;
10756 char_u *p, *s;
10757 char_u *retval;
10758 char_u *sname;
10759
10760 if (curbuf->b_ffname == NULL)
10761 {
10762 EMSG(_(e_noname));
10763 return NULL;
10764 }
10765 sname = home_replace_save(NULL, curbuf->b_ffname);
10766 if (sname == NULL)
10767 return NULL;
10768
10769 /*
10770 * We want a file name without separators, because we're not going to make
10771 * a directory.
10772 * "normal" path separator -> "=+"
10773 * "=" -> "=="
10774 * ":" path separator -> "=-"
10775 */
10776 for (p = sname; *p; ++p)
10777 if (*p == '=' || vim_ispathsep(*p))
10778 ++len;
10779 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10780 if (retval != NULL)
10781 {
10782 STRCPY(retval, p_vdir);
10783 add_pathsep(retval);
10784 s = retval + STRLEN(retval);
10785 for (p = sname; *p; ++p)
10786 {
10787 if (*p == '=')
10788 {
10789 *s++ = '=';
10790 *s++ = '=';
10791 }
10792 else if (vim_ispathsep(*p))
10793 {
10794 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010795#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796 || defined(VMS)
10797 if (*p == ':')
10798 *s++ = '-';
10799 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010801 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 }
10803 else
10804 *s++ = *p;
10805 }
10806 *s++ = '=';
10807 *s++ = c;
10808 STRCPY(s, ".vim");
10809 }
10810
10811 vim_free(sname);
10812 return retval;
10813}
10814
10815#endif /* FEAT_SESSION */
10816
10817/*
10818 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10819 * Return FAIL for a write error.
10820 */
10821 int
10822put_eol(fd)
10823 FILE *fd;
10824{
10825 if (
10826#ifdef USE_CRNL
10827 (
10828# ifdef MKSESSION_NL
10829 !mksession_nl &&
10830# endif
10831 (putc('\r', fd) < 0)) ||
10832#endif
10833 (putc('\n', fd) < 0))
10834 return FAIL;
10835 return OK;
10836}
10837
10838/*
10839 * Write a line to "fd".
10840 * Return FAIL for a write error.
10841 */
10842 int
10843put_line(fd, s)
10844 FILE *fd;
10845 char *s;
10846{
10847 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10848 return FAIL;
10849 return OK;
10850}
10851
10852#ifdef FEAT_VIMINFO
10853/*
10854 * ":rviminfo" and ":wviminfo".
10855 */
10856 static void
10857ex_viminfo(eap)
10858 exarg_T *eap;
10859{
10860 char_u *save_viminfo;
10861
10862 save_viminfo = p_viminfo;
10863 if (*p_viminfo == NUL)
10864 p_viminfo = (char_u *)"'100";
10865 if (eap->cmdidx == CMD_rviminfo)
10866 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010867 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10868 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 EMSG(_("E195: Cannot open viminfo file for reading"));
10870 }
10871 else
10872 write_viminfo(eap->arg, eap->forceit);
10873 p_viminfo = save_viminfo;
10874}
10875#endif
10876
10877#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010878/*
10879 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010880 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010881 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 void
10883dialog_msg(buff, format, fname)
10884 char_u *buff;
10885 char *format;
10886 char_u *fname;
10887{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 if (fname == NULL)
10889 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010890 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891}
10892#endif
10893
10894/*
10895 * ":behave {mswin,xterm}"
10896 */
10897 static void
10898ex_behave(eap)
10899 exarg_T *eap;
10900{
10901 if (STRCMP(eap->arg, "mswin") == 0)
10902 {
10903 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10904 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10905 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10906 set_option_value((char_u *)"keymodel", 0L,
10907 (char_u *)"startsel,stopsel", 0);
10908 }
10909 else if (STRCMP(eap->arg, "xterm") == 0)
10910 {
10911 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10912 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10913 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10914 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10915 }
10916 else
10917 EMSG2(_(e_invarg2), eap->arg);
10918}
10919
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010920#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10921/*
10922 * Function given to ExpandGeneric() to obtain the possible arguments of the
10923 * ":behave {mswin,xterm}" command.
10924 */
10925 char_u *
10926get_behave_arg(xp, idx)
10927 expand_T *xp UNUSED;
10928 int idx;
10929{
10930 if (idx == 0)
10931 return (char_u *)"mswin";
10932 if (idx == 1)
10933 return (char_u *)"xterm";
10934 return NULL;
10935}
10936#endif
10937
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938#ifdef FEAT_AUTOCMD
10939static int filetype_detect = FALSE;
10940static int filetype_plugin = FALSE;
10941static int filetype_indent = FALSE;
10942
10943/*
10944 * ":filetype [plugin] [indent] {on,off,detect}"
10945 * on: Load the filetype.vim file to install autocommands for file types.
10946 * off: Load the ftoff.vim file to remove all autocommands for file types.
10947 * plugin on: load filetype.vim and ftplugin.vim
10948 * plugin off: load ftplugof.vim
10949 * indent on: load filetype.vim and indent.vim
10950 * indent off: load indoff.vim
10951 */
10952 static void
10953ex_filetype(eap)
10954 exarg_T *eap;
10955{
10956 char_u *arg = eap->arg;
10957 int plugin = FALSE;
10958 int indent = FALSE;
10959
10960 if (*eap->arg == NUL)
10961 {
10962 /* Print current status. */
10963 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10964 filetype_detect ? "ON" : "OFF",
10965 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10966 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10967 return;
10968 }
10969
10970 /* Accept "plugin" and "indent" in any order. */
10971 for (;;)
10972 {
10973 if (STRNCMP(arg, "plugin", 6) == 0)
10974 {
10975 plugin = TRUE;
10976 arg = skipwhite(arg + 6);
10977 continue;
10978 }
10979 if (STRNCMP(arg, "indent", 6) == 0)
10980 {
10981 indent = TRUE;
10982 arg = skipwhite(arg + 6);
10983 continue;
10984 }
10985 break;
10986 }
10987 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10988 {
10989 if (*arg == 'o' || !filetype_detect)
10990 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010991 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992 filetype_detect = TRUE;
10993 if (plugin)
10994 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010995 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 filetype_plugin = TRUE;
10997 }
10998 if (indent)
10999 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011000 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 filetype_indent = TRUE;
11002 }
11003 }
11004 if (*arg == 'd')
11005 {
11006 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011007 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 }
11009 }
11010 else if (STRCMP(arg, "off") == 0)
11011 {
11012 if (plugin || indent)
11013 {
11014 if (plugin)
11015 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011016 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 filetype_plugin = FALSE;
11018 }
11019 if (indent)
11020 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011021 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 filetype_indent = FALSE;
11023 }
11024 }
11025 else
11026 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011027 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 filetype_detect = FALSE;
11029 }
11030 }
11031 else
11032 EMSG2(_(e_invarg2), arg);
11033}
11034
11035/*
11036 * ":setfiletype {name}"
11037 */
11038 static void
11039ex_setfiletype(eap)
11040 exarg_T *eap;
11041{
11042 if (!did_filetype)
11043 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11044}
11045#endif
11046
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 static void
11048ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011049 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050{
11051#ifdef FEAT_DIGRAPHS
11052 if (*eap->arg != NUL)
11053 putdigraph(eap->arg);
11054 else
11055 listdigraphs();
11056#else
11057 EMSG(_("E196: No digraphs in this version"));
11058#endif
11059}
11060
11061 static void
11062ex_set(eap)
11063 exarg_T *eap;
11064{
11065 int flags = 0;
11066
11067 if (eap->cmdidx == CMD_setlocal)
11068 flags = OPT_LOCAL;
11069 else if (eap->cmdidx == CMD_setglobal)
11070 flags = OPT_GLOBAL;
11071#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11072 if (cmdmod.browse && flags == 0)
11073 ex_options(eap);
11074 else
11075#endif
11076 (void)do_set(eap->arg, flags);
11077}
11078
11079#ifdef FEAT_SEARCH_EXTRA
11080/*
11081 * ":nohlsearch"
11082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 static void
11084ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011085 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086{
11087 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011088 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089}
11090
11091/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011092 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093 * Sets nextcmd to the start of the next command, if any. Also called when
11094 * skipping commands to find the next command.
11095 */
11096 static void
11097ex_match(eap)
11098 exarg_T *eap;
11099{
11100 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011101 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 char_u *end;
11103 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011104 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011105
11106 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011107 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011108 else
11109 {
11110 EMSG(e_invcmd);
11111 return;
11112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113
11114 /* First clear any old pattern. */
11115 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011116 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117
11118 if (ends_excmd(*eap->arg))
11119 end = eap->arg;
11120 else if ((STRNICMP(eap->arg, "none", 4) == 0
11121 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11122 end = eap->arg + 4;
11123 else
11124 {
11125 p = skiptowhite(eap->arg);
11126 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011127 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128 p = skipwhite(p);
11129 if (*p == NUL)
11130 {
11131 /* There must be two arguments. */
11132 EMSG2(_(e_invarg2), eap->arg);
11133 return;
11134 }
11135 end = skip_regexp(p + 1, *p, TRUE, NULL);
11136 if (!eap->skip)
11137 {
11138 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11139 {
11140 eap->errmsg = e_trailing;
11141 return;
11142 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011143 if (*end != *p)
11144 {
11145 EMSG2(_(e_invarg2), p);
11146 return;
11147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148
11149 c = *end;
11150 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011151 match_add(curwin, g, p + 1, 10, id);
11152 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011153 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154 }
11155 }
11156 eap->nextcmd = find_nextcmd(end);
11157}
11158#endif
11159
11160#ifdef FEAT_CRYPT
11161/*
11162 * ":X": Get crypt key
11163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164 static void
11165ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011166 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167{
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011168 if (curbuf->b_p_cm == 0 || blowfish_self_test() == OK)
11169 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170}
11171#endif
11172
11173#ifdef FEAT_FOLDING
11174 static void
11175ex_fold(eap)
11176 exarg_T *eap;
11177{
11178 if (foldManualAllowed(TRUE))
11179 foldCreate(eap->line1, eap->line2);
11180}
11181
11182 static void
11183ex_foldopen(eap)
11184 exarg_T *eap;
11185{
11186 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11187 eap->forceit, FALSE);
11188}
11189
11190 static void
11191ex_folddo(eap)
11192 exarg_T *eap;
11193{
11194 linenr_T lnum;
11195
11196 /* First set the marks for all lines closed/open. */
11197 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11198 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11199 ml_setmarked(lnum);
11200
11201 /* Execute the command on the marked lines. */
11202 global_exe(eap->arg);
11203 ml_clearmarked(); /* clear rest of the marks */
11204}
11205#endif