blob: aaed068ec610b32e723ababc6e80d777d950c33f [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 Moolenaara6f4d612011-09-21 19:10:46 +020064static void append_command __ARGS((char_u *cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +000065static char_u *find_command __ARGS((exarg_T *eap, int *full));
66
67static void ex_abbreviate __ARGS((exarg_T *eap));
68static void ex_map __ARGS((exarg_T *eap));
69static void ex_unmap __ARGS((exarg_T *eap));
70static void ex_mapclear __ARGS((exarg_T *eap));
71static void ex_abclear __ARGS((exarg_T *eap));
72#ifndef FEAT_MENU
73# define ex_emenu ex_ni
74# define ex_menu ex_ni
75# define ex_menutranslate ex_ni
76#endif
77#ifdef FEAT_AUTOCMD
78static void ex_autocmd __ARGS((exarg_T *eap));
79static void ex_doautocmd __ARGS((exarg_T *eap));
80#else
81# define ex_autocmd ex_ni
82# define ex_doautocmd ex_ni
83# define ex_doautoall ex_ni
84#endif
85#ifdef FEAT_LISTCMDS
86static void ex_bunload __ARGS((exarg_T *eap));
87static void ex_buffer __ARGS((exarg_T *eap));
88static void ex_bmodified __ARGS((exarg_T *eap));
89static void ex_bnext __ARGS((exarg_T *eap));
90static void ex_bprevious __ARGS((exarg_T *eap));
91static void ex_brewind __ARGS((exarg_T *eap));
92static void ex_blast __ARGS((exarg_T *eap));
93#else
94# define ex_bunload ex_ni
95# define ex_buffer ex_ni
96# define ex_bmodified ex_ni
97# define ex_bnext ex_ni
98# define ex_bprevious ex_ni
99# define ex_brewind ex_ni
100# define ex_blast ex_ni
101# define buflist_list ex_ni
102# define ex_checktime ex_ni
103#endif
104#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
105# define ex_buffer_all ex_ni
106#endif
107static char_u *getargcmd __ARGS((char_u **));
108static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
109static int getargopt __ARGS((exarg_T *eap));
110#ifndef FEAT_QUICKFIX
111# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000112# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113# define ex_cc ex_ni
114# define ex_cnext ex_ni
115# define ex_cfile ex_ni
116# define qf_list ex_ni
117# define qf_age ex_ni
118# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
122# define ex_cclose ex_ni
123# define ex_copen ex_ni
124# define ex_cwindow ex_ni
125#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000126#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
127# define ex_cexpr ex_ni
128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
130static int check_more __ARGS((int, int));
131static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000132static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200133#if !defined(FEAT_PERL) \
134 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
135 || !defined(FEAT_TCL) \
136 || !defined(FEAT_RUBY) \
137 || !defined(FEAT_LUA) \
138 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000139# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static void ex_script_ni __ARGS((exarg_T *eap));
141#endif
142static char_u *invalid_range __ARGS((exarg_T *eap));
143static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000144#ifdef FEAT_QUICKFIX
145static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
148static void ex_highlight __ARGS((exarg_T *eap));
149static void ex_colorscheme __ARGS((exarg_T *eap));
150static void ex_quit __ARGS((exarg_T *eap));
151static void ex_cquit __ARGS((exarg_T *eap));
152static void ex_quit_all __ARGS((exarg_T *eap));
153#ifdef FEAT_WINDOWS
154static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000155static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157static void ex_resize __ARGS((exarg_T *eap));
158static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000159static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000160static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000161static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000162static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000163static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#else
165# define ex_close ex_ni
166# define ex_only ex_ni
167# define ex_all ex_ni
168# define ex_resize ex_ni
169# define ex_splitview ex_ni
170# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000171# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000172# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000173# define ex_tabs ex_ni
174# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000175# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#endif
177#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
178static void ex_pclose __ARGS((exarg_T *eap));
179static void ex_ptag __ARGS((exarg_T *eap));
180static void ex_pedit __ARGS((exarg_T *eap));
181#else
182# define ex_pclose ex_ni
183# define ex_ptag ex_ni
184# define ex_pedit ex_ni
185#endif
186static void ex_hide __ARGS((exarg_T *eap));
187static void ex_stop __ARGS((exarg_T *eap));
188static void ex_exit __ARGS((exarg_T *eap));
189static void ex_print __ARGS((exarg_T *eap));
190#ifdef FEAT_BYTEOFF
191static void ex_goto __ARGS((exarg_T *eap));
192#else
193# define ex_goto ex_ni
194#endif
195static void ex_shell __ARGS((exarg_T *eap));
196static void ex_preserve __ARGS((exarg_T *eap));
197static void ex_recover __ARGS((exarg_T *eap));
198#ifndef FEAT_LISTCMDS
199# define ex_argedit ex_ni
200# define ex_argadd ex_ni
201# define ex_argdelete ex_ni
202# define ex_listdo ex_ni
203#endif
204static void ex_mode __ARGS((exarg_T *eap));
205static void ex_wrongmodifier __ARGS((exarg_T *eap));
206static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000207static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208static void ex_edit __ARGS((exarg_T *eap));
209#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
210# define ex_drop ex_ni
211#endif
212#ifndef FEAT_GUI
213# define ex_gui ex_nogui
214static void ex_nogui __ARGS((exarg_T *eap));
215#endif
216#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
217static void ex_tearoff __ARGS((exarg_T *eap));
218#else
219# define ex_tearoff ex_ni
220#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000221#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222static void ex_popup __ARGS((exarg_T *eap));
223#else
224# define ex_popup ex_ni
225#endif
226#ifndef FEAT_GUI_MSWIN
227# define ex_simalt ex_ni
228#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000229#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230# define gui_mch_find_dialog ex_ni
231# define gui_mch_replace_dialog ex_ni
232#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000233#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# define ex_helpfind ex_ni
235#endif
236#ifndef FEAT_CSCOPE
237# define do_cscope ex_ni
238# define do_scscope ex_ni
239# define do_cstag ex_ni
240#endif
241#ifndef FEAT_SYN_HL
242# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200243# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000244#endif
245#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000246# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000247# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000248# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000249# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000250# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000251#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200252#ifndef FEAT_PERSISTENT_UNDO
253# define ex_rundo ex_ni
254# define ex_wundo ex_ni
255#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200256#ifndef FEAT_LUA
257# define ex_lua ex_script_ni
258# define ex_luado ex_ni
259# define ex_luafile ex_ni
260#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000261#ifndef FEAT_MZSCHEME
262# define ex_mzscheme ex_script_ni
263# define ex_mzfile ex_ni
264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifndef FEAT_PERL
266# define ex_perl ex_script_ni
267# define ex_perldo ex_ni
268#endif
269#ifndef FEAT_PYTHON
270# define ex_python ex_script_ni
271# define ex_pyfile ex_ni
272#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200273#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200274# define ex_py3 ex_script_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200275# define ex_py3file ex_ni
276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifndef FEAT_TCL
278# define ex_tcl ex_script_ni
279# define ex_tcldo ex_ni
280# define ex_tclfile ex_ni
281#endif
282#ifndef FEAT_RUBY
283# define ex_ruby ex_script_ni
284# define ex_rubydo ex_ni
285# define ex_rubyfile ex_ni
286#endif
287#ifndef FEAT_SNIFF
288# define ex_sniff ex_ni
289#endif
290#ifndef FEAT_KEYMAP
291# define ex_loadkeymap ex_ni
292#endif
293static void ex_swapname __ARGS((exarg_T *eap));
294static void ex_syncbind __ARGS((exarg_T *eap));
295static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296static void ex_pwd __ARGS((exarg_T *eap));
297static void ex_equal __ARGS((exarg_T *eap));
298static void ex_sleep __ARGS((exarg_T *eap));
299static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
300static void ex_winsize __ARGS((exarg_T *eap));
301#ifdef FEAT_WINDOWS
302static void ex_wincmd __ARGS((exarg_T *eap));
303#else
304# define ex_wincmd ex_ni
305#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000306#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307static void ex_winpos __ARGS((exarg_T *eap));
308#else
309# define ex_winpos ex_ni
310#endif
311static void ex_operators __ARGS((exarg_T *eap));
312static void ex_put __ARGS((exarg_T *eap));
313static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000314static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315static void ex_submagic __ARGS((exarg_T *eap));
316static void ex_join __ARGS((exarg_T *eap));
317static void ex_at __ARGS((exarg_T *eap));
318static void ex_bang __ARGS((exarg_T *eap));
319static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200320#ifdef FEAT_PERSISTENT_UNDO
321static void ex_wundo __ARGS((exarg_T *eap));
322static void ex_rundo __ARGS((exarg_T *eap));
323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000325static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326static void ex_redir __ARGS((exarg_T *eap));
327static void ex_redraw __ARGS((exarg_T *eap));
328static void ex_redrawstatus __ARGS((exarg_T *eap));
329static void close_redir __ARGS((void));
330static void ex_mkrc __ARGS((exarg_T *eap));
331static void ex_mark __ARGS((exarg_T *eap));
332#ifdef FEAT_USR_CMDS
333static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000334static 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 +0000335#endif
336#ifdef FEAT_EX_EXTRA
337static void ex_normal __ARGS((exarg_T *eap));
338static void ex_startinsert __ARGS((exarg_T *eap));
339static void ex_stopinsert __ARGS((exarg_T *eap));
340#else
341# define ex_normal ex_ni
342# define ex_align ex_ni
343# define ex_retab ex_ni
344# define ex_startinsert ex_ni
345# define ex_stopinsert ex_ni
346# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000347# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#endif
349#ifdef FEAT_FIND_ID
350static void ex_checkpath __ARGS((exarg_T *eap));
351static void ex_findpat __ARGS((exarg_T *eap));
352#else
353# define ex_findpat ex_ni
354# define ex_checkpath ex_ni
355#endif
356#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
357static void ex_psearch __ARGS((exarg_T *eap));
358#else
359# define ex_psearch ex_ni
360#endif
361static void ex_tag __ARGS((exarg_T *eap));
362static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
363#ifndef FEAT_EVAL
364# define ex_scriptnames ex_ni
365# define ex_finish ex_ni
366# define ex_echo ex_ni
367# define ex_echohl ex_ni
368# define ex_execute ex_ni
369# define ex_call ex_ni
370# define ex_if ex_ni
371# define ex_endif ex_ni
372# define ex_else ex_ni
373# define ex_while ex_ni
374# define ex_continue ex_ni
375# define ex_break ex_ni
376# define ex_endwhile ex_ni
377# define ex_throw ex_ni
378# define ex_try ex_ni
379# define ex_catch ex_ni
380# define ex_finally ex_ni
381# define ex_endtry ex_ni
382# define ex_endfunction ex_ni
383# define ex_let ex_ni
384# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000385# define ex_lockvar ex_ni
386# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387# define ex_function ex_ni
388# define ex_delfunction ex_ni
389# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000390# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#endif
392static char_u *arg_all __ARGS((void));
393#ifdef FEAT_SESSION
394static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000395static 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 +0000396static void ex_loadview __ARGS((exarg_T *eap));
397static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000398static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399#else
400# define ex_loadview ex_ni
401#endif
402#ifndef FEAT_EVAL
403# define ex_compiler ex_ni
404#endif
405#ifdef FEAT_VIMINFO
406static void ex_viminfo __ARGS((exarg_T *eap));
407#else
408# define ex_viminfo ex_ni
409#endif
410static void ex_behave __ARGS((exarg_T *eap));
411#ifdef FEAT_AUTOCMD
412static void ex_filetype __ARGS((exarg_T *eap));
413static void ex_setfiletype __ARGS((exarg_T *eap));
414#else
415# define ex_filetype ex_ni
416# define ex_setfiletype ex_ni
417#endif
418#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000419# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420# define ex_diffpatch ex_ni
421# define ex_diffgetput ex_ni
422# define ex_diffsplit ex_ni
423# define ex_diffthis ex_ni
424# define ex_diffupdate ex_ni
425#endif
426static void ex_digraphs __ARGS((exarg_T *eap));
427static void ex_set __ARGS((exarg_T *eap));
428#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
429# define ex_options ex_ni
430#endif
431#ifdef FEAT_SEARCH_EXTRA
432static void ex_nohlsearch __ARGS((exarg_T *eap));
433static void ex_match __ARGS((exarg_T *eap));
434#else
435# define ex_nohlsearch ex_ni
436# define ex_match ex_ni
437#endif
438#ifdef FEAT_CRYPT
439static void ex_X __ARGS((exarg_T *eap));
440#else
441# define ex_X ex_ni
442#endif
443#ifdef FEAT_FOLDING
444static void ex_fold __ARGS((exarg_T *eap));
445static void ex_foldopen __ARGS((exarg_T *eap));
446static void ex_folddo __ARGS((exarg_T *eap));
447#else
448# define ex_fold ex_ni
449# define ex_foldopen ex_ni
450# define ex_folddo ex_ni
451#endif
452#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
453 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
454# define ex_language ex_ni
455#endif
456#ifndef FEAT_SIGNS
457# define ex_sign ex_ni
458#endif
459#ifndef FEAT_SUN_WORKSHOP
460# define ex_wsverb ex_ni
461#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000462#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200463# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000464# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200465# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467
468#ifndef FEAT_EVAL
469# define ex_debug ex_ni
470# define ex_breakadd ex_ni
471# define ex_debuggreedy ex_ni
472# define ex_breakdel ex_ni
473# define ex_breaklist ex_ni
474#endif
475
476#ifndef FEAT_CMDHIST
477# define ex_history ex_ni
478#endif
479#ifndef FEAT_JUMPLIST
480# define ex_jumps ex_ni
481# define ex_changes ex_ni
482#endif
483
Bram Moolenaar05159a02005-02-26 23:04:13 +0000484#ifndef FEAT_PROFILE
485# define ex_profile ex_ni
486#endif
487
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488/*
489 * Declare cmdnames[].
490 */
491#define DO_DECLARE_EXCMD
492#include "ex_cmds.h"
493
494/*
495 * Table used to quickly search for a command, based on its first character.
496 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000497static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
499 CMD_append,
500 CMD_buffer,
501 CMD_change,
502 CMD_delete,
503 CMD_edit,
504 CMD_file,
505 CMD_global,
506 CMD_help,
507 CMD_insert,
508 CMD_join,
509 CMD_k,
510 CMD_list,
511 CMD_move,
512 CMD_next,
513 CMD_open,
514 CMD_print,
515 CMD_quit,
516 CMD_read,
517 CMD_substitute,
518 CMD_t,
519 CMD_undo,
520 CMD_vglobal,
521 CMD_write,
522 CMD_xit,
523 CMD_yank,
524 CMD_z,
525 CMD_bang
526};
527
528static char_u dollar_command[2] = {'$', 0};
529
530
531#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000532/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533typedef struct
534{
535 char_u *line; /* command line */
536 linenr_T lnum; /* sourcing_lnum of the line */
537} wcmd_T;
538
539/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000540 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000542 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000544struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 garray_T *lines_gap; /* growarray with line info */
547 int current_line; /* last read line from growarray */
548 int repeating; /* TRUE when looping a second time */
549 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
550 char_u *(*getline) __ARGS((int, void *, int));
551 void *cookie;
552};
553
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000554static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
555static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000557
558/* Struct to save a few things while debugging. Used in do_cmdline() only. */
559struct dbg_stuff
560{
561 int trylevel;
562 int force_abort;
563 except_T *caught_stack;
564 char_u *vv_exception;
565 char_u *vv_throwpoint;
566 int did_emsg;
567 int got_int;
568 int did_throw;
569 int need_rethrow;
570 int check_cstack;
571 except_T *current_exception;
572};
573
574static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
575static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
576
577 static void
578save_dbg_stuff(dsp)
579 struct dbg_stuff *dsp;
580{
581 dsp->trylevel = trylevel; trylevel = 0;
582 dsp->force_abort = force_abort; force_abort = FALSE;
583 dsp->caught_stack = caught_stack; caught_stack = NULL;
584 dsp->vv_exception = v_exception(NULL);
585 dsp->vv_throwpoint = v_throwpoint(NULL);
586
587 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
588 dsp->did_emsg = did_emsg; did_emsg = FALSE;
589 dsp->got_int = got_int; got_int = FALSE;
590 dsp->did_throw = did_throw; did_throw = FALSE;
591 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
592 dsp->check_cstack = check_cstack; check_cstack = FALSE;
593 dsp->current_exception = current_exception; current_exception = NULL;
594}
595
596 static void
597restore_dbg_stuff(dsp)
598 struct dbg_stuff *dsp;
599{
600 suppress_errthrow = FALSE;
601 trylevel = dsp->trylevel;
602 force_abort = dsp->force_abort;
603 caught_stack = dsp->caught_stack;
604 (void)v_exception(dsp->vv_exception);
605 (void)v_throwpoint(dsp->vv_throwpoint);
606 did_emsg = dsp->did_emsg;
607 got_int = dsp->got_int;
608 did_throw = dsp->did_throw;
609 need_rethrow = dsp->need_rethrow;
610 check_cstack = dsp->check_cstack;
611 current_exception = dsp->current_exception;
612}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613#endif
614
615
616/*
617 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
618 * command is given.
619 */
620 void
621do_exmode(improved)
622 int improved; /* TRUE for "improved Ex" mode */
623{
624 int save_msg_scroll;
625 int prev_msg_row;
626 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000627 int changedtick;
628
629 if (improved)
630 exmode_active = EXMODE_VIM;
631 else
632 exmode_active = EXMODE_NORMAL;
633 State = NORMAL;
634
635 /* When using ":global /pat/ visual" and then "Q" we return to continue
636 * the :global command. */
637 if (global_busy)
638 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
640 save_msg_scroll = msg_scroll;
641 ++RedrawingDisabled; /* don't redisplay the window */
642 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643#ifdef FEAT_GUI
644 /* Ignore scrollbar and mouse events in Ex mode */
645 ++hold_gui_events;
646#endif
647#ifdef FEAT_SNIFF
648 want_sniff_request = 0; /* No K_SNIFF wanted */
649#endif
650
651 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
652 while (exmode_active)
653 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000654#ifdef FEAT_EX_EXTRA
655 /* Check for a ":normal" command and no more characters left. */
656 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
657 {
658 exmode_active = FALSE;
659 break;
660 }
661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 msg_scroll = TRUE;
663 need_wait_return = FALSE;
664 ex_pressedreturn = FALSE;
665 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000666 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 prev_msg_row = msg_row;
668 prev_line = curwin->w_cursor.lnum;
669#ifdef FEAT_SNIFF
670 ProcessSniffRequests();
671#endif
672 if (improved)
673 {
674 cmdline_row = msg_row;
675 do_cmdline(NULL, getexline, NULL, 0);
676 }
677 else
678 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
679 lines_left = Rows - 1;
680
Bram Moolenaardf177f62005-02-22 08:39:57 +0000681 if ((prev_line != curwin->w_cursor.lnum
682 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000684 if (curbuf->b_ml.ml_flags & ML_EMPTY)
685 EMSG(_(e_emptybuf));
686 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000688 if (ex_pressedreturn)
689 {
690 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000691 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000692 msg_row = prev_msg_row;
693 if (prev_msg_row == Rows - 1)
694 msg_row--;
695 }
696 msg_col = 0;
697 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
698 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000701 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
702 {
703 if (curbuf->b_ml.ml_flags & ML_EMPTY)
704 EMSG(_(e_emptybuf));
705 else
706 EMSG(_("E501: At end-of-file"));
707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 }
709
710#ifdef FEAT_GUI
711 --hold_gui_events;
712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 --RedrawingDisabled;
714 --no_wait_return;
715 update_screen(CLEAR);
716 need_wait_return = FALSE;
717 msg_scroll = save_msg_scroll;
718}
719
720/*
721 * Execute a simple command line. Used for translated commands like "*".
722 */
723 int
724do_cmdline_cmd(cmd)
725 char_u *cmd;
726{
727 return do_cmdline(cmd, NULL, NULL,
728 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
729}
730
731/*
732 * do_cmdline(): execute one Ex command line
733 *
734 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100735 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 * 2. Split up in parts separated with '|'.
737 *
738 * This function can be called recursively!
739 *
740 * flags:
741 * DOCMD_VERBOSE - The command will be included in the error message.
742 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100743 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 * DOCMD_KEYTYPED - Don't reset KeyTyped.
745 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
746 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
747 *
748 * return FAIL if cmdline could not be executed, OK otherwise
749 */
750 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100751do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100753 char_u *(*fgetline) __ARGS((int, void *, int));
754 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 int flags;
756{
757 char_u *next_cmdline; /* next cmd to execute */
758 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100759 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 static int recursive = 0; /* recursive depth */
761 int msg_didout_before_start = 0;
762 int count = 0; /* line number count */
763 int did_inc = FALSE; /* incremented RedrawingDisabled */
764 int retval = OK;
765#ifdef FEAT_EVAL
766 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000767 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 int current_line = 0; /* active line in lines_ga */
769 char_u *fname = NULL; /* function or script name */
770 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
771 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000772 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 int initial_trylevel;
774 struct msglist **saved_msg_list = NULL;
775 struct msglist *private_msg_list;
776
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100777 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 char_u *(*cmd_getline) __ARGS((int, void *, int));
779 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000780 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000782 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100784# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785# define cmd_cookie cookie
786#endif
787 static int call_depth = 0; /* recursiveness */
788
789#ifdef FEAT_EVAL
790 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
791 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000792 * This ensures that the do_errthrow() call in do_one_cmd() does not
793 * combine the messages stored by an earlier invocation of do_one_cmd()
794 * with the command name of the later one. This would happen when
795 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 saved_msg_list = msg_list;
797 msg_list = &private_msg_list;
798 private_msg_list = NULL;
799#endif
800
801 /* It's possible to create an endless loop with ":execute", catch that
802 * here. The value of 200 allows nested function calls, ":source", etc. */
803 if (call_depth == 200)
804 {
805 EMSG(_("E169: Command too recursive"));
806#ifdef FEAT_EVAL
807 /* When converting to an exception, we do not include the command name
808 * since this is not an error of the specific command. */
809 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
810 msg_list = saved_msg_list;
811#endif
812 return FAIL;
813 }
814 ++call_depth;
815
816#ifdef FEAT_EVAL
817 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000818 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 cstack.cs_trylevel = 0;
820 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000821 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
823
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100824 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
826 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000828 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 ++ex_nesting_level;
830
831 /* Get the function or script name and the address where the next breakpoint
832 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {
835 fname = func_name(real_cookie);
836 breakpoint = func_breakpoint(real_cookie);
837 dbg_tick = func_dbg_tick(real_cookie);
838 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100839 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 {
841 fname = sourcing_name;
842 breakpoint = source_breakpoint(real_cookie);
843 dbg_tick = source_dbg_tick(real_cookie);
844 }
845
846 /*
847 * Initialize "force_abort" and "suppress_errthrow" at the top level.
848 */
849 if (!recursive)
850 {
851 force_abort = FALSE;
852 suppress_errthrow = FALSE;
853 }
854
855 /*
856 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000857 * exception handling (used when debugging). Otherwise clear it to avoid
858 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000860 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000861 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000862 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200863 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864
865 initial_trylevel = trylevel;
866
867 /*
868 * "did_throw" will be set to TRUE when an exception is being thrown.
869 */
870 did_throw = FALSE;
871#endif
872 /*
873 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000874 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 * If force_abort is set, we cancel everything.
876 */
877 did_emsg = FALSE;
878
879 /*
880 * KeyTyped is only set when calling vgetc(). Reset it here when not
881 * calling vgetc() (sourced command lines).
882 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 if (!(flags & DOCMD_KEYTYPED)
884 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 KeyTyped = FALSE;
886
887 /*
888 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000889 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 * - for multiple commands on one line, separated with '|'
891 * - when repeating until there are no more lines (for ":source")
892 */
893 next_cmdline = cmdline;
894 do
895 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100897 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000898#endif
899
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000900 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (next_cmdline == NULL
902#ifdef FEAT_EVAL
903 && !force_abort
904 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000905 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906#endif
907 )
908 did_emsg = FALSE;
909
910 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000911 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100912 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 * 3. If a line is given: Make a copy, so we can mess with it.
914 */
915
916#ifdef FEAT_EVAL
917 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000918 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
920 /* Each '|' separated command is stored separately in lines_ga, to
921 * be able to jump to it. Don't use next_cmdline now. */
922 vim_free(cmdline_copy);
923 cmdline_copy = NULL;
924
925 /* Check if a function has returned or, unless it has an unclosed
926 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000929# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000930 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000931 func_line_end(real_cookie);
932# endif
933 if (func_has_ended(real_cookie))
934 {
935 retval = FAIL;
936 break;
937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000939#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000940 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100941 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000942 script_line_end();
943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
945 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100946 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 {
948 retval = FAIL;
949 break;
950 }
951
952 /* If breakpoints have been added/deleted need to check for it. */
953 if (breakpoint != NULL && dbg_tick != NULL
954 && *dbg_tick != debug_tick)
955 {
956 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100957 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 fname, sourcing_lnum);
959 *dbg_tick = debug_tick;
960 }
961
962 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
963 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
964
965 /* Did we encounter a breakpoint? */
966 if (breakpoint != NULL && *breakpoint != 0
967 && *breakpoint <= sourcing_lnum)
968 {
969 dbg_breakpoint(fname, sourcing_lnum);
970 /* Find next breakpoint. */
971 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100972 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 fname, sourcing_lnum);
974 *dbg_tick = debug_tick;
975 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000976# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000977 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000978 {
979 if (getline_is_func)
980 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100981 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000982 script_line_start();
983 }
984# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 }
986
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100990 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 * below, so that it stores lines in or reads them from
992 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000993 * while/for loop. */
994 cmd_getline = get_loop_line;
995 cmd_cookie = (void *)&cmd_loop_cookie;
996 cmd_loop_cookie.lines_gap = &lines_ga;
997 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100998 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000999 cmd_loop_cookie.cookie = cookie;
1000 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 }
1002 else
1003 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001004 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 cmd_cookie = cookie;
1006 }
1007#endif
1008
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001009 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 if (next_cmdline == NULL)
1011 {
1012 /*
1013 * Need to set msg_didout for the first line after an ":if",
1014 * otherwise the ":if" will be overwritten.
1015 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001016 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001018 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019#ifdef FEAT_EVAL
1020 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1021#else
1022 0
1023#endif
1024 )) == NULL)
1025 {
1026 /* Don't call wait_return for aborted command line. The NULL
1027 * returned for the end of a sourced file or executed function
1028 * doesn't do this. */
1029 if (KeyTyped && !(flags & DOCMD_REPEAT))
1030 need_wait_return = FALSE;
1031 retval = FAIL;
1032 break;
1033 }
1034 used_getline = TRUE;
1035
1036 /*
1037 * Keep the first typed line. Clear it when more lines are typed.
1038 */
1039 if (flags & DOCMD_KEEPLINE)
1040 {
1041 vim_free(repeat_cmdline);
1042 if (count == 0)
1043 repeat_cmdline = vim_strsave(next_cmdline);
1044 else
1045 repeat_cmdline = NULL;
1046 }
1047 }
1048
1049 /* 3. Make a copy of the command so we can mess with it. */
1050 else if (cmdline_copy == NULL)
1051 {
1052 next_cmdline = vim_strsave(next_cmdline);
1053 if (next_cmdline == NULL)
1054 {
1055 EMSG(_(e_outofmem));
1056 retval = FAIL;
1057 break;
1058 }
1059 }
1060 cmdline_copy = next_cmdline;
1061
1062#ifdef FEAT_EVAL
1063 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001064 * Save the current line when inside a ":while" or ":for", and when
1065 * the command looks like a ":while" or ":for", because we may need it
1066 * later. When there is a '|' and another command, it is stored
1067 * separately, because we need to be able to jump back to it from an
1068 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001070 if (current_line == lines_ga.ga_len
1071 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001073 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 {
1075 retval = FAIL;
1076 break;
1077 }
1078 }
1079 did_endif = FALSE;
1080#endif
1081
1082 if (count++ == 0)
1083 {
1084 /*
1085 * All output from the commands is put below each other, without
1086 * waiting for a return. Don't do this when executing commands
1087 * from a script or when being called recursive (e.g. for ":e
1088 * +command file").
1089 */
1090 if (!(flags & DOCMD_NOWAIT) && !recursive)
1091 {
1092 msg_didout_before_start = msg_didout;
1093 msg_didany = FALSE; /* no output yet */
1094 msg_start();
1095 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001096 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 ++RedrawingDisabled;
1098 did_inc = TRUE;
1099 }
1100 }
1101
1102 if (p_verbose >= 15 && sourcing_name != NULL)
1103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001105 verbose_enter_scroll();
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 smsg((char_u *)_("line %ld: %s"),
1108 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001109 if (msg_silent == 0)
1110 msg_puts((char_u *)"\n"); /* don't overwrite this */
1111
1112 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 --no_wait_return;
1114 }
1115
1116 /*
1117 * 2. Execute one '|' separated command.
1118 * do_one_cmd() will return NULL if there is no trailing '|'.
1119 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1120 */
1121 ++recursive;
1122 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1123#ifdef FEAT_EVAL
1124 &cstack,
1125#endif
1126 cmd_getline, cmd_cookie);
1127 --recursive;
1128
1129#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 if (cmd_cookie == (void *)&cmd_loop_cookie)
1131 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134#endif
1135
1136 if (next_cmdline == NULL)
1137 {
1138 vim_free(cmdline_copy);
1139 cmdline_copy = NULL;
1140#ifdef FEAT_CMDHIST
1141 /*
1142 * If the command was typed, remember it for the ':' register.
1143 * Do this AFTER executing the command to make :@: work.
1144 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001145 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 && new_last_cmdline != NULL)
1147 {
1148 vim_free(last_cmdline);
1149 last_cmdline = new_last_cmdline;
1150 new_last_cmdline = NULL;
1151 }
1152#endif
1153 }
1154 else
1155 {
1156 /* need to copy the command after the '|' to cmdline_copy, for the
1157 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001158 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 next_cmdline = cmdline_copy;
1160 }
1161
1162
1163#ifdef FEAT_EVAL
1164 /* reset did_emsg for a function that is not aborted by an error */
1165 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001166 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 && !func_has_abort(real_cookie))
1168 did_emsg = FALSE;
1169
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
1172 ++current_line;
1173
1174 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 * An ":endwhile", ":endfor" and ":continue" is handled here.
1176 * If we were executing commands, jump back to the ":while" or
1177 * ":for".
1178 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 /* Jump back to the matching ":while" or ":for". Be careful
1185 * not to use a cs_line[] from an entry that isn't a ":while"
1186 * or ":for": It would make "current_line" invalid and can
1187 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (!did_emsg && !got_int && !did_throw
1189 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001190 && (cstack.cs_flags[cstack.cs_idx]
1191 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 && cstack.cs_line[cstack.cs_idx] >= 0
1193 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1194 {
1195 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001196 /* remember we jumped there */
1197 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 line_breakcheck(); /* check if CTRL-C typed */
1199
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 /* Check for the next breakpoint at or after the ":while"
1201 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (breakpoint != NULL)
1203 {
1204 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001205 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 fname,
1207 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1208 *dbg_tick = debug_tick;
1209 }
1210 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001213 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001215 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1216 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 }
1218 }
1219
1220 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1227 }
1228 }
1229
1230 /*
1231 * When not inside any ":while" loop, clear remembered lines.
1232 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001233 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 {
1235 if (lines_ga.ga_len > 0)
1236 {
1237 sourcing_lnum =
1238 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1239 free_cmdlines(&lines_ga);
1240 }
1241 current_line = 0;
1242 }
1243
1244 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001245 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1246 * being restored at the ":endtry". Reset them here and set the
1247 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1248 * This includes the case where a missing ":endif", ":endwhile" or
1249 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001251 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001253 cstack.cs_lflags &= ~CSL_HAD_FINA;
1254 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1255 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 did_throw ? (void *)current_exception : NULL);
1257 did_emsg = got_int = did_throw = FALSE;
1258 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1259 }
1260
1261 /* Update global "trylevel" for recursive calls to do_cmdline() from
1262 * within this loop. */
1263 trylevel = initial_trylevel + cstack.cs_trylevel;
1264
1265 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001266 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 * files) is aborted because of an error, an interrupt, or an uncaught
1268 * exception, cancel everything. If it is left normally, reset
1269 * force_abort to get the non-EH compatible abortion behavior for
1270 * the rest of the script.
1271 */
1272 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1273 force_abort = FALSE;
1274
1275 /* Convert an interrupt to an exception if appropriate. */
1276 (void)do_intthrow(&cstack);
1277#endif /* FEAT_EVAL */
1278
1279 }
1280 /*
1281 * Continue executing command lines when:
1282 * - no CTRL-C typed, no aborting error, no exception thrown or try
1283 * conditionals need to be checked for executing finally clauses or
1284 * catching an interrupt exception
1285 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001286 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 * looping for ":source" command or function call.
1288 */
1289 while (!((got_int
1290#ifdef FEAT_EVAL
1291 || (did_emsg && force_abort) || did_throw
1292#endif
1293 )
1294#ifdef FEAT_EVAL
1295 && cstack.cs_trylevel == 0
1296#endif
1297 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001298 && !(did_emsg
1299#ifdef FEAT_EVAL
1300 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001301 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001302 * the :endtry to be missed. */
1303 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1304#endif
1305 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001306 && (getline_equal(fgetline, cookie, getexmodeline)
1307 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 && (next_cmdline != NULL
1309#ifdef FEAT_EVAL
1310 || cstack.cs_idx >= 0
1311#endif
1312 || (flags & DOCMD_REPEAT)));
1313
1314 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001315 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316#ifdef FEAT_EVAL
1317 free_cmdlines(&lines_ga);
1318 ga_clear(&lines_ga);
1319
1320 if (cstack.cs_idx >= 0)
1321 {
1322 /*
1323 * If a sourced file or executed function ran to its end, report the
1324 * unclosed conditional.
1325 */
1326 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001327 && ((getline_equal(fgetline, cookie, getsourceline)
1328 && !source_finished(fgetline, cookie))
1329 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 && !func_has_ended(real_cookie))))
1331 {
1332 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1333 EMSG(_(e_endtry));
1334 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1335 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001336 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1337 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 else
1339 EMSG(_(e_endif));
1340 }
1341
1342 /*
1343 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1344 * ":endtry" in a sourced file or executed function. If the try
1345 * conditional is in its finally clause, ignore anything pending.
1346 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001347 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 */
1349 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001350 {
1351 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1352
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001353 if (idx >= 0)
1354 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001355 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1356 &cstack.cs_looplevel);
1357 }
1358 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 trylevel = initial_trylevel;
1360 }
1361
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001362 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1363 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001365 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 ? (char_u *)"endfunction" : (char_u *)NULL);
1367
1368 if (trylevel == 0)
1369 {
1370 /*
1371 * When an exception is being thrown out of the outermost try
1372 * conditional, discard the uncaught exception, disable the conversion
1373 * of interrupts or errors to exceptions, and ensure that no more
1374 * commands are executed.
1375 */
1376 if (did_throw)
1377 {
1378 void *p = NULL;
1379 char_u *saved_sourcing_name;
1380 int saved_sourcing_lnum;
1381 struct msglist *messages = NULL, *next;
1382
1383 /*
1384 * If the uncaught exception is a user exception, report it as an
1385 * error. If it is an error exception, display the saved error
1386 * message now. For an interrupt exception, do nothing; the
1387 * interrupt message is given elsewhere.
1388 */
1389 switch (current_exception->type)
1390 {
1391 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001392 vim_snprintf((char *)IObuff, IOSIZE,
1393 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 current_exception->value);
1395 p = vim_strsave(IObuff);
1396 break;
1397 case ET_ERROR:
1398 messages = current_exception->messages;
1399 current_exception->messages = NULL;
1400 break;
1401 case ET_INTERRUPT:
1402 break;
1403 default:
1404 p = vim_strsave((char_u *)_(e_internal));
1405 }
1406
1407 saved_sourcing_name = sourcing_name;
1408 saved_sourcing_lnum = sourcing_lnum;
1409 sourcing_name = current_exception->throw_name;
1410 sourcing_lnum = current_exception->throw_lnum;
1411 current_exception->throw_name = NULL;
1412
1413 discard_current_exception(); /* uses IObuff if 'verbose' */
1414 suppress_errthrow = TRUE;
1415 force_abort = TRUE;
1416
1417 if (messages != NULL)
1418 {
1419 do
1420 {
1421 next = messages->next;
1422 emsg(messages->msg);
1423 vim_free(messages->msg);
1424 vim_free(messages);
1425 messages = next;
1426 }
1427 while (messages != NULL);
1428 }
1429 else if (p != NULL)
1430 {
1431 emsg(p);
1432 vim_free(p);
1433 }
1434 vim_free(sourcing_name);
1435 sourcing_name = saved_sourcing_name;
1436 sourcing_lnum = saved_sourcing_lnum;
1437 }
1438
1439 /*
1440 * On an interrupt or an aborting error not converted to an exception,
1441 * disable the conversion of errors to exceptions. (Interrupts are not
1442 * converted any more, here.) This enables also the interrupt message
1443 * when force_abort is set and did_emsg unset in case of an interrupt
1444 * from a finally clause after an error.
1445 */
1446 else if (got_int || (did_emsg && force_abort))
1447 suppress_errthrow = TRUE;
1448 }
1449
1450 /*
1451 * The current cstack will be freed when do_cmdline() returns. An uncaught
1452 * exception will have to be rethrown in the previous cstack. If a function
1453 * has just returned or a script file was just finished and the previous
1454 * cstack belongs to the same function or, respectively, script file, it
1455 * will have to be checked for finally clauses to be executed due to the
1456 * ":return" or ":finish". This is done in do_one_cmd().
1457 */
1458 if (did_throw)
1459 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001460 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001462 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 && ex_nesting_level > func_level(real_cookie) + 1))
1464 {
1465 if (!did_throw)
1466 check_cstack = TRUE;
1467 }
1468 else
1469 {
1470 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001471 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 --ex_nesting_level;
1473 /*
1474 * Go to debug mode when returning from a function in which we are
1475 * single-stepping.
1476 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001477 if ((getline_equal(fgetline, cookie, getsourceline)
1478 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001480 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 ? (char_u *)_("End of sourced file")
1482 : (char_u *)_("End of function"));
1483 }
1484
1485 /*
1486 * Restore the exception environment (done after returning from the
1487 * debugger).
1488 */
1489 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001490 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
1492 msg_list = saved_msg_list;
1493#endif /* FEAT_EVAL */
1494
1495 /*
1496 * If there was too much output to fit on the command line, ask the user to
1497 * hit return before redrawing the screen. With the ":global" command we do
1498 * this only once after the command is finished.
1499 */
1500 if (did_inc)
1501 {
1502 --RedrawingDisabled;
1503 --no_wait_return;
1504 msg_scroll = FALSE;
1505
1506 /*
1507 * When just finished an ":if"-":else" which was typed, no need to
1508 * wait for hit-return. Also for an error situation.
1509 */
1510 if (retval == FAIL
1511#ifdef FEAT_EVAL
1512 || (did_endif && KeyTyped && !did_emsg)
1513#endif
1514 )
1515 {
1516 need_wait_return = FALSE;
1517 msg_didany = FALSE; /* don't wait when restarting edit */
1518 }
1519 else if (need_wait_return)
1520 {
1521 /*
1522 * The msg_start() above clears msg_didout. The wait_return we do
1523 * here should not overwrite the command that may be shown before
1524 * doing that.
1525 */
1526 msg_didout |= msg_didout_before_start;
1527 wait_return(FALSE);
1528 }
1529 }
1530
Bram Moolenaar2a942252012-11-28 23:03:07 +01001531#ifdef FEAT_EVAL
1532 did_endif = FALSE; /* in case do_cmdline used recursively */
1533#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 /*
1535 * Reset if_level, in case a sourced script file contains more ":if" than
1536 * ":endif" (could be ":if x | foo | endif").
1537 */
1538 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001539#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001540
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 --call_depth;
1542 return retval;
1543}
1544
1545#ifdef FEAT_EVAL
1546/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001547 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 */
1549 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001550get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 int c;
1552 void *cookie;
1553 int indent;
1554{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001555 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 wcmd_T *wp;
1557 char_u *line;
1558
1559 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1560 {
1561 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001562 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001564 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 if (cp->getline == NULL)
1566 line = getcmdline(c, 0L, indent);
1567 else
1568 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001569 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 ++cp->current_line;
1571
1572 return line;
1573 }
1574
1575 KeyTyped = FALSE;
1576 ++cp->current_line;
1577 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1578 sourcing_lnum = wp->lnum;
1579 return vim_strsave(wp->line);
1580}
1581
1582/*
1583 * Store a line in "gap" so that a ":while" loop can execute it again.
1584 */
1585 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001586store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 garray_T *gap;
1588 char_u *line;
1589{
1590 if (ga_grow(gap, 1) == FAIL)
1591 return FAIL;
1592 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1593 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1594 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 return OK;
1596}
1597
1598/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001599 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 */
1601 static void
1602free_cmdlines(gap)
1603 garray_T *gap;
1604{
1605 while (gap->ga_len > 0)
1606 {
1607 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1608 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
1610}
1611#endif
1612
1613/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001614 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1615 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001618getline_equal(fgetline, cookie, func)
1619 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001620 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 char_u *(*func) __ARGS((int, void *, int));
1622{
1623#ifdef FEAT_EVAL
1624 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001625 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
Bram Moolenaar89d40322006-08-29 15:30:07 +00001627 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001628 * function that's originally used to obtain the lines. This may be
1629 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001630 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001631 cp = (struct loop_cookie *)cookie;
1632 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 {
1634 gp = cp->getline;
1635 cp = cp->cookie;
1636 }
1637 return gp == func;
1638#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001639 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640#endif
1641}
1642
1643#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1644/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001645 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 * getline function. Otherwise return "cookie".
1647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001649getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001650 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001651 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652{
1653# ifdef FEAT_EVAL
1654 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001655 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
Bram Moolenaar89d40322006-08-29 15:30:07 +00001657 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001658 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001660 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001661 cp = (struct loop_cookie *)cookie;
1662 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
1664 gp = cp->getline;
1665 cp = cp->cookie;
1666 }
1667 return cp;
1668# else
1669 return cookie;
1670# endif
1671}
1672#endif
1673
1674/*
1675 * Execute one Ex command.
1676 *
1677 * If 'sourcing' is TRUE, the command will be included in the error message.
1678 *
1679 * 1. skip comment lines and leading space
1680 * 2. handle command modifiers
1681 * 3. parse range
1682 * 4. parse command
1683 * 5. parse arguments
1684 * 6. switch on command name
1685 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001686 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 *
1688 * This function may be called recursively!
1689 */
1690#if (_MSC_VER == 1200)
1691/*
Bram Moolenaared203462004-06-16 11:19:22 +00001692 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001694 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695#endif
1696 static char_u *
1697do_one_cmd(cmdlinep, sourcing,
1698#ifdef FEAT_EVAL
1699 cstack,
1700#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001701 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 char_u **cmdlinep;
1703 int sourcing;
1704#ifdef FEAT_EVAL
1705 struct condstack *cstack;
1706#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001707 char_u *(*fgetline) __ARGS((int, void *, int));
1708 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
1710 char_u *p;
1711 linenr_T lnum;
1712 long n;
1713 char_u *errormsg = NULL; /* error message */
1714 exarg_T ea; /* Ex command arguments */
1715 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001716 int save_msg_scroll = msg_scroll;
1717 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001719#ifdef HAVE_SANDBOX
1720 int did_sandbox = FALSE;
1721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 cmdmod_T save_cmdmod;
1723 int ni; /* set when Not Implemented */
1724
1725 vim_memset(&ea, 0, sizeof(ea));
1726 ea.line1 = 1;
1727 ea.line2 = 1;
1728#ifdef FEAT_EVAL
1729 ++ex_nesting_level;
1730#endif
1731
Bram Moolenaar21691f82012-12-05 19:13:18 +01001732 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 if (quitmore
1734#ifdef FEAT_EVAL
1735 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001736 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001737#endif
1738#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001739 /* avoid that an autocommand, e.g. QuitPre, does this */
1740 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741#endif
1742 )
1743 --quitmore;
1744
1745 /*
1746 * Reset browse, confirm, etc.. They are restored when returning, for
1747 * recursive calls.
1748 */
1749 save_cmdmod = cmdmod;
1750 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1751
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001752 /* "#!anything" is handled like a comment. */
1753 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1754 goto doend;
1755
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 /*
1757 * Repeat until no more command modifiers are found.
1758 */
1759 ea.cmd = *cmdlinep;
1760 for (;;)
1761 {
1762/*
1763 * 1. skip comment lines and leading white space and colons
1764 */
1765 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1766 ++ea.cmd;
1767
1768 /* in ex mode, an empty line works like :+ */
1769 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001770 && (getline_equal(fgetline, cookie, getexmodeline)
1771 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001772 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 {
1774 ea.cmd = (char_u *)"+";
1775 ex_pressedreturn = TRUE;
1776 }
1777
1778 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001779 if (*ea.cmd == '"')
1780 goto doend;
1781 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001782 {
1783 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
1787/*
1788 * 2. handle command modifiers.
1789 */
1790 p = ea.cmd;
1791 if (VIM_ISDIGIT(*ea.cmd))
1792 p = skipwhite(skipdigits(ea.cmd));
1793 switch (*p)
1794 {
1795 /* When adding an entry, also modify cmd_exists(). */
1796 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1797 break;
1798#ifdef FEAT_WINDOWS
1799 cmdmod.split |= WSP_ABOVE;
1800#endif
1801 continue;
1802
1803 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1804 {
1805#ifdef FEAT_WINDOWS
1806 cmdmod.split |= WSP_BELOW;
1807#endif
1808 continue;
1809 }
1810 if (checkforcmd(&ea.cmd, "browse", 3))
1811 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001812#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 cmdmod.browse = TRUE;
1814#endif
1815 continue;
1816 }
1817 if (!checkforcmd(&ea.cmd, "botright", 2))
1818 break;
1819#ifdef FEAT_WINDOWS
1820 cmdmod.split |= WSP_BOT;
1821#endif
1822 continue;
1823
1824 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1825 break;
1826#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1827 cmdmod.confirm = TRUE;
1828#endif
1829 continue;
1830
1831 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1832 {
1833 cmdmod.keepmarks = TRUE;
1834 continue;
1835 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001836 if (checkforcmd(&ea.cmd, "keepalt", 5))
1837 {
1838 cmdmod.keepalt = TRUE;
1839 continue;
1840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1842 break;
1843 cmdmod.keepjumps = TRUE;
1844 continue;
1845
1846 /* ":hide" and ":hide | cmd" are not modifiers */
1847 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1848 || *p == NUL || ends_excmd(*p))
1849 break;
1850 ea.cmd = p;
1851 cmdmod.hide = TRUE;
1852 continue;
1853
1854 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1855 {
1856 cmdmod.lockmarks = TRUE;
1857 continue;
1858 }
1859
1860 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1861 break;
1862#ifdef FEAT_WINDOWS
1863 cmdmod.split |= WSP_ABOVE;
1864#endif
1865 continue;
1866
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001867 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1868 break;
1869#ifdef FEAT_AUTOCMD
1870 if (cmdmod.save_ei == NULL)
1871 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001872 /* Set 'eventignore' to "all". Restore the
1873 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001874 cmdmod.save_ei = vim_strsave(p_ei);
1875 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001876 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001877 }
1878#endif
1879 continue;
1880
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1882 break;
1883#ifdef FEAT_WINDOWS
1884 cmdmod.split |= WSP_BELOW;
1885#endif
1886 continue;
1887
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001888 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1889 {
1890#ifdef HAVE_SANDBOX
1891 if (!did_sandbox)
1892 ++sandbox;
1893 did_sandbox = TRUE;
1894#endif
1895 continue;
1896 }
1897 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001899 if (save_msg_silent == -1)
1900 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1903 {
1904 /* ":silent!", but not "silent !cmd" */
1905 ea.cmd = skipwhite(ea.cmd + 1);
1906 ++emsg_silent;
1907 ++did_esilent;
1908 }
1909 continue;
1910
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001911 case 't': if (checkforcmd(&p, "tab", 3))
1912 {
1913#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001914 if (vim_isdigit(*ea.cmd))
1915 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1916 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001917 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001918 ea.cmd = p;
1919#endif
1920 continue;
1921 }
1922 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 break;
1924#ifdef FEAT_WINDOWS
1925 cmdmod.split |= WSP_TOP;
1926#endif
1927 continue;
1928
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001929 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1930 break;
1931 if (save_msg_silent == -1)
1932 save_msg_silent = msg_silent;
1933 msg_silent = 0;
1934 continue;
1935
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1937 {
1938#ifdef FEAT_VERTSPLIT
1939 cmdmod.split |= WSP_VERT;
1940#endif
1941 continue;
1942 }
1943 if (!checkforcmd(&p, "verbose", 4))
1944 break;
1945 if (verbose_save < 0)
1946 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001947 if (vim_isdigit(*ea.cmd))
1948 p_verbose = atoi((char *)ea.cmd);
1949 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 p_verbose = 1;
1951 ea.cmd = p;
1952 continue;
1953 }
1954 break;
1955 }
1956
1957#ifdef FEAT_EVAL
1958 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1959 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1960#else
1961 ea.skip = (if_level > 0);
1962#endif
1963
1964#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001965# ifdef FEAT_PROFILE
1966 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001967 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001968 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001969 if (getline_equal(fgetline, cookie, get_func_line))
1970 func_line_exec(getline_cookie(fgetline, cookie));
1971 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001972 script_line_exec();
1973 }
1974#endif
1975
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 /* May go to debug mode. If this happens and the ">quit" debug command is
1977 * used, throw an interrupt exception and skip the next command. */
1978 dbg_check_breakpoint(&ea);
1979 if (!ea.skip && got_int)
1980 {
1981 ea.skip = TRUE;
1982 (void)do_intthrow(cstack);
1983 }
1984#endif
1985
1986/*
1987 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1988 *
1989 * where 'addr' is:
1990 *
1991 * % (entire file)
1992 * $ [+-NUM]
1993 * 'x [+-NUM] (where x denotes a currently defined mark)
1994 * . [+-NUM]
1995 * [+-NUM]..
1996 * NUM
1997 *
1998 * The ea.cmd pointer is updated to point to the first character following the
1999 * range spec. If an initial address is found, but no second, the upper bound
2000 * is equal to the lower.
2001 */
2002
2003 /* repeat for all ',' or ';' separated addresses */
2004 for (;;)
2005 {
2006 ea.line1 = ea.line2;
2007 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
2008 ea.cmd = skipwhite(ea.cmd);
2009 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
2010 if (ea.cmd == NULL) /* error detected */
2011 goto doend;
2012 if (lnum == MAXLNUM)
2013 {
2014 if (*ea.cmd == '%') /* '%' - all lines */
2015 {
2016 ++ea.cmd;
2017 ea.line1 = 1;
2018 ea.line2 = curbuf->b_ml.ml_line_count;
2019 ++ea.addr_count;
2020 }
2021 /* '*' - visual area */
2022 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2023 {
2024 pos_T *fp;
2025
2026 ++ea.cmd;
2027 if (!ea.skip)
2028 {
2029 fp = getmark('<', FALSE);
2030 if (check_mark(fp) == FAIL)
2031 goto doend;
2032 ea.line1 = fp->lnum;
2033 fp = getmark('>', FALSE);
2034 if (check_mark(fp) == FAIL)
2035 goto doend;
2036 ea.line2 = fp->lnum;
2037 ++ea.addr_count;
2038 }
2039 }
2040 }
2041 else
2042 ea.line2 = lnum;
2043 ea.addr_count++;
2044
2045 if (*ea.cmd == ';')
2046 {
2047 if (!ea.skip)
2048 curwin->w_cursor.lnum = ea.line2;
2049 }
2050 else if (*ea.cmd != ',')
2051 break;
2052 ++ea.cmd;
2053 }
2054
2055 /* One address given: set start and end lines */
2056 if (ea.addr_count == 1)
2057 {
2058 ea.line1 = ea.line2;
2059 /* ... but only implicit: really no address given */
2060 if (lnum == MAXLNUM)
2061 ea.addr_count = 0;
2062 }
2063
2064 /* Don't leave the cursor on an illegal line (caused by ';') */
2065 check_cursor_lnum();
2066
2067/*
2068 * 4. parse command
2069 */
2070
2071 /*
2072 * Skip ':' and any white space
2073 */
2074 ea.cmd = skipwhite(ea.cmd);
2075 while (*ea.cmd == ':')
2076 ea.cmd = skipwhite(ea.cmd + 1);
2077
2078 /*
2079 * If we got a line, but no command, then go to the line.
2080 * If we find a '|' or '\n' we set ea.nextcmd.
2081 */
2082 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2083 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2084 {
2085 /*
2086 * strange vi behaviour:
2087 * ":3" jumps to line 3
2088 * ":3|..." prints line 3
2089 * ":|" prints current line
2090 */
2091 if (ea.skip) /* skip this if inside :if */
2092 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002093 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 {
2095 ea.cmdidx = CMD_print;
2096 ea.argt = RANGE+COUNT+TRLBAR;
2097 if ((errormsg = invalid_range(&ea)) == NULL)
2098 {
2099 correct_range(&ea);
2100 ex_print(&ea);
2101 }
2102 }
2103 else if (ea.addr_count != 0)
2104 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002105 if (ea.line2 > curbuf->b_ml.ml_line_count)
2106 {
2107 /* With '-' in 'cpoptions' a line number past the file is an
2108 * error, otherwise put it at the end of the file. */
2109 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2110 ea.line2 = -1;
2111 else
2112 ea.line2 = curbuf->b_ml.ml_line_count;
2113 }
2114
2115 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002116 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 else
2118 {
2119 if (ea.line2 == 0)
2120 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 else
2122 curwin->w_cursor.lnum = ea.line2;
2123 beginline(BL_SOL | BL_FIX);
2124 }
2125 }
2126 goto doend;
2127 }
2128
2129 /* Find the command and let "p" point to after it. */
2130 p = find_command(&ea, NULL);
2131
2132#ifdef FEAT_USR_CMDS
2133 if (p == NULL)
2134 {
2135 if (!ea.skip)
2136 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2137 goto doend;
2138 }
2139 /* Check for wrong commands. */
2140 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2141 {
2142 errormsg = uc_fun_cmd();
2143 goto doend;
2144 }
2145#endif
2146 if (ea.cmdidx == CMD_SIZE)
2147 {
2148 if (!ea.skip)
2149 {
2150 STRCPY(IObuff, _("E492: Not an editor command"));
2151 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002152 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002154 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
2156 goto doend;
2157 }
2158
2159 ni = (
2160#ifdef FEAT_USR_CMDS
2161 !USER_CMDIDX(ea.cmdidx) &&
2162#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002163 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002164#ifdef HAVE_EX_SCRIPT_NI
2165 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2166#endif
2167 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
2169#ifndef FEAT_EVAL
2170 /*
2171 * When the expression evaluation is disabled, recognize the ":if" and
2172 * ":endif" commands and ignore everything in between it.
2173 */
2174 if (ea.cmdidx == CMD_if)
2175 ++if_level;
2176 if (if_level)
2177 {
2178 if (ea.cmdidx == CMD_endif)
2179 --if_level;
2180 goto doend;
2181 }
2182
2183#endif
2184
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002185 /* forced commands */
2186 if (*p == '!' && ea.cmdidx != CMD_substitute
2187 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 {
2189 ++p;
2190 ea.forceit = TRUE;
2191 }
2192 else
2193 ea.forceit = FALSE;
2194
2195/*
2196 * 5. parse arguments
2197 */
2198#ifdef FEAT_USR_CMDS
2199 if (!USER_CMDIDX(ea.cmdidx))
2200#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002201 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202
2203 if (!ea.skip)
2204 {
2205#ifdef HAVE_SANDBOX
2206 if (sandbox != 0 && !(ea.argt & SBOXOK))
2207 {
2208 /* Command not allowed in sandbox. */
2209 errormsg = (char_u *)_(e_sandbox);
2210 goto doend;
2211 }
2212#endif
2213 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2214 {
2215 /* Command not allowed in non-'modifiable' buffer */
2216 errormsg = (char_u *)_(e_modifiable);
2217 goto doend;
2218 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002219
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002220 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221# ifdef FEAT_USR_CMDS
2222 && !USER_CMDIDX(ea.cmdidx)
2223# endif
2224 )
2225 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002226 /* Command not allowed when editing the command line. */
2227#ifdef FEAT_CMDWIN
2228 if (cmdwin_type != 0)
2229 errormsg = (char_u *)_(e_cmdwin);
2230 else
2231#endif
2232 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 goto doend;
2234 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002235#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002236 /* Disallow editing another buffer when "curbuf_lock" is set.
2237 * Do allow ":edit" (check for argument later).
2238 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002239 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002240 && ea.cmdidx != CMD_edit
2241 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002242# ifdef FEAT_USR_CMDS
2243 && !USER_CMDIDX(ea.cmdidx)
2244# endif
2245 && curbuf_locked())
2246 goto doend;
2247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248
2249 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2250 {
2251 /* no range allowed */
2252 errormsg = (char_u *)_(e_norange);
2253 goto doend;
2254 }
2255 }
2256
2257 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2258 {
2259 errormsg = (char_u *)_(e_nobang);
2260 goto doend;
2261 }
2262
2263 /*
2264 * Don't complain about the range if it is not used
2265 * (could happen if line_count is accidentally set to 0).
2266 */
2267 if (!ea.skip && !ni)
2268 {
2269 /*
2270 * If the range is backwards, ask for confirmation and, if given, swap
2271 * ea.line1 & ea.line2 so it's forwards again.
2272 * When global command is busy, don't ask, will fail below.
2273 */
2274 if (!global_busy && ea.line1 > ea.line2)
2275 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002276 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002278 if (sourcing || exmode_active)
2279 {
2280 errormsg = (char_u *)_("E493: Backwards range given");
2281 goto doend;
2282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 if (ask_yesno((char_u *)
2284 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002285 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 }
2287 lnum = ea.line1;
2288 ea.line1 = ea.line2;
2289 ea.line2 = lnum;
2290 }
2291 if ((errormsg = invalid_range(&ea)) != NULL)
2292 goto doend;
2293 }
2294
2295 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2296 ea.line2 = 1;
2297
2298 correct_range(&ea);
2299
2300#ifdef FEAT_FOLDING
2301 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2302 {
2303 /* Put the first line at the start of a closed fold, put the last line
2304 * at the end of a closed fold. */
2305 (void)hasFolding(ea.line1, &ea.line1, NULL);
2306 (void)hasFolding(ea.line2, NULL, &ea.line2);
2307 }
2308#endif
2309
2310#ifdef FEAT_QUICKFIX
2311 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002312 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 * option here, so things like % get expanded.
2314 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002315 p = replace_makeprg(&ea, p, cmdlinep);
2316 if (p == NULL)
2317 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318#endif
2319
2320 /*
2321 * Skip to start of argument.
2322 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2323 */
2324 if (ea.cmdidx == CMD_bang)
2325 ea.arg = p;
2326 else
2327 ea.arg = skipwhite(p);
2328
2329 /*
2330 * Check for "++opt=val" argument.
2331 * Must be first, allow ":w ++enc=utf8 !cmd"
2332 */
2333 if (ea.argt & ARGOPT)
2334 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2335 if (getargopt(&ea) == FAIL && !ni)
2336 {
2337 errormsg = (char_u *)_(e_invarg);
2338 goto doend;
2339 }
2340
2341 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2342 {
2343 if (*ea.arg == '>') /* append */
2344 {
2345 if (*++ea.arg != '>') /* typed wrong */
2346 {
2347 errormsg = (char_u *)_("E494: Use w or w>>");
2348 goto doend;
2349 }
2350 ea.arg = skipwhite(ea.arg + 1);
2351 ea.append = TRUE;
2352 }
2353 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2354 {
2355 ++ea.arg;
2356 ea.usefilter = TRUE;
2357 }
2358 }
2359
2360 if (ea.cmdidx == CMD_read)
2361 {
2362 if (ea.forceit)
2363 {
2364 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2365 ea.forceit = FALSE;
2366 }
2367 else if (*ea.arg == '!') /* :r !filter */
2368 {
2369 ++ea.arg;
2370 ea.usefilter = TRUE;
2371 }
2372 }
2373
2374 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2375 {
2376 ea.amount = 1;
2377 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2378 {
2379 ++ea.arg;
2380 ++ea.amount;
2381 }
2382 ea.arg = skipwhite(ea.arg);
2383 }
2384
2385 /*
2386 * Check for "+command" argument, before checking for next command.
2387 * Don't do this for ":read !cmd" and ":write !cmd".
2388 */
2389 if ((ea.argt & EDITCMD) && !ea.usefilter)
2390 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2391
2392 /*
2393 * Check for '|' to separate commands and '"' to start comments.
2394 * Don't do this for ":read !cmd" and ":write !cmd".
2395 */
2396 if ((ea.argt & TRLBAR) && !ea.usefilter)
2397 separate_nextcmd(&ea);
2398
2399 /*
2400 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002401 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2402 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002404 else if (ea.cmdidx == CMD_bang
2405 || ea.cmdidx == CMD_global
2406 || ea.cmdidx == CMD_vglobal
2407 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
2409 for (p = ea.arg; *p; ++p)
2410 {
2411 /* Remove one backslash before a newline, so that it's possible to
2412 * pass a newline to the shell and also a newline that is preceded
2413 * with a backslash. This makes it impossible to end a shell
2414 * command in a backslash, but that doesn't appear useful.
2415 * Halving the number of backslashes is incompatible with previous
2416 * versions. */
2417 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002418 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 else if (*p == '\n')
2420 {
2421 ea.nextcmd = p + 1;
2422 *p = NUL;
2423 break;
2424 }
2425 }
2426 }
2427
2428 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2429 {
2430 ea.line1 = 1;
2431 ea.line2 = curbuf->b_ml.ml_line_count;
2432 }
2433
2434 /* accept numbered register only when no count allowed (:put) */
2435 if ( (ea.argt & REGSTR)
2436 && *ea.arg != NUL
2437#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 /* Do not allow register = for user commands */
2439 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440#endif
2441 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2442 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002443#ifndef FEAT_CLIPBOARD
2444 /* check these explicitly for a more specific error message */
2445 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002447 errormsg = (char_u *)_(e_invalidreg);
2448 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 }
2450#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002451 if (
2452#ifdef FEAT_USR_CMDS
2453 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2454 && USER_CMDIDX(ea.cmdidx)))
2455#else
2456 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2457#endif
2458 )
2459 {
2460 ea.regname = *ea.arg++;
2461#ifdef FEAT_EVAL
2462 /* for '=' register: accept the rest of the line as an expression */
2463 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2464 {
2465 set_expr_line(vim_strsave(ea.arg));
2466 ea.arg += STRLEN(ea.arg);
2467 }
2468#endif
2469 ea.arg = skipwhite(ea.arg);
2470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 }
2472
2473 /*
2474 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2475 * count, it's a buffer name.
2476 */
2477 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2478 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2479 || vim_iswhite(*p)))
2480 {
2481 n = getdigits(&ea.arg);
2482 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002483 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
2485 errormsg = (char_u *)_(e_zerocount);
2486 goto doend;
2487 }
2488 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2489 {
2490 ea.line2 = n;
2491 if (ea.addr_count == 0)
2492 ea.addr_count = 1;
2493 }
2494 else
2495 {
2496 ea.line1 = ea.line2;
2497 ea.line2 += n - 1;
2498 ++ea.addr_count;
2499 /*
2500 * Be vi compatible: no error message for out of range.
2501 */
2502 if (ea.line2 > curbuf->b_ml.ml_line_count)
2503 ea.line2 = curbuf->b_ml.ml_line_count;
2504 }
2505 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002506
2507 /*
2508 * Check for flags: 'l', 'p' and '#'.
2509 */
2510 if (ea.argt & EXFLAGS)
2511 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002513 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002514 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 {
2516 errormsg = (char_u *)_(e_trailing);
2517 goto doend;
2518 }
2519
2520 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2521 {
2522 errormsg = (char_u *)_(e_argreq);
2523 goto doend;
2524 }
2525
2526#ifdef FEAT_EVAL
2527 /*
2528 * Skip the command when it's not going to be executed.
2529 * The commands like :if, :endif, etc. always need to be executed.
2530 * Also make an exception for commands that handle a trailing command
2531 * themselves.
2532 */
2533 if (ea.skip)
2534 {
2535 switch (ea.cmdidx)
2536 {
2537 /* commands that need evaluation */
2538 case CMD_while:
2539 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002540 case CMD_for:
2541 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 case CMD_if:
2543 case CMD_elseif:
2544 case CMD_else:
2545 case CMD_endif:
2546 case CMD_try:
2547 case CMD_catch:
2548 case CMD_finally:
2549 case CMD_endtry:
2550 case CMD_function:
2551 break;
2552
2553 /* Commands that handle '|' themselves. Check: A command should
2554 * either have the TRLBAR flag, appear in this list or appear in
2555 * the list at ":help :bar". */
2556 case CMD_aboveleft:
2557 case CMD_and:
2558 case CMD_belowright:
2559 case CMD_botright:
2560 case CMD_browse:
2561 case CMD_call:
2562 case CMD_confirm:
2563 case CMD_delfunction:
2564 case CMD_djump:
2565 case CMD_dlist:
2566 case CMD_dsearch:
2567 case CMD_dsplit:
2568 case CMD_echo:
2569 case CMD_echoerr:
2570 case CMD_echomsg:
2571 case CMD_echon:
2572 case CMD_execute:
2573 case CMD_help:
2574 case CMD_hide:
2575 case CMD_ijump:
2576 case CMD_ilist:
2577 case CMD_isearch:
2578 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002579 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 case CMD_keepjumps:
2581 case CMD_keepmarks:
2582 case CMD_leftabove:
2583 case CMD_let:
2584 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002585 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002587 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 case CMD_perl:
2589 case CMD_psearch:
2590 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002591 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002592 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 case CMD_return:
2594 case CMD_rightbelow:
2595 case CMD_ruby:
2596 case CMD_silent:
2597 case CMD_smagic:
2598 case CMD_snomagic:
2599 case CMD_substitute:
2600 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002601 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 case CMD_tcl:
2603 case CMD_throw:
2604 case CMD_tilde:
2605 case CMD_topleft:
2606 case CMD_unlet:
2607 case CMD_verbose:
2608 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002609 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 break;
2611
2612 default: goto doend;
2613 }
2614 }
2615#endif
2616
2617 if (ea.argt & XFILE)
2618 {
2619 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2620 goto doend;
2621 }
2622
2623#ifdef FEAT_LISTCMDS
2624 /*
2625 * Accept buffer name. Cannot be used at the same time with a buffer
2626 * number. Don't do this for a user command.
2627 */
2628 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2629# ifdef FEAT_USR_CMDS
2630 && !USER_CMDIDX(ea.cmdidx)
2631# endif
2632 )
2633 {
2634 /*
2635 * :bdelete, :bwipeout and :bunload take several arguments, separated
2636 * by spaces: find next space (skipping over escaped characters).
2637 * The others take one argument: ignore trailing spaces.
2638 */
2639 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2640 || ea.cmdidx == CMD_bunload)
2641 p = skiptowhite_esc(ea.arg);
2642 else
2643 {
2644 p = ea.arg + STRLEN(ea.arg);
2645 while (p > ea.arg && vim_iswhite(p[-1]))
2646 --p;
2647 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002648 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2649 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 if (ea.line2 < 0) /* failed */
2651 goto doend;
2652 ea.addr_count = 1;
2653 ea.arg = skipwhite(p);
2654 }
2655#endif
2656
2657/*
2658 * 6. switch on command name
2659 *
2660 * The "ea" structure holds the arguments that can be used.
2661 */
2662 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002663 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 ea.cookie = cookie;
2665#ifdef FEAT_EVAL
2666 ea.cstack = cstack;
2667#endif
2668
2669#ifdef FEAT_USR_CMDS
2670 if (USER_CMDIDX(ea.cmdidx))
2671 {
2672 /*
2673 * Execute a user-defined command.
2674 */
2675 do_ucmd(&ea);
2676 }
2677 else
2678#endif
2679 {
2680 /*
2681 * Call the function to execute the command.
2682 */
2683 ea.errmsg = NULL;
2684 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2685 if (ea.errmsg != NULL)
2686 errormsg = (char_u *)_(ea.errmsg);
2687 }
2688
2689#ifdef FEAT_EVAL
2690 /*
2691 * If the command just executed called do_cmdline(), any throw or ":return"
2692 * or ":finish" encountered there must also check the cstack of the still
2693 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2694 * exception, or reanimate a returned function or finished script file and
2695 * return or finish it again.
2696 */
2697 if (need_rethrow)
2698 do_throw(cstack);
2699 else if (check_cstack)
2700 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002701 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002703 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 && current_func_returned())
2705 do_return(&ea, TRUE, FALSE, NULL);
2706 }
2707 need_rethrow = check_cstack = FALSE;
2708#endif
2709
2710doend:
2711 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2712 curwin->w_cursor.lnum = 1;
2713
2714 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2715 {
2716 if (sourcing)
2717 {
2718 if (errormsg != IObuff)
2719 {
2720 STRCPY(IObuff, errormsg);
2721 errormsg = IObuff;
2722 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002723 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
2725 emsg(errormsg);
2726 }
2727#ifdef FEAT_EVAL
2728 do_errthrow(cstack,
2729 (ea.cmdidx != CMD_SIZE
2730# ifdef FEAT_USR_CMDS
2731 && !USER_CMDIDX(ea.cmdidx)
2732# endif
2733 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2734#endif
2735
2736 if (verbose_save >= 0)
2737 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002738#ifdef FEAT_AUTOCMD
2739 if (cmdmod.save_ei != NULL)
2740 {
2741 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002742 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2743 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002744 free_string_option(cmdmod.save_ei);
2745 }
2746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747
2748 cmdmod = save_cmdmod;
2749
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002750 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 {
2752 /* messages could be enabled for a serious error, need to check if the
2753 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002754 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002755 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 emsg_silent -= did_esilent;
2757 if (emsg_silent < 0)
2758 emsg_silent = 0;
2759 /* Restore msg_scroll, it's set by file I/O commands, even when no
2760 * message is actually displayed. */
2761 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002762
2763 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2764 * somewhere in the line. Put it back in the first column. */
2765 if (redirecting())
2766 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 }
2768
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002769#ifdef HAVE_SANDBOX
2770 if (did_sandbox)
2771 --sandbox;
2772#endif
2773
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2775 ea.nextcmd = NULL;
2776
2777#ifdef FEAT_EVAL
2778 --ex_nesting_level;
2779#endif
2780
2781 return ea.nextcmd;
2782}
2783#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002784 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785#endif
2786
2787/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002788 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 * If there is a match advance "pp" to the argument and return TRUE.
2790 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002791 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002793 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 char *cmd; /* name of command */
2795 int len; /* required length */
2796{
2797 int i;
2798
2799 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002800 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 break;
2802 if (i >= len && !isalpha((*pp)[i]))
2803 {
2804 *pp = skipwhite(*pp + i);
2805 return TRUE;
2806 }
2807 return FALSE;
2808}
2809
2810/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002811 * Append "cmd" to the error message in IObuff.
2812 * Takes care of limiting the length and handling 0xa0, which would be
2813 * invisible otherwise.
2814 */
2815 static void
2816append_command(cmd)
2817 char_u *cmd;
2818{
2819 char_u *s = cmd;
2820 char_u *d;
2821
2822 STRCAT(IObuff, ": ");
2823 d = IObuff + STRLEN(IObuff);
2824 while (*s != NUL && d - IObuff < IOSIZE - 7)
2825 {
2826 if (
2827#ifdef FEAT_MBYTE
2828 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2829#endif
2830 *s == 0xa0)
2831 {
2832 s +=
2833#ifdef FEAT_MBYTE
2834 enc_utf8 ? 2 :
2835#endif
2836 1;
2837 STRCPY(d, "<a0>");
2838 d += 4;
2839 }
2840 else
2841 MB_COPY_CHAR(s, d);
2842 }
2843 *d = NUL;
2844}
2845
2846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002848 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002850 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 * Returns NULL for an ambiguous user command.
2852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 static char_u *
2854find_command(eap, full)
2855 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002856 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857{
2858 int len;
2859 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002860 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861
2862 /*
2863 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002864 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 * - the 'k' command can directly be followed by any character.
2866 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2867 * but :sre[wind] is another command, as are :scrip[tnames],
2868 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002869 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 */
2871 p = eap->cmd;
2872 if (*p == 'k')
2873 {
2874 eap->cmdidx = CMD_k;
2875 ++p;
2876 }
2877 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002878 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2879 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 || p[1] == 'g'
2881 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2882 || p[1] == 'I'
2883 || (p[1] == 'r' && p[2] != 'e')))
2884 {
2885 eap->cmdidx = CMD_substitute;
2886 ++p;
2887 }
2888 else
2889 {
2890 while (ASCII_ISALPHA(*p))
2891 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002892 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002893 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002894 while (ASCII_ISALNUM(*p))
2895 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002896
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 /* check for non-alpha command */
2898 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2899 ++p;
2900 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002901 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2902 {
2903 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2904 * :delete with the 'l' flag. Same for 'p'. */
2905 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002906 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002907 break;
2908 if (i == len - 1)
2909 {
2910 --len;
2911 if (p[-1] == 'l')
2912 eap->flags |= EXFLAG_LIST;
2913 else
2914 eap->flags |= EXFLAG_PRINT;
2915 }
2916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917
2918 if (ASCII_ISLOWER(*eap->cmd))
2919 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2920 else
2921 eap->cmdidx = cmdidxs[26];
2922
2923 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2924 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2925 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2926 (size_t)len) == 0)
2927 {
2928#ifdef FEAT_EVAL
2929 if (full != NULL
2930 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2931 *full = TRUE;
2932#endif
2933 break;
2934 }
2935
2936#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002937 /* Look for a user defined command as a last resort. Let ":Print" be
2938 * overruled by a user defined command. */
2939 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2940 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002942 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 while (ASCII_ISALNUM(*p))
2944 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002945 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 }
2947#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002948 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 eap->cmdidx = CMD_SIZE;
2950 }
2951
2952 return p;
2953}
2954
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002955#ifdef FEAT_USR_CMDS
2956/*
2957 * Search for a user command that matches "eap->cmd".
2958 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2959 * Return a pointer to just after the command.
2960 * Return NULL if there is no matching command.
2961 */
2962 static char_u *
2963find_ucmd(eap, p, full, xp, compl)
2964 exarg_T *eap;
2965 char_u *p; /* end of the command (possibly including count) */
2966 int *full; /* set to TRUE for a full match */
2967 expand_T *xp; /* used for completion, NULL otherwise */
2968 int *compl; /* completion flags or NULL */
2969{
2970 int len = (int)(p - eap->cmd);
2971 int j, k, matchlen = 0;
2972 ucmd_T *uc;
2973 int found = FALSE;
2974 int possible = FALSE;
2975 char_u *cp, *np; /* Point into typed cmd and test name */
2976 garray_T *gap;
2977 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2978 only full match global is accepted. */
2979
2980 /*
2981 * Look for buffer-local user commands first, then global ones.
2982 */
2983 gap = &curbuf->b_ucmds;
2984 for (;;)
2985 {
2986 for (j = 0; j < gap->ga_len; ++j)
2987 {
2988 uc = USER_CMD_GA(gap, j);
2989 cp = eap->cmd;
2990 np = uc->uc_name;
2991 k = 0;
2992 while (k < len && *np != NUL && *cp++ == *np++)
2993 k++;
2994 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2995 {
2996 /* If finding a second match, the command is ambiguous. But
2997 * not if a buffer-local command wasn't a full match and a
2998 * global command is a full match. */
2999 if (k == len && found && *np != NUL)
3000 {
3001 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003002 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003003 amb_local = TRUE;
3004 }
3005
3006 if (!found || (k == len && *np == NUL))
3007 {
3008 /* If we matched up to a digit, then there could
3009 * be another command including the digit that we
3010 * should use instead.
3011 */
3012 if (k == len)
3013 found = TRUE;
3014 else
3015 possible = TRUE;
3016
3017 if (gap == &ucmds)
3018 eap->cmdidx = CMD_USER;
3019 else
3020 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003021 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003022 eap->useridx = j;
3023
3024# ifdef FEAT_CMDL_COMPL
3025 if (compl != NULL)
3026 *compl = uc->uc_compl;
3027# ifdef FEAT_EVAL
3028 if (xp != NULL)
3029 {
3030 xp->xp_arg = uc->uc_compl_arg;
3031 xp->xp_scriptID = uc->uc_scriptID;
3032 }
3033# endif
3034# endif
3035 /* Do not search for further abbreviations
3036 * if this is an exact match. */
3037 matchlen = k;
3038 if (k == len && *np == NUL)
3039 {
3040 if (full != NULL)
3041 *full = TRUE;
3042 amb_local = FALSE;
3043 break;
3044 }
3045 }
3046 }
3047 }
3048
3049 /* Stop if we found a full match or searched all. */
3050 if (j < gap->ga_len || gap == &ucmds)
3051 break;
3052 gap = &ucmds;
3053 }
3054
3055 /* Only found ambiguous matches. */
3056 if (amb_local)
3057 {
3058 if (xp != NULL)
3059 xp->xp_context = EXPAND_UNSUCCESSFUL;
3060 return NULL;
3061 }
3062
3063 /* The match we found may be followed immediately by a number. Move "p"
3064 * back to point to it. */
3065 if (found || possible)
3066 return p + (matchlen - len);
3067 return p;
3068}
3069#endif
3070
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003072static struct cmdmod
3073{
3074 char *name;
3075 int minlen;
3076 int has_count; /* :123verbose :3tab */
3077} cmdmods[] = {
3078 {"aboveleft", 3, FALSE},
3079 {"belowright", 3, FALSE},
3080 {"botright", 2, FALSE},
3081 {"browse", 3, FALSE},
3082 {"confirm", 4, FALSE},
3083 {"hide", 3, FALSE},
3084 {"keepalt", 5, FALSE},
3085 {"keepjumps", 5, FALSE},
3086 {"keepmarks", 3, FALSE},
3087 {"leftabove", 5, FALSE},
3088 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003089 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003090 {"rightbelow", 6, FALSE},
3091 {"sandbox", 3, FALSE},
3092 {"silent", 3, FALSE},
3093 {"tab", 3, TRUE},
3094 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003095 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003096 {"verbose", 4, TRUE},
3097 {"vertical", 4, FALSE},
3098};
3099
3100/*
3101 * Return length of a command modifier (including optional count).
3102 * Return zero when it's not a modifier.
3103 */
3104 int
3105modifier_len(cmd)
3106 char_u *cmd;
3107{
3108 int i, j;
3109 char_u *p = cmd;
3110
3111 if (VIM_ISDIGIT(*cmd))
3112 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003113 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003114 {
3115 for (j = 0; p[j] != NUL; ++j)
3116 if (p[j] != cmdmods[i].name[j])
3117 break;
3118 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3119 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003120 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003121 }
3122 return 0;
3123}
3124
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125/*
3126 * Return > 0 if an Ex command "name" exists.
3127 * Return 2 if there is an exact match.
3128 * Return 3 if there is an ambiguous match.
3129 */
3130 int
3131cmd_exists(name)
3132 char_u *name;
3133{
3134 exarg_T ea;
3135 int full = FALSE;
3136 int i;
3137 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003138 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139
3140 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003141 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 {
3143 for (j = 0; name[j] != NUL; ++j)
3144 if (name[j] != cmdmods[i].name[j])
3145 break;
3146 if (name[j] == NUL && j >= cmdmods[i].minlen)
3147 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3148 }
3149
Bram Moolenaara9587612006-05-04 21:47:50 +00003150 /* Check built-in commands and user defined commands.
3151 * For ":2match" and ":3match" we need to skip the number. */
3152 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003154 p = find_command(&ea, &full);
3155 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003157 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3158 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003159 if (*skipwhite(p) != NUL)
3160 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3162}
3163#endif
3164
3165/*
3166 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3167 * we don't need/want deleted. Maybe this could be done better if we didn't
3168 * repeat all this stuff. The only problem is that they may not stay
3169 * perfectly compatible with each other, but then the command line syntax
3170 * probably won't change that much -- webb.
3171 */
3172 char_u *
3173set_one_cmd_context(xp, buff)
3174 expand_T *xp;
3175 char_u *buff; /* buffer for command string */
3176{
3177 char_u *p;
3178 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003179 int len = 0;
3180 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3182 int compl = EXPAND_NOTHING;
3183#endif
3184#ifdef FEAT_CMDL_COMPL
3185 int delim;
3186#endif
3187 int forceit = FALSE;
3188 int usefilter = FALSE; /* filter instead of file name */
3189
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003190 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 xp->xp_pattern = buff;
3192 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003193 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
3195/*
3196 * 2. skip comment lines and leading space, colons or bars
3197 */
3198 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3199 ;
3200 xp->xp_pattern = cmd;
3201
3202 if (*cmd == NUL)
3203 return NULL;
3204 if (*cmd == '"') /* ignore comment lines */
3205 {
3206 xp->xp_context = EXPAND_NOTHING;
3207 return NULL;
3208 }
3209
3210/*
3211 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3212 */
3213 cmd = skip_range(cmd, &xp->xp_context);
3214
3215/*
3216 * 4. parse command
3217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 xp->xp_pattern = cmd;
3219 if (*cmd == NUL)
3220 return NULL;
3221 if (*cmd == '"')
3222 {
3223 xp->xp_context = EXPAND_NOTHING;
3224 return NULL;
3225 }
3226
3227 if (*cmd == '|' || *cmd == '\n')
3228 return cmd + 1; /* There's another command */
3229
3230 /*
3231 * Isolate the command and search for it in the command table.
3232 * Exceptions:
3233 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003234 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3236 */
3237 if (*cmd == 'k' && cmd[1] != 'e')
3238 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003239 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 p = cmd + 1;
3241 }
3242 else
3243 {
3244 p = cmd;
3245 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3246 ++p;
3247 /* check for non-alpha command */
3248 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3249 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003250 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003252 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 {
3254 xp->xp_context = EXPAND_UNSUCCESSFUL;
3255 return NULL;
3256 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003257 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003258 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3259 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3260 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 break;
3262
3263#ifdef FEAT_USR_CMDS
3264 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3266 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267#endif
3268 }
3269
3270 /*
3271 * If the cursor is touching the command, and it ends in an alpha-numeric
3272 * character, complete the command name.
3273 */
3274 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3275 return NULL;
3276
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003277 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
3279 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3280 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003281 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 p = cmd + 1;
3283 }
3284#ifdef FEAT_USR_CMDS
3285 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3286 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003287 ea.cmd = cmd;
3288 p = find_ucmd(&ea, p, NULL, xp,
3289# if defined(FEAT_CMDL_COMPL)
3290 &compl
3291# else
3292 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003294 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003295 if (p == NULL)
3296 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 }
3298#endif
3299 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003300 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 {
3302 /* Not still touching the command and it was an illegal one */
3303 xp->xp_context = EXPAND_UNSUCCESSFUL;
3304 return NULL;
3305 }
3306
3307 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3308
3309 if (*p == '!') /* forced commands */
3310 {
3311 forceit = TRUE;
3312 ++p;
3313 }
3314
3315/*
3316 * 5. parse arguments
3317 */
3318#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003319 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003321 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322
3323 arg = skipwhite(p);
3324
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003325 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 {
3327 if (*arg == '>') /* append */
3328 {
3329 if (*++arg == '>')
3330 ++arg;
3331 arg = skipwhite(arg);
3332 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003333 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 {
3335 ++arg;
3336 usefilter = TRUE;
3337 }
3338 }
3339
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003340 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 {
3342 usefilter = forceit; /* :r! filter if forced */
3343 if (*arg == '!') /* :r !filter */
3344 {
3345 ++arg;
3346 usefilter = TRUE;
3347 }
3348 }
3349
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003350 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 {
3352 while (*arg == *cmd) /* allow any number of '>' or '<' */
3353 ++arg;
3354 arg = skipwhite(arg);
3355 }
3356
3357 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003358 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 {
3360 /* Check if we're in the +command */
3361 p = arg + 1;
3362 arg = skip_cmd_arg(arg, FALSE);
3363
3364 /* Still touching the command after '+'? */
3365 if (*arg == NUL)
3366 return p;
3367
3368 /* Skip space(s) after +command to get to the real argument */
3369 arg = skipwhite(arg);
3370 }
3371
3372 /*
3373 * Check for '|' to separate commands and '"' to start comments.
3374 * Don't do this for ":read !cmd" and ":write !cmd".
3375 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003376 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
3378 p = arg;
3379 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003380 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 p += 2;
3382 while (*p)
3383 {
3384 if (*p == Ctrl_V)
3385 {
3386 if (p[1] != NUL)
3387 ++p;
3388 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003389 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 || *p == '|' || *p == '\n')
3391 {
3392 if (*(p - 1) != '\\')
3393 {
3394 if (*p == '|' || *p == '\n')
3395 return p + 1;
3396 return NULL; /* It's a comment */
3397 }
3398 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003399 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 }
3401 }
3402
3403 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003404 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 vim_strchr((char_u *)"|\"", *arg) == NULL)
3406 return NULL;
3407
3408 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003409 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003411 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003412 while (*p && p < buff + len)
3413 {
3414 if (*p == ' ' || *p == TAB)
3415 {
3416 /* argument starts after a space */
3417 xp->xp_pattern = ++p;
3418 }
3419 else
3420 {
3421 if (*p == '\\' && *(p + 1) != NUL)
3422 ++p; /* skip over escaped character */
3423 mb_ptr_adv(p);
3424 }
3425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003427 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003429 int c;
3430 int in_quote = FALSE;
3431 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432
3433 /*
3434 * Allow spaces within back-quotes to count as part of the argument
3435 * being expanded.
3436 */
3437 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003438 p = xp->xp_pattern;
3439 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003441#ifdef FEAT_MBYTE
3442 if (has_mbyte)
3443 c = mb_ptr2char(p);
3444 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003446 c = *p;
3447 if (c == '\\' && p[1] != NUL)
3448 ++p;
3449 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 {
3451 if (!in_quote)
3452 {
3453 xp->xp_pattern = p;
3454 bow = p + 1;
3455 }
3456 in_quote = !in_quote;
3457 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003458 /* An argument can contain just about everything, except
3459 * characters that end the command and white space. */
3460 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003461#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003462 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003463#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003464 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003465 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003466 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003467 while (*p != NUL)
3468 {
3469#ifdef FEAT_MBYTE
3470 if (has_mbyte)
3471 c = mb_ptr2char(p);
3472 else
3473#endif
3474 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003475 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003476 break;
3477#ifdef FEAT_MBYTE
3478 if (has_mbyte)
3479 len = (*mb_ptr2len)(p);
3480 else
3481#endif
3482 len = 1;
3483 mb_ptr_adv(p);
3484 }
3485 if (in_quote)
3486 bow = p;
3487 else
3488 xp->xp_pattern = p;
3489 p -= len;
3490 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003491 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
3493
3494 /*
3495 * If we are still inside the quotes, and we passed a space, just
3496 * expand from there.
3497 */
3498 if (bow != NULL && in_quote)
3499 xp->xp_pattern = bow;
3500 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003501
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003502 /* For a shell command more chars need to be escaped. */
3503 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003504 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003505#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003506 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003507#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003508 /* When still after the command name expand executables. */
3509 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003510 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
3513 /* Check for environment variable */
3514 if (*xp->xp_pattern == '$'
3515#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3516 || *xp->xp_pattern == '%'
3517#endif
3518 )
3519 {
3520 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3521 if (!vim_isIDc(*p))
3522 break;
3523 if (*p == NUL)
3524 {
3525 xp->xp_context = EXPAND_ENV_VARS;
3526 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003527#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3528 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003529 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003530 compl = EXPAND_ENV_VARS;
3531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 }
3533 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003534#if defined(FEAT_CMDL_COMPL)
3535 /* Check for user names */
3536 if (*xp->xp_pattern == '~')
3537 {
3538 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3539 ;
3540 /* Complete ~user only if it partially matches a user name.
3541 * A full match ~user<Tab> will be replaced by user's home
3542 * directory i.e. something like ~user<Tab> -> /home/user/ */
3543 if (*p == NUL && p > xp->xp_pattern + 1
3544 && match_user(xp->xp_pattern + 1) == 1)
3545 {
3546 xp->xp_context = EXPAND_USER;
3547 ++xp->xp_pattern;
3548 }
3549 }
3550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
3552
3553/*
3554 * 6. switch on command name
3555 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003556 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003558 case CMD_find:
3559 case CMD_sfind:
3560 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003561 if (xp->xp_context == EXPAND_FILES)
3562 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003563 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 case CMD_cd:
3565 case CMD_chdir:
3566 case CMD_lcd:
3567 case CMD_lchdir:
3568 if (xp->xp_context == EXPAND_FILES)
3569 xp->xp_context = EXPAND_DIRECTORIES;
3570 break;
3571 case CMD_help:
3572 xp->xp_context = EXPAND_HELP;
3573 xp->xp_pattern = arg;
3574 break;
3575
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003576 /* Command modifiers: return the argument.
3577 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003579 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 case CMD_belowright:
3581 case CMD_botright:
3582 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003583 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003585 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 case CMD_folddoclosed:
3587 case CMD_folddoopen:
3588 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003589 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 case CMD_keepjumps:
3591 case CMD_keepmarks:
3592 case CMD_leftabove:
3593 case CMD_lockmarks:
3594 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003595 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003597 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 case CMD_topleft:
3599 case CMD_verbose:
3600 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003601 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 return arg;
3603
Bram Moolenaar4f688582007-07-24 12:34:30 +00003604#ifdef FEAT_CMDL_COMPL
3605# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 case CMD_match:
3607 if (*arg == NUL || !ends_excmd(*arg))
3608 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003609 /* also complete "None" */
3610 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 arg = skipwhite(skiptowhite(arg));
3612 if (*arg != NUL)
3613 {
3614 xp->xp_context = EXPAND_NOTHING;
3615 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3616 }
3617 }
3618 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003619# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621/*
3622 * All completion for the +cmdline_compl feature goes here.
3623 */
3624
3625# ifdef FEAT_USR_CMDS
3626 case CMD_command:
3627 /* Check for attributes */
3628 while (*arg == '-')
3629 {
3630 arg++; /* Skip "-" */
3631 p = skiptowhite(arg);
3632 if (*p == NUL)
3633 {
3634 /* Cursor is still in the attribute */
3635 p = vim_strchr(arg, '=');
3636 if (p == NULL)
3637 {
3638 /* No "=", so complete attribute names */
3639 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3640 xp->xp_pattern = arg;
3641 return NULL;
3642 }
3643
3644 /* For the -complete and -nargs attributes, we complete
3645 * their arguments as well.
3646 */
3647 if (STRNICMP(arg, "complete", p - arg) == 0)
3648 {
3649 xp->xp_context = EXPAND_USER_COMPLETE;
3650 xp->xp_pattern = p + 1;
3651 return NULL;
3652 }
3653 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3654 {
3655 xp->xp_context = EXPAND_USER_NARGS;
3656 xp->xp_pattern = p + 1;
3657 return NULL;
3658 }
3659 return NULL;
3660 }
3661 arg = skipwhite(p);
3662 }
3663
3664 /* After the attributes comes the new command name */
3665 p = skiptowhite(arg);
3666 if (*p == NUL)
3667 {
3668 xp->xp_context = EXPAND_USER_COMMANDS;
3669 xp->xp_pattern = arg;
3670 break;
3671 }
3672
3673 /* And finally comes a normal command */
3674 return skipwhite(p);
3675
3676 case CMD_delcommand:
3677 xp->xp_context = EXPAND_USER_COMMANDS;
3678 xp->xp_pattern = arg;
3679 break;
3680# endif
3681
3682 case CMD_global:
3683 case CMD_vglobal:
3684 delim = *arg; /* get the delimiter */
3685 if (delim)
3686 ++arg; /* skip delimiter if there is one */
3687
3688 while (arg[0] != NUL && arg[0] != delim)
3689 {
3690 if (arg[0] == '\\' && arg[1] != NUL)
3691 ++arg;
3692 ++arg;
3693 }
3694 if (arg[0] != NUL)
3695 return arg + 1;
3696 break;
3697 case CMD_and:
3698 case CMD_substitute:
3699 delim = *arg;
3700 if (delim)
3701 {
3702 /* skip "from" part */
3703 ++arg;
3704 arg = skip_regexp(arg, delim, p_magic, NULL);
3705 }
3706 /* skip "to" part */
3707 while (arg[0] != NUL && arg[0] != delim)
3708 {
3709 if (arg[0] == '\\' && arg[1] != NUL)
3710 ++arg;
3711 ++arg;
3712 }
3713 if (arg[0] != NUL) /* skip delimiter */
3714 ++arg;
3715 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3716 ++arg;
3717 if (arg[0] != NUL)
3718 return arg;
3719 break;
3720 case CMD_isearch:
3721 case CMD_dsearch:
3722 case CMD_ilist:
3723 case CMD_dlist:
3724 case CMD_ijump:
3725 case CMD_psearch:
3726 case CMD_djump:
3727 case CMD_isplit:
3728 case CMD_dsplit:
3729 arg = skipwhite(skipdigits(arg)); /* skip count */
3730 if (*arg == '/') /* Match regexp, not just whole words */
3731 {
3732 for (++arg; *arg && *arg != '/'; arg++)
3733 if (*arg == '\\' && arg[1] != NUL)
3734 arg++;
3735 if (*arg)
3736 {
3737 arg = skipwhite(arg + 1);
3738
3739 /* Check for trailing illegal characters */
3740 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3741 xp->xp_context = EXPAND_NOTHING;
3742 else
3743 return arg;
3744 }
3745 }
3746 break;
3747#ifdef FEAT_AUTOCMD
3748 case CMD_autocmd:
3749 return set_context_in_autocmd(xp, arg, FALSE);
3750
3751 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003752 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 return set_context_in_autocmd(xp, arg, TRUE);
3754#endif
3755 case CMD_set:
3756 set_context_in_set_cmd(xp, arg, 0);
3757 break;
3758 case CMD_setglobal:
3759 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3760 break;
3761 case CMD_setlocal:
3762 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3763 break;
3764 case CMD_tag:
3765 case CMD_stag:
3766 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003767 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 case CMD_tselect:
3769 case CMD_stselect:
3770 case CMD_ptselect:
3771 case CMD_tjump:
3772 case CMD_stjump:
3773 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003774 if (*p_wop != NUL)
3775 xp->xp_context = EXPAND_TAGS_LISTFILES;
3776 else
3777 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 xp->xp_pattern = arg;
3779 break;
3780 case CMD_augroup:
3781 xp->xp_context = EXPAND_AUGROUP;
3782 xp->xp_pattern = arg;
3783 break;
3784#ifdef FEAT_SYN_HL
3785 case CMD_syntax:
3786 set_context_in_syntax_cmd(xp, arg);
3787 break;
3788#endif
3789#ifdef FEAT_EVAL
3790 case CMD_let:
3791 case CMD_if:
3792 case CMD_elseif:
3793 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003794 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 case CMD_echo:
3796 case CMD_echon:
3797 case CMD_execute:
3798 case CMD_echomsg:
3799 case CMD_echoerr:
3800 case CMD_call:
3801 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003802 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 break;
3804
3805 case CMD_unlet:
3806 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3807 arg = xp->xp_pattern + 1;
3808 xp->xp_context = EXPAND_USER_VARS;
3809 xp->xp_pattern = arg;
3810 break;
3811
3812 case CMD_function:
3813 case CMD_delfunction:
3814 xp->xp_context = EXPAND_USER_FUNC;
3815 xp->xp_pattern = arg;
3816 break;
3817
3818 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003819 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 break;
3821#endif
3822 case CMD_highlight:
3823 set_context_in_highlight_cmd(xp, arg);
3824 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003825#ifdef FEAT_CSCOPE
3826 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003827 case CMD_lcscope:
3828 case CMD_scscope:
3829 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003830 break;
3831#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003832#ifdef FEAT_SIGNS
3833 case CMD_sign:
3834 set_context_in_sign_cmd(xp, arg);
3835 break;
3836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837#ifdef FEAT_LISTCMDS
3838 case CMD_bdelete:
3839 case CMD_bwipeout:
3840 case CMD_bunload:
3841 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3842 arg = xp->xp_pattern + 1;
3843 /*FALLTHROUGH*/
3844 case CMD_buffer:
3845 case CMD_sbuffer:
3846 case CMD_checktime:
3847 xp->xp_context = EXPAND_BUFFERS;
3848 xp->xp_pattern = arg;
3849 break;
3850#endif
3851#ifdef FEAT_USR_CMDS
3852 case CMD_USER:
3853 case CMD_USER_BUF:
3854 if (compl != EXPAND_NOTHING)
3855 {
3856 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003857 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 {
3859# ifdef FEAT_MENU
3860 if (compl == EXPAND_MENUS)
3861 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3862# endif
3863 if (compl == EXPAND_COMMANDS)
3864 return arg;
3865 if (compl == EXPAND_MAPPINGS)
3866 return set_context_in_map_cmd(xp, (char_u *)"map",
3867 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003868 /* Find start of last argument. */
3869 p = arg;
3870 while (*p)
3871 {
3872 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003873 /* argument starts after a space */
3874 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003875 else if (*p == '\\' && *(p + 1) != NUL)
3876 ++p; /* skip over escaped character */
3877 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 xp->xp_pattern = arg;
3880 }
3881 xp->xp_context = compl;
3882 }
3883 break;
3884#endif
3885 case CMD_map: case CMD_noremap:
3886 case CMD_nmap: case CMD_nnoremap:
3887 case CMD_vmap: case CMD_vnoremap:
3888 case CMD_omap: case CMD_onoremap:
3889 case CMD_imap: case CMD_inoremap:
3890 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003891 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003892 case CMD_smap: case CMD_snoremap:
3893 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003895 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 case CMD_unmap:
3897 case CMD_nunmap:
3898 case CMD_vunmap:
3899 case CMD_ounmap:
3900 case CMD_iunmap:
3901 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003902 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003903 case CMD_sunmap:
3904 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003906 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 case CMD_abbreviate: case CMD_noreabbrev:
3908 case CMD_cabbrev: case CMD_cnoreabbrev:
3909 case CMD_iabbrev: case CMD_inoreabbrev:
3910 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003911 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 case CMD_unabbreviate:
3913 case CMD_cunabbrev:
3914 case CMD_iunabbrev:
3915 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003916 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917#ifdef FEAT_MENU
3918 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3919 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3920 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3921 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3922 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3923 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3924 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3925 case CMD_tmenu: case CMD_tunmenu:
3926 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3927 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3928#endif
3929
3930 case CMD_colorscheme:
3931 xp->xp_context = EXPAND_COLORS;
3932 xp->xp_pattern = arg;
3933 break;
3934
3935 case CMD_compiler:
3936 xp->xp_context = EXPAND_COMPILER;
3937 xp->xp_pattern = arg;
3938 break;
3939
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003940 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003941 xp->xp_context = EXPAND_OWNSYNTAX;
3942 xp->xp_pattern = arg;
3943 break;
3944
3945 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003946 xp->xp_context = EXPAND_FILETYPE;
3947 xp->xp_pattern = arg;
3948 break;
3949
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3951 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3952 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003953 p = skiptowhite(arg);
3954 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 {
3956 xp->xp_context = EXPAND_LANGUAGE;
3957 xp->xp_pattern = arg;
3958 }
3959 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003960 {
3961 if ( STRNCMP(arg, "messages", p - arg) == 0
3962 || STRNCMP(arg, "ctype", p - arg) == 0
3963 || STRNCMP(arg, "time", p - arg) == 0)
3964 {
3965 xp->xp_context = EXPAND_LOCALES;
3966 xp->xp_pattern = skipwhite(p);
3967 }
3968 else
3969 xp->xp_context = EXPAND_NOTHING;
3970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 break;
3972#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003973#if defined(FEAT_PROFILE)
3974 case CMD_profile:
3975 set_context_in_profile_cmd(xp, arg);
3976 break;
3977#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003978 case CMD_behave:
3979 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003980 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003981 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003983#if defined(FEAT_CMDHIST)
3984 case CMD_history:
3985 xp->xp_context = EXPAND_HISTORY;
3986 xp->xp_pattern = arg;
3987 break;
3988#endif
3989
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990#endif /* FEAT_CMDL_COMPL */
3991
3992 default:
3993 break;
3994 }
3995 return NULL;
3996}
3997
3998/*
3999 * skip a range specifier of the form: addr [,addr] [;addr] ..
4000 *
4001 * Backslashed delimiters after / or ? will be skipped, and commands will
4002 * not be expanded between /'s and ?'s or after "'".
4003 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004004 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 * Returns the "cmd" pointer advanced to beyond the range.
4006 */
4007 char_u *
4008skip_range(cmd, ctx)
4009 char_u *cmd;
4010 int *ctx; /* pointer to xp_context or NULL */
4011{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004012 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
Bram Moolenaardf177f62005-02-22 08:39:57 +00004014 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
4016 if (*cmd == '\'')
4017 {
4018 if (*++cmd == NUL && ctx != NULL)
4019 *ctx = EXPAND_NOTHING;
4020 }
4021 else if (*cmd == '/' || *cmd == '?')
4022 {
4023 delim = *cmd++;
4024 while (*cmd != NUL && *cmd != delim)
4025 if (*cmd++ == '\\' && *cmd != NUL)
4026 ++cmd;
4027 if (*cmd == NUL && ctx != NULL)
4028 *ctx = EXPAND_NOTHING;
4029 }
4030 if (*cmd != NUL)
4031 ++cmd;
4032 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004033
4034 /* Skip ":" and white space. */
4035 while (*cmd == ':')
4036 cmd = skipwhite(cmd + 1);
4037
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 return cmd;
4039}
4040
4041/*
4042 * get a single EX address
4043 *
4044 * Set ptr to the next character after the part that was interpreted.
4045 * Set ptr to NULL when an error is encountered.
4046 *
4047 * Return MAXLNUM when no Ex address was found.
4048 */
4049 static linenr_T
4050get_address(ptr, skip, to_other_file)
4051 char_u **ptr;
4052 int skip; /* only skip the address, don't use it */
4053 int to_other_file; /* flag: may jump to other file */
4054{
4055 int c;
4056 int i;
4057 long n;
4058 char_u *cmd;
4059 pos_T pos;
4060 pos_T *fp;
4061 linenr_T lnum;
4062
4063 cmd = skipwhite(*ptr);
4064 lnum = MAXLNUM;
4065 do
4066 {
4067 switch (*cmd)
4068 {
4069 case '.': /* '.' - Cursor position */
4070 ++cmd;
4071 lnum = curwin->w_cursor.lnum;
4072 break;
4073
4074 case '$': /* '$' - last line */
4075 ++cmd;
4076 lnum = curbuf->b_ml.ml_line_count;
4077 break;
4078
4079 case '\'': /* ''' - mark */
4080 if (*++cmd == NUL)
4081 {
4082 cmd = NULL;
4083 goto error;
4084 }
4085 if (skip)
4086 ++cmd;
4087 else
4088 {
4089 /* Only accept a mark in another file when it is
4090 * used by itself: ":'M". */
4091 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4092 ++cmd;
4093 if (fp == (pos_T *)-1)
4094 /* Jumped to another file. */
4095 lnum = curwin->w_cursor.lnum;
4096 else
4097 {
4098 if (check_mark(fp) == FAIL)
4099 {
4100 cmd = NULL;
4101 goto error;
4102 }
4103 lnum = fp->lnum;
4104 }
4105 }
4106 break;
4107
4108 case '/':
4109 case '?': /* '/' or '?' - search */
4110 c = *cmd++;
4111 if (skip) /* skip "/pat/" */
4112 {
4113 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4114 if (*cmd == c)
4115 ++cmd;
4116 }
4117 else
4118 {
4119 pos = curwin->w_cursor; /* save curwin->w_cursor */
4120 /*
4121 * When '/' or '?' follows another address, start
4122 * from there.
4123 */
4124 if (lnum != MAXLNUM)
4125 curwin->w_cursor.lnum = lnum;
4126 /*
4127 * Start a forward search at the end of the line.
4128 * Start a backward search at the start of the line.
4129 * This makes sure we never match in the current
4130 * line, and can match anywhere in the
4131 * next/previous line.
4132 */
4133 if (c == '/')
4134 curwin->w_cursor.col = MAXCOL;
4135 else
4136 curwin->w_cursor.col = 0;
4137 searchcmdlen = 0;
4138 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004139 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 {
4141 curwin->w_cursor = pos;
4142 cmd = NULL;
4143 goto error;
4144 }
4145 lnum = curwin->w_cursor.lnum;
4146 curwin->w_cursor = pos;
4147 /* adjust command string pointer */
4148 cmd += searchcmdlen;
4149 }
4150 break;
4151
4152 case '\\': /* "\?", "\/" or "\&", repeat search */
4153 ++cmd;
4154 if (*cmd == '&')
4155 i = RE_SUBST;
4156 else if (*cmd == '?' || *cmd == '/')
4157 i = RE_SEARCH;
4158 else
4159 {
4160 EMSG(_(e_backslash));
4161 cmd = NULL;
4162 goto error;
4163 }
4164
4165 if (!skip)
4166 {
4167 /*
4168 * When search follows another address, start from
4169 * there.
4170 */
4171 if (lnum != MAXLNUM)
4172 pos.lnum = lnum;
4173 else
4174 pos.lnum = curwin->w_cursor.lnum;
4175
4176 /*
4177 * Start the search just like for the above
4178 * do_search().
4179 */
4180 if (*cmd != '?')
4181 pos.col = MAXCOL;
4182 else
4183 pos.col = 0;
4184 if (searchit(curwin, curbuf, &pos,
4185 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004186 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004187 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 lnum = pos.lnum;
4189 else
4190 {
4191 cmd = NULL;
4192 goto error;
4193 }
4194 }
4195 ++cmd;
4196 break;
4197
4198 default:
4199 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4200 lnum = getdigits(&cmd);
4201 }
4202
4203 for (;;)
4204 {
4205 cmd = skipwhite(cmd);
4206 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4207 break;
4208
4209 if (lnum == MAXLNUM)
4210 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4211 if (VIM_ISDIGIT(*cmd))
4212 i = '+'; /* "number" is same as "+number" */
4213 else
4214 i = *cmd++;
4215 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4216 n = 1;
4217 else
4218 n = getdigits(&cmd);
4219 if (i == '-')
4220 lnum -= n;
4221 else
4222 lnum += n;
4223 }
4224 } while (*cmd == '/' || *cmd == '?');
4225
4226error:
4227 *ptr = cmd;
4228 return lnum;
4229}
4230
4231/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004232 * Get flags from an Ex command argument.
4233 */
4234 static void
4235get_flags(eap)
4236 exarg_T *eap;
4237{
4238 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4239 {
4240 if (*eap->arg == 'l')
4241 eap->flags |= EXFLAG_LIST;
4242 else if (*eap->arg == 'p')
4243 eap->flags |= EXFLAG_PRINT;
4244 else
4245 eap->flags |= EXFLAG_NR;
4246 eap->arg = skipwhite(eap->arg + 1);
4247 }
4248}
4249
4250/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 * Function called for command which is Not Implemented. NI!
4252 */
4253 void
4254ex_ni(eap)
4255 exarg_T *eap;
4256{
4257 if (!eap->skip)
4258 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4259}
4260
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004261#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262/*
4263 * Function called for script command which is Not Implemented. NI!
4264 * Skips over ":perl <<EOF" constructs.
4265 */
4266 static void
4267ex_script_ni(eap)
4268 exarg_T *eap;
4269{
4270 if (!eap->skip)
4271 ex_ni(eap);
4272 else
4273 vim_free(script_get(eap, eap->arg));
4274}
4275#endif
4276
4277/*
4278 * Check range in Ex command for validity.
4279 * Return NULL when valid, error message when invalid.
4280 */
4281 static char_u *
4282invalid_range(eap)
4283 exarg_T *eap;
4284{
4285 if ( eap->line1 < 0
4286 || eap->line2 < 0
4287 || eap->line1 > eap->line2
4288 || ((eap->argt & RANGE)
4289 && !(eap->argt & NOTADR)
4290 && eap->line2 > curbuf->b_ml.ml_line_count
4291#ifdef FEAT_DIFF
4292 + (eap->cmdidx == CMD_diffget)
4293#endif
4294 ))
4295 return (char_u *)_(e_invrange);
4296 return NULL;
4297}
4298
4299/*
4300 * Correct the range for zero line number, if required.
4301 */
4302 static void
4303correct_range(eap)
4304 exarg_T *eap;
4305{
4306 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4307 {
4308 if (eap->line1 == 0)
4309 eap->line1 = 1;
4310 if (eap->line2 == 0)
4311 eap->line2 = 1;
4312 }
4313}
4314
Bram Moolenaar748bf032005-02-02 23:04:36 +00004315#ifdef FEAT_QUICKFIX
4316static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4317
4318/*
4319 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4320 * pattern. Otherwise return eap->arg.
4321 */
4322 static char_u *
4323skip_grep_pat(eap)
4324 exarg_T *eap;
4325{
4326 char_u *p = eap->arg;
4327
Bram Moolenaara37420f2006-02-04 22:37:47 +00004328 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4329 || eap->cmdidx == CMD_vimgrepadd
4330 || eap->cmdidx == CMD_lvimgrepadd
4331 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004332 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004333 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004334 if (p == NULL)
4335 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004336 }
4337 return p;
4338}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004339
4340/*
4341 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4342 * in the command line, so that things like % get expanded.
4343 */
4344 static char_u *
4345replace_makeprg(eap, p, cmdlinep)
4346 exarg_T *eap;
4347 char_u *p;
4348 char_u **cmdlinep;
4349{
4350 char_u *new_cmdline;
4351 char_u *program;
4352 char_u *pos;
4353 char_u *ptr;
4354 int len;
4355 int i;
4356
4357 /*
4358 * Don't do it when ":vimgrep" is used for ":grep".
4359 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004360 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4361 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4362 || eap->cmdidx == CMD_grepadd
4363 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004364 && !grep_internal(eap->cmdidx))
4365 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004366 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4367 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004368 {
4369 if (*curbuf->b_p_gp == NUL)
4370 program = p_gp;
4371 else
4372 program = curbuf->b_p_gp;
4373 }
4374 else
4375 {
4376 if (*curbuf->b_p_mp == NUL)
4377 program = p_mp;
4378 else
4379 program = curbuf->b_p_mp;
4380 }
4381
4382 p = skipwhite(p);
4383
4384 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4385 {
4386 /* replace $* by given arguments */
4387 i = 1;
4388 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4389 ++i;
4390 len = (int)STRLEN(p);
4391 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4392 if (new_cmdline == NULL)
4393 return NULL; /* out of memory */
4394 ptr = new_cmdline;
4395 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4396 {
4397 i = (int)(pos - program);
4398 STRNCPY(ptr, program, i);
4399 STRCPY(ptr += i, p);
4400 ptr += len;
4401 program = pos + 2;
4402 }
4403 STRCPY(ptr, program);
4404 }
4405 else
4406 {
4407 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4408 if (new_cmdline == NULL)
4409 return NULL; /* out of memory */
4410 STRCPY(new_cmdline, program);
4411 STRCAT(new_cmdline, " ");
4412 STRCAT(new_cmdline, p);
4413 }
4414 msg_make(p);
4415
4416 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4417 vim_free(*cmdlinep);
4418 *cmdlinep = new_cmdline;
4419 p = new_cmdline;
4420 }
4421 return p;
4422}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004423#endif
4424
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425/*
4426 * Expand file name in Ex command argument.
4427 * Return FAIL for failure, OK otherwise.
4428 */
4429 int
4430expand_filename(eap, cmdlinep, errormsgp)
4431 exarg_T *eap;
4432 char_u **cmdlinep;
4433 char_u **errormsgp;
4434{
4435 int has_wildcards; /* need to expand wildcards */
4436 char_u *repl;
4437 int srclen;
4438 char_u *p;
4439 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004440 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
Bram Moolenaar748bf032005-02-02 23:04:36 +00004442#ifdef FEAT_QUICKFIX
4443 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4444 p = skip_grep_pat(eap);
4445#else
4446 p = eap->arg;
4447#endif
4448
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 /*
4450 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4451 * the file name contains a wildcard it should not cause expanding.
4452 * (it will be expanded anyway if there is a wildcard before replacing).
4453 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004454 has_wildcards = mch_has_wildcard(p);
4455 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004457#ifdef FEAT_EVAL
4458 /* Skip over `=expr`, wildcards in it are not expanded. */
4459 if (p[0] == '`' && p[1] == '=')
4460 {
4461 p += 2;
4462 (void)skip_expr(&p);
4463 if (*p == '`')
4464 ++p;
4465 continue;
4466 }
4467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 /*
4469 * Quick check if this cannot be the start of a special string.
4470 * Also removes backslash before '%', '#' and '<'.
4471 */
4472 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4473 {
4474 ++p;
4475 continue;
4476 }
4477
4478 /*
4479 * Try to find a match at this position.
4480 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004481 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4482 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 if (*errormsgp != NULL) /* error detected */
4484 return FAIL;
4485 if (repl == NULL) /* no match found */
4486 {
4487 p += srclen;
4488 continue;
4489 }
4490
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004491 /* Wildcards won't be expanded below, the replacement is taken
4492 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4493 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4494 {
4495 char_u *l = repl;
4496
4497 repl = expand_env_save(repl);
4498 vim_free(l);
4499 }
4500
Bram Moolenaar63b92542007-03-27 14:57:09 +00004501 /* Need to escape white space et al. with a backslash.
4502 * Don't do this for:
4503 * - replacement that already has been escaped: "##"
4504 * - shell commands (may have to use quotes instead).
4505 * - non-unix systems when there is a single argument (spaces don't
4506 * separate arguments then).
4507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004509 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 && eap->cmdidx != CMD_bang
4511 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004512 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004514 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004516 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517#ifndef UNIX
4518 && !(eap->argt & NOSPC)
4519#endif
4520 )
4521 {
4522 char_u *l;
4523#ifdef BACKSLASH_IN_FILENAME
4524 /* Don't escape a backslash here, because rem_backslash() doesn't
4525 * remove it later. */
4526 static char_u *nobslash = (char_u *)" \t\"|";
4527# define ESCAPE_CHARS nobslash
4528#else
4529# define ESCAPE_CHARS escape_chars
4530#endif
4531
4532 for (l = repl; *l; ++l)
4533 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4534 {
4535 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4536 if (l != NULL)
4537 {
4538 vim_free(repl);
4539 repl = l;
4540 }
4541 break;
4542 }
4543 }
4544
4545 /* For a shell command a '!' must be escaped. */
4546 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004547 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 {
4549 char_u *l;
4550
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004551 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 if (l != NULL)
4553 {
4554 vim_free(repl);
4555 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004556 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (strstr((char *)p_sh, "sh") != NULL)
4558 {
4559 l = vim_strsave_escaped(repl, (char_u *)"!");
4560 if (l != NULL)
4561 {
4562 vim_free(repl);
4563 repl = l;
4564 }
4565 }
4566 }
4567 }
4568
4569 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4570 vim_free(repl);
4571 if (p == NULL)
4572 return FAIL;
4573 }
4574
4575 /*
4576 * One file argument: Expand wildcards.
4577 * Don't do this with ":r !command" or ":w !command".
4578 */
4579 if ((eap->argt & NOSPC) && !eap->usefilter)
4580 {
4581 /*
4582 * May do this twice:
4583 * 1. Replace environment variables.
4584 * 2. Replace any other wildcards, remove backslashes.
4585 */
4586 for (n = 1; n <= 2; ++n)
4587 {
4588 if (n == 2)
4589 {
4590#ifdef UNIX
4591 /*
4592 * Only for Unix we check for more than one file name.
4593 * For other systems spaces are considered to be part
4594 * of the file name.
4595 * Only check here if there is no wildcard, otherwise
4596 * ExpandOne() will check for errors. This allows
4597 * ":e `ls ve*.c`" on Unix.
4598 */
4599 if (!has_wildcards)
4600 for (p = eap->arg; *p; ++p)
4601 {
4602 /* skip escaped characters */
4603 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4604 ++p;
4605 else if (vim_iswhite(*p))
4606 {
4607 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4608 return FAIL;
4609 }
4610 }
4611#endif
4612
4613 /*
4614 * Halve the number of backslashes (this is Vi compatible).
4615 * For Unix and OS/2, when wildcards are expanded, this is
4616 * done by ExpandOne() below.
4617 */
4618#if defined(UNIX) || defined(OS2)
4619 if (!has_wildcards)
4620#endif
4621 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 }
4623
4624 if (has_wildcards)
4625 {
4626 if (n == 1)
4627 {
4628 /*
4629 * First loop: May expand environment variables. This
4630 * can be done much faster with expand_env() than with
4631 * something else (e.g., calling a shell).
4632 * After expanding environment variables, check again
4633 * if there are still wildcards present.
4634 */
4635 if (vim_strchr(eap->arg, '$') != NULL
4636 || vim_strchr(eap->arg, '~') != NULL)
4637 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004638 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004639 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 has_wildcards = mch_has_wildcard(NameBuff);
4641 p = NameBuff;
4642 }
4643 else
4644 p = NULL;
4645 }
4646 else /* n == 2 */
4647 {
4648 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004649 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650
4651 ExpandInit(&xpc);
4652 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004653 if (p_wic)
4654 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004656 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 if (p == NULL)
4658 return FAIL;
4659 }
4660 if (p != NULL)
4661 {
4662 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4663 p, cmdlinep);
4664 if (n == 2) /* p came from ExpandOne() */
4665 vim_free(p);
4666 }
4667 }
4668 }
4669 }
4670 return OK;
4671}
4672
4673/*
4674 * Replace part of the command line, keeping eap->cmd, eap->arg and
4675 * eap->nextcmd correct.
4676 * "src" points to the part that is to be replaced, of length "srclen".
4677 * "repl" is the replacement string.
4678 * Returns a pointer to the character after the replaced string.
4679 * Returns NULL for failure.
4680 */
4681 static char_u *
4682repl_cmdline(eap, src, srclen, repl, cmdlinep)
4683 exarg_T *eap;
4684 char_u *src;
4685 int srclen;
4686 char_u *repl;
4687 char_u **cmdlinep;
4688{
4689 int len;
4690 int i;
4691 char_u *new_cmdline;
4692
4693 /*
4694 * The new command line is build in new_cmdline[].
4695 * First allocate it.
4696 * Careful: a "+cmd" argument may have been NUL terminated.
4697 */
4698 len = (int)STRLEN(repl);
4699 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004700 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4702 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4703 return NULL; /* out of memory! */
4704
4705 /*
4706 * Copy the stuff before the expanded part.
4707 * Copy the expanded stuff.
4708 * Copy what came after the expanded part.
4709 * Copy the next commands, if there are any.
4710 */
4711 i = (int)(src - *cmdlinep); /* length of part before match */
4712 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004713
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 mch_memmove(new_cmdline + i, repl, (size_t)len);
4715 i += len; /* remember the end of the string */
4716 STRCPY(new_cmdline + i, src + srclen);
4717 src = new_cmdline + i; /* remember where to continue */
4718
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004719 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 {
4721 i = (int)STRLEN(new_cmdline) + 1;
4722 STRCPY(new_cmdline + i, eap->nextcmd);
4723 eap->nextcmd = new_cmdline + i;
4724 }
4725 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4726 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4727 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4728 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4729 vim_free(*cmdlinep);
4730 *cmdlinep = new_cmdline;
4731
4732 return src;
4733}
4734
4735/*
4736 * Check for '|' to separate commands and '"' to start comments.
4737 */
4738 void
4739separate_nextcmd(eap)
4740 exarg_T *eap;
4741{
4742 char_u *p;
4743
Bram Moolenaar86b68352004-12-27 21:59:20 +00004744#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004745 p = skip_grep_pat(eap);
4746#else
4747 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004748#endif
4749
4750 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 {
4752 if (*p == Ctrl_V)
4753 {
4754 if (eap->argt & (USECTRLV | XFILE))
4755 ++p; /* skip CTRL-V and next char */
4756 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004757 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004758 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 if (*p == NUL) /* stop at NUL after CTRL-V */
4760 break;
4761 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004762
4763#ifdef FEAT_EVAL
4764 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004765 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004766 {
4767 p += 2;
4768 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004769 }
4770#endif
4771
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 /* Check for '"': start of comment or '|': next command */
4773 /* :@" and :*" do not start a comment!
4774 * :redir @" doesn't either. */
4775 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4776 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4777 || p != eap->arg)
4778 && (eap->cmdidx != CMD_redir
4779 || p != eap->arg + 1 || p[-1] != '@'))
4780 || *p == '|' || *p == '\n')
4781 {
4782 /*
4783 * We remove the '\' before the '|', unless USECTRLV is used
4784 * AND 'b' is present in 'cpoptions'.
4785 */
4786 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4787 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4788 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004789 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 --p;
4791 }
4792 else
4793 {
4794 eap->nextcmd = check_nextcmd(p);
4795 *p = NUL;
4796 break;
4797 }
4798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004800
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4802 del_trailing_spaces(eap->arg);
4803}
4804
4805/*
4806 * get + command from ex argument
4807 */
4808 static char_u *
4809getargcmd(argp)
4810 char_u **argp;
4811{
4812 char_u *arg = *argp;
4813 char_u *command = NULL;
4814
4815 if (*arg == '+') /* +[command] */
4816 {
4817 ++arg;
4818 if (vim_isspace(*arg))
4819 command = dollar_command;
4820 else
4821 {
4822 command = arg;
4823 arg = skip_cmd_arg(command, TRUE);
4824 if (*arg != NUL)
4825 *arg++ = NUL; /* terminate command with NUL */
4826 }
4827
4828 arg = skipwhite(arg); /* skip over spaces */
4829 *argp = arg;
4830 }
4831 return command;
4832}
4833
4834/*
4835 * Find end of "+command" argument. Skip over "\ " and "\\".
4836 */
4837 static char_u *
4838skip_cmd_arg(p, rembs)
4839 char_u *p;
4840 int rembs; /* TRUE to halve the number of backslashes */
4841{
4842 while (*p && !vim_isspace(*p))
4843 {
4844 if (*p == '\\' && p[1] != NUL)
4845 {
4846 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004847 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 else
4849 ++p;
4850 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004851 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
4853 return p;
4854}
4855
4856/*
4857 * Get "++opt=arg" argument.
4858 * Return FAIL or OK.
4859 */
4860 static int
4861getargopt(eap)
4862 exarg_T *eap;
4863{
4864 char_u *arg = eap->arg + 2;
4865 int *pp = NULL;
4866#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004867 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 char_u *p;
4869#endif
4870
4871 /* ":edit ++[no]bin[ary] file" */
4872 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4873 {
4874 if (*arg == 'n')
4875 {
4876 arg += 2;
4877 eap->force_bin = FORCE_NOBIN;
4878 }
4879 else
4880 eap->force_bin = FORCE_BIN;
4881 if (!checkforcmd(&arg, "binary", 3))
4882 return FAIL;
4883 eap->arg = skipwhite(arg);
4884 return OK;
4885 }
4886
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004887 /* ":read ++edit file" */
4888 if (STRNCMP(arg, "edit", 4) == 0)
4889 {
4890 eap->read_edit = TRUE;
4891 eap->arg = skipwhite(arg + 4);
4892 return OK;
4893 }
4894
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 if (STRNCMP(arg, "ff", 2) == 0)
4896 {
4897 arg += 2;
4898 pp = &eap->force_ff;
4899 }
4900 else if (STRNCMP(arg, "fileformat", 10) == 0)
4901 {
4902 arg += 10;
4903 pp = &eap->force_ff;
4904 }
4905#ifdef FEAT_MBYTE
4906 else if (STRNCMP(arg, "enc", 3) == 0)
4907 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004908 if (STRNCMP(arg, "encoding", 8) == 0)
4909 arg += 8;
4910 else
4911 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 pp = &eap->force_enc;
4913 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004914 else if (STRNCMP(arg, "bad", 3) == 0)
4915 {
4916 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004917 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919#endif
4920
4921 if (pp == NULL || *arg != '=')
4922 return FAIL;
4923
4924 ++arg;
4925 *pp = (int)(arg - eap->cmd);
4926 arg = skip_cmd_arg(arg, FALSE);
4927 eap->arg = skipwhite(arg);
4928 *arg = NUL;
4929
4930#ifdef FEAT_MBYTE
4931 if (pp == &eap->force_ff)
4932 {
4933#endif
4934 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4935 return FAIL;
4936#ifdef FEAT_MBYTE
4937 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004938 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 {
4940 /* Make 'fileencoding' lower case. */
4941 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4942 *p = TOLOWER_ASC(*p);
4943 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004944 else
4945 {
4946 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4947 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004948 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004949 if (STRICMP(p, "keep") == 0)
4950 eap->bad_char = BAD_KEEP;
4951 else if (STRICMP(p, "drop") == 0)
4952 eap->bad_char = BAD_DROP;
4953 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4954 eap->bad_char = *p;
4955 else
4956 return FAIL;
4957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958#endif
4959
4960 return OK;
4961}
4962
4963/*
4964 * ":abbreviate" and friends.
4965 */
4966 static void
4967ex_abbreviate(eap)
4968 exarg_T *eap;
4969{
4970 do_exmap(eap, TRUE); /* almost the same as mapping */
4971}
4972
4973/*
4974 * ":map" and friends.
4975 */
4976 static void
4977ex_map(eap)
4978 exarg_T *eap;
4979{
4980 /*
4981 * If we are sourcing .exrc or .vimrc in current directory we
4982 * print the mappings for security reasons.
4983 */
4984 if (secure)
4985 {
4986 secure = 2;
4987 msg_outtrans(eap->cmd);
4988 msg_putchar('\n');
4989 }
4990 do_exmap(eap, FALSE);
4991}
4992
4993/*
4994 * ":unmap" and friends.
4995 */
4996 static void
4997ex_unmap(eap)
4998 exarg_T *eap;
4999{
5000 do_exmap(eap, FALSE);
5001}
5002
5003/*
5004 * ":mapclear" and friends.
5005 */
5006 static void
5007ex_mapclear(eap)
5008 exarg_T *eap;
5009{
5010 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5011}
5012
5013/*
5014 * ":abclear" and friends.
5015 */
5016 static void
5017ex_abclear(eap)
5018 exarg_T *eap;
5019{
5020 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5021}
5022
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005023#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 static void
5025ex_autocmd(eap)
5026 exarg_T *eap;
5027{
5028 /*
5029 * Disallow auto commands from .exrc and .vimrc in current
5030 * directory for security reasons.
5031 */
5032 if (secure)
5033 {
5034 secure = 2;
5035 eap->errmsg = e_curdir;
5036 }
5037 else if (eap->cmdidx == CMD_autocmd)
5038 do_autocmd(eap->arg, eap->forceit);
5039 else
5040 do_augroup(eap->arg, eap->forceit);
5041}
5042
5043/*
5044 * ":doautocmd": Apply the automatic commands to the current buffer.
5045 */
5046 static void
5047ex_doautocmd(eap)
5048 exarg_T *eap;
5049{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005050 char_u *arg = eap->arg;
5051 int call_do_modelines = check_nomodeline(&arg);
5052
5053 (void)do_doautocmd(arg, TRUE);
5054 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5055 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056}
5057#endif
5058
5059#ifdef FEAT_LISTCMDS
5060/*
5061 * :[N]bunload[!] [N] [bufname] unload buffer
5062 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5063 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5064 */
5065 static void
5066ex_bunload(eap)
5067 exarg_T *eap;
5068{
5069 eap->errmsg = do_bufdel(
5070 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5071 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5072 : DOBUF_UNLOAD, eap->arg,
5073 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5074}
5075
5076/*
5077 * :[N]buffer [N] to buffer N
5078 * :[N]sbuffer [N] to buffer N
5079 */
5080 static void
5081ex_buffer(eap)
5082 exarg_T *eap;
5083{
5084 if (*eap->arg)
5085 eap->errmsg = e_trailing;
5086 else
5087 {
5088 if (eap->addr_count == 0) /* default is current buffer */
5089 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5090 else
5091 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5092 }
5093}
5094
5095/*
5096 * :[N]bmodified [N] to next mod. buffer
5097 * :[N]sbmodified [N] to next mod. buffer
5098 */
5099 static void
5100ex_bmodified(eap)
5101 exarg_T *eap;
5102{
5103 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5104}
5105
5106/*
5107 * :[N]bnext [N] to next buffer
5108 * :[N]sbnext [N] split and to next buffer
5109 */
5110 static void
5111ex_bnext(eap)
5112 exarg_T *eap;
5113{
5114 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5115}
5116
5117/*
5118 * :[N]bNext [N] to previous buffer
5119 * :[N]bprevious [N] to previous buffer
5120 * :[N]sbNext [N] split and to previous buffer
5121 * :[N]sbprevious [N] split and to previous buffer
5122 */
5123 static void
5124ex_bprevious(eap)
5125 exarg_T *eap;
5126{
5127 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5128}
5129
5130/*
5131 * :brewind to first buffer
5132 * :bfirst to first buffer
5133 * :sbrewind split and to first buffer
5134 * :sbfirst split and to first buffer
5135 */
5136 static void
5137ex_brewind(eap)
5138 exarg_T *eap;
5139{
5140 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5141}
5142
5143/*
5144 * :blast to last buffer
5145 * :sblast split and to last buffer
5146 */
5147 static void
5148ex_blast(eap)
5149 exarg_T *eap;
5150{
5151 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5152}
5153#endif
5154
5155 int
5156ends_excmd(c)
5157 int c;
5158{
5159 return (c == NUL || c == '|' || c == '"' || c == '\n');
5160}
5161
5162#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5163 || defined(PROTO)
5164/*
5165 * Return the next command, after the first '|' or '\n'.
5166 * Return NULL if not found.
5167 */
5168 char_u *
5169find_nextcmd(p)
5170 char_u *p;
5171{
5172 while (*p != '|' && *p != '\n')
5173 {
5174 if (*p == NUL)
5175 return NULL;
5176 ++p;
5177 }
5178 return (p + 1);
5179}
5180#endif
5181
5182/*
5183 * Check if *p is a separator between Ex commands.
5184 * Return NULL if it isn't, (p + 1) if it is.
5185 */
5186 char_u *
5187check_nextcmd(p)
5188 char_u *p;
5189{
5190 p = skipwhite(p);
5191 if (*p == '|' || *p == '\n')
5192 return (p + 1);
5193 else
5194 return NULL;
5195}
5196
5197/*
5198 * - if there are more files to edit
5199 * - and this is the last window
5200 * - and forceit not used
5201 * - and not repeated twice on a row
5202 * return FAIL and give error message if 'message' TRUE
5203 * return OK otherwise
5204 */
5205 static int
5206check_more(message, forceit)
5207 int message; /* when FALSE check only, no messages */
5208 int forceit;
5209{
5210 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5211
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005212 if (!forceit && only_one_window()
5213 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 {
5215 if (message)
5216 {
5217#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5218 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5219 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005220 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221
5222 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005223 vim_strncpy(buff,
5224 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005225 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005227 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 _("%d more files to edit. Quit anyway?"), n);
5229 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5230 return OK;
5231 return FAIL;
5232 }
5233#endif
5234 if (n == 1)
5235 EMSG(_("E173: 1 more file to edit"));
5236 else
5237 EMSGN(_("E173: %ld more files to edit"), n);
5238 quitmore = 2; /* next try to quit is allowed */
5239 }
5240 return FAIL;
5241 }
5242 return OK;
5243}
5244
5245#ifdef FEAT_CMDL_COMPL
5246/*
5247 * Function given to ExpandGeneric() to obtain the list of command names.
5248 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 char_u *
5250get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005251 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 int idx;
5253{
5254 if (idx >= (int)CMD_SIZE)
5255# ifdef FEAT_USR_CMDS
5256 return get_user_command_name(idx);
5257# else
5258 return NULL;
5259# endif
5260 return cmdnames[idx].cmd_name;
5261}
5262#endif
5263
5264#if defined(FEAT_USR_CMDS) || defined(PROTO)
5265static 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));
5266static void uc_list __ARGS((char_u *name, size_t name_len));
5267static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5268static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5269static 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));
5270
5271 static int
5272uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5273 char_u *name;
5274 size_t name_len;
5275 char_u *rep;
5276 long argt;
5277 long def;
5278 int flags;
5279 int compl;
5280 char_u *compl_arg;
5281 int force;
5282{
5283 ucmd_T *cmd = NULL;
5284 char_u *p;
5285 int i;
5286 int cmp = 1;
5287 char_u *rep_buf = NULL;
5288 garray_T *gap;
5289
Bram Moolenaar9c102382006-05-03 21:26:49 +00005290 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 if (rep_buf == NULL)
5292 {
5293 /* Can't replace termcodes - try using the string as is */
5294 rep_buf = vim_strsave(rep);
5295
5296 /* Give up if out of memory */
5297 if (rep_buf == NULL)
5298 return FAIL;
5299 }
5300
5301 /* get address of growarray: global or in curbuf */
5302 if (flags & UC_BUFFER)
5303 {
5304 gap = &curbuf->b_ucmds;
5305 if (gap->ga_itemsize == 0)
5306 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5307 }
5308 else
5309 gap = &ucmds;
5310
5311 /* Search for the command in the already defined commands. */
5312 for (i = 0; i < gap->ga_len; ++i)
5313 {
5314 size_t len;
5315
5316 cmd = USER_CMD_GA(gap, i);
5317 len = STRLEN(cmd->uc_name);
5318 cmp = STRNCMP(name, cmd->uc_name, name_len);
5319 if (cmp == 0)
5320 {
5321 if (name_len < len)
5322 cmp = -1;
5323 else if (name_len > len)
5324 cmp = 1;
5325 }
5326
5327 if (cmp == 0)
5328 {
5329 if (!force)
5330 {
5331 EMSG(_("E174: Command already exists: add ! to replace it"));
5332 goto fail;
5333 }
5334
5335 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005336 cmd->uc_rep = NULL;
5337#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5338 vim_free(cmd->uc_compl_arg);
5339 cmd->uc_compl_arg = NULL;
5340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 break;
5342 }
5343
5344 /* Stop as soon as we pass the name to add */
5345 if (cmp < 0)
5346 break;
5347 }
5348
5349 /* Extend the array unless we're replacing an existing command */
5350 if (cmp != 0)
5351 {
5352 if (ga_grow(gap, 1) != OK)
5353 goto fail;
5354 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5355 goto fail;
5356
5357 cmd = USER_CMD_GA(gap, i);
5358 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5359
5360 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361
5362 cmd->uc_name = p;
5363 }
5364
5365 cmd->uc_rep = rep_buf;
5366 cmd->uc_argt = argt;
5367 cmd->uc_def = def;
5368 cmd->uc_compl = compl;
5369#ifdef FEAT_EVAL
5370 cmd->uc_scriptID = current_SID;
5371# ifdef FEAT_CMDL_COMPL
5372 cmd->uc_compl_arg = compl_arg;
5373# endif
5374#endif
5375
5376 return OK;
5377
5378fail:
5379 vim_free(rep_buf);
5380#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5381 vim_free(compl_arg);
5382#endif
5383 return FAIL;
5384}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005387#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388/*
5389 * List of names for completion for ":command" with the EXPAND_ flag.
5390 * Must be alphabetical for completion.
5391 */
5392static struct
5393{
5394 int expand;
5395 char *name;
5396} command_complete[] =
5397{
5398 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005399 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005401 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005403 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005404#if defined(FEAT_CSCOPE)
5405 {EXPAND_CSCOPE, "cscope"},
5406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5408 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005409 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410#endif
5411 {EXPAND_DIRECTORIES, "dir"},
5412 {EXPAND_ENV_VARS, "environment"},
5413 {EXPAND_EVENTS, "event"},
5414 {EXPAND_EXPRESSION, "expression"},
5415 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005416 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005417 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 {EXPAND_FUNCTIONS, "function"},
5419 {EXPAND_HELP, "help"},
5420 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005421#if defined(FEAT_CMDHIST)
5422 {EXPAND_HISTORY, "history"},
5423#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005424#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005425 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005426 {EXPAND_LOCALES, "locale"},
5427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 {EXPAND_MAPPINGS, "mapping"},
5429 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005430 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005432 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005433#if defined(FEAT_SIGNS)
5434 {EXPAND_SIGN, "sign"},
5435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 {EXPAND_TAGS, "tag"},
5437 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005438 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 {EXPAND_USER_VARS, "var"},
5440 {0, NULL}
5441};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005444#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 static void
5446uc_list(name, name_len)
5447 char_u *name;
5448 size_t name_len;
5449{
5450 int i, j;
5451 int found = FALSE;
5452 ucmd_T *cmd;
5453 int len;
5454 long a;
5455 garray_T *gap;
5456
5457 gap = &curbuf->b_ucmds;
5458 for (;;)
5459 {
5460 for (i = 0; i < gap->ga_len; ++i)
5461 {
5462 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005463 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464
5465 /* Skip commands which don't match the requested prefix */
5466 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5467 continue;
5468
5469 /* Put out the title first time */
5470 if (!found)
5471 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5472 found = TRUE;
5473 msg_putchar('\n');
5474 if (got_int)
5475 break;
5476
5477 /* Special cases */
5478 msg_putchar(a & BANG ? '!' : ' ');
5479 msg_putchar(a & REGSTR ? '"' : ' ');
5480 msg_putchar(gap != &ucmds ? 'b' : ' ');
5481 msg_putchar(' ');
5482
5483 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5484 len = (int)STRLEN(cmd->uc_name) + 4;
5485
5486 do {
5487 msg_putchar(' ');
5488 ++len;
5489 } while (len < 16);
5490
5491 len = 0;
5492
5493 /* Arguments */
5494 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5495 {
5496 case 0: IObuff[len++] = '0'; break;
5497 case (EXTRA): IObuff[len++] = '*'; break;
5498 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5499 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5500 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5501 }
5502
5503 do {
5504 IObuff[len++] = ' ';
5505 } while (len < 5);
5506
5507 /* Range */
5508 if (a & (RANGE|COUNT))
5509 {
5510 if (a & COUNT)
5511 {
5512 /* -count=N */
5513 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5514 len += (int)STRLEN(IObuff + len);
5515 }
5516 else if (a & DFLALL)
5517 IObuff[len++] = '%';
5518 else if (cmd->uc_def >= 0)
5519 {
5520 /* -range=N */
5521 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5522 len += (int)STRLEN(IObuff + len);
5523 }
5524 else
5525 IObuff[len++] = '.';
5526 }
5527
5528 do {
5529 IObuff[len++] = ' ';
5530 } while (len < 11);
5531
5532 /* Completion */
5533 for (j = 0; command_complete[j].expand != 0; ++j)
5534 if (command_complete[j].expand == cmd->uc_compl)
5535 {
5536 STRCPY(IObuff + len, command_complete[j].name);
5537 len += (int)STRLEN(IObuff + len);
5538 break;
5539 }
5540
5541 do {
5542 IObuff[len++] = ' ';
5543 } while (len < 21);
5544
5545 IObuff[len] = '\0';
5546 msg_outtrans(IObuff);
5547
5548 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005549#ifdef FEAT_EVAL
5550 if (p_verbose > 0)
5551 last_set_msg(cmd->uc_scriptID);
5552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 out_flush();
5554 ui_breakcheck();
5555 if (got_int)
5556 break;
5557 }
5558 if (gap == &ucmds || i < gap->ga_len)
5559 break;
5560 gap = &ucmds;
5561 }
5562
5563 if (!found)
5564 MSG(_("No user-defined commands found"));
5565}
5566
5567 static char_u *
5568uc_fun_cmd()
5569{
5570 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5571 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5572 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5573 0xb9, 0x7f, 0};
5574 int i;
5575
5576 for (i = 0; fcmd[i]; ++i)
5577 IObuff[i] = fcmd[i] - 0x40;
5578 IObuff[i] = 0;
5579 return IObuff;
5580}
5581
5582 static int
5583uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5584 char_u *attr;
5585 size_t len;
5586 long *argt;
5587 long *def;
5588 int *flags;
5589 int *compl;
5590 char_u **compl_arg;
5591{
5592 char_u *p;
5593
5594 if (len == 0)
5595 {
5596 EMSG(_("E175: No attribute specified"));
5597 return FAIL;
5598 }
5599
5600 /* First, try the simple attributes (no arguments) */
5601 if (STRNICMP(attr, "bang", len) == 0)
5602 *argt |= BANG;
5603 else if (STRNICMP(attr, "buffer", len) == 0)
5604 *flags |= UC_BUFFER;
5605 else if (STRNICMP(attr, "register", len) == 0)
5606 *argt |= REGSTR;
5607 else if (STRNICMP(attr, "bar", len) == 0)
5608 *argt |= TRLBAR;
5609 else
5610 {
5611 int i;
5612 char_u *val = NULL;
5613 size_t vallen = 0;
5614 size_t attrlen = len;
5615
5616 /* Look for the attribute name - which is the part before any '=' */
5617 for (i = 0; i < (int)len; ++i)
5618 {
5619 if (attr[i] == '=')
5620 {
5621 val = &attr[i + 1];
5622 vallen = len - i - 1;
5623 attrlen = i;
5624 break;
5625 }
5626 }
5627
5628 if (STRNICMP(attr, "nargs", attrlen) == 0)
5629 {
5630 if (vallen == 1)
5631 {
5632 if (*val == '0')
5633 /* Do nothing - this is the default */;
5634 else if (*val == '1')
5635 *argt |= (EXTRA | NOSPC | NEEDARG);
5636 else if (*val == '*')
5637 *argt |= EXTRA;
5638 else if (*val == '?')
5639 *argt |= (EXTRA | NOSPC);
5640 else if (*val == '+')
5641 *argt |= (EXTRA | NEEDARG);
5642 else
5643 goto wrong_nargs;
5644 }
5645 else
5646 {
5647wrong_nargs:
5648 EMSG(_("E176: Invalid number of arguments"));
5649 return FAIL;
5650 }
5651 }
5652 else if (STRNICMP(attr, "range", attrlen) == 0)
5653 {
5654 *argt |= RANGE;
5655 if (vallen == 1 && *val == '%')
5656 *argt |= DFLALL;
5657 else if (val != NULL)
5658 {
5659 p = val;
5660 if (*def >= 0)
5661 {
5662two_count:
5663 EMSG(_("E177: Count cannot be specified twice"));
5664 return FAIL;
5665 }
5666
5667 *def = getdigits(&p);
5668 *argt |= (ZEROR | NOTADR);
5669
5670 if (p != val + vallen || vallen == 0)
5671 {
5672invalid_count:
5673 EMSG(_("E178: Invalid default value for count"));
5674 return FAIL;
5675 }
5676 }
5677 }
5678 else if (STRNICMP(attr, "count", attrlen) == 0)
5679 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005680 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681
5682 if (val != NULL)
5683 {
5684 p = val;
5685 if (*def >= 0)
5686 goto two_count;
5687
5688 *def = getdigits(&p);
5689
5690 if (p != val + vallen)
5691 goto invalid_count;
5692 }
5693
5694 if (*def < 0)
5695 *def = 0;
5696 }
5697 else if (STRNICMP(attr, "complete", attrlen) == 0)
5698 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 if (val == NULL)
5700 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005701 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 return FAIL;
5703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005705 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5706 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 }
5709 else
5710 {
5711 char_u ch = attr[len];
5712 attr[len] = '\0';
5713 EMSG2(_("E181: Invalid attribute: %s"), attr);
5714 attr[len] = ch;
5715 return FAIL;
5716 }
5717 }
5718
5719 return OK;
5720}
5721
Bram Moolenaara850a712009-01-28 14:42:59 +00005722/*
5723 * ":command ..."
5724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 static void
5726ex_command(eap)
5727 exarg_T *eap;
5728{
5729 char_u *name;
5730 char_u *end;
5731 char_u *p;
5732 long argt = 0;
5733 long def = -1;
5734 int flags = 0;
5735 int compl = EXPAND_NOTHING;
5736 char_u *compl_arg = NULL;
5737 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005738 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739
5740 p = eap->arg;
5741
5742 /* Check for attributes */
5743 while (*p == '-')
5744 {
5745 ++p;
5746 end = skiptowhite(p);
5747 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5748 == FAIL)
5749 return;
5750 p = skipwhite(end);
5751 }
5752
5753 /* Get the name (if any) and skip to the following argument */
5754 name = p;
5755 if (ASCII_ISALPHA(*p))
5756 while (ASCII_ISALNUM(*p))
5757 ++p;
5758 if (!ends_excmd(*p) && !vim_iswhite(*p))
5759 {
5760 EMSG(_("E182: Invalid command name"));
5761 return;
5762 }
5763 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005764 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
5766 /* If there is nothing after the name, and no attributes were specified,
5767 * we are listing commands
5768 */
5769 p = skipwhite(end);
5770 if (!has_attr && ends_excmd(*p))
5771 {
5772 uc_list(name, end - name);
5773 }
5774 else if (!ASCII_ISUPPER(*name))
5775 {
5776 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5777 return;
5778 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005779 else if ((name_len == 1 && *name == 'X')
5780 || (name_len <= 4
5781 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5782 {
5783 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5784 return;
5785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 else
5787 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5788 eap->forceit);
5789}
5790
5791/*
5792 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 * Clear all user commands, global and for current buffer.
5794 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005795 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005797 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798{
5799 uc_clear(&ucmds);
5800 uc_clear(&curbuf->b_ucmds);
5801}
5802
5803/*
5804 * Clear all user commands for "gap".
5805 */
5806 void
5807uc_clear(gap)
5808 garray_T *gap;
5809{
5810 int i;
5811 ucmd_T *cmd;
5812
5813 for (i = 0; i < gap->ga_len; ++i)
5814 {
5815 cmd = USER_CMD_GA(gap, i);
5816 vim_free(cmd->uc_name);
5817 vim_free(cmd->uc_rep);
5818# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5819 vim_free(cmd->uc_compl_arg);
5820# endif
5821 }
5822 ga_clear(gap);
5823}
5824
5825 static void
5826ex_delcommand(eap)
5827 exarg_T *eap;
5828{
5829 int i = 0;
5830 ucmd_T *cmd = NULL;
5831 int cmp = -1;
5832 garray_T *gap;
5833
5834 gap = &curbuf->b_ucmds;
5835 for (;;)
5836 {
5837 for (i = 0; i < gap->ga_len; ++i)
5838 {
5839 cmd = USER_CMD_GA(gap, i);
5840 cmp = STRCMP(eap->arg, cmd->uc_name);
5841 if (cmp <= 0)
5842 break;
5843 }
5844 if (gap == &ucmds || cmp == 0)
5845 break;
5846 gap = &ucmds;
5847 }
5848
5849 if (cmp != 0)
5850 {
5851 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5852 return;
5853 }
5854
5855 vim_free(cmd->uc_name);
5856 vim_free(cmd->uc_rep);
5857# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5858 vim_free(cmd->uc_compl_arg);
5859# endif
5860
5861 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862
5863 if (i < gap->ga_len)
5864 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5865}
5866
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005867/*
5868 * split and quote args for <f-args>
5869 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 static char_u *
5871uc_split_args(arg, lenp)
5872 char_u *arg;
5873 size_t *lenp;
5874{
5875 char_u *buf;
5876 char_u *p;
5877 char_u *q;
5878 int len;
5879
5880 /* Precalculate length */
5881 p = arg;
5882 len = 2; /* Initial and final quotes */
5883
5884 while (*p)
5885 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005886 if (p[0] == '\\' && p[1] == '\\')
5887 {
5888 len += 2;
5889 p += 2;
5890 }
5891 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 {
5893 len += 1;
5894 p += 2;
5895 }
5896 else if (*p == '\\' || *p == '"')
5897 {
5898 len += 2;
5899 p += 1;
5900 }
5901 else if (vim_iswhite(*p))
5902 {
5903 p = skipwhite(p);
5904 if (*p == NUL)
5905 break;
5906 len += 3; /* "," */
5907 }
5908 else
5909 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005910#ifdef FEAT_MBYTE
5911 int charlen = (*mb_ptr2len)(p);
5912 len += charlen;
5913 p += charlen;
5914#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 ++len;
5916 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 }
5919 }
5920
5921 buf = alloc(len + 1);
5922 if (buf == NULL)
5923 {
5924 *lenp = 0;
5925 return buf;
5926 }
5927
5928 p = arg;
5929 q = buf;
5930 *q++ = '"';
5931 while (*p)
5932 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005933 if (p[0] == '\\' && p[1] == '\\')
5934 {
5935 *q++ = '\\';
5936 *q++ = '\\';
5937 p += 2;
5938 }
5939 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 {
5941 *q++ = p[1];
5942 p += 2;
5943 }
5944 else if (*p == '\\' || *p == '"')
5945 {
5946 *q++ = '\\';
5947 *q++ = *p++;
5948 }
5949 else if (vim_iswhite(*p))
5950 {
5951 p = skipwhite(p);
5952 if (*p == NUL)
5953 break;
5954 *q++ = '"';
5955 *q++ = ',';
5956 *q++ = '"';
5957 }
5958 else
5959 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005960 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 }
5962 }
5963 *q++ = '"';
5964 *q = 0;
5965
5966 *lenp = len;
5967 return buf;
5968}
5969
5970/*
5971 * Check for a <> code in a user command.
5972 * "code" points to the '<'. "len" the length of the <> (inclusive).
5973 * "buf" is where the result is to be added.
5974 * "split_buf" points to a buffer used for splitting, caller should free it.
5975 * "split_len" is the length of what "split_buf" contains.
5976 * Returns the length of the replacement, which has been added to "buf".
5977 * Returns -1 if there was no match, and only the "<" has been copied.
5978 */
5979 static size_t
5980uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5981 char_u *code;
5982 size_t len;
5983 char_u *buf;
5984 ucmd_T *cmd; /* the user command we're expanding */
5985 exarg_T *eap; /* ex arguments */
5986 char_u **split_buf;
5987 size_t *split_len;
5988{
5989 size_t result = 0;
5990 char_u *p = code + 1;
5991 size_t l = len - 2;
5992 int quote = 0;
5993 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5994 ct_LT, ct_NONE } type = ct_NONE;
5995
5996 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5997 {
5998 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5999 p += 2;
6000 l -= 2;
6001 }
6002
Bram Moolenaar371d5402006-03-20 21:47:49 +00006003 ++l;
6004 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006006 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006008 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006010 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006012 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006014 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006016 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006018 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 type = ct_REGISTER;
6020
6021 switch (type)
6022 {
6023 case ct_ARGS:
6024 /* Simple case first */
6025 if (*eap->arg == NUL)
6026 {
6027 if (quote == 1)
6028 {
6029 result = 2;
6030 if (buf != NULL)
6031 STRCPY(buf, "''");
6032 }
6033 else
6034 result = 0;
6035 break;
6036 }
6037
6038 /* When specified there is a single argument don't split it.
6039 * Works for ":Cmd %" when % is "a b c". */
6040 if ((eap->argt & NOSPC) && quote == 2)
6041 quote = 1;
6042
6043 switch (quote)
6044 {
6045 case 0: /* No quoting, no splitting */
6046 result = STRLEN(eap->arg);
6047 if (buf != NULL)
6048 STRCPY(buf, eap->arg);
6049 break;
6050 case 1: /* Quote, but don't split */
6051 result = STRLEN(eap->arg) + 2;
6052 for (p = eap->arg; *p; ++p)
6053 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006054#ifdef FEAT_MBYTE
6055 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6056 /* DBCS can contain \ in a trail byte, skip the
6057 * double-byte character. */
6058 ++p;
6059 else
6060#endif
6061 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 ++result;
6063 }
6064
6065 if (buf != NULL)
6066 {
6067 *buf++ = '"';
6068 for (p = eap->arg; *p; ++p)
6069 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006070#ifdef FEAT_MBYTE
6071 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6072 /* DBCS can contain \ in a trail byte, copy the
6073 * double-byte character to avoid escaping. */
6074 *buf++ = *p++;
6075 else
6076#endif
6077 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 *buf++ = '\\';
6079 *buf++ = *p;
6080 }
6081 *buf = '"';
6082 }
6083
6084 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006085 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 /* This is hard, so only do it once, and cache the result */
6087 if (*split_buf == NULL)
6088 *split_buf = uc_split_args(eap->arg, split_len);
6089
6090 result = *split_len;
6091 if (buf != NULL && result != 0)
6092 STRCPY(buf, *split_buf);
6093
6094 break;
6095 }
6096 break;
6097
6098 case ct_BANG:
6099 result = eap->forceit ? 1 : 0;
6100 if (quote)
6101 result += 2;
6102 if (buf != NULL)
6103 {
6104 if (quote)
6105 *buf++ = '"';
6106 if (eap->forceit)
6107 *buf++ = '!';
6108 if (quote)
6109 *buf = '"';
6110 }
6111 break;
6112
6113 case ct_LINE1:
6114 case ct_LINE2:
6115 case ct_COUNT:
6116 {
6117 char num_buf[20];
6118 long num = (type == ct_LINE1) ? eap->line1 :
6119 (type == ct_LINE2) ? eap->line2 :
6120 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6121 size_t num_len;
6122
6123 sprintf(num_buf, "%ld", num);
6124 num_len = STRLEN(num_buf);
6125 result = num_len;
6126
6127 if (quote)
6128 result += 2;
6129
6130 if (buf != NULL)
6131 {
6132 if (quote)
6133 *buf++ = '"';
6134 STRCPY(buf, num_buf);
6135 buf += num_len;
6136 if (quote)
6137 *buf = '"';
6138 }
6139
6140 break;
6141 }
6142
6143 case ct_REGISTER:
6144 result = eap->regname ? 1 : 0;
6145 if (quote)
6146 result += 2;
6147 if (buf != NULL)
6148 {
6149 if (quote)
6150 *buf++ = '\'';
6151 if (eap->regname)
6152 *buf++ = eap->regname;
6153 if (quote)
6154 *buf = '\'';
6155 }
6156 break;
6157
6158 case ct_LT:
6159 result = 1;
6160 if (buf != NULL)
6161 *buf = '<';
6162 break;
6163
6164 default:
6165 /* Not recognized: just copy the '<' and return -1. */
6166 result = (size_t)-1;
6167 if (buf != NULL)
6168 *buf = '<';
6169 break;
6170 }
6171
6172 return result;
6173}
6174
6175 static void
6176do_ucmd(eap)
6177 exarg_T *eap;
6178{
6179 char_u *buf;
6180 char_u *p;
6181 char_u *q;
6182
6183 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006184 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006185 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 size_t len, totlen;
6187
6188 size_t split_len = 0;
6189 char_u *split_buf = NULL;
6190 ucmd_T *cmd;
6191#ifdef FEAT_EVAL
6192 scid_T save_current_SID = current_SID;
6193#endif
6194
6195 if (eap->cmdidx == CMD_USER)
6196 cmd = USER_CMD(eap->useridx);
6197 else
6198 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6199
6200 /*
6201 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006202 * First round: "buf" is NULL, compute length, allocate "buf".
6203 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 */
6205 buf = NULL;
6206 for (;;)
6207 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006208 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006209 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006211
6212 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006214 start = vim_strchr(p, '<');
6215 if (start != NULL)
6216 end = vim_strchr(start + 1, '>');
6217 if (buf != NULL)
6218 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006219 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6220 ;
6221 if (*ksp == K_SPECIAL
6222 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006223 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6224# ifdef FEAT_GUI
6225 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6226# endif
6227 ))
6228 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006229 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006230 * KS_SPECIAL KE_FILLER, like for mappings, but
6231 * do_cmdline() doesn't handle that, so convert it back.
6232 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6233 len = ksp - p;
6234 if (len > 0)
6235 {
6236 mch_memmove(q, p, len);
6237 q += len;
6238 }
6239 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6240 p = ksp + 3;
6241 continue;
6242 }
6243 }
6244
6245 /* break if there no <item> is found */
6246 if (start == NULL || end == NULL)
6247 break;
6248
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 /* Include the '>' */
6250 ++end;
6251
6252 /* Take everything up to the '<' */
6253 len = start - p;
6254 if (buf == NULL)
6255 totlen += len;
6256 else
6257 {
6258 mch_memmove(q, p, len);
6259 q += len;
6260 }
6261
6262 len = uc_check_code(start, end - start, q, cmd, eap,
6263 &split_buf, &split_len);
6264 if (len == (size_t)-1)
6265 {
6266 /* no match, continue after '<' */
6267 p = start + 1;
6268 len = 1;
6269 }
6270 else
6271 p = end;
6272 if (buf == NULL)
6273 totlen += len;
6274 else
6275 q += len;
6276 }
6277 if (buf != NULL) /* second time here, finished */
6278 {
6279 STRCPY(q, p);
6280 break;
6281 }
6282
6283 totlen += STRLEN(p); /* Add on the trailing characters */
6284 buf = alloc((unsigned)(totlen + 1));
6285 if (buf == NULL)
6286 {
6287 vim_free(split_buf);
6288 return;
6289 }
6290 }
6291
6292#ifdef FEAT_EVAL
6293 current_SID = cmd->uc_scriptID;
6294#endif
6295 (void)do_cmdline(buf, eap->getline, eap->cookie,
6296 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6297#ifdef FEAT_EVAL
6298 current_SID = save_current_SID;
6299#endif
6300 vim_free(buf);
6301 vim_free(split_buf);
6302}
6303
6304# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6305 static char_u *
6306get_user_command_name(idx)
6307 int idx;
6308{
6309 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6310}
6311
6312/*
6313 * Function given to ExpandGeneric() to obtain the list of user command names.
6314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 char_u *
6316get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006317 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 int idx;
6319{
6320 if (idx < curbuf->b_ucmds.ga_len)
6321 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6322 idx -= curbuf->b_ucmds.ga_len;
6323 if (idx < ucmds.ga_len)
6324 return USER_CMD(idx)->uc_name;
6325 return NULL;
6326}
6327
6328/*
6329 * Function given to ExpandGeneric() to obtain the list of user command
6330 * attributes.
6331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 char_u *
6333get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006334 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 int idx;
6336{
6337 static char *user_cmd_flags[] =
6338 {"bang", "bar", "buffer", "complete", "count",
6339 "nargs", "range", "register"};
6340
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006341 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 return NULL;
6343 return (char_u *)user_cmd_flags[idx];
6344}
6345
6346/*
6347 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 char_u *
6350get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006351 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 int idx;
6353{
6354 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6355
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006356 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 return NULL;
6358 return (char_u *)user_cmd_nargs[idx];
6359}
6360
6361/*
6362 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 char_u *
6365get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006366 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 int idx;
6368{
6369 return (char_u *)command_complete[idx].name;
6370}
6371# endif /* FEAT_CMDL_COMPL */
6372
6373#endif /* FEAT_USR_CMDS */
6374
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006375#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6376/*
6377 * Parse a completion argument "value[vallen]".
6378 * The detected completion goes in "*complp", argument type in "*argt".
6379 * When there is an argument, for function and user defined completion, it's
6380 * copied to allocated memory and stored in "*compl_arg".
6381 * Returns FAIL if something is wrong.
6382 */
6383 int
6384parse_compl_arg(value, vallen, complp, argt, compl_arg)
6385 char_u *value;
6386 int vallen;
6387 int *complp;
6388 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006389 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006390{
6391 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006392# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006393 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006394# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006395 int i;
6396 int valend = vallen;
6397
6398 /* Look for any argument part - which is the part after any ',' */
6399 for (i = 0; i < vallen; ++i)
6400 {
6401 if (value[i] == ',')
6402 {
6403 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006404# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006405 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006406# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006407 valend = i;
6408 break;
6409 }
6410 }
6411
6412 for (i = 0; command_complete[i].expand != 0; ++i)
6413 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006414 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006415 && STRNCMP(value, command_complete[i].name, valend) == 0)
6416 {
6417 *complp = command_complete[i].expand;
6418 if (command_complete[i].expand == EXPAND_BUFFERS)
6419 *argt |= BUFNAME;
6420 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6421 || command_complete[i].expand == EXPAND_FILES)
6422 *argt |= XFILE;
6423 break;
6424 }
6425 }
6426
6427 if (command_complete[i].expand == 0)
6428 {
6429 EMSG2(_("E180: Invalid complete value: %s"), value);
6430 return FAIL;
6431 }
6432
6433# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6434 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6435 && arg != NULL)
6436# else
6437 if (arg != NULL)
6438# endif
6439 {
6440 EMSG(_("E468: Completion argument only allowed for custom completion"));
6441 return FAIL;
6442 }
6443
6444# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6445 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6446 && arg == NULL)
6447 {
6448 EMSG(_("E467: Custom completion requires a function argument"));
6449 return FAIL;
6450 }
6451
6452 if (arg != NULL)
6453 *compl_arg = vim_strnsave(arg, (int)arglen);
6454# endif
6455 return OK;
6456}
6457#endif
6458
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 static void
6460ex_colorscheme(eap)
6461 exarg_T *eap;
6462{
Bram Moolenaare6850792010-05-14 15:28:44 +02006463 if (*eap->arg == NUL)
6464 {
6465#ifdef FEAT_EVAL
6466 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6467 char_u *p = NULL;
6468
6469 if (expr != NULL)
6470 {
6471 ++emsg_off;
6472 p = eval_to_string(expr, NULL, FALSE);
6473 --emsg_off;
6474 vim_free(expr);
6475 }
6476 if (p != NULL)
6477 {
6478 MSG(p);
6479 vim_free(p);
6480 }
6481 else
6482 MSG("default");
6483#else
6484 MSG(_("unknown"));
6485#endif
6486 }
6487 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006488 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489}
6490
6491 static void
6492ex_highlight(eap)
6493 exarg_T *eap;
6494{
6495 if (*eap->arg == NUL && eap->cmd[2] == '!')
6496 MSG(_("Greetings, Vim user!"));
6497 do_highlight(eap->arg, eap->forceit, FALSE);
6498}
6499
6500
6501/*
6502 * Call this function if we thought we were going to exit, but we won't
6503 * (because of an error). May need to restore the terminal mode.
6504 */
6505 void
6506not_exiting()
6507{
6508 exiting = FALSE;
6509 settmode(TMODE_RAW);
6510}
6511
6512/*
6513 * ":quit": quit current window, quit Vim if closed the last window.
6514 */
6515 static void
6516ex_quit(eap)
6517 exarg_T *eap;
6518{
6519#ifdef FEAT_CMDWIN
6520 if (cmdwin_type != 0)
6521 {
6522 cmdwin_result = Ctrl_C;
6523 return;
6524 }
6525#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006526 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006527 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006528 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006529 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006530 return;
6531 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006532#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006533 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006534 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006535 * being closed (can only happen in autocommands). */
6536 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006537 return;
6538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539
6540#ifdef FEAT_NETBEANS_INTG
6541 netbeansForcedQuit = eap->forceit;
6542#endif
6543
6544 /*
6545 * If there are more files or windows we won't exit.
6546 */
6547 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6548 exiting = TRUE;
6549 if ((!P_HID(curbuf)
6550 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6551 || check_more(TRUE, eap->forceit) == FAIL
6552 || (only_one_window() && check_changed_any(eap->forceit)))
6553 {
6554 not_exiting();
6555 }
6556 else
6557 {
6558#ifdef FEAT_WINDOWS
6559 if (only_one_window()) /* quit last window */
6560#endif
6561 getout(0);
6562#ifdef FEAT_WINDOWS
6563# ifdef FEAT_GUI
6564 need_mouse_correct = TRUE;
6565# endif
6566 /* close window; may free buffer */
6567 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6568#endif
6569 }
6570}
6571
6572/*
6573 * ":cquit".
6574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 static void
6576ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006577 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578{
6579 getout(1); /* this does not always pass on the exit code to the Manx
6580 compiler. why? */
6581}
6582
6583/*
6584 * ":qall": try to quit all windows
6585 */
6586 static void
6587ex_quit_all(eap)
6588 exarg_T *eap;
6589{
6590# ifdef FEAT_CMDWIN
6591 if (cmdwin_type != 0)
6592 {
6593 if (eap->forceit)
6594 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6595 else
6596 cmdwin_result = K_XF2;
6597 return;
6598 }
6599# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006600
6601 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006602 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006603 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006604 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006605 return;
6606 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006607#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006608 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6609 /* Refuse to quit when locked or when the buffer in the last window is
6610 * being closed (can only happen in autocommands). */
6611 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006612 return;
6613#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006614
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 exiting = TRUE;
6616 if (eap->forceit || !check_changed_any(FALSE))
6617 getout(0);
6618 not_exiting();
6619}
6620
Bram Moolenaard9967712006-03-11 21:18:15 +00006621#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622/*
6623 * ":close": close current window, unless it is the last one
6624 */
6625 static void
6626ex_close(eap)
6627 exarg_T *eap;
6628{
6629# ifdef FEAT_CMDWIN
6630 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006631 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 else
6633# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006634 if (!text_locked()
6635#ifdef FEAT_AUTOCMD
6636 && !curbuf_locked()
6637#endif
6638 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006639 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640}
6641
Bram Moolenaard9967712006-03-11 21:18:15 +00006642# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006643/*
6644 * ":pclose": Close any preview window.
6645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006647ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006649{
6650 win_T *win;
6651
6652 for (win = firstwin; win != NULL; win = win->w_next)
6653 if (win->w_p_pvw)
6654 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006655 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006656 break;
6657 }
6658}
Bram Moolenaard9967712006-03-11 21:18:15 +00006659# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006660
Bram Moolenaarf740b292006-02-16 22:11:02 +00006661/*
6662 * Close window "win" and take care of handling closing the last window for a
6663 * modified buffer.
6664 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006665 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006666ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006667 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006669 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670{
6671 int need_hide;
6672 buf_T *buf = win->w_buffer;
6673
6674 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006675 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006677# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 if ((p_confirm || cmdmod.confirm) && p_write)
6679 {
6680 dialog_changed(buf, FALSE);
6681 if (buf_valid(buf) && bufIsChanged(buf))
6682 return;
6683 need_hide = FALSE;
6684 }
6685 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006686# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 {
6688 EMSG(_(e_nowrtmsg));
6689 return;
6690 }
6691 }
6692
Bram Moolenaard9967712006-03-11 21:18:15 +00006693# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006695# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006696
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006698 if (tp == NULL)
6699 win_close(win, !need_hide && !P_HID(buf));
6700 else
6701 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702}
6703
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006705 * ":tabclose": close current tab page, unless it is the last one.
6706 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 */
6708 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006709ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 exarg_T *eap;
6711{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006712 tabpage_T *tp;
6713
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006714# ifdef FEAT_CMDWIN
6715 if (cmdwin_type != 0)
6716 cmdwin_result = K_IGNORE;
6717 else
6718# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006719 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006720 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006721 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006723 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006724 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006725 tp = find_tabpage((int)eap->line2);
6726 if (tp == NULL)
6727 {
6728 beep_flush();
6729 return;
6730 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006731 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006732 {
6733 tabpage_close_other(tp, eap->forceit);
6734 return;
6735 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006736 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006737 if (!text_locked()
6738#ifdef FEAT_AUTOCMD
6739 && !curbuf_locked()
6740#endif
6741 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006742 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006744}
6745
6746/*
6747 * ":tabonly": close all tab pages except the current one
6748 */
6749 static void
6750ex_tabonly(eap)
6751 exarg_T *eap;
6752{
6753 tabpage_T *tp;
6754 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006755
6756# ifdef FEAT_CMDWIN
6757 if (cmdwin_type != 0)
6758 cmdwin_result = K_IGNORE;
6759 else
6760# endif
6761 if (first_tabpage->tp_next == NULL)
6762 MSG(_("Already only one tab page"));
6763 else
6764 {
6765 /* Repeat this up to a 1000 times, because autocommands may mess
6766 * up the lists. */
6767 for (done = 0; done < 1000; ++done)
6768 {
6769 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6770 if (tp->tp_topframe != topframe)
6771 {
6772 tabpage_close_other(tp, eap->forceit);
6773 /* if we failed to close it quit */
6774 if (valid_tabpage(tp))
6775 done = 1000;
6776 /* start over, "tp" is now invalid */
6777 break;
6778 }
6779 if (first_tabpage->tp_next == NULL)
6780 break;
6781 }
6782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784
6785/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006786 * Close the current tab page.
6787 */
6788 void
6789tabpage_close(forceit)
6790 int forceit;
6791{
6792 /* First close all the windows but the current one. If that worked then
6793 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006794 if (lastwin != firstwin)
6795 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006796 if (lastwin == firstwin)
6797 ex_win_close(forceit, curwin, NULL);
6798# ifdef FEAT_GUI
6799 need_mouse_correct = TRUE;
6800# endif
6801}
6802
6803/*
6804 * Close tab page "tp", which is not the current tab page.
6805 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006806 * Also takes care of the tab pages line disappearing when closing the
6807 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006808 */
6809 void
6810tabpage_close_other(tp, forceit)
6811 tabpage_T *tp;
6812 int forceit;
6813{
6814 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006815 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006816 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006817
6818 /* Limit to 1000 windows, autocommands may add a window while we close
6819 * one. OK, so I'm paranoid... */
6820 while (++done < 1000)
6821 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006822 wp = tp->tp_firstwin;
6823 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006824
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006825 /* Autocommands may delete the tab page under our fingers and we may
6826 * fail to close a window with a modified buffer. */
6827 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006828 break;
6829 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006830
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006831 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006832 if (h != tabline_height())
6833 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006834}
6835
6836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 * ":only".
6838 */
6839 static void
6840ex_only(eap)
6841 exarg_T *eap;
6842{
6843# ifdef FEAT_GUI
6844 need_mouse_correct = TRUE;
6845# endif
6846 close_others(TRUE, eap->forceit);
6847}
6848
6849/*
6850 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006851 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006853 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854ex_all(eap)
6855 exarg_T *eap;
6856{
6857 if (eap->addr_count == 0)
6858 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006859 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860}
6861#endif /* FEAT_WINDOWS */
6862
6863 static void
6864ex_hide(eap)
6865 exarg_T *eap;
6866{
6867 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6868 eap->errmsg = e_invarg;
6869 else
6870 {
6871 /* ":hide" or ":hide | cmd": hide current window */
6872 eap->nextcmd = check_nextcmd(eap->arg);
6873#ifdef FEAT_WINDOWS
6874 if (!eap->skip)
6875 {
6876# ifdef FEAT_GUI
6877 need_mouse_correct = TRUE;
6878# endif
6879 win_close(curwin, FALSE); /* don't free buffer */
6880 }
6881#endif
6882 }
6883}
6884
6885/*
6886 * ":stop" and ":suspend": Suspend Vim.
6887 */
6888 static void
6889ex_stop(eap)
6890 exarg_T *eap;
6891{
6892 /*
6893 * Disallow suspending for "rvim".
6894 */
6895 if (!check_restricted()
6896#ifdef WIN3264
6897 /*
6898 * Check if external commands are allowed now.
6899 */
6900 && can_end_termcap_mode(TRUE)
6901#endif
6902 )
6903 {
6904 if (!eap->forceit)
6905 autowrite_all();
6906 windgoto((int)Rows - 1, 0);
6907 out_char('\n');
6908 out_flush();
6909 stoptermcap();
6910 out_flush(); /* needed for SUN to restore xterm buffer */
6911#ifdef FEAT_TITLE
6912 mch_restore_title(3); /* restore window titles */
6913#endif
6914 ui_suspend(); /* call machine specific function */
6915#ifdef FEAT_TITLE
6916 maketitle();
6917 resettitle(); /* force updating the title */
6918#endif
6919 starttermcap();
6920 scroll_start(); /* scroll screen before redrawing */
6921 redraw_later_clear();
6922 shell_resized(); /* may have resized window */
6923 }
6924}
6925
6926/*
6927 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6928 */
6929 static void
6930ex_exit(eap)
6931 exarg_T *eap;
6932{
6933#ifdef FEAT_CMDWIN
6934 if (cmdwin_type != 0)
6935 {
6936 cmdwin_result = Ctrl_C;
6937 return;
6938 }
6939#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006940 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006941 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006942 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006943 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006944 return;
6945 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006946#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006947 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6948 /* Refuse to quit when locked or when the buffer in the last window is
6949 * being closed (can only happen in autocommands). */
6950 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006951 return;
6952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
6954 /*
6955 * if more files or windows we won't exit
6956 */
6957 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6958 exiting = TRUE;
6959 if ( ((eap->cmdidx == CMD_wq
6960 || curbufIsChanged())
6961 && do_write(eap) == FAIL)
6962 || check_more(TRUE, eap->forceit) == FAIL
6963 || (only_one_window() && check_changed_any(eap->forceit)))
6964 {
6965 not_exiting();
6966 }
6967 else
6968 {
6969#ifdef FEAT_WINDOWS
6970 if (only_one_window()) /* quit last window, exit Vim */
6971#endif
6972 getout(0);
6973#ifdef FEAT_WINDOWS
6974# ifdef FEAT_GUI
6975 need_mouse_correct = TRUE;
6976# endif
6977 /* quit current window, may free buffer */
6978 win_close(curwin, !P_HID(curwin->w_buffer));
6979#endif
6980 }
6981}
6982
6983/*
6984 * ":print", ":list", ":number".
6985 */
6986 static void
6987ex_print(eap)
6988 exarg_T *eap;
6989{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006990 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6991 EMSG(_(e_emptybuf));
6992 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006994 for ( ;!got_int; ui_breakcheck())
6995 {
6996 print_line(eap->line1,
6997 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6998 || (eap->flags & EXFLAG_NR)),
6999 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7000 if (++eap->line1 > eap->line2)
7001 break;
7002 out_flush(); /* show one line at a time */
7003 }
7004 setpcmark();
7005 /* put cursor at last line */
7006 curwin->w_cursor.lnum = eap->line2;
7007 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 }
7009
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011}
7012
7013#ifdef FEAT_BYTEOFF
7014 static void
7015ex_goto(eap)
7016 exarg_T *eap;
7017{
7018 goto_byte(eap->line2);
7019}
7020#endif
7021
7022/*
7023 * ":shell".
7024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 static void
7026ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007027 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028{
7029 do_shell(NULL, 0);
7030}
7031
7032#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7033 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007034 || defined(FEAT_GUI_MSWIN) \
7035 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 || defined(PROTO)
7037
7038/*
7039 * Handle a file drop. The code is here because a drop is *nearly* like an
7040 * :args command, but not quite (we have a list of exact filenames, so we
7041 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7042 * code is very similar to :args and hence needs access to a lot of the static
7043 * functions in this file.
7044 *
7045 * The list should be allocated using alloc(), as should each item in the
7046 * list. This function takes over responsibility for freeing the list.
7047 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007048 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7050 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7051 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7052 * file functionality is (currently) not in EMX this is not presently a
7053 * problem.
7054 */
7055 void
7056handle_drop(filec, filev, split)
7057 int filec; /* the number of files dropped */
7058 char_u **filev; /* the list of files dropped */
7059 int split; /* force splitting the window */
7060{
7061 exarg_T ea;
7062 int save_msg_scroll = msg_scroll;
7063
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007064 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007065 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007067#ifdef FEAT_AUTOCMD
7068 if (curbuf_locked())
7069 return;
7070#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007071 /* When the screen is being updated we should not change buffers and
7072 * windows structures, it may cause freed memory to be used. */
7073 if (updating_screen)
7074 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075
7076 /* Check whether the current buffer is changed. If so, we will need
7077 * to split the current window or data could be lost.
7078 * We don't need to check if the 'hidden' option is set, as in this
7079 * case the buffer won't be lost.
7080 */
7081 if (!P_HID(curbuf) && !split)
7082 {
7083 ++emsg_off;
7084 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
7085 --emsg_off;
7086 }
7087 if (split)
7088 {
7089# ifdef FEAT_WINDOWS
7090 if (win_split(0, 0) == FAIL)
7091 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007092 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093
7094 /* When splitting the window, create a new alist. Otherwise the
7095 * existing one is overwritten. */
7096 alist_unlink(curwin->w_alist);
7097 alist_new();
7098# else
7099 return; /* can't split, always fail */
7100# endif
7101 }
7102
7103 /*
7104 * Set up the new argument list.
7105 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007106 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107
7108 /*
7109 * Move to the first file.
7110 */
7111 /* Fake up a minimal "next" command for do_argfile() */
7112 vim_memset(&ea, 0, sizeof(ea));
7113 ea.cmd = (char_u *)"next";
7114 do_argfile(&ea, 0);
7115
7116 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7117 * mode that is not needed here. */
7118 need_start_insertmode = FALSE;
7119
7120 /* Restore msg_scroll, otherwise a following command may cause scrolling
7121 * unexpectedly. The screen will be redrawn by the caller, thus
7122 * msg_scroll being set by displaying a message is irrelevant. */
7123 msg_scroll = save_msg_scroll;
7124}
7125#endif
7126
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127/*
7128 * Clear an argument list: free all file names and reset it to zero entries.
7129 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007130 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131alist_clear(al)
7132 alist_T *al;
7133{
7134 while (--al->al_ga.ga_len >= 0)
7135 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7136 ga_clear(&al->al_ga);
7137}
7138
7139/*
7140 * Init an argument list.
7141 */
7142 void
7143alist_init(al)
7144 alist_T *al;
7145{
7146 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7147}
7148
7149#if defined(FEAT_WINDOWS) || defined(PROTO)
7150
7151/*
7152 * Remove a reference from an argument list.
7153 * Ignored when the argument list is the global one.
7154 * If the argument list is no longer used by any window, free it.
7155 */
7156 void
7157alist_unlink(al)
7158 alist_T *al;
7159{
7160 if (al != &global_alist && --al->al_refcount <= 0)
7161 {
7162 alist_clear(al);
7163 vim_free(al);
7164 }
7165}
7166
7167# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7168/*
7169 * Create a new argument list and use it for the current window.
7170 */
7171 void
7172alist_new()
7173{
7174 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7175 if (curwin->w_alist == NULL)
7176 {
7177 curwin->w_alist = &global_alist;
7178 ++global_alist.al_refcount;
7179 }
7180 else
7181 {
7182 curwin->w_alist->al_refcount = 1;
7183 alist_init(curwin->w_alist);
7184 }
7185}
7186# endif
7187#endif
7188
7189#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7190/*
7191 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007192 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7193 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 */
7195 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007196alist_expand(fnum_list, fnum_len)
7197 int *fnum_list;
7198 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199{
7200 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007201 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 char_u **new_arg_files;
7203 int new_arg_file_count;
7204 char_u *save_p_su = p_su;
7205 int i;
7206
7207 /* Don't use 'suffixes' here. This should work like the shell did the
7208 * expansion. Also, the vimrc file isn't read yet, thus the user
7209 * can't set the options. */
7210 p_su = empty_option;
7211 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7212 if (old_arg_files != NULL)
7213 {
7214 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007215 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7216 old_arg_count = GARGCOUNT;
7217 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007219 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 && new_arg_file_count > 0)
7221 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007222 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7223 TRUE, fnum_list, fnum_len);
7224 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 }
7227 p_su = save_p_su;
7228}
7229#endif
7230
7231/*
7232 * Set the argument list for the current window.
7233 * Takes over the allocated files[] and the allocated fnames in it.
7234 */
7235 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007236alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 alist_T *al;
7238 int count;
7239 char_u **files;
7240 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007241 int *fnum_list;
7242 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243{
7244 int i;
7245
7246 alist_clear(al);
7247 if (ga_grow(&al->al_ga, count) == OK)
7248 {
7249 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007250 {
7251 if (got_int)
7252 {
7253 /* When adding many buffers this can take a long time. Allow
7254 * interrupting here. */
7255 while (i < count)
7256 vim_free(files[i++]);
7257 break;
7258 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007259
7260 /* May set buffer name of a buffer previously used for the
7261 * argument list, so that it's re-used by alist_add. */
7262 if (fnum_list != NULL && i < fnum_len)
7263 buf_set_name(fnum_list[i], files[i]);
7264
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007266 ui_breakcheck();
7267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 vim_free(files);
7269 }
7270 else
7271 FreeWild(count, files);
7272#ifdef FEAT_WINDOWS
7273 if (al == &global_alist)
7274#endif
7275 arg_had_last = FALSE;
7276}
7277
7278/*
7279 * Add file "fname" to argument list "al".
7280 * "fname" must have been allocated and "al" must have been checked for room.
7281 */
7282 void
7283alist_add(al, fname, set_fnum)
7284 alist_T *al;
7285 char_u *fname;
7286 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7287{
7288 if (fname == NULL) /* don't add NULL file names */
7289 return;
7290#ifdef BACKSLASH_IN_FILENAME
7291 slash_adjust(fname);
7292#endif
7293 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7294 if (set_fnum > 0)
7295 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7296 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7297 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298}
7299
7300#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7301/*
7302 * Adjust slashes in file names. Called after 'shellslash' was set.
7303 */
7304 void
7305alist_slash_adjust()
7306{
7307 int i;
7308# ifdef FEAT_WINDOWS
7309 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007310 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311# endif
7312
7313 for (i = 0; i < GARGCOUNT; ++i)
7314 if (GARGLIST[i].ae_fname != NULL)
7315 slash_adjust(GARGLIST[i].ae_fname);
7316# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007317 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 if (wp->w_alist != &global_alist)
7319 for (i = 0; i < WARGCOUNT(wp); ++i)
7320 if (WARGLIST(wp)[i].ae_fname != NULL)
7321 slash_adjust(WARGLIST(wp)[i].ae_fname);
7322# endif
7323}
7324#endif
7325
7326/*
7327 * ":preserve".
7328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 static void
7330ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007331 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007333 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 ml_preserve(curbuf, TRUE);
7335}
7336
7337/*
7338 * ":recover".
7339 */
7340 static void
7341ex_recover(eap)
7342 exarg_T *eap;
7343{
7344 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7345 recoverymode = TRUE;
7346 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7347 && (*eap->arg == NUL
7348 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7349 ml_recover();
7350 recoverymode = FALSE;
7351}
7352
7353/*
7354 * Command modifier used in a wrong way.
7355 */
7356 static void
7357ex_wrongmodifier(eap)
7358 exarg_T *eap;
7359{
7360 eap->errmsg = e_invcmd;
7361}
7362
7363#ifdef FEAT_WINDOWS
7364/*
7365 * :sview [+command] file split window with new file, read-only
7366 * :split [[+command] file] split window with current or new file
7367 * :vsplit [[+command] file] split window vertically with current or new file
7368 * :new [[+command] file] split window with no or new file
7369 * :vnew [[+command] file] split vertically window with no or new file
7370 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007371 *
7372 * :tabedit open new Tab page with empty window
7373 * :tabedit [+command] file open new Tab page and edit "file"
7374 * :tabnew [[+command] file] just like :tabedit
7375 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 */
7377 void
7378ex_splitview(eap)
7379 exarg_T *eap;
7380{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007381 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007382# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007384# endif
7385# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007387# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007389# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7391 {
7392 ex_ni(eap);
7393 return;
7394 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007395# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007397# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007399# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007401# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007403 * quickfix windows. But it's OK when doing ":tab split". */
7404 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 {
7406 if (eap->cmdidx == CMD_split)
7407 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007408# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 if (eap->cmdidx == CMD_vsplit)
7410 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007411# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007413# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007415# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007416 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 {
7418 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7419 FNAME_MESS, TRUE, curbuf->b_ffname);
7420 if (fname == NULL)
7421 goto theend;
7422 eap->arg = fname;
7423 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007424# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007426# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007428# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007430# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007432# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 && eap->cmdidx != CMD_new)
7434 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007435# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007436 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007437# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007438 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007439# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007440 au_has_group((char_u *)"FileExplorer"))
7441 {
7442 /* No browsing supported but we do have the file explorer:
7443 * Edit the directory. */
7444 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7445 eap->arg = (char_u *)".";
7446 }
7447 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007448# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007449 {
7450 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007452 if (fname == NULL)
7453 goto theend;
7454 eap->arg = fname;
7455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 }
7457 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007458# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007460 /*
7461 * Either open new tab page or split the window.
7462 */
7463 if (eap->cmdidx == CMD_tabedit
7464 || eap->cmdidx == CMD_tabfind
7465 || eap->cmdidx == CMD_tabnew)
7466 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007467 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7468 : eap->addr_count == 0 ? 0
7469 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007470 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007471 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007472
7473 /* set the alternate buffer for the window we came from */
7474 if (curwin != old_curwin
7475 && win_valid(old_curwin)
7476 && old_curwin->w_buffer != curbuf
7477 && !cmdmod.keepalt)
7478 old_curwin->w_alt_fnum = curbuf->b_fnum;
7479 }
7480 }
7481 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7483 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007484# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 /* Reset 'scrollbind' when editing another file, but keep it when
7486 * doing ":split" without arguments. */
7487 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007488# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007490# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007492 {
7493 RESET_BINDING(curwin);
7494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 else
7496 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007497# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 do_exedit(eap, old_curwin);
7499 }
7500
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007501# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007503# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007505# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506theend:
7507 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007510
7511/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007512 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007513 */
7514 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007515tabpage_new()
7516{
7517 exarg_T ea;
7518
7519 vim_memset(&ea, 0, sizeof(ea));
7520 ea.cmdidx = CMD_tabnew;
7521 ea.cmd = (char_u *)"tabn";
7522 ea.arg = (char_u *)"";
7523 ex_splitview(&ea);
7524}
7525
7526/*
7527 * :tabnext command
7528 */
7529 static void
7530ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007531 exarg_T *eap;
7532{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007533 switch (eap->cmdidx)
7534 {
7535 case CMD_tabfirst:
7536 case CMD_tabrewind:
7537 goto_tabpage(1);
7538 break;
7539 case CMD_tablast:
7540 goto_tabpage(9999);
7541 break;
7542 case CMD_tabprevious:
7543 case CMD_tabNext:
7544 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7545 break;
7546 default: /* CMD_tabnext */
7547 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7548 break;
7549 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007550}
7551
7552/*
7553 * :tabmove command
7554 */
7555 static void
7556ex_tabmove(eap)
7557 exarg_T *eap;
7558{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007559 int tab_number = 9999;
7560
7561 if (eap->arg && *eap->arg != NUL)
7562 {
7563 char_u *p = eap->arg;
7564 int relative = 0; /* argument +N/-N means: move N places to the
7565 * right/left relative to the current position. */
7566
7567 if (*eap->arg == '-')
7568 {
7569 relative = -1;
7570 p = eap->arg + 1;
7571 }
7572 else if (*eap->arg == '+')
7573 {
7574 relative = 1;
7575 p = eap->arg + 1;
7576 }
7577 else
7578 p = eap->arg;
7579
7580 if (p == skipdigits(p))
7581 {
7582 /* No numbers as argument. */
7583 eap->errmsg = e_invarg;
7584 return;
7585 }
7586
7587 tab_number = getdigits(&p);
7588 if (relative != 0)
7589 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7590 }
7591 else if (eap->addr_count != 0)
7592 tab_number = eap->line2;
7593
7594 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007595}
7596
7597/*
7598 * :tabs command: List tabs and their contents.
7599 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007600 static void
7601ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007602 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007603{
7604 tabpage_T *tp;
7605 win_T *wp;
7606 int tabcount = 1;
7607
7608 msg_start();
7609 msg_scroll = TRUE;
7610 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7611 {
7612 msg_putchar('\n');
7613 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7614 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7615 out_flush(); /* output one line at a time */
7616 ui_breakcheck();
7617
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007618 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007619 wp = firstwin;
7620 else
7621 wp = tp->tp_firstwin;
7622 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7623 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007624 msg_putchar('\n');
7625 msg_putchar(wp == curwin ? '>' : ' ');
7626 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007627 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7628 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007629 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007630 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007631 else
7632 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7633 IObuff, IOSIZE, TRUE);
7634 msg_outtrans(IObuff);
7635 out_flush(); /* output one line at a time */
7636 ui_breakcheck();
7637 }
7638 }
7639}
7640
7641#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642
7643/*
7644 * ":mode": Set screen mode.
7645 * If no argument given, just get the screen size and redraw.
7646 */
7647 static void
7648ex_mode(eap)
7649 exarg_T *eap;
7650{
7651 if (*eap->arg == NUL)
7652 shell_resized();
7653 else
7654 mch_screenmode(eap->arg);
7655}
7656
7657#ifdef FEAT_WINDOWS
7658/*
7659 * ":resize".
7660 * set, increment or decrement current window height
7661 */
7662 static void
7663ex_resize(eap)
7664 exarg_T *eap;
7665{
7666 int n;
7667 win_T *wp = curwin;
7668
7669 if (eap->addr_count > 0)
7670 {
7671 n = eap->line2;
7672 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7673 ;
7674 }
7675
7676#ifdef FEAT_GUI
7677 need_mouse_correct = TRUE;
7678#endif
7679 n = atol((char *)eap->arg);
7680#ifdef FEAT_VERTSPLIT
7681 if (cmdmod.split & WSP_VERT)
7682 {
7683 if (*eap->arg == '-' || *eap->arg == '+')
7684 n += W_WIDTH(curwin);
7685 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7686 n = 9999;
7687 win_setwidth_win((int)n, wp);
7688 }
7689 else
7690#endif
7691 {
7692 if (*eap->arg == '-' || *eap->arg == '+')
7693 n += curwin->w_height;
7694 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7695 n = 9999;
7696 win_setheight_win((int)n, wp);
7697 }
7698}
7699#endif
7700
7701/*
7702 * ":find [+command] <file>" command.
7703 */
7704 static void
7705ex_find(eap)
7706 exarg_T *eap;
7707{
7708#ifdef FEAT_SEARCHPATH
7709 char_u *fname;
7710 int count;
7711
7712 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7713 TRUE, curbuf->b_ffname);
7714 if (eap->addr_count > 0)
7715 {
7716 /* Repeat finding the file "count" times. This matters when it
7717 * appears several times in the path. */
7718 count = eap->line2;
7719 while (fname != NULL && --count > 0)
7720 {
7721 vim_free(fname);
7722 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7723 FALSE, curbuf->b_ffname);
7724 }
7725 }
7726
7727 if (fname != NULL)
7728 {
7729 eap->arg = fname;
7730#endif
7731 do_exedit(eap, NULL);
7732#ifdef FEAT_SEARCHPATH
7733 vim_free(fname);
7734 }
7735#endif
7736}
7737
7738/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007739 * ":open" simulation: for now just work like ":visual".
7740 */
7741 static void
7742ex_open(eap)
7743 exarg_T *eap;
7744{
7745 regmatch_T regmatch;
7746 char_u *p;
7747
7748 curwin->w_cursor.lnum = eap->line2;
7749 beginline(BL_SOL | BL_FIX);
7750 if (*eap->arg == '/')
7751 {
7752 /* ":open /pattern/": put cursor in column found with pattern */
7753 ++eap->arg;
7754 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7755 *p = NUL;
7756 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7757 if (regmatch.regprog != NULL)
7758 {
7759 regmatch.rm_ic = p_ic;
7760 p = ml_get_curline();
7761 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007762 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007763 else
7764 EMSG(_(e_nomatch));
7765 vim_free(regmatch.regprog);
7766 }
7767 /* Move to the NUL, ignore any other arguments. */
7768 eap->arg += STRLEN(eap->arg);
7769 }
7770 check_cursor();
7771
7772 eap->cmdidx = CMD_visual;
7773 do_exedit(eap, NULL);
7774}
7775
7776/*
7777 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 */
7779 static void
7780ex_edit(eap)
7781 exarg_T *eap;
7782{
7783 do_exedit(eap, NULL);
7784}
7785
7786/*
7787 * ":edit <file>" command and alikes.
7788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 void
7790do_exedit(eap, old_curwin)
7791 exarg_T *eap;
7792 win_T *old_curwin; /* curwin before doing a split or NULL */
7793{
7794 int n;
7795#ifdef FEAT_WINDOWS
7796 int need_hide;
7797#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007798 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799
7800 /*
7801 * ":vi" command ends Ex mode.
7802 */
7803 if (exmode_active && (eap->cmdidx == CMD_visual
7804 || eap->cmdidx == CMD_view))
7805 {
7806 exmode_active = FALSE;
7807 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007808 {
7809 /* Special case: ":global/pat/visual\NLvi-commands" */
7810 if (global_busy)
7811 {
7812 int rd = RedrawingDisabled;
7813 int nwr = no_wait_return;
7814 int ms = msg_scroll;
7815#ifdef FEAT_GUI
7816 int he = hold_gui_events;
7817#endif
7818
7819 if (eap->nextcmd != NULL)
7820 {
7821 stuffReadbuff(eap->nextcmd);
7822 eap->nextcmd = NULL;
7823 }
7824
7825 if (exmode_was != EXMODE_VIM)
7826 settmode(TMODE_RAW);
7827 RedrawingDisabled = 0;
7828 no_wait_return = 0;
7829 need_wait_return = FALSE;
7830 msg_scroll = 0;
7831#ifdef FEAT_GUI
7832 hold_gui_events = 0;
7833#endif
7834 must_redraw = CLEAR;
7835
7836 main_loop(FALSE, TRUE);
7837
7838 RedrawingDisabled = rd;
7839 no_wait_return = nwr;
7840 msg_scroll = ms;
7841#ifdef FEAT_GUI
7842 hold_gui_events = he;
7843#endif
7844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 }
7848
7849 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007850 || eap->cmdidx == CMD_tabnew
7851 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852#ifdef FEAT_VERTSPLIT
7853 || eap->cmdidx == CMD_vnew
7854#endif
7855 ) && *eap->arg == NUL)
7856 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007857 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 setpcmark();
7859 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007860 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7861 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 }
7863 else if ((eap->cmdidx != CMD_split
7864#ifdef FEAT_VERTSPLIT
7865 && eap->cmdidx != CMD_vsplit
7866#endif
7867 )
7868 || *eap->arg != NUL
7869#ifdef FEAT_BROWSE
7870 || cmdmod.browse
7871#endif
7872 )
7873 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007874#ifdef FEAT_AUTOCMD
7875 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7876 * can bring us here, others are stopped earlier. */
7877 if (*eap->arg != NUL && curbuf_locked())
7878 return;
7879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 n = readonlymode;
7881 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7882 readonlymode = TRUE;
7883 else if (eap->cmdidx == CMD_enew)
7884 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7885 empty buffer */
7886 setpcmark();
7887 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7888 NULL, eap,
7889 /* ":edit" goes to first line if Vi compatible */
7890 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7891 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7892 ? ECMD_ONE : eap->do_ecmd_lnum,
7893 (P_HID(curbuf) ? ECMD_HIDE : 0)
7894 + (eap->forceit ? ECMD_FORCEIT : 0)
7895#ifdef FEAT_LISTCMDS
7896 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7897#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007898 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 {
7900 /* Editing the file failed. If the window was split, close it. */
7901#ifdef FEAT_WINDOWS
7902 if (old_curwin != NULL)
7903 {
7904 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7905 if (!need_hide || P_HID(curbuf))
7906 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007907# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7908 cleanup_T cs;
7909
7910 /* Reset the error/interrupt/exception state here so that
7911 * aborting() returns FALSE when closing a window. */
7912 enter_cleanup(&cs);
7913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914# ifdef FEAT_GUI
7915 need_mouse_correct = TRUE;
7916# endif
7917 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007918
7919# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7920 /* Restore the error/interrupt/exception state if not
7921 * discarded by a new aborting error, interrupt, or
7922 * uncaught exception. */
7923 leave_cleanup(&cs);
7924# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 }
7926 }
7927#endif
7928 }
7929 else if (readonlymode && curbuf->b_nwindows == 1)
7930 {
7931 /* When editing an already visited buffer, 'readonly' won't be set
7932 * but the previous value is kept. With ":view" and ":sview" we
7933 * want the file to be readonly, except when another window is
7934 * editing the same buffer. */
7935 curbuf->b_p_ro = TRUE;
7936 }
7937 readonlymode = n;
7938 }
7939 else
7940 {
7941 if (eap->do_ecmd_cmd != NULL)
7942 do_cmdline_cmd(eap->do_ecmd_cmd);
7943#ifdef FEAT_TITLE
7944 n = curwin->w_arg_idx_invalid;
7945#endif
7946 check_arg_idx(curwin);
7947#ifdef FEAT_TITLE
7948 if (n != curwin->w_arg_idx_invalid)
7949 maketitle();
7950#endif
7951 }
7952
7953#ifdef FEAT_WINDOWS
7954 /*
7955 * if ":split file" worked, set alternate file name in old window to new
7956 * file
7957 */
7958 if (old_curwin != NULL
7959 && *eap->arg != NUL
7960 && curwin != old_curwin
7961 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007962 && old_curwin->w_buffer != curbuf
7963 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 old_curwin->w_alt_fnum = curbuf->b_fnum;
7965#endif
7966
7967 ex_no_reprint = TRUE;
7968}
7969
7970#ifndef FEAT_GUI
7971/*
7972 * ":gui" and ":gvim" when there is no GUI.
7973 */
7974 static void
7975ex_nogui(eap)
7976 exarg_T *eap;
7977{
7978 eap->errmsg = e_nogvim;
7979}
7980#endif
7981
7982#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7983 static void
7984ex_tearoff(eap)
7985 exarg_T *eap;
7986{
7987 gui_make_tearoff(eap->arg);
7988}
7989#endif
7990
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007991#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 static void
7993ex_popup(eap)
7994 exarg_T *eap;
7995{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007996 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997}
7998#endif
7999
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 static void
8001ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008002 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003{
8004 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8005 MSG(_("No swap file"));
8006 else
8007 msg(curbuf->b_ml.ml_mfp->mf_fname);
8008}
8009
8010/*
8011 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8012 * offset.
8013 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 static void
8016ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008017 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018{
8019#ifdef FEAT_SCROLLBIND
8020 win_T *wp;
8021 long topline;
8022 long y;
8023 linenr_T old_linenr = curwin->w_cursor.lnum;
8024
8025 setpcmark();
8026
8027 /*
8028 * determine max topline
8029 */
8030 if (curwin->w_p_scb)
8031 {
8032 topline = curwin->w_topline;
8033 for (wp = firstwin; wp; wp = wp->w_next)
8034 {
8035 if (wp->w_p_scb && wp->w_buffer)
8036 {
8037 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8038 if (topline > y)
8039 topline = y;
8040 }
8041 }
8042 if (topline < 1)
8043 topline = 1;
8044 }
8045 else
8046 {
8047 topline = 1;
8048 }
8049
8050
8051 /*
8052 * set all scrollbind windows to the same topline
8053 */
8054 wp = curwin;
8055 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8056 {
8057 if (curwin->w_p_scb)
8058 {
8059 y = topline - curwin->w_topline;
8060 if (y > 0)
8061 scrollup(y, TRUE);
8062 else
8063 scrolldown(-y, TRUE);
8064 curwin->w_scbind_pos = topline;
8065 redraw_later(VALID);
8066 cursor_correct();
8067#ifdef FEAT_WINDOWS
8068 curwin->w_redr_status = TRUE;
8069#endif
8070 }
8071 }
8072 curwin = wp;
8073 if (curwin->w_p_scb)
8074 {
8075 did_syncbind = TRUE;
8076 checkpcmark();
8077 if (old_linenr != curwin->w_cursor.lnum)
8078 {
8079 char_u ctrl_o[2];
8080
8081 ctrl_o[0] = Ctrl_O;
8082 ctrl_o[1] = 0;
8083 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8084 }
8085 }
8086#endif
8087}
8088
8089
8090 static void
8091ex_read(eap)
8092 exarg_T *eap;
8093{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008094 int i;
8095 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8096 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097
8098 if (eap->usefilter) /* :r!cmd */
8099 do_bang(1, eap, FALSE, FALSE, TRUE);
8100 else
8101 {
8102 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8103 return;
8104
8105#ifdef FEAT_BROWSE
8106 if (cmdmod.browse)
8107 {
8108 char_u *browseFile;
8109
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008110 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 NULL, NULL, NULL, curbuf);
8112 if (browseFile != NULL)
8113 {
8114 i = readfile(browseFile, NULL,
8115 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8116 vim_free(browseFile);
8117 }
8118 else
8119 i = OK;
8120 }
8121 else
8122#endif
8123 if (*eap->arg == NUL)
8124 {
8125 if (check_fname() == FAIL) /* check for no file name */
8126 return;
8127 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8128 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8129 }
8130 else
8131 {
8132 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8133 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8134 i = readfile(eap->arg, NULL,
8135 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8136
8137 }
8138 if (i == FAIL)
8139 {
8140#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8141 if (!aborting())
8142#endif
8143 EMSG2(_(e_notopen), eap->arg);
8144 }
8145 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008146 {
8147 if (empty && exmode_active)
8148 {
8149 /* Delete the empty line that remains. Historically ex does
8150 * this but vi doesn't. */
8151 if (eap->line2 == 0)
8152 lnum = curbuf->b_ml.ml_line_count;
8153 else
8154 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008155 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008156 {
8157 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008158 if (curwin->w_cursor.lnum > 1
8159 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008160 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008161 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008162 }
8163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 }
8167}
8168
Bram Moolenaarea408852005-06-25 22:49:46 +00008169static char_u *prev_dir = NULL;
8170
8171#if defined(EXITFREE) || defined(PROTO)
8172 void
8173free_cd_dir()
8174{
8175 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008176 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008177
8178 vim_free(globaldir);
8179 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008180}
8181#endif
8182
8183
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184/*
8185 * ":cd", ":lcd", ":chdir" and ":lchdir".
8186 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008187 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188ex_cd(eap)
8189 exarg_T *eap;
8190{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 char_u *new_dir;
8192 char_u *tofree;
8193
8194 new_dir = eap->arg;
8195#if !defined(UNIX) && !defined(VMS)
8196 /* for non-UNIX ":cd" means: print current directory */
8197 if (*new_dir == NUL)
8198 ex_pwd(NULL);
8199 else
8200#endif
8201 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008202#ifdef FEAT_AUTOCMD
8203 if (allbuf_locked())
8204 return;
8205#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008206 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8207 && !eap->forceit)
8208 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008209 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008210 return;
8211 }
8212
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 /* ":cd -": Change to previous directory */
8214 if (STRCMP(new_dir, "-") == 0)
8215 {
8216 if (prev_dir == NULL)
8217 {
8218 EMSG(_("E186: No previous directory"));
8219 return;
8220 }
8221 new_dir = prev_dir;
8222 }
8223
8224 /* Save current directory for next ":cd -" */
8225 tofree = prev_dir;
8226 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8227 prev_dir = vim_strsave(NameBuff);
8228 else
8229 prev_dir = NULL;
8230
8231#if defined(UNIX) || defined(VMS)
8232 /* for UNIX ":cd" means: go to home directory */
8233 if (*new_dir == NUL)
8234 {
8235 /* use NameBuff for home directory name */
8236# ifdef VMS
8237 char_u *p;
8238
8239 p = mch_getenv((char_u *)"SYS$LOGIN");
8240 if (p == NULL || *p == NUL) /* empty is the same as not set */
8241 NameBuff[0] = NUL;
8242 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008243 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244# else
8245 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8246# endif
8247 new_dir = NameBuff;
8248 }
8249#endif
8250 if (new_dir == NULL || vim_chdir(new_dir))
8251 EMSG(_(e_failed));
8252 else
8253 {
8254 vim_free(curwin->w_localdir);
8255 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8256 {
8257 /* If still in global directory, need to remember current
8258 * directory as global directory. */
8259 if (globaldir == NULL && prev_dir != NULL)
8260 globaldir = vim_strsave(prev_dir);
8261 /* Remember this local directory for the window. */
8262 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8263 curwin->w_localdir = vim_strsave(NameBuff);
8264 }
8265 else
8266 {
8267 /* We are now in the global directory, no need to remember its
8268 * name. */
8269 vim_free(globaldir);
8270 globaldir = NULL;
8271 curwin->w_localdir = NULL;
8272 }
8273
8274 shorten_fnames(TRUE);
8275
8276 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008277 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 ex_pwd(eap);
8279 }
8280 vim_free(tofree);
8281 }
8282}
8283
8284/*
8285 * ":pwd".
8286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 static void
8288ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008289 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290{
8291 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8292 {
8293#ifdef BACKSLASH_IN_FILENAME
8294 slash_adjust(NameBuff);
8295#endif
8296 msg(NameBuff);
8297 }
8298 else
8299 EMSG(_("E187: Unknown"));
8300}
8301
8302/*
8303 * ":=".
8304 */
8305 static void
8306ex_equal(eap)
8307 exarg_T *eap;
8308{
8309 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008310 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311}
8312
8313 static void
8314ex_sleep(eap)
8315 exarg_T *eap;
8316{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008317 int n;
8318 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319
8320 if (cursor_valid())
8321 {
8322 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8323 if (n >= 0)
8324 windgoto((int)n, curwin->w_wcol);
8325 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008326
8327 len = eap->line2;
8328 switch (*eap->arg)
8329 {
8330 case 'm': break;
8331 case NUL: len *= 1000L; break;
8332 default: EMSG2(_(e_invarg2), eap->arg); return;
8333 }
8334 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335}
8336
8337/*
8338 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8339 */
8340 void
8341do_sleep(msec)
8342 long msec;
8343{
8344 long done;
8345
8346 cursor_on();
8347 out_flush();
8348 for (done = 0; !got_int && done < msec; done += 1000L)
8349 {
8350 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8351 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008352#ifdef FEAT_NETBEANS_INTG
8353 /* Process the netbeans messages that may have been received in the
8354 * call to ui_breakcheck() when the GUI is in use. This may occur when
8355 * running a test case. */
8356 netbeans_parse_messages();
8357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 }
8359}
8360
8361 static void
8362do_exmap(eap, isabbrev)
8363 exarg_T *eap;
8364 int isabbrev;
8365{
8366 int mode;
8367 char_u *cmdp;
8368
8369 cmdp = eap->cmd;
8370 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8371
8372 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8373 eap->arg, mode, isabbrev))
8374 {
8375 case 1: EMSG(_(e_invarg));
8376 break;
8377 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8378 break;
8379 }
8380}
8381
8382/*
8383 * ":winsize" command (obsolete).
8384 */
8385 static void
8386ex_winsize(eap)
8387 exarg_T *eap;
8388{
8389 int w, h;
8390 char_u *arg = eap->arg;
8391 char_u *p;
8392
8393 w = getdigits(&arg);
8394 arg = skipwhite(arg);
8395 p = arg;
8396 h = getdigits(&arg);
8397 if (*p != NUL && *arg == NUL)
8398 set_shellsize(w, h, TRUE);
8399 else
8400 EMSG(_("E465: :winsize requires two number arguments"));
8401}
8402
8403#ifdef FEAT_WINDOWS
8404 static void
8405ex_wincmd(eap)
8406 exarg_T *eap;
8407{
8408 int xchar = NUL;
8409 char_u *p;
8410
8411 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8412 {
8413 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8414 if (eap->arg[1] == NUL)
8415 {
8416 EMSG(_(e_invarg));
8417 return;
8418 }
8419 xchar = eap->arg[1];
8420 p = eap->arg + 2;
8421 }
8422 else
8423 p = eap->arg + 1;
8424
8425 eap->nextcmd = check_nextcmd(p);
8426 p = skipwhite(p);
8427 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8428 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008429 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {
8431 /* Pass flags on for ":vertical wincmd ]". */
8432 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008433 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8435 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008436 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 }
8438}
8439#endif
8440
Bram Moolenaar843ee412004-06-30 16:16:41 +00008441#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442/*
8443 * ":winpos".
8444 */
8445 static void
8446ex_winpos(eap)
8447 exarg_T *eap;
8448{
8449 int x, y;
8450 char_u *arg = eap->arg;
8451 char_u *p;
8452
8453 if (*arg == NUL)
8454 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008455# if defined(FEAT_GUI) || defined(MSWIN)
8456# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008458# else
8459 if (mch_get_winpos(&x, &y) != FAIL)
8460# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 {
8462 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8463 msg(IObuff);
8464 }
8465 else
8466# endif
8467 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8468 }
8469 else
8470 {
8471 x = getdigits(&arg);
8472 arg = skipwhite(arg);
8473 p = arg;
8474 y = getdigits(&arg);
8475 if (*p == NUL || *arg != NUL)
8476 {
8477 EMSG(_("E466: :winpos requires two number arguments"));
8478 return;
8479 }
8480# ifdef FEAT_GUI
8481 if (gui.in_use)
8482 gui_mch_set_winpos(x, y);
8483 else if (gui.starting)
8484 {
8485 /* Remember the coordinates for when the window is opened. */
8486 gui_win_x = x;
8487 gui_win_y = y;
8488 }
8489# ifdef HAVE_TGETENT
8490 else
8491# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008492# else
8493# ifdef MSWIN
8494 mch_set_winpos(x, y);
8495# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496# endif
8497# ifdef HAVE_TGETENT
8498 if (*T_CWP)
8499 term_set_winpos(x, y);
8500# endif
8501 }
8502}
8503#endif
8504
8505/*
8506 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8507 */
8508 static void
8509ex_operators(eap)
8510 exarg_T *eap;
8511{
8512 oparg_T oa;
8513
8514 clear_oparg(&oa);
8515 oa.regname = eap->regname;
8516 oa.start.lnum = eap->line1;
8517 oa.end.lnum = eap->line2;
8518 oa.line_count = eap->line2 - eap->line1 + 1;
8519 oa.motion_type = MLINE;
8520#ifdef FEAT_VIRTUALEDIT
8521 virtual_op = FALSE;
8522#endif
8523 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8524 {
8525 setpcmark();
8526 curwin->w_cursor.lnum = eap->line1;
8527 beginline(BL_SOL | BL_FIX);
8528 }
8529
8530 switch (eap->cmdidx)
8531 {
8532 case CMD_delete:
8533 oa.op_type = OP_DELETE;
8534 op_delete(&oa);
8535 break;
8536
8537 case CMD_yank:
8538 oa.op_type = OP_YANK;
8539 (void)op_yank(&oa, FALSE, TRUE);
8540 break;
8541
8542 default: /* CMD_rshift or CMD_lshift */
8543 if ((eap->cmdidx == CMD_rshift)
8544#ifdef FEAT_RIGHTLEFT
8545 ^ curwin->w_p_rl
8546#endif
8547 )
8548 oa.op_type = OP_RSHIFT;
8549 else
8550 oa.op_type = OP_LSHIFT;
8551 op_shift(&oa, FALSE, eap->amount);
8552 break;
8553 }
8554#ifdef FEAT_VIRTUALEDIT
8555 virtual_op = MAYBE;
8556#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008557 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558}
8559
8560/*
8561 * ":put".
8562 */
8563 static void
8564ex_put(eap)
8565 exarg_T *eap;
8566{
8567 /* ":0put" works like ":1put!". */
8568 if (eap->line2 == 0)
8569 {
8570 eap->line2 = 1;
8571 eap->forceit = TRUE;
8572 }
8573 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008574 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8575 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576}
8577
8578/*
8579 * Handle ":copy" and ":move".
8580 */
8581 static void
8582ex_copymove(eap)
8583 exarg_T *eap;
8584{
8585 long n;
8586
8587 n = get_address(&eap->arg, FALSE, FALSE);
8588 if (eap->arg == NULL) /* error detected */
8589 {
8590 eap->nextcmd = NULL;
8591 return;
8592 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008593 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594
8595 /*
8596 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8597 */
8598 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8599 {
8600 EMSG(_(e_invaddr));
8601 return;
8602 }
8603
8604 if (eap->cmdidx == CMD_move)
8605 {
8606 if (do_move(eap->line1, eap->line2, n) == FAIL)
8607 return;
8608 }
8609 else
8610 ex_copy(eap->line1, eap->line2, n);
8611 u_clearline();
8612 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008613 ex_may_print(eap);
8614}
8615
8616/*
8617 * Print the current line if flags were given to the Ex command.
8618 */
8619 static void
8620ex_may_print(eap)
8621 exarg_T *eap;
8622{
8623 if (eap->flags != 0)
8624 {
8625 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8626 (eap->flags & EXFLAG_LIST));
8627 ex_no_reprint = TRUE;
8628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629}
8630
8631/*
8632 * ":smagic" and ":snomagic".
8633 */
8634 static void
8635ex_submagic(eap)
8636 exarg_T *eap;
8637{
8638 int magic_save = p_magic;
8639
8640 p_magic = (eap->cmdidx == CMD_smagic);
8641 do_sub(eap);
8642 p_magic = magic_save;
8643}
8644
8645/*
8646 * ":join".
8647 */
8648 static void
8649ex_join(eap)
8650 exarg_T *eap;
8651{
8652 curwin->w_cursor.lnum = eap->line1;
8653 if (eap->line1 == eap->line2)
8654 {
8655 if (eap->addr_count >= 2) /* :2,2join does nothing */
8656 return;
8657 if (eap->line2 == curbuf->b_ml.ml_line_count)
8658 {
8659 beep_flush();
8660 return;
8661 }
8662 ++eap->line2;
8663 }
Bram Moolenaar81340392012-06-06 16:12:59 +02008664 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008666 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667}
8668
8669/*
8670 * ":[addr]@r" or ":[addr]*r": execute register
8671 */
8672 static void
8673ex_at(eap)
8674 exarg_T *eap;
8675{
8676 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008677 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678
8679 curwin->w_cursor.lnum = eap->line2;
8680
8681#ifdef USE_ON_FLY_SCROLL
8682 dont_scroll = TRUE; /* disallow scrolling here */
8683#endif
8684
8685 /* get the register name. No name means to use the previous one */
8686 c = *eap->arg;
8687 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8688 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008689 /* Put the register in the typeahead buffer with the "silent" flag. */
8690 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8691 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008692 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 else
8696 {
8697 int save_efr = exec_from_reg;
8698
8699 exec_from_reg = TRUE;
8700
8701 /*
8702 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008703 * Continue until the stuff buffer is empty and all added characters
8704 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008706 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8708
8709 exec_from_reg = save_efr;
8710 }
8711}
8712
8713/*
8714 * ":!".
8715 */
8716 static void
8717ex_bang(eap)
8718 exarg_T *eap;
8719{
8720 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8721}
8722
8723/*
8724 * ":undo".
8725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 static void
8727ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008728 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008730 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008731 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008732 else
8733 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734}
8735
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008736#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008737 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008738ex_wundo(eap)
8739 exarg_T *eap;
8740{
8741 char_u hash[UNDO_HASH_SIZE];
8742
8743 u_compute_hash(hash);
8744 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8745}
8746
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008747 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008748ex_rundo(eap)
8749 exarg_T *eap;
8750{
8751 char_u hash[UNDO_HASH_SIZE];
8752
8753 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008754 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008755}
8756#endif
8757
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758/*
8759 * ":redo".
8760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 static void
8762ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008763 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764{
8765 u_redo(1);
8766}
8767
8768/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008769 * ":earlier" and ":later".
8770 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008771 static void
8772ex_later(eap)
8773 exarg_T *eap;
8774{
8775 long count = 0;
8776 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008777 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008778 char_u *p = eap->arg;
8779
8780 if (*p == NUL)
8781 count = 1;
8782 else if (isdigit(*p))
8783 {
8784 count = getdigits(&p);
8785 switch (*p)
8786 {
8787 case 's': ++p; sec = TRUE; break;
8788 case 'm': ++p; sec = TRUE; count *= 60; break;
8789 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008790 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8791 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008792 }
8793 }
8794
8795 if (*p != NUL)
8796 EMSG2(_(e_invarg2), eap->arg);
8797 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008798 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8799 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008800}
8801
8802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 * ":redir": start/stop redirection.
8804 */
8805 static void
8806ex_redir(eap)
8807 exarg_T *eap;
8808{
8809 char *mode;
8810 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008811 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812
8813 if (STRICMP(eap->arg, "END") == 0)
8814 close_redir();
8815 else
8816 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008817 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008819 ++arg;
8820 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008822 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 mode = "a";
8824 }
8825 else
8826 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008827 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828
8829 close_redir();
8830
8831 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008832 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 if (fname == NULL)
8834 return;
8835#ifdef FEAT_BROWSE
8836 if (cmdmod.browse)
8837 {
8838 char_u *browseFile;
8839
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008840 browseFile = do_browse(BROWSE_SAVE,
8841 (char_u *)_("Save Redirection"),
8842 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 if (browseFile == NULL)
8844 return; /* operation cancelled */
8845 vim_free(fname);
8846 fname = browseFile;
8847 eap->forceit = TRUE; /* since dialog already asked */
8848 }
8849#endif
8850
8851 redir_fd = open_exfile(fname, eap->forceit, mode);
8852 vim_free(fname);
8853 }
8854#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008855 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 {
8857 /* redirect to a register a-z (resp. A-Z for appending) */
8858 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008859 ++arg;
8860 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008862 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008863 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008865 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008867 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008868 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008869 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008870 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008872 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008873 if (*arg == '>')
8874 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008875 /* Make register empty when not using @A-@Z and the
8876 * command is valid. */
8877 if (*arg == NUL && !isupper(redir_reg))
8878 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008880 }
8881 if (*arg != NUL)
8882 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008883 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008884 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008885 }
8886 }
8887 else if (*arg == '=' && arg[1] == '>')
8888 {
8889 int append;
8890
8891 /* redirect to a variable */
8892 close_redir();
8893 arg += 2;
8894
8895 if (*arg == '>')
8896 {
8897 ++arg;
8898 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 }
8900 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008901 append = FALSE;
8902
8903 if (var_redir_start(skipwhite(arg), append) == OK)
8904 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 }
8906#endif
8907
8908 /* TODO: redirect to a buffer */
8909
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008911 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008913
8914 /* Make sure redirection is not off. Can happen for cmdline completion
8915 * that indirectly invokes a command to catch its output. */
8916 if (redir_fd != NULL
8917#ifdef FEAT_EVAL
8918 || redir_reg || redir_vname
8919#endif
8920 )
8921 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922}
8923
8924/*
8925 * ":redraw": force redraw
8926 */
8927 static void
8928ex_redraw(eap)
8929 exarg_T *eap;
8930{
8931 int r = RedrawingDisabled;
8932 int p = p_lz;
8933
8934 RedrawingDisabled = 0;
8935 p_lz = FALSE;
8936 update_topline();
8937 update_screen(eap->forceit ? CLEAR :
8938#ifdef FEAT_VISUAL
8939 VIsual_active ? INVERTED :
8940#endif
8941 0);
8942#ifdef FEAT_TITLE
8943 if (need_maketitle)
8944 maketitle();
8945#endif
8946 RedrawingDisabled = r;
8947 p_lz = p;
8948
8949 /* Reset msg_didout, so that a message that's there is overwritten. */
8950 msg_didout = FALSE;
8951 msg_col = 0;
8952
8953 out_flush();
8954}
8955
8956/*
8957 * ":redrawstatus": force redraw of status line(s)
8958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 static void
8960ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008961 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962{
8963#if defined(FEAT_WINDOWS)
8964 int r = RedrawingDisabled;
8965 int p = p_lz;
8966
8967 RedrawingDisabled = 0;
8968 p_lz = FALSE;
8969 if (eap->forceit)
8970 status_redraw_all();
8971 else
8972 status_redraw_curbuf();
8973 update_screen(
8974# ifdef FEAT_VISUAL
8975 VIsual_active ? INVERTED :
8976# endif
8977 0);
8978 RedrawingDisabled = r;
8979 p_lz = p;
8980 out_flush();
8981#endif
8982}
8983
8984 static void
8985close_redir()
8986{
8987 if (redir_fd != NULL)
8988 {
8989 fclose(redir_fd);
8990 redir_fd = NULL;
8991 }
8992#ifdef FEAT_EVAL
8993 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008994 if (redir_vname)
8995 {
8996 var_redir_stop();
8997 redir_vname = 0;
8998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999#endif
9000}
9001
9002#if defined(FEAT_SESSION) && defined(USE_CRNL)
9003# define MKSESSION_NL
9004static int mksession_nl = FALSE; /* use NL only in put_eol() */
9005#endif
9006
9007/*
9008 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9009 */
9010 static void
9011ex_mkrc(eap)
9012 exarg_T *eap;
9013{
9014 FILE *fd;
9015 int failed = FALSE;
9016 char_u *fname;
9017#ifdef FEAT_BROWSE
9018 char_u *browseFile = NULL;
9019#endif
9020#ifdef FEAT_SESSION
9021 int view_session = FALSE;
9022 int using_vdir = FALSE; /* using 'viewdir'? */
9023 char_u *viewFile = NULL;
9024 unsigned *flagp;
9025#endif
9026
9027 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9028 {
9029#ifdef FEAT_SESSION
9030 view_session = TRUE;
9031#else
9032 ex_ni(eap);
9033 return;
9034#endif
9035 }
9036
9037#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009038 /* Use the short file name until ":lcd" is used. We also don't use the
9039 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009040 did_lcd = FALSE;
9041
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9043 if (eap->cmdidx == CMD_mkview
9044 && (*eap->arg == NUL
9045 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9046 {
9047 eap->forceit = TRUE;
9048 fname = get_view_file(*eap->arg);
9049 if (fname == NULL)
9050 return;
9051 viewFile = fname;
9052 using_vdir = TRUE;
9053 }
9054 else
9055#endif
9056 if (*eap->arg != NUL)
9057 fname = eap->arg;
9058 else if (eap->cmdidx == CMD_mkvimrc)
9059 fname = (char_u *)VIMRC_FILE;
9060#ifdef FEAT_SESSION
9061 else if (eap->cmdidx == CMD_mksession)
9062 fname = (char_u *)SESSION_FILE;
9063#endif
9064 else
9065 fname = (char_u *)EXRC_FILE;
9066
9067#ifdef FEAT_BROWSE
9068 if (cmdmod.browse)
9069 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009070 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071# ifdef FEAT_SESSION
9072 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9073 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9074# endif
9075 (char_u *)_("Save Setup"),
9076 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9077 if (browseFile == NULL)
9078 goto theend;
9079 fname = browseFile;
9080 eap->forceit = TRUE; /* since dialog already asked */
9081 }
9082#endif
9083
9084#if defined(FEAT_SESSION) && defined(vim_mkdir)
9085 /* When using 'viewdir' may have to create the directory. */
9086 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009087 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088#endif
9089
9090 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9091 if (fd != NULL)
9092 {
9093#ifdef FEAT_SESSION
9094 if (eap->cmdidx == CMD_mkview)
9095 flagp = &vop_flags;
9096 else
9097 flagp = &ssop_flags;
9098#endif
9099
9100#ifdef MKSESSION_NL
9101 /* "unix" in 'sessionoptions': use NL line separator */
9102 if (view_session && (*flagp & SSOP_UNIX))
9103 mksession_nl = TRUE;
9104#endif
9105
9106 /* Write the version command for :mkvimrc */
9107 if (eap->cmdidx == CMD_mkvimrc)
9108 (void)put_line(fd, "version 6.0");
9109
9110#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009111 if (eap->cmdidx == CMD_mksession)
9112 {
9113 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9114 failed = TRUE;
9115 }
9116
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 if (eap->cmdidx != CMD_mkview)
9118#endif
9119 {
9120 /* Write setting 'compatible' first, because it has side effects.
9121 * For that same reason only do it when needed. */
9122 if (p_cp)
9123 (void)put_line(fd, "if !&cp | set cp | endif");
9124 else
9125 (void)put_line(fd, "if &cp | set nocp | endif");
9126 }
9127
9128#ifdef FEAT_SESSION
9129 if (!view_session
9130 || (eap->cmdidx == CMD_mksession
9131 && (*flagp & SSOP_OPTIONS)))
9132#endif
9133 failed |= (makemap(fd, NULL) == FAIL
9134 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9135
9136#ifdef FEAT_SESSION
9137 if (!failed && view_session)
9138 {
9139 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9140 failed = TRUE;
9141 if (eap->cmdidx == CMD_mksession)
9142 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009143 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144
Bram Moolenaard9462e32011-04-11 21:35:11 +02009145 dirnow = alloc(MAXPATHL);
9146 if (dirnow == NULL)
9147 failed = TRUE;
9148 else
9149 {
9150 /*
9151 * Change to session file's dir.
9152 */
9153 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009155 *dirnow = NUL;
9156 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9157 {
9158 if (vim_chdirfile(fname) == OK)
9159 shorten_fnames(TRUE);
9160 }
9161 else if (*dirnow != NUL
9162 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9163 {
9164 if (mch_chdir((char *)globaldir) == 0)
9165 shorten_fnames(TRUE);
9166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167
Bram Moolenaard9462e32011-04-11 21:35:11 +02009168 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169
Bram Moolenaard9462e32011-04-11 21:35:11 +02009170 /* restore original dir */
9171 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009173 {
9174 if (mch_chdir((char *)dirnow) != 0)
9175 EMSG(_(e_prev_dir));
9176 shorten_fnames(TRUE);
9177 }
9178 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 }
9180 }
9181 else
9182 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009183 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9184 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 }
9186 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9187 == FAIL)
9188 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009189 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9190 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009191 if (eap->cmdidx == CMD_mksession)
9192 {
9193 if (put_line(fd, "unlet SessionLoad") == FAIL)
9194 failed = TRUE;
9195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 }
9197#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009198 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9199 failed = TRUE;
9200
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 failed |= fclose(fd);
9202
9203 if (failed)
9204 EMSG(_(e_write));
9205#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9206 else if (eap->cmdidx == CMD_mksession)
9207 {
9208 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009209 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210
Bram Moolenaard9462e32011-04-11 21:35:11 +02009211 tbuf = alloc(MAXPATHL);
9212 if (tbuf != NULL)
9213 {
9214 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9215 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9216 vim_free(tbuf);
9217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 }
9219#endif
9220#ifdef MKSESSION_NL
9221 mksession_nl = FALSE;
9222#endif
9223 }
9224
9225#ifdef FEAT_BROWSE
9226theend:
9227 vim_free(browseFile);
9228#endif
9229#ifdef FEAT_SESSION
9230 vim_free(viewFile);
9231#endif
9232}
9233
Bram Moolenaardf177f62005-02-22 08:39:57 +00009234#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9235 || defined(PROTO)
9236 int
9237vim_mkdir_emsg(name, prot)
9238 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009239 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009240{
9241 if (vim_mkdir(name, prot) != 0)
9242 {
9243 EMSG2(_("E739: Cannot create directory: %s"), name);
9244 return FAIL;
9245 }
9246 return OK;
9247}
9248#endif
9249
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250/*
9251 * Open a file for writing for an Ex command, with some checks.
9252 * Return file descriptor, or NULL on failure.
9253 */
9254 FILE *
9255open_exfile(fname, forceit, mode)
9256 char_u *fname;
9257 int forceit;
9258 char *mode; /* "w" for create new file or "a" for append */
9259{
9260 FILE *fd;
9261
9262#ifdef UNIX
9263 /* with Unix it is possible to open a directory */
9264 if (mch_isdir(fname))
9265 {
9266 EMSG2(_(e_isadir2), fname);
9267 return NULL;
9268 }
9269#endif
9270 if (!forceit && *mode != 'a' && vim_fexists(fname))
9271 {
9272 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9273 return NULL;
9274 }
9275
9276 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9277 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9278
9279 return fd;
9280}
9281
9282/*
9283 * ":mark" and ":k".
9284 */
9285 static void
9286ex_mark(eap)
9287 exarg_T *eap;
9288{
9289 pos_T pos;
9290
9291 if (*eap->arg == NUL) /* No argument? */
9292 EMSG(_(e_argreq));
9293 else if (eap->arg[1] != NUL) /* more than one character? */
9294 EMSG(_(e_trailing));
9295 else
9296 {
9297 pos = curwin->w_cursor; /* save curwin->w_cursor */
9298 curwin->w_cursor.lnum = eap->line2;
9299 beginline(BL_WHITE | BL_FIX);
9300 if (setmark(*eap->arg) == FAIL) /* set mark */
9301 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9302 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9303 }
9304}
9305
9306/*
9307 * Update w_topline, w_leftcol and the cursor position.
9308 */
9309 void
9310update_topline_cursor()
9311{
9312 check_cursor(); /* put cursor on valid line */
9313 update_topline();
9314 if (!curwin->w_p_wrap)
9315 validate_cursor();
9316 update_curswant();
9317}
9318
9319#ifdef FEAT_EX_EXTRA
9320/*
9321 * ":normal[!] {commands}": Execute normal mode commands.
9322 */
9323 static void
9324ex_normal(eap)
9325 exarg_T *eap;
9326{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 int save_msg_scroll = msg_scroll;
9328 int save_restart_edit = restart_edit;
9329 int save_msg_didout = msg_didout;
9330 int save_State = State;
9331 tasave_T tabuf;
9332 int save_insertmode = p_im;
9333 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009334 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335#ifdef FEAT_MBYTE
9336 char_u *arg = NULL;
9337 int l;
9338 char_u *p;
9339#endif
9340
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009341 if (ex_normal_lock > 0)
9342 {
9343 EMSG(_(e_secure));
9344 return;
9345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 if (ex_normal_busy >= p_mmd)
9347 {
9348 EMSG(_("E192: Recursive use of :normal too deep"));
9349 return;
9350 }
9351 ++ex_normal_busy;
9352
9353 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9354 restart_edit = 0; /* don't go to Insert mode */
9355 p_im = FALSE; /* don't use 'insertmode' */
9356
9357#ifdef FEAT_MBYTE
9358 /*
9359 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9360 * this for the K_SPECIAL leading byte, otherwise special keys will not
9361 * work.
9362 */
9363 if (has_mbyte)
9364 {
9365 int len = 0;
9366
9367 /* Count the number of characters to be escaped. */
9368 for (p = eap->arg; *p != NUL; ++p)
9369 {
9370# ifdef FEAT_GUI
9371 if (*p == CSI) /* leadbyte CSI */
9372 len += 2;
9373# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009374 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9376# ifdef FEAT_GUI
9377 || *p == CSI
9378# endif
9379 )
9380 len += 2;
9381 }
9382 if (len > 0)
9383 {
9384 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9385 if (arg != NULL)
9386 {
9387 len = 0;
9388 for (p = eap->arg; *p != NUL; ++p)
9389 {
9390 arg[len++] = *p;
9391# ifdef FEAT_GUI
9392 if (*p == CSI)
9393 {
9394 arg[len++] = KS_EXTRA;
9395 arg[len++] = (int)KE_CSI;
9396 }
9397# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009398 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 {
9400 arg[len++] = *++p;
9401 if (*p == K_SPECIAL)
9402 {
9403 arg[len++] = KS_SPECIAL;
9404 arg[len++] = KE_FILLER;
9405 }
9406# ifdef FEAT_GUI
9407 else if (*p == CSI)
9408 {
9409 arg[len++] = KS_EXTRA;
9410 arg[len++] = (int)KE_CSI;
9411 }
9412# endif
9413 }
9414 arg[len] = NUL;
9415 }
9416 }
9417 }
9418 }
9419#endif
9420
9421 /*
9422 * Save the current typeahead. This is required to allow using ":normal"
9423 * from an event handler and makes sure we don't hang when the argument
9424 * ends with half a command.
9425 */
9426 save_typeahead(&tabuf);
9427 if (tabuf.typebuf_valid)
9428 {
9429 /*
9430 * Repeat the :normal command for each line in the range. When no
9431 * range given, execute it just once, without positioning the cursor
9432 * first.
9433 */
9434 do
9435 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 if (eap->addr_count != 0)
9437 {
9438 curwin->w_cursor.lnum = eap->line1++;
9439 curwin->w_cursor.col = 0;
9440 }
9441
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009442 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443#ifdef FEAT_MBYTE
9444 arg != NULL ? arg :
9445#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009446 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 }
9448 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9449 }
9450
9451 /* Might not return to the main loop when in an event handler. */
9452 update_topline_cursor();
9453
9454 /* Restore the previous typeahead. */
9455 restore_typeahead(&tabuf);
9456
9457 --ex_normal_busy;
9458 msg_scroll = save_msg_scroll;
9459 restart_edit = save_restart_edit;
9460 p_im = save_insertmode;
9461 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009462 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9464
9465 /* Restore the state (needed when called from a function executed for
9466 * 'indentexpr'). */
9467 State = save_State;
9468#ifdef FEAT_MBYTE
9469 vim_free(arg);
9470#endif
9471}
9472
9473/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009474 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 */
9476 static void
9477ex_startinsert(eap)
9478 exarg_T *eap;
9479{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009480 if (eap->forceit)
9481 {
9482 coladvance((colnr_T)MAXCOL);
9483 curwin->w_curswant = MAXCOL;
9484 curwin->w_set_curswant = FALSE;
9485 }
9486
Bram Moolenaara40c5002005-01-09 21:16:21 +00009487 /* Ignore the command when already in Insert mode. Inserting an
9488 * expression register that invokes a function can do this. */
9489 if (State & INSERT)
9490 return;
9491
Bram Moolenaar64969662005-12-14 21:59:55 +00009492 if (eap->cmdidx == CMD_startinsert)
9493 restart_edit = 'a';
9494 else if (eap->cmdidx == CMD_startreplace)
9495 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009497 restart_edit = 'V';
9498
9499 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009501 if (eap->cmdidx == CMD_startinsert)
9502 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 curwin->w_curswant = 0; /* avoid MAXCOL */
9504 }
9505}
9506
9507/*
9508 * ":stopinsert"
9509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 static void
9511ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009512 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513{
9514 restart_edit = 0;
9515 stop_insert_mode = TRUE;
9516}
9517#endif
9518
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009519#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9520/*
9521 * Execute normal mode command "cmd".
9522 * "remap" can be REMAP_NONE or REMAP_YES.
9523 */
9524 void
9525exec_normal_cmd(cmd, remap, silent)
9526 char_u *cmd;
9527 int remap;
9528 int silent;
9529{
9530 oparg_T oa;
9531
9532 /*
9533 * Stuff the argument into the typeahead buffer.
9534 * Execute normal_cmd() until there is no typeahead left.
9535 */
9536 clear_oparg(&oa);
9537 finish_op = FALSE;
9538 ins_typebuf(cmd, remap, 0, TRUE, silent);
9539 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9540 && !got_int)
9541 {
9542 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009543 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009544 }
9545}
9546#endif
9547
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548#ifdef FEAT_FIND_ID
9549 static void
9550ex_checkpath(eap)
9551 exarg_T *eap;
9552{
9553 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9554 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9555 (linenr_T)1, (linenr_T)MAXLNUM);
9556}
9557
9558#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9559/*
9560 * ":psearch"
9561 */
9562 static void
9563ex_psearch(eap)
9564 exarg_T *eap;
9565{
9566 g_do_tagpreview = p_pvh;
9567 ex_findpat(eap);
9568 g_do_tagpreview = 0;
9569}
9570#endif
9571
9572 static void
9573ex_findpat(eap)
9574 exarg_T *eap;
9575{
9576 int whole = TRUE;
9577 long n;
9578 char_u *p;
9579 int action;
9580
9581 switch (cmdnames[eap->cmdidx].cmd_name[2])
9582 {
9583 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9584 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9585 action = ACTION_GOTO;
9586 else
9587 action = ACTION_SHOW;
9588 break;
9589 case 'i': /* ":ilist" and ":dlist" */
9590 action = ACTION_SHOW_ALL;
9591 break;
9592 case 'u': /* ":ijump" and ":djump" */
9593 action = ACTION_GOTO;
9594 break;
9595 default: /* ":isplit" and ":dsplit" */
9596 action = ACTION_SPLIT;
9597 break;
9598 }
9599
9600 n = 1;
9601 if (vim_isdigit(*eap->arg)) /* get count */
9602 {
9603 n = getdigits(&eap->arg);
9604 eap->arg = skipwhite(eap->arg);
9605 }
9606 if (*eap->arg == '/') /* Match regexp, not just whole words */
9607 {
9608 whole = FALSE;
9609 ++eap->arg;
9610 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9611 if (*p)
9612 {
9613 *p++ = NUL;
9614 p = skipwhite(p);
9615
9616 /* Check for trailing illegal characters */
9617 if (!ends_excmd(*p))
9618 eap->errmsg = e_trailing;
9619 else
9620 eap->nextcmd = check_nextcmd(p);
9621 }
9622 }
9623 if (!eap->skip)
9624 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9625 whole, !eap->forceit,
9626 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9627 n, action, eap->line1, eap->line2);
9628}
9629#endif
9630
9631#ifdef FEAT_WINDOWS
9632
9633# ifdef FEAT_QUICKFIX
9634/*
9635 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9636 */
9637 static void
9638ex_ptag(eap)
9639 exarg_T *eap;
9640{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009641 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9643}
9644
9645/*
9646 * ":pedit"
9647 */
9648 static void
9649ex_pedit(eap)
9650 exarg_T *eap;
9651{
9652 win_T *curwin_save = curwin;
9653
9654 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009655 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 keep_help_flag = curwin_save->w_buffer->b_help;
9657 do_exedit(eap, NULL);
9658 keep_help_flag = FALSE;
9659 if (curwin != curwin_save && win_valid(curwin_save))
9660 {
9661 /* Return cursor to where we were */
9662 validate_cursor();
9663 redraw_later(VALID);
9664 win_enter(curwin_save, TRUE);
9665 }
9666 g_do_tagpreview = 0;
9667}
9668# endif
9669
9670/*
9671 * ":stag", ":stselect" and ":stjump".
9672 */
9673 static void
9674ex_stag(eap)
9675 exarg_T *eap;
9676{
9677 postponed_split = -1;
9678 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009679 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9681 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009682 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683}
9684#endif
9685
9686/*
9687 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9688 */
9689 static void
9690ex_tag(eap)
9691 exarg_T *eap;
9692{
9693 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9694}
9695
9696 static void
9697ex_tag_cmd(eap, name)
9698 exarg_T *eap;
9699 char_u *name;
9700{
9701 int cmd;
9702
9703 switch (name[1])
9704 {
9705 case 'j': cmd = DT_JUMP; /* ":tjump" */
9706 break;
9707 case 's': cmd = DT_SELECT; /* ":tselect" */
9708 break;
9709 case 'p': cmd = DT_PREV; /* ":tprevious" */
9710 break;
9711 case 'N': cmd = DT_PREV; /* ":tNext" */
9712 break;
9713 case 'n': cmd = DT_NEXT; /* ":tnext" */
9714 break;
9715 case 'o': cmd = DT_POP; /* ":pop" */
9716 break;
9717 case 'f': /* ":tfirst" */
9718 case 'r': cmd = DT_FIRST; /* ":trewind" */
9719 break;
9720 case 'l': cmd = DT_LAST; /* ":tlast" */
9721 break;
9722 default: /* ":tag" */
9723#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009724 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 {
9726 do_cstag(eap);
9727 return;
9728 }
9729#endif
9730 cmd = DT_TAG;
9731 break;
9732 }
9733
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009734 if (name[0] == 'l')
9735 {
9736#ifndef FEAT_QUICKFIX
9737 ex_ni(eap);
9738 return;
9739#else
9740 cmd = DT_LTAG;
9741#endif
9742 }
9743
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9745 eap->forceit, TRUE);
9746}
9747
9748/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009749 * Check "str" for starting with a special cmdline variable.
9750 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9751 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9752 */
9753 int
9754find_cmdline_var(src, usedlen)
9755 char_u *src;
9756 int *usedlen;
9757{
9758 int len;
9759 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009760 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009761 "%",
9762#define SPEC_PERC 0
9763 "#",
9764#define SPEC_HASH 1
9765 "<cword>", /* cursor word */
9766#define SPEC_CWORD 2
9767 "<cWORD>", /* cursor WORD */
9768#define SPEC_CCWORD 3
9769 "<cfile>", /* cursor path name */
9770#define SPEC_CFILE 4
9771 "<sfile>", /* ":so" file name */
9772#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009773 "<slnum>", /* ":so" file line number */
9774#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009775#ifdef FEAT_AUTOCMD
9776 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009777# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009778 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009779# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009780 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009781# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009782#endif
9783#ifdef FEAT_CLIENTSERVER
9784 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009785# ifdef FEAT_AUTOCMD
9786# define SPEC_CLIENT 10
9787# else
9788# define SPEC_CLIENT 7
9789# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009790#endif
9791 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009792
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009793 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009794 {
9795 len = (int)STRLEN(spec_str[i]);
9796 if (STRNCMP(src, spec_str[i], len) == 0)
9797 {
9798 *usedlen = len;
9799 return i;
9800 }
9801 }
9802 return -1;
9803}
9804
9805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 * Evaluate cmdline variables.
9807 *
9808 * change '%' to curbuf->b_ffname
9809 * '#' to curwin->w_altfile
9810 * '<cword>' to word under the cursor
9811 * '<cWORD>' to WORD under the cursor
9812 * '<cfile>' to path name under the cursor
9813 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009814 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 * '<afile>' to file name for autocommand
9816 * '<abuf>' to buffer number for autocommand
9817 * '<amatch>' to matching name for autocommand
9818 *
9819 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9820 * "" for error without a message) and NULL is returned.
9821 * Returns an allocated string if a valid match was found.
9822 * Returns NULL if no match was found. "usedlen" then still contains the
9823 * number of characters to skip.
9824 */
9825 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009826eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009828 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 int *usedlen; /* characters after src that are used */
9830 linenr_T *lnump; /* line number for :e command, or NULL */
9831 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009832 int *escaped; /* return value has escaped white space (can
9833 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834{
9835 int i;
9836 char_u *s;
9837 char_u *result;
9838 char_u *resultbuf = NULL;
9839 int resultlen;
9840 buf_T *buf;
9841 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9842 int spec_idx;
9843#ifdef FEAT_MODIFY_FNAME
9844 int skip_mod = FALSE;
9845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847
9848 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009849 if (escaped != NULL)
9850 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851
9852 /*
9853 * Check if there is something to do.
9854 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009855 spec_idx = find_cmdline_var(src, usedlen);
9856 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 {
9858 *usedlen = 1;
9859 return NULL;
9860 }
9861
9862 /*
9863 * Skip when preceded with a backslash "\%" and "\#".
9864 * Note: In "\\%" the % is also not recognized!
9865 */
9866 if (src > srcstart && src[-1] == '\\')
9867 {
9868 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009869 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 return NULL;
9871 }
9872
9873 /*
9874 * word or WORD under cursor
9875 */
9876 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9877 {
9878 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9879 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9880 if (resultlen == 0)
9881 {
9882 *errormsg = (char_u *)"";
9883 return NULL;
9884 }
9885 }
9886
9887 /*
9888 * '#': Alternate file name
9889 * '%': Current file name
9890 * File name under the cursor
9891 * File name for autocommand
9892 * and following modifiers
9893 */
9894 else
9895 {
9896 switch (spec_idx)
9897 {
9898 case SPEC_PERC: /* '%': current file */
9899 if (curbuf->b_fname == NULL)
9900 {
9901 result = (char_u *)"";
9902 valid = 0; /* Must have ":p:h" to be valid */
9903 }
9904 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 break;
9907
9908 case SPEC_HASH: /* '#' or "#99": alternate file */
9909 if (src[1] == '#') /* "##": the argument list */
9910 {
9911 result = arg_all();
9912 resultbuf = result;
9913 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009914 if (escaped != NULL)
9915 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916#ifdef FEAT_MODIFY_FNAME
9917 skip_mod = TRUE;
9918#endif
9919 break;
9920 }
9921 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009922 if (*s == '<') /* "#<99" uses v:oldfiles */
9923 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 i = (int)getdigits(&s);
9925 *usedlen = (int)(s - src); /* length of what we expand */
9926
Bram Moolenaard812df62008-11-09 12:46:09 +00009927 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009929 if (*usedlen < 2)
9930 {
9931 /* Should we give an error message for #<text? */
9932 *usedlen = 1;
9933 return NULL;
9934 }
9935#ifdef FEAT_EVAL
9936 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9937 (long)i);
9938 if (result == NULL)
9939 {
9940 *errormsg = (char_u *)"";
9941 return NULL;
9942 }
9943#else
9944 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 }
9948 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009949 {
9950 buf = buflist_findnr(i);
9951 if (buf == NULL)
9952 {
9953 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9954 return NULL;
9955 }
9956 if (lnump != NULL)
9957 *lnump = ECMD_LAST;
9958 if (buf->b_fname == NULL)
9959 {
9960 result = (char_u *)"";
9961 valid = 0; /* Must have ":p:h" to be valid */
9962 }
9963 else
9964 result = buf->b_fname;
9965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 break;
9967
9968#ifdef FEAT_SEARCHPATH
9969 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009970 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 if (result == NULL)
9972 {
9973 *errormsg = (char_u *)"";
9974 return NULL;
9975 }
9976 resultbuf = result; /* remember allocated string */
9977 break;
9978#endif
9979
9980#ifdef FEAT_AUTOCMD
9981 case SPEC_AFILE: /* file name for autocommand */
9982 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009983 if (result != NULL && !autocmd_fname_full)
9984 {
9985 /* Still need to turn the fname into a full path. It is
9986 * postponed to avoid a delay when <afile> is not used. */
9987 autocmd_fname_full = TRUE;
9988 result = FullName_save(autocmd_fname, FALSE);
9989 vim_free(autocmd_fname);
9990 autocmd_fname = result;
9991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 if (result == NULL)
9993 {
9994 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9995 return NULL;
9996 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009997 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 break;
9999
10000 case SPEC_ABUF: /* buffer number for autocommand */
10001 if (autocmd_bufnr <= 0)
10002 {
10003 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10004 return NULL;
10005 }
10006 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10007 result = strbuf;
10008 break;
10009
10010 case SPEC_AMATCH: /* match name for autocommand */
10011 result = autocmd_match;
10012 if (result == NULL)
10013 {
10014 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10015 return NULL;
10016 }
10017 break;
10018
10019#endif
10020 case SPEC_SFILE: /* file name for ":so" command */
10021 result = sourcing_name;
10022 if (result == NULL)
10023 {
10024 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10025 return NULL;
10026 }
10027 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010028 case SPEC_SLNUM: /* line in file for ":so" command */
10029 if (sourcing_name == NULL || sourcing_lnum == 0)
10030 {
10031 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10032 return NULL;
10033 }
10034 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10035 result = strbuf;
10036 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037#if defined(FEAT_CLIENTSERVER)
10038 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010039 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10040 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 result = strbuf;
10042 break;
10043#endif
10044 }
10045
10046 resultlen = (int)STRLEN(result); /* length of new string */
10047 if (src[*usedlen] == '<') /* remove the file name extension */
10048 {
10049 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 resultlen = (int)(s - result);
10052 }
10053#ifdef FEAT_MODIFY_FNAME
10054 else if (!skip_mod)
10055 {
10056 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10057 &resultlen);
10058 if (result == NULL)
10059 {
10060 *errormsg = (char_u *)"";
10061 return NULL;
10062 }
10063 }
10064#endif
10065 }
10066
10067 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10068 {
10069 if (valid != VALID_HEAD + VALID_PATH)
10070 /* xgettext:no-c-format */
10071 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10072 else
10073 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10074 result = NULL;
10075 }
10076 else
10077 result = vim_strnsave(result, resultlen);
10078 vim_free(resultbuf);
10079 return result;
10080}
10081
10082/*
10083 * Concatenate all files in the argument list, separated by spaces, and return
10084 * it in one allocated string.
10085 * Spaces and backslashes in the file names are escaped with a backslash.
10086 * Returns NULL when out of memory.
10087 */
10088 static char_u *
10089arg_all()
10090{
10091 int len;
10092 int idx;
10093 char_u *retval = NULL;
10094 char_u *p;
10095
10096 /*
10097 * Do this loop two times:
10098 * first time: compute the total length
10099 * second time: concatenate the names
10100 */
10101 for (;;)
10102 {
10103 len = 0;
10104 for (idx = 0; idx < ARGCOUNT; ++idx)
10105 {
10106 p = alist_name(&ARGLIST[idx]);
10107 if (p != NULL)
10108 {
10109 if (len > 0)
10110 {
10111 /* insert a space in between names */
10112 if (retval != NULL)
10113 retval[len] = ' ';
10114 ++len;
10115 }
10116 for ( ; *p != NUL; ++p)
10117 {
10118 if (*p == ' ' || *p == '\\')
10119 {
10120 /* insert a backslash */
10121 if (retval != NULL)
10122 retval[len] = '\\';
10123 ++len;
10124 }
10125 if (retval != NULL)
10126 retval[len] = *p;
10127 ++len;
10128 }
10129 }
10130 }
10131
10132 /* second time: break here */
10133 if (retval != NULL)
10134 {
10135 retval[len] = NUL;
10136 break;
10137 }
10138
10139 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010140 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 if (retval == NULL)
10142 break;
10143 }
10144
10145 return retval;
10146}
10147
10148#if defined(FEAT_AUTOCMD) || defined(PROTO)
10149/*
10150 * Expand the <sfile> string in "arg".
10151 *
10152 * Returns an allocated string, or NULL for any error.
10153 */
10154 char_u *
10155expand_sfile(arg)
10156 char_u *arg;
10157{
10158 char_u *errormsg;
10159 int len;
10160 char_u *result;
10161 char_u *newres;
10162 char_u *repl;
10163 int srclen;
10164 char_u *p;
10165
10166 result = vim_strsave(arg);
10167 if (result == NULL)
10168 return NULL;
10169
10170 for (p = result; *p; )
10171 {
10172 if (STRNCMP(p, "<sfile>", 7) != 0)
10173 ++p;
10174 else
10175 {
10176 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010177 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 if (errormsg != NULL)
10179 {
10180 if (*errormsg)
10181 emsg(errormsg);
10182 vim_free(result);
10183 return NULL;
10184 }
10185 if (repl == NULL) /* no match (cannot happen) */
10186 {
10187 p += srclen;
10188 continue;
10189 }
10190 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10191 newres = alloc(len);
10192 if (newres == NULL)
10193 {
10194 vim_free(repl);
10195 vim_free(result);
10196 return NULL;
10197 }
10198 mch_memmove(newres, result, (size_t)(p - result));
10199 STRCPY(newres + (p - result), repl);
10200 len = (int)STRLEN(newres);
10201 STRCAT(newres, p + srclen);
10202 vim_free(repl);
10203 vim_free(result);
10204 result = newres;
10205 p = newres + len; /* continue after the match */
10206 }
10207 }
10208
10209 return result;
10210}
10211#endif
10212
10213#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010214static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10215 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10217static frame_T *ses_skipframe __ARGS((frame_T *fr));
10218static int ses_do_frame __ARGS((frame_T *fr));
10219static int ses_do_win __ARGS((win_T *wp));
10220static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10221static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10222static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10223
10224/*
10225 * Write openfile commands for the current buffers to an .exrc file.
10226 * Return FAIL on error, OK otherwise.
10227 */
10228 static int
10229makeopens(fd, dirnow)
10230 FILE *fd;
10231 char_u *dirnow; /* Current directory name */
10232{
10233 buf_T *buf;
10234 int only_save_windows = TRUE;
10235 int nr;
10236 int cnr = 1;
10237 int restore_size = TRUE;
10238 win_T *wp;
10239 char_u *sname;
10240 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010241 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010242 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010243 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010244 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010245 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246
10247 if (ssop_flags & SSOP_BUFFERS)
10248 only_save_windows = FALSE; /* Save ALL buffers */
10249
10250 /*
10251 * Begin by setting the this_session variable, and then other
10252 * sessionable variables.
10253 */
10254#ifdef FEAT_EVAL
10255 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10256 return FAIL;
10257 if (ssop_flags & SSOP_GLOBALS)
10258 if (store_session_globals(fd) == FAIL)
10259 return FAIL;
10260#endif
10261
10262 /*
10263 * Close all windows but one.
10264 */
10265 if (put_line(fd, "silent only") == FAIL)
10266 return FAIL;
10267
10268 /*
10269 * Now a :cd command to the session directory or the current directory
10270 */
10271 if (ssop_flags & SSOP_SESDIR)
10272 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010273 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10274 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 return FAIL;
10276 }
10277 else if (ssop_flags & SSOP_CURDIR)
10278 {
10279 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10280 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010281 || fputs("cd ", fd) < 0
10282 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10283 || put_eol(fd) == FAIL)
10284 {
10285 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 vim_free(sname);
10289 }
10290
10291 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010292 * If there is an empty, unnamed buffer we will wipe it out later.
10293 * Remember the buffer number.
10294 */
10295 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10296 return FAIL;
10297 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10298 return FAIL;
10299 if (put_line(fd, "endif") == FAIL)
10300 return FAIL;
10301
10302 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 * Now save the current files, current buffer first.
10304 */
10305 if (put_line(fd, "set shortmess=aoO") == FAIL)
10306 return FAIL;
10307
10308 /* Now put the other buffers into the buffer list */
10309 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10310 {
10311 if (!(only_save_windows && buf->b_nwindows == 0)
10312 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10313 && buf->b_fname != NULL
10314 && buf->b_p_bl)
10315 {
10316 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10317 : buf->b_wininfo->wi_fpos.lnum) < 0
10318 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10319 return FAIL;
10320 }
10321 }
10322
10323 /* the global argument list */
10324 if (ses_arglist(fd, "args", &global_alist.al_ga,
10325 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10326 return FAIL;
10327
10328 if (ssop_flags & SSOP_RESIZE)
10329 {
10330 /* Note: after the restore we still check it worked!*/
10331 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10332 || put_eol(fd) == FAIL)
10333 return FAIL;
10334 }
10335
10336#ifdef FEAT_GUI
10337 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10338 {
10339 int x, y;
10340
10341 if (gui_mch_get_winpos(&x, &y) == OK)
10342 {
10343 /* Note: after the restore we still check it worked!*/
10344 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10345 return FAIL;
10346 }
10347 }
10348#endif
10349
10350 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010351 * May repeat putting Windows for each tab, when "tabpages" is in
10352 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010353 * Don't use goto_tabpage(), it may change directory and trigger
10354 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010356 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010357 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010358 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010360 int need_tabnew = FALSE;
10361
Bram Moolenaar18144c82006-04-12 21:52:12 +000010362 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010364 tabpage_T *tp = find_tabpage(tabnr);
10365
10366 if (tp == NULL)
10367 break; /* done all tab pages */
10368 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010369 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010370 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010371 tab_topframe = topframe;
10372 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010373 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010374 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010375 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010376 tab_topframe = tp->tp_topframe;
10377 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010378 if (tabnr > 1)
10379 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381
Bram Moolenaar18144c82006-04-12 21:52:12 +000010382 /*
10383 * Before creating the window layout, try loading one file. If this
10384 * is aborted we don't end up with a number of useless windows.
10385 * This may have side effects! (e.g., compressed or network file).
10386 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010387 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010388 {
10389 if (ses_do_win(wp)
10390 && wp->w_buffer->b_ffname != NULL
10391 && !wp->w_buffer->b_help
10392#ifdef FEAT_QUICKFIX
10393 && !bt_nofile(wp->w_buffer)
10394#endif
10395 )
10396 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010397 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010398 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10399 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010400 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010401 if (!wp->w_arg_idx_invalid)
10402 edited_win = wp;
10403 break;
10404 }
10405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010407 /* If no file got edited create an empty tab page. */
10408 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10409 return FAIL;
10410
Bram Moolenaar18144c82006-04-12 21:52:12 +000010411 /*
10412 * Save current window layout.
10413 */
10414 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010416 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010418 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10419 return FAIL;
10420 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10421 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422
Bram Moolenaar18144c82006-04-12 21:52:12 +000010423 /*
10424 * Check if window sizes can be restored (no windows omitted).
10425 * Remember the window number of the current window after restoring.
10426 */
10427 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010428 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010429 {
10430 if (ses_do_win(wp))
10431 ++nr;
10432 else
10433 restore_size = FALSE;
10434 if (curwin == wp)
10435 cnr = nr;
10436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437
Bram Moolenaar18144c82006-04-12 21:52:12 +000010438 /* Go to the first window. */
10439 if (put_line(fd, "wincmd t") == FAIL)
10440 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010441
Bram Moolenaar18144c82006-04-12 21:52:12 +000010442 /*
10443 * If more than one window, see if sizes can be restored.
10444 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10445 * resized when moving between windows.
10446 * Do this before restoring the view, so that the topline and the
10447 * cursor can be set. This is done again below.
10448 */
10449 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10450 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010451 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010452 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453
Bram Moolenaar18144c82006-04-12 21:52:12 +000010454 /*
10455 * Restore the view of the window (options, file, cursor, etc.).
10456 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010457 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010458 {
10459 if (!ses_do_win(wp))
10460 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010461 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10462 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010463 return FAIL;
10464 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10465 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010466 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010467 }
10468
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010469 /* The argument index in the first tab page is zero, need to set it in
10470 * each window. For further tab pages it's the window where we do
10471 * "tabedit". */
10472 cur_arg_idx = next_arg_idx;
10473
Bram Moolenaar18144c82006-04-12 21:52:12 +000010474 /*
10475 * Restore cursor to the current window if it's not the first one.
10476 */
10477 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10478 || put_eol(fd) == FAIL))
10479 return FAIL;
10480
10481 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010482 * Restore window sizes again after jumping around in windows, because
10483 * the current window has a minimum size while others may not.
10484 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010485 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010486 return FAIL;
10487
Bram Moolenaar18144c82006-04-12 21:52:12 +000010488 /* Don't continue in another tab page when doing only the current one
10489 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010490 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010491 break;
10492 }
10493
10494 if (ssop_flags & SSOP_TABPAGES)
10495 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010496 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10497 || put_eol(fd) == FAIL)
10498 return FAIL;
10499 }
10500
Bram Moolenaar9c102382006-05-03 21:26:49 +000010501 /*
10502 * Wipe out an empty unnamed buffer we started in.
10503 */
10504 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10505 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010506 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010507 return FAIL;
10508 if (put_line(fd, "endif") == FAIL)
10509 return FAIL;
10510 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10511 return FAIL;
10512
10513 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10514 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10515 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10516 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517
10518 /*
10519 * Lastly, execute the x.vim file if it exists.
10520 */
10521 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10522 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010523 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 || put_line(fd, "endif") == FAIL)
10525 return FAIL;
10526
10527 return OK;
10528}
10529
10530 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010531ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 FILE *fd;
10533 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010534 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535{
10536 int n = 0;
10537 win_T *wp;
10538
10539 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10540 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010541 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 {
10543 if (!ses_do_win(wp))
10544 continue;
10545 ++n;
10546
10547 /* restore height when not full height */
10548 if (wp->w_height + wp->w_status_height < topframe->fr_height
10549 && (fprintf(fd,
10550 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10551 n, (long)wp->w_height, Rows / 2, Rows) < 0
10552 || put_eol(fd) == FAIL))
10553 return FAIL;
10554
10555 /* restore width when not full width */
10556 if (wp->w_width < Columns && (fprintf(fd,
10557 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10558 n, (long)wp->w_width, Columns / 2, Columns) < 0
10559 || put_eol(fd) == FAIL))
10560 return FAIL;
10561 }
10562 }
10563 else
10564 {
10565 /* Just equalise window sizes */
10566 if (put_line(fd, "wincmd =") == FAIL)
10567 return FAIL;
10568 }
10569 return OK;
10570}
10571
10572/*
10573 * Write commands to "fd" to recursively create windows for frame "fr",
10574 * horizontally and vertically split.
10575 * After the commands the last window in the frame is the current window.
10576 * Returns FAIL when writing the commands to "fd" fails.
10577 */
10578 static int
10579ses_win_rec(fd, fr)
10580 FILE *fd;
10581 frame_T *fr;
10582{
10583 frame_T *frc;
10584 int count = 0;
10585
10586 if (fr->fr_layout != FR_LEAF)
10587 {
10588 /* Find first frame that's not skipped and then create a window for
10589 * each following one (first frame is already there). */
10590 frc = ses_skipframe(fr->fr_child);
10591 if (frc != NULL)
10592 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10593 {
10594 /* Make window as big as possible so that we have lots of room
10595 * to split. */
10596 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10597 || put_line(fd, fr->fr_layout == FR_COL
10598 ? "split" : "vsplit") == FAIL)
10599 return FAIL;
10600 ++count;
10601 }
10602
10603 /* Go back to the first window. */
10604 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10605 ? "%dwincmd k" : "%dwincmd h", count) < 0
10606 || put_eol(fd) == FAIL))
10607 return FAIL;
10608
10609 /* Recursively create frames/windows in each window of this column or
10610 * row. */
10611 frc = ses_skipframe(fr->fr_child);
10612 while (frc != NULL)
10613 {
10614 ses_win_rec(fd, frc);
10615 frc = ses_skipframe(frc->fr_next);
10616 /* Go to next window. */
10617 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10618 return FAIL;
10619 }
10620 }
10621 return OK;
10622}
10623
10624/*
10625 * Skip frames that don't contain windows we want to save in the Session.
10626 * Returns NULL when there none.
10627 */
10628 static frame_T *
10629ses_skipframe(fr)
10630 frame_T *fr;
10631{
10632 frame_T *frc;
10633
10634 for (frc = fr; frc != NULL; frc = frc->fr_next)
10635 if (ses_do_frame(frc))
10636 break;
10637 return frc;
10638}
10639
10640/*
10641 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10642 * the Session.
10643 */
10644 static int
10645ses_do_frame(fr)
10646 frame_T *fr;
10647{
10648 frame_T *frc;
10649
10650 if (fr->fr_layout == FR_LEAF)
10651 return ses_do_win(fr->fr_win);
10652 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10653 if (ses_do_frame(frc))
10654 return TRUE;
10655 return FALSE;
10656}
10657
10658/*
10659 * Return non-zero if window "wp" is to be stored in the Session.
10660 */
10661 static int
10662ses_do_win(wp)
10663 win_T *wp;
10664{
10665 if (wp->w_buffer->b_fname == NULL
10666#ifdef FEAT_QUICKFIX
10667 /* When 'buftype' is "nofile" can't restore the window contents. */
10668 || bt_nofile(wp->w_buffer)
10669#endif
10670 )
10671 return (ssop_flags & SSOP_BLANK);
10672 if (wp->w_buffer->b_help)
10673 return (ssop_flags & SSOP_HELP);
10674 return TRUE;
10675}
10676
10677/*
10678 * Write commands to "fd" to restore the view of a window.
10679 * Caller must make sure 'scrolloff' is zero.
10680 */
10681 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010682put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 FILE *fd;
10684 win_T *wp;
10685 int add_edit; /* add ":edit" command to view */
10686 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010687 int current_arg_idx; /* current argument index of the window, use
10688 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689{
10690 win_T *save_curwin;
10691 int f;
10692 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010693 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694
10695 /* Always restore cursor position for ":mksession". For ":mkview" only
10696 * when 'viewoptions' contains "cursor". */
10697 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10698
10699 /*
10700 * Local argument list.
10701 */
10702 if (wp->w_alist == &global_alist)
10703 {
10704 if (put_line(fd, "argglobal") == FAIL)
10705 return FAIL;
10706 }
10707 else
10708 {
10709 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10710 flagp == &vop_flags
10711 || !(*flagp & SSOP_CURDIR)
10712 || wp->w_localdir != NULL, flagp) == FAIL)
10713 return FAIL;
10714 }
10715
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010716 /* Only when part of a session: restore the argument index. Some
10717 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010718 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010719 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010721 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 || put_eol(fd) == FAIL)
10723 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010724 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725 }
10726
10727 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010728 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 {
10730 /*
10731 * Load the file.
10732 */
10733 if (wp->w_buffer->b_ffname != NULL
10734#ifdef FEAT_QUICKFIX
10735 && !bt_nofile(wp->w_buffer)
10736#endif
10737 )
10738 {
10739 /*
10740 * Editing a file in this buffer: use ":edit file".
10741 * This may have side effects! (e.g., compressed or network file).
10742 */
10743 if (fputs("edit ", fd) < 0
10744 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10745 return FAIL;
10746 }
10747 else
10748 {
10749 /* No file in this buffer, just make it empty. */
10750 if (put_line(fd, "enew") == FAIL)
10751 return FAIL;
10752#ifdef FEAT_QUICKFIX
10753 if (wp->w_buffer->b_ffname != NULL)
10754 {
10755 /* The buffer does have a name, but it's not a file name. */
10756 if (fputs("file ", fd) < 0
10757 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10758 return FAIL;
10759 }
10760#endif
10761 do_cursor = FALSE;
10762 }
10763 }
10764
10765 /*
10766 * Local mappings and abbreviations.
10767 */
10768 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10769 && makemap(fd, wp->w_buffer) == FAIL)
10770 return FAIL;
10771
10772 /*
10773 * Local options. Need to go to the window temporarily.
10774 * Store only local values when using ":mkview" and when ":mksession" is
10775 * used and 'sessionoptions' doesn't include "options".
10776 * Some folding options are always stored when "folds" is included,
10777 * otherwise the folds would not be restored correctly.
10778 */
10779 save_curwin = curwin;
10780 curwin = wp;
10781 curbuf = curwin->w_buffer;
10782 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10783 f = makeset(fd, OPT_LOCAL,
10784 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10785#ifdef FEAT_FOLDING
10786 else if (*flagp & SSOP_FOLDS)
10787 f = makefoldset(fd);
10788#endif
10789 else
10790 f = OK;
10791 curwin = save_curwin;
10792 curbuf = curwin->w_buffer;
10793 if (f == FAIL)
10794 return FAIL;
10795
10796#ifdef FEAT_FOLDING
10797 /*
10798 * Save Folds when 'buftype' is empty and for help files.
10799 */
10800 if ((*flagp & SSOP_FOLDS)
10801 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010802# ifdef FEAT_QUICKFIX
10803 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10804# endif
10805 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 {
10807 if (put_folds(fd, wp) == FAIL)
10808 return FAIL;
10809 }
10810#endif
10811
10812 /*
10813 * Set the cursor after creating folds, since that moves the cursor.
10814 */
10815 if (do_cursor)
10816 {
10817
10818 /* Restore the cursor line in the file and relatively in the
10819 * window. Don't use "G", it changes the jumplist. */
10820 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10821 (long)wp->w_cursor.lnum,
10822 (long)(wp->w_cursor.lnum - wp->w_topline),
10823 (long)wp->w_height / 2, (long)wp->w_height) < 0
10824 || put_eol(fd) == FAIL
10825 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10826 || put_line(fd, "exe s:l") == FAIL
10827 || put_line(fd, "normal! zt") == FAIL
10828 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10829 || put_eol(fd) == FAIL)
10830 return FAIL;
10831 /* Restore the cursor column and left offset when not wrapping. */
10832 if (wp->w_cursor.col == 0)
10833 {
10834 if (put_line(fd, "normal! 0") == FAIL)
10835 return FAIL;
10836 }
10837 else
10838 {
10839 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10840 {
10841 if (fprintf(fd,
10842 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010843 (long)wp->w_virtcol + 1,
10844 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 (long)wp->w_width / 2, (long)wp->w_width) < 0
10846 || put_eol(fd) == FAIL
10847 || put_line(fd, "if s:c > 0") == FAIL
10848 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010849 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10850 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 || put_eol(fd) == FAIL
10852 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010010853 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 || put_eol(fd) == FAIL
10855 || put_line(fd, "endif") == FAIL)
10856 return FAIL;
10857 }
10858 else
10859 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010860 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 || put_eol(fd) == FAIL)
10862 return FAIL;
10863 }
10864 }
10865 }
10866
10867 /*
10868 * Local directory.
10869 */
10870 if (wp->w_localdir != NULL)
10871 {
10872 if (fputs("lcd ", fd) < 0
10873 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10874 || put_eol(fd) == FAIL)
10875 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010876 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 }
10878
10879 return OK;
10880}
10881
10882/*
10883 * Write an argument list to the session file.
10884 * Returns FAIL if writing fails.
10885 */
10886 static int
10887ses_arglist(fd, cmd, gap, fullname, flagp)
10888 FILE *fd;
10889 char *cmd;
10890 garray_T *gap;
10891 int fullname; /* TRUE: use full path name */
10892 unsigned *flagp;
10893{
10894 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010895 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 char_u *s;
10897
10898 if (gap->ga_len == 0)
10899 return put_line(fd, "silent! argdel *");
10900 if (fputs(cmd, fd) < 0)
10901 return FAIL;
10902 for (i = 0; i < gap->ga_len; ++i)
10903 {
10904 /* NULL file names are skipped (only happens when out of memory). */
10905 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10906 if (s != NULL)
10907 {
10908 if (fullname)
10909 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010910 buf = alloc(MAXPATHL);
10911 if (buf != NULL)
10912 {
10913 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10914 s = buf;
10915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 }
10917 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010918 {
10919 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010921 }
10922 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 }
10924 }
10925 return put_eol(fd);
10926}
10927
10928/*
10929 * Write a buffer name to the session file.
10930 * Also ends the line.
10931 * Returns FAIL if writing fails.
10932 */
10933 static int
10934ses_fname(fd, buf, flagp)
10935 FILE *fd;
10936 buf_T *buf;
10937 unsigned *flagp;
10938{
10939 char_u *name;
10940
10941 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010942 * the session file will be sourced.
10943 * Don't do this for ":mkview", we don't know the current directory.
10944 * Don't do this after ":lcd", we don't keep track of what the current
10945 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 if (buf->b_sfname != NULL
10947 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010948 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010949#ifdef FEAT_AUTOCHDIR
10950 && !p_acd
10951#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010952 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953 name = buf->b_sfname;
10954 else
10955 name = buf->b_ffname;
10956 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10957 return FAIL;
10958 return OK;
10959}
10960
10961/*
10962 * Write a file name to the session file.
10963 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10964 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010965 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 */
10967 static int
10968ses_put_fname(fd, name, flagp)
10969 FILE *fd;
10970 char_u *name;
10971 unsigned *flagp;
10972{
10973 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010974 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976
10977 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010978 if (sname == NULL)
10979 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010981 if (*flagp & SSOP_SLASH)
10982 {
10983 /* change all backslashes to forward slashes */
10984 for (p = sname; *p != NUL; mb_ptr_adv(p))
10985 if (*p == '\\')
10986 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010988
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020010989 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010990 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010992 if (p == NULL)
10993 return FAIL;
10994
10995 /* write the result */
10996 if (fputs((char *)p, fd) < 0)
10997 retval = FAIL;
10998
10999 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 return retval;
11001}
11002
11003/*
11004 * ":loadview [nr]"
11005 */
11006 static void
11007ex_loadview(eap)
11008 exarg_T *eap;
11009{
11010 char_u *fname;
11011
11012 fname = get_view_file(*eap->arg);
11013 if (fname != NULL)
11014 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011015 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 vim_free(fname);
11017 }
11018}
11019
11020/*
11021 * Get the name of the view file for the current buffer.
11022 */
11023 static char_u *
11024get_view_file(c)
11025 int c;
11026{
11027 int len = 0;
11028 char_u *p, *s;
11029 char_u *retval;
11030 char_u *sname;
11031
11032 if (curbuf->b_ffname == NULL)
11033 {
11034 EMSG(_(e_noname));
11035 return NULL;
11036 }
11037 sname = home_replace_save(NULL, curbuf->b_ffname);
11038 if (sname == NULL)
11039 return NULL;
11040
11041 /*
11042 * We want a file name without separators, because we're not going to make
11043 * a directory.
11044 * "normal" path separator -> "=+"
11045 * "=" -> "=="
11046 * ":" path separator -> "=-"
11047 */
11048 for (p = sname; *p; ++p)
11049 if (*p == '=' || vim_ispathsep(*p))
11050 ++len;
11051 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11052 if (retval != NULL)
11053 {
11054 STRCPY(retval, p_vdir);
11055 add_pathsep(retval);
11056 s = retval + STRLEN(retval);
11057 for (p = sname; *p; ++p)
11058 {
11059 if (*p == '=')
11060 {
11061 *s++ = '=';
11062 *s++ = '=';
11063 }
11064 else if (vim_ispathsep(*p))
11065 {
11066 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011067#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 if (*p == ':')
11069 *s++ = '-';
11070 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011072 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 }
11074 else
11075 *s++ = *p;
11076 }
11077 *s++ = '=';
11078 *s++ = c;
11079 STRCPY(s, ".vim");
11080 }
11081
11082 vim_free(sname);
11083 return retval;
11084}
11085
11086#endif /* FEAT_SESSION */
11087
11088/*
11089 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11090 * Return FAIL for a write error.
11091 */
11092 int
11093put_eol(fd)
11094 FILE *fd;
11095{
11096 if (
11097#ifdef USE_CRNL
11098 (
11099# ifdef MKSESSION_NL
11100 !mksession_nl &&
11101# endif
11102 (putc('\r', fd) < 0)) ||
11103#endif
11104 (putc('\n', fd) < 0))
11105 return FAIL;
11106 return OK;
11107}
11108
11109/*
11110 * Write a line to "fd".
11111 * Return FAIL for a write error.
11112 */
11113 int
11114put_line(fd, s)
11115 FILE *fd;
11116 char *s;
11117{
11118 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11119 return FAIL;
11120 return OK;
11121}
11122
11123#ifdef FEAT_VIMINFO
11124/*
11125 * ":rviminfo" and ":wviminfo".
11126 */
11127 static void
11128ex_viminfo(eap)
11129 exarg_T *eap;
11130{
11131 char_u *save_viminfo;
11132
11133 save_viminfo = p_viminfo;
11134 if (*p_viminfo == NUL)
11135 p_viminfo = (char_u *)"'100";
11136 if (eap->cmdidx == CMD_rviminfo)
11137 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011138 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11139 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 EMSG(_("E195: Cannot open viminfo file for reading"));
11141 }
11142 else
11143 write_viminfo(eap->arg, eap->forceit);
11144 p_viminfo = save_viminfo;
11145}
11146#endif
11147
11148#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011149/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011150 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011151 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011152 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 void
11154dialog_msg(buff, format, fname)
11155 char_u *buff;
11156 char *format;
11157 char_u *fname;
11158{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159 if (fname == NULL)
11160 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011161 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162}
11163#endif
11164
11165/*
11166 * ":behave {mswin,xterm}"
11167 */
11168 static void
11169ex_behave(eap)
11170 exarg_T *eap;
11171{
11172 if (STRCMP(eap->arg, "mswin") == 0)
11173 {
11174 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11175 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11176 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11177 set_option_value((char_u *)"keymodel", 0L,
11178 (char_u *)"startsel,stopsel", 0);
11179 }
11180 else if (STRCMP(eap->arg, "xterm") == 0)
11181 {
11182 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11183 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11184 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11185 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11186 }
11187 else
11188 EMSG2(_(e_invarg2), eap->arg);
11189}
11190
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011191#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11192/*
11193 * Function given to ExpandGeneric() to obtain the possible arguments of the
11194 * ":behave {mswin,xterm}" command.
11195 */
11196 char_u *
11197get_behave_arg(xp, idx)
11198 expand_T *xp UNUSED;
11199 int idx;
11200{
11201 if (idx == 0)
11202 return (char_u *)"mswin";
11203 if (idx == 1)
11204 return (char_u *)"xterm";
11205 return NULL;
11206}
11207#endif
11208
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209#ifdef FEAT_AUTOCMD
11210static int filetype_detect = FALSE;
11211static int filetype_plugin = FALSE;
11212static int filetype_indent = FALSE;
11213
11214/*
11215 * ":filetype [plugin] [indent] {on,off,detect}"
11216 * on: Load the filetype.vim file to install autocommands for file types.
11217 * off: Load the ftoff.vim file to remove all autocommands for file types.
11218 * plugin on: load filetype.vim and ftplugin.vim
11219 * plugin off: load ftplugof.vim
11220 * indent on: load filetype.vim and indent.vim
11221 * indent off: load indoff.vim
11222 */
11223 static void
11224ex_filetype(eap)
11225 exarg_T *eap;
11226{
11227 char_u *arg = eap->arg;
11228 int plugin = FALSE;
11229 int indent = FALSE;
11230
11231 if (*eap->arg == NUL)
11232 {
11233 /* Print current status. */
11234 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11235 filetype_detect ? "ON" : "OFF",
11236 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11237 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11238 return;
11239 }
11240
11241 /* Accept "plugin" and "indent" in any order. */
11242 for (;;)
11243 {
11244 if (STRNCMP(arg, "plugin", 6) == 0)
11245 {
11246 plugin = TRUE;
11247 arg = skipwhite(arg + 6);
11248 continue;
11249 }
11250 if (STRNCMP(arg, "indent", 6) == 0)
11251 {
11252 indent = TRUE;
11253 arg = skipwhite(arg + 6);
11254 continue;
11255 }
11256 break;
11257 }
11258 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11259 {
11260 if (*arg == 'o' || !filetype_detect)
11261 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011262 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 filetype_detect = TRUE;
11264 if (plugin)
11265 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011266 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 filetype_plugin = TRUE;
11268 }
11269 if (indent)
11270 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011271 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 filetype_indent = TRUE;
11273 }
11274 }
11275 if (*arg == 'd')
11276 {
11277 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011278 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279 }
11280 }
11281 else if (STRCMP(arg, "off") == 0)
11282 {
11283 if (plugin || indent)
11284 {
11285 if (plugin)
11286 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011287 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288 filetype_plugin = FALSE;
11289 }
11290 if (indent)
11291 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011292 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 filetype_indent = FALSE;
11294 }
11295 }
11296 else
11297 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011298 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299 filetype_detect = FALSE;
11300 }
11301 }
11302 else
11303 EMSG2(_(e_invarg2), arg);
11304}
11305
11306/*
11307 * ":setfiletype {name}"
11308 */
11309 static void
11310ex_setfiletype(eap)
11311 exarg_T *eap;
11312{
11313 if (!did_filetype)
11314 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11315}
11316#endif
11317
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318 static void
11319ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011320 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321{
11322#ifdef FEAT_DIGRAPHS
11323 if (*eap->arg != NUL)
11324 putdigraph(eap->arg);
11325 else
11326 listdigraphs();
11327#else
11328 EMSG(_("E196: No digraphs in this version"));
11329#endif
11330}
11331
11332 static void
11333ex_set(eap)
11334 exarg_T *eap;
11335{
11336 int flags = 0;
11337
11338 if (eap->cmdidx == CMD_setlocal)
11339 flags = OPT_LOCAL;
11340 else if (eap->cmdidx == CMD_setglobal)
11341 flags = OPT_GLOBAL;
11342#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11343 if (cmdmod.browse && flags == 0)
11344 ex_options(eap);
11345 else
11346#endif
11347 (void)do_set(eap->arg, flags);
11348}
11349
11350#ifdef FEAT_SEARCH_EXTRA
11351/*
11352 * ":nohlsearch"
11353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354 static void
11355ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011356 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357{
11358 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011359 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360}
11361
11362/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011363 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364 * Sets nextcmd to the start of the next command, if any. Also called when
11365 * skipping commands to find the next command.
11366 */
11367 static void
11368ex_match(eap)
11369 exarg_T *eap;
11370{
11371 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011372 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373 char_u *end;
11374 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011375 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011376
11377 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011378 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011379 else
11380 {
11381 EMSG(e_invcmd);
11382 return;
11383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384
11385 /* First clear any old pattern. */
11386 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011387 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388
11389 if (ends_excmd(*eap->arg))
11390 end = eap->arg;
11391 else if ((STRNICMP(eap->arg, "none", 4) == 0
11392 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11393 end = eap->arg + 4;
11394 else
11395 {
11396 p = skiptowhite(eap->arg);
11397 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011398 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399 p = skipwhite(p);
11400 if (*p == NUL)
11401 {
11402 /* There must be two arguments. */
11403 EMSG2(_(e_invarg2), eap->arg);
11404 return;
11405 }
11406 end = skip_regexp(p + 1, *p, TRUE, NULL);
11407 if (!eap->skip)
11408 {
11409 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11410 {
11411 eap->errmsg = e_trailing;
11412 return;
11413 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011414 if (*end != *p)
11415 {
11416 EMSG2(_(e_invarg2), p);
11417 return;
11418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419
11420 c = *end;
11421 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011422 match_add(curwin, g, p + 1, 10, id);
11423 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011424 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425 }
11426 }
11427 eap->nextcmd = find_nextcmd(end);
11428}
11429#endif
11430
11431#ifdef FEAT_CRYPT
11432/*
11433 * ":X": Get crypt key
11434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 static void
11436ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011437 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011439 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011440 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441}
11442#endif
11443
11444#ifdef FEAT_FOLDING
11445 static void
11446ex_fold(eap)
11447 exarg_T *eap;
11448{
11449 if (foldManualAllowed(TRUE))
11450 foldCreate(eap->line1, eap->line2);
11451}
11452
11453 static void
11454ex_foldopen(eap)
11455 exarg_T *eap;
11456{
11457 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11458 eap->forceit, FALSE);
11459}
11460
11461 static void
11462ex_folddo(eap)
11463 exarg_T *eap;
11464{
11465 linenr_T lnum;
11466
11467 /* First set the marks for all lines closed/open. */
11468 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11469 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11470 ml_setmarked(lnum);
11471
11472 /* Execute the command on the marked lines. */
11473 global_exe(eap->arg);
11474 ml_clearmarked(); /* clear rest of the marks */
11475}
11476#endif