blob: b6fc3766842e0400c7d621ac73e6cd9fa75edf64 [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
Bram Moolenaard620aa92013-05-17 16:40:06 +0200271# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272# define ex_pyfile ex_ni
273#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200274#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200275# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200276# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200277# define ex_py3file ex_ni
278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279#ifndef FEAT_TCL
280# define ex_tcl ex_script_ni
281# define ex_tcldo ex_ni
282# define ex_tclfile ex_ni
283#endif
284#ifndef FEAT_RUBY
285# define ex_ruby ex_script_ni
286# define ex_rubydo ex_ni
287# define ex_rubyfile ex_ni
288#endif
289#ifndef FEAT_SNIFF
290# define ex_sniff ex_ni
291#endif
292#ifndef FEAT_KEYMAP
293# define ex_loadkeymap ex_ni
294#endif
295static void ex_swapname __ARGS((exarg_T *eap));
296static void ex_syncbind __ARGS((exarg_T *eap));
297static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298static void ex_pwd __ARGS((exarg_T *eap));
299static void ex_equal __ARGS((exarg_T *eap));
300static void ex_sleep __ARGS((exarg_T *eap));
301static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
302static void ex_winsize __ARGS((exarg_T *eap));
303#ifdef FEAT_WINDOWS
304static void ex_wincmd __ARGS((exarg_T *eap));
305#else
306# define ex_wincmd ex_ni
307#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000308#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309static void ex_winpos __ARGS((exarg_T *eap));
310#else
311# define ex_winpos ex_ni
312#endif
313static void ex_operators __ARGS((exarg_T *eap));
314static void ex_put __ARGS((exarg_T *eap));
315static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000316static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317static void ex_submagic __ARGS((exarg_T *eap));
318static void ex_join __ARGS((exarg_T *eap));
319static void ex_at __ARGS((exarg_T *eap));
320static void ex_bang __ARGS((exarg_T *eap));
321static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200322#ifdef FEAT_PERSISTENT_UNDO
323static void ex_wundo __ARGS((exarg_T *eap));
324static void ex_rundo __ARGS((exarg_T *eap));
325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000327static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328static void ex_redir __ARGS((exarg_T *eap));
329static void ex_redraw __ARGS((exarg_T *eap));
330static void ex_redrawstatus __ARGS((exarg_T *eap));
331static void close_redir __ARGS((void));
332static void ex_mkrc __ARGS((exarg_T *eap));
333static void ex_mark __ARGS((exarg_T *eap));
334#ifdef FEAT_USR_CMDS
335static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000336static 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 +0000337#endif
338#ifdef FEAT_EX_EXTRA
339static void ex_normal __ARGS((exarg_T *eap));
340static void ex_startinsert __ARGS((exarg_T *eap));
341static void ex_stopinsert __ARGS((exarg_T *eap));
342#else
343# define ex_normal ex_ni
344# define ex_align ex_ni
345# define ex_retab ex_ni
346# define ex_startinsert ex_ni
347# define ex_stopinsert ex_ni
348# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000349# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#endif
351#ifdef FEAT_FIND_ID
352static void ex_checkpath __ARGS((exarg_T *eap));
353static void ex_findpat __ARGS((exarg_T *eap));
354#else
355# define ex_findpat ex_ni
356# define ex_checkpath ex_ni
357#endif
358#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
359static void ex_psearch __ARGS((exarg_T *eap));
360#else
361# define ex_psearch ex_ni
362#endif
363static void ex_tag __ARGS((exarg_T *eap));
364static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
365#ifndef FEAT_EVAL
366# define ex_scriptnames ex_ni
367# define ex_finish ex_ni
368# define ex_echo ex_ni
369# define ex_echohl ex_ni
370# define ex_execute ex_ni
371# define ex_call ex_ni
372# define ex_if ex_ni
373# define ex_endif ex_ni
374# define ex_else ex_ni
375# define ex_while ex_ni
376# define ex_continue ex_ni
377# define ex_break ex_ni
378# define ex_endwhile ex_ni
379# define ex_throw ex_ni
380# define ex_try ex_ni
381# define ex_catch ex_ni
382# define ex_finally ex_ni
383# define ex_endtry ex_ni
384# define ex_endfunction ex_ni
385# define ex_let ex_ni
386# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000387# define ex_lockvar ex_ni
388# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389# define ex_function ex_ni
390# define ex_delfunction ex_ni
391# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000392# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393#endif
394static char_u *arg_all __ARGS((void));
395#ifdef FEAT_SESSION
396static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000397static 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 +0000398static void ex_loadview __ARGS((exarg_T *eap));
399static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000400static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401#else
402# define ex_loadview ex_ni
403#endif
404#ifndef FEAT_EVAL
405# define ex_compiler ex_ni
406#endif
407#ifdef FEAT_VIMINFO
408static void ex_viminfo __ARGS((exarg_T *eap));
409#else
410# define ex_viminfo ex_ni
411#endif
412static void ex_behave __ARGS((exarg_T *eap));
413#ifdef FEAT_AUTOCMD
414static void ex_filetype __ARGS((exarg_T *eap));
415static void ex_setfiletype __ARGS((exarg_T *eap));
416#else
417# define ex_filetype ex_ni
418# define ex_setfiletype ex_ni
419#endif
420#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000421# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422# define ex_diffpatch ex_ni
423# define ex_diffgetput ex_ni
424# define ex_diffsplit ex_ni
425# define ex_diffthis ex_ni
426# define ex_diffupdate ex_ni
427#endif
428static void ex_digraphs __ARGS((exarg_T *eap));
429static void ex_set __ARGS((exarg_T *eap));
430#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
431# define ex_options ex_ni
432#endif
433#ifdef FEAT_SEARCH_EXTRA
434static void ex_nohlsearch __ARGS((exarg_T *eap));
435static void ex_match __ARGS((exarg_T *eap));
436#else
437# define ex_nohlsearch ex_ni
438# define ex_match ex_ni
439#endif
440#ifdef FEAT_CRYPT
441static void ex_X __ARGS((exarg_T *eap));
442#else
443# define ex_X ex_ni
444#endif
445#ifdef FEAT_FOLDING
446static void ex_fold __ARGS((exarg_T *eap));
447static void ex_foldopen __ARGS((exarg_T *eap));
448static void ex_folddo __ARGS((exarg_T *eap));
449#else
450# define ex_fold ex_ni
451# define ex_foldopen ex_ni
452# define ex_folddo ex_ni
453#endif
454#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
455 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
456# define ex_language ex_ni
457#endif
458#ifndef FEAT_SIGNS
459# define ex_sign ex_ni
460#endif
461#ifndef FEAT_SUN_WORKSHOP
462# define ex_wsverb ex_ni
463#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000464#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200465# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000466# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200467# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469
470#ifndef FEAT_EVAL
471# define ex_debug ex_ni
472# define ex_breakadd ex_ni
473# define ex_debuggreedy ex_ni
474# define ex_breakdel ex_ni
475# define ex_breaklist ex_ni
476#endif
477
478#ifndef FEAT_CMDHIST
479# define ex_history ex_ni
480#endif
481#ifndef FEAT_JUMPLIST
482# define ex_jumps ex_ni
483# define ex_changes ex_ni
484#endif
485
Bram Moolenaar05159a02005-02-26 23:04:13 +0000486#ifndef FEAT_PROFILE
487# define ex_profile ex_ni
488#endif
489
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490/*
491 * Declare cmdnames[].
492 */
493#define DO_DECLARE_EXCMD
494#include "ex_cmds.h"
495
496/*
497 * Table used to quickly search for a command, based on its first character.
498 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000499static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500{
501 CMD_append,
502 CMD_buffer,
503 CMD_change,
504 CMD_delete,
505 CMD_edit,
506 CMD_file,
507 CMD_global,
508 CMD_help,
509 CMD_insert,
510 CMD_join,
511 CMD_k,
512 CMD_list,
513 CMD_move,
514 CMD_next,
515 CMD_open,
516 CMD_print,
517 CMD_quit,
518 CMD_read,
519 CMD_substitute,
520 CMD_t,
521 CMD_undo,
522 CMD_vglobal,
523 CMD_write,
524 CMD_xit,
525 CMD_yank,
526 CMD_z,
527 CMD_bang
528};
529
530static char_u dollar_command[2] = {'$', 0};
531
532
533#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000534/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535typedef struct
536{
537 char_u *line; /* command line */
538 linenr_T lnum; /* sourcing_lnum of the line */
539} wcmd_T;
540
541/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000542 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000544 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000546struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
548 garray_T *lines_gap; /* growarray with line info */
549 int current_line; /* last read line from growarray */
550 int repeating; /* TRUE when looping a second time */
551 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
552 char_u *(*getline) __ARGS((int, void *, int));
553 void *cookie;
554};
555
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000556static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
557static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000559
560/* Struct to save a few things while debugging. Used in do_cmdline() only. */
561struct dbg_stuff
562{
563 int trylevel;
564 int force_abort;
565 except_T *caught_stack;
566 char_u *vv_exception;
567 char_u *vv_throwpoint;
568 int did_emsg;
569 int got_int;
570 int did_throw;
571 int need_rethrow;
572 int check_cstack;
573 except_T *current_exception;
574};
575
576static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
577static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
578
579 static void
580save_dbg_stuff(dsp)
581 struct dbg_stuff *dsp;
582{
583 dsp->trylevel = trylevel; trylevel = 0;
584 dsp->force_abort = force_abort; force_abort = FALSE;
585 dsp->caught_stack = caught_stack; caught_stack = NULL;
586 dsp->vv_exception = v_exception(NULL);
587 dsp->vv_throwpoint = v_throwpoint(NULL);
588
589 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
590 dsp->did_emsg = did_emsg; did_emsg = FALSE;
591 dsp->got_int = got_int; got_int = FALSE;
592 dsp->did_throw = did_throw; did_throw = FALSE;
593 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
594 dsp->check_cstack = check_cstack; check_cstack = FALSE;
595 dsp->current_exception = current_exception; current_exception = NULL;
596}
597
598 static void
599restore_dbg_stuff(dsp)
600 struct dbg_stuff *dsp;
601{
602 suppress_errthrow = FALSE;
603 trylevel = dsp->trylevel;
604 force_abort = dsp->force_abort;
605 caught_stack = dsp->caught_stack;
606 (void)v_exception(dsp->vv_exception);
607 (void)v_throwpoint(dsp->vv_throwpoint);
608 did_emsg = dsp->did_emsg;
609 got_int = dsp->got_int;
610 did_throw = dsp->did_throw;
611 need_rethrow = dsp->need_rethrow;
612 check_cstack = dsp->check_cstack;
613 current_exception = dsp->current_exception;
614}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615#endif
616
617
618/*
619 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
620 * command is given.
621 */
622 void
623do_exmode(improved)
624 int improved; /* TRUE for "improved Ex" mode */
625{
626 int save_msg_scroll;
627 int prev_msg_row;
628 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000629 int changedtick;
630
631 if (improved)
632 exmode_active = EXMODE_VIM;
633 else
634 exmode_active = EXMODE_NORMAL;
635 State = NORMAL;
636
637 /* When using ":global /pat/ visual" and then "Q" we return to continue
638 * the :global command. */
639 if (global_busy)
640 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641
642 save_msg_scroll = msg_scroll;
643 ++RedrawingDisabled; /* don't redisplay the window */
644 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#ifdef FEAT_GUI
646 /* Ignore scrollbar and mouse events in Ex mode */
647 ++hold_gui_events;
648#endif
649#ifdef FEAT_SNIFF
650 want_sniff_request = 0; /* No K_SNIFF wanted */
651#endif
652
653 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
654 while (exmode_active)
655 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000656#ifdef FEAT_EX_EXTRA
657 /* Check for a ":normal" command and no more characters left. */
658 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
659 {
660 exmode_active = FALSE;
661 break;
662 }
663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 msg_scroll = TRUE;
665 need_wait_return = FALSE;
666 ex_pressedreturn = FALSE;
667 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000668 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 prev_msg_row = msg_row;
670 prev_line = curwin->w_cursor.lnum;
671#ifdef FEAT_SNIFF
672 ProcessSniffRequests();
673#endif
674 if (improved)
675 {
676 cmdline_row = msg_row;
677 do_cmdline(NULL, getexline, NULL, 0);
678 }
679 else
680 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
681 lines_left = Rows - 1;
682
Bram Moolenaardf177f62005-02-22 08:39:57 +0000683 if ((prev_line != curwin->w_cursor.lnum
684 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000686 if (curbuf->b_ml.ml_flags & ML_EMPTY)
687 EMSG(_(e_emptybuf));
688 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000690 if (ex_pressedreturn)
691 {
692 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000693 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000694 msg_row = prev_msg_row;
695 if (prev_msg_row == Rows - 1)
696 msg_row--;
697 }
698 msg_col = 0;
699 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
700 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000703 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
704 {
705 if (curbuf->b_ml.ml_flags & ML_EMPTY)
706 EMSG(_(e_emptybuf));
707 else
708 EMSG(_("E501: At end-of-file"));
709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 }
711
712#ifdef FEAT_GUI
713 --hold_gui_events;
714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 --RedrawingDisabled;
716 --no_wait_return;
717 update_screen(CLEAR);
718 need_wait_return = FALSE;
719 msg_scroll = save_msg_scroll;
720}
721
722/*
723 * Execute a simple command line. Used for translated commands like "*".
724 */
725 int
726do_cmdline_cmd(cmd)
727 char_u *cmd;
728{
729 return do_cmdline(cmd, NULL, NULL,
730 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
731}
732
733/*
734 * do_cmdline(): execute one Ex command line
735 *
736 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100737 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 * 2. Split up in parts separated with '|'.
739 *
740 * This function can be called recursively!
741 *
742 * flags:
743 * DOCMD_VERBOSE - The command will be included in the error message.
744 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100745 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 * DOCMD_KEYTYPED - Don't reset KeyTyped.
747 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
748 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
749 *
750 * return FAIL if cmdline could not be executed, OK otherwise
751 */
752 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100753do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100755 char_u *(*fgetline) __ARGS((int, void *, int));
756 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 int flags;
758{
759 char_u *next_cmdline; /* next cmd to execute */
760 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100761 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 static int recursive = 0; /* recursive depth */
763 int msg_didout_before_start = 0;
764 int count = 0; /* line number count */
765 int did_inc = FALSE; /* incremented RedrawingDisabled */
766 int retval = OK;
767#ifdef FEAT_EVAL
768 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000769 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 int current_line = 0; /* active line in lines_ga */
771 char_u *fname = NULL; /* function or script name */
772 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
773 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000774 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 int initial_trylevel;
776 struct msglist **saved_msg_list = NULL;
777 struct msglist *private_msg_list;
778
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100779 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 char_u *(*cmd_getline) __ARGS((int, void *, int));
781 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000782 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000784 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100786# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787# define cmd_cookie cookie
788#endif
789 static int call_depth = 0; /* recursiveness */
790
791#ifdef FEAT_EVAL
792 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
793 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000794 * This ensures that the do_errthrow() call in do_one_cmd() does not
795 * combine the messages stored by an earlier invocation of do_one_cmd()
796 * with the command name of the later one. This would happen when
797 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 saved_msg_list = msg_list;
799 msg_list = &private_msg_list;
800 private_msg_list = NULL;
801#endif
802
803 /* It's possible to create an endless loop with ":execute", catch that
804 * here. The value of 200 allows nested function calls, ":source", etc. */
805 if (call_depth == 200)
806 {
807 EMSG(_("E169: Command too recursive"));
808#ifdef FEAT_EVAL
809 /* When converting to an exception, we do not include the command name
810 * since this is not an error of the specific command. */
811 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
812 msg_list = saved_msg_list;
813#endif
814 return FAIL;
815 }
816 ++call_depth;
817
818#ifdef FEAT_EVAL
819 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000820 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 cstack.cs_trylevel = 0;
822 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000823 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
825
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100826 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827
828 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100829 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000830 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 ++ex_nesting_level;
832
833 /* Get the function or script name and the address where the next breakpoint
834 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000835 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {
837 fname = func_name(real_cookie);
838 breakpoint = func_breakpoint(real_cookie);
839 dbg_tick = func_dbg_tick(real_cookie);
840 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100841 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 {
843 fname = sourcing_name;
844 breakpoint = source_breakpoint(real_cookie);
845 dbg_tick = source_dbg_tick(real_cookie);
846 }
847
848 /*
849 * Initialize "force_abort" and "suppress_errthrow" at the top level.
850 */
851 if (!recursive)
852 {
853 force_abort = FALSE;
854 suppress_errthrow = FALSE;
855 }
856
857 /*
858 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000859 * exception handling (used when debugging). Otherwise clear it to avoid
860 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000862 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000863 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000864 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200865 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
867 initial_trylevel = trylevel;
868
869 /*
870 * "did_throw" will be set to TRUE when an exception is being thrown.
871 */
872 did_throw = FALSE;
873#endif
874 /*
875 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000876 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 * If force_abort is set, we cancel everything.
878 */
879 did_emsg = FALSE;
880
881 /*
882 * KeyTyped is only set when calling vgetc(). Reset it here when not
883 * calling vgetc() (sourced command lines).
884 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100885 if (!(flags & DOCMD_KEYTYPED)
886 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 KeyTyped = FALSE;
888
889 /*
890 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000891 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 * - for multiple commands on one line, separated with '|'
893 * - when repeating until there are no more lines (for ":source")
894 */
895 next_cmdline = cmdline;
896 do
897 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000898#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100899 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000900#endif
901
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000902 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if (next_cmdline == NULL
904#ifdef FEAT_EVAL
905 && !force_abort
906 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000907 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908#endif
909 )
910 did_emsg = FALSE;
911
912 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000913 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100914 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 * 3. If a line is given: Make a copy, so we can mess with it.
916 */
917
918#ifdef FEAT_EVAL
919 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000920 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 {
922 /* Each '|' separated command is stored separately in lines_ga, to
923 * be able to jump to it. Don't use next_cmdline now. */
924 vim_free(cmdline_copy);
925 cmdline_copy = NULL;
926
927 /* Check if a function has returned or, unless it has an unclosed
928 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000929 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000931# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000932 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000933 func_line_end(real_cookie);
934# endif
935 if (func_has_ended(real_cookie))
936 {
937 retval = FAIL;
938 break;
939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000941#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000942 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100943 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000944 script_line_end();
945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946
947 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100948 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 {
950 retval = FAIL;
951 break;
952 }
953
954 /* If breakpoints have been added/deleted need to check for it. */
955 if (breakpoint != NULL && dbg_tick != NULL
956 && *dbg_tick != debug_tick)
957 {
958 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100959 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 fname, sourcing_lnum);
961 *dbg_tick = debug_tick;
962 }
963
964 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
965 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
966
967 /* Did we encounter a breakpoint? */
968 if (breakpoint != NULL && *breakpoint != 0
969 && *breakpoint <= sourcing_lnum)
970 {
971 dbg_breakpoint(fname, sourcing_lnum);
972 /* Find next breakpoint. */
973 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100974 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 fname, sourcing_lnum);
976 *dbg_tick = debug_tick;
977 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000978# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000979 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000980 {
981 if (getline_is_func)
982 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100983 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000984 script_line_start();
985 }
986# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 }
988
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000991 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100992 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 * below, so that it stores lines in or reads them from
994 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000995 * while/for loop. */
996 cmd_getline = get_loop_line;
997 cmd_cookie = (void *)&cmd_loop_cookie;
998 cmd_loop_cookie.lines_gap = &lines_ga;
999 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001000 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001001 cmd_loop_cookie.cookie = cookie;
1002 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 }
1004 else
1005 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001006 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 cmd_cookie = cookie;
1008 }
1009#endif
1010
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001011 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 if (next_cmdline == NULL)
1013 {
1014 /*
1015 * Need to set msg_didout for the first line after an ":if",
1016 * otherwise the ":if" will be overwritten.
1017 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001018 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001020 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021#ifdef FEAT_EVAL
1022 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1023#else
1024 0
1025#endif
1026 )) == NULL)
1027 {
1028 /* Don't call wait_return for aborted command line. The NULL
1029 * returned for the end of a sourced file or executed function
1030 * doesn't do this. */
1031 if (KeyTyped && !(flags & DOCMD_REPEAT))
1032 need_wait_return = FALSE;
1033 retval = FAIL;
1034 break;
1035 }
1036 used_getline = TRUE;
1037
1038 /*
1039 * Keep the first typed line. Clear it when more lines are typed.
1040 */
1041 if (flags & DOCMD_KEEPLINE)
1042 {
1043 vim_free(repeat_cmdline);
1044 if (count == 0)
1045 repeat_cmdline = vim_strsave(next_cmdline);
1046 else
1047 repeat_cmdline = NULL;
1048 }
1049 }
1050
1051 /* 3. Make a copy of the command so we can mess with it. */
1052 else if (cmdline_copy == NULL)
1053 {
1054 next_cmdline = vim_strsave(next_cmdline);
1055 if (next_cmdline == NULL)
1056 {
1057 EMSG(_(e_outofmem));
1058 retval = FAIL;
1059 break;
1060 }
1061 }
1062 cmdline_copy = next_cmdline;
1063
1064#ifdef FEAT_EVAL
1065 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001066 * Save the current line when inside a ":while" or ":for", and when
1067 * the command looks like a ":while" or ":for", because we may need it
1068 * later. When there is a '|' and another command, it is stored
1069 * separately, because we need to be able to jump back to it from an
1070 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001072 if (current_line == lines_ga.ga_len
1073 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001075 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 {
1077 retval = FAIL;
1078 break;
1079 }
1080 }
1081 did_endif = FALSE;
1082#endif
1083
1084 if (count++ == 0)
1085 {
1086 /*
1087 * All output from the commands is put below each other, without
1088 * waiting for a return. Don't do this when executing commands
1089 * from a script or when being called recursive (e.g. for ":e
1090 * +command file").
1091 */
1092 if (!(flags & DOCMD_NOWAIT) && !recursive)
1093 {
1094 msg_didout_before_start = msg_didout;
1095 msg_didany = FALSE; /* no output yet */
1096 msg_start();
1097 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001098 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 ++RedrawingDisabled;
1100 did_inc = TRUE;
1101 }
1102 }
1103
1104 if (p_verbose >= 15 && sourcing_name != NULL)
1105 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001107 verbose_enter_scroll();
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 smsg((char_u *)_("line %ld: %s"),
1110 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001111 if (msg_silent == 0)
1112 msg_puts((char_u *)"\n"); /* don't overwrite this */
1113
1114 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 --no_wait_return;
1116 }
1117
1118 /*
1119 * 2. Execute one '|' separated command.
1120 * do_one_cmd() will return NULL if there is no trailing '|'.
1121 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1122 */
1123 ++recursive;
1124 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1125#ifdef FEAT_EVAL
1126 &cstack,
1127#endif
1128 cmd_getline, cmd_cookie);
1129 --recursive;
1130
1131#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001132 if (cmd_cookie == (void *)&cmd_loop_cookie)
1133 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001135 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136#endif
1137
1138 if (next_cmdline == NULL)
1139 {
1140 vim_free(cmdline_copy);
1141 cmdline_copy = NULL;
1142#ifdef FEAT_CMDHIST
1143 /*
1144 * If the command was typed, remember it for the ':' register.
1145 * Do this AFTER executing the command to make :@: work.
1146 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001147 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 && new_last_cmdline != NULL)
1149 {
1150 vim_free(last_cmdline);
1151 last_cmdline = new_last_cmdline;
1152 new_last_cmdline = NULL;
1153 }
1154#endif
1155 }
1156 else
1157 {
1158 /* need to copy the command after the '|' to cmdline_copy, for the
1159 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001160 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 next_cmdline = cmdline_copy;
1162 }
1163
1164
1165#ifdef FEAT_EVAL
1166 /* reset did_emsg for a function that is not aborted by an error */
1167 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001168 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 && !func_has_abort(real_cookie))
1170 did_emsg = FALSE;
1171
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
1174 ++current_line;
1175
1176 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001177 * An ":endwhile", ":endfor" and ":continue" is handled here.
1178 * If we were executing commands, jump back to the ":while" or
1179 * ":for".
1180 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 if (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 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001186 /* Jump back to the matching ":while" or ":for". Be careful
1187 * not to use a cs_line[] from an entry that isn't a ":while"
1188 * or ":for": It would make "current_line" invalid and can
1189 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (!did_emsg && !got_int && !did_throw
1191 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001192 && (cstack.cs_flags[cstack.cs_idx]
1193 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 && cstack.cs_line[cstack.cs_idx] >= 0
1195 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1196 {
1197 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001198 /* remember we jumped there */
1199 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 line_breakcheck(); /* check if CTRL-C typed */
1201
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001202 /* Check for the next breakpoint at or after the ":while"
1203 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 if (breakpoint != NULL)
1205 {
1206 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001207 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 fname,
1209 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1210 *dbg_tick = debug_tick;
1211 }
1212 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001213 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001215 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001217 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1218 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 }
1220 }
1221
1222 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001227 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1229 }
1230 }
1231
1232 /*
1233 * When not inside any ":while" loop, clear remembered lines.
1234 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001235 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 {
1237 if (lines_ga.ga_len > 0)
1238 {
1239 sourcing_lnum =
1240 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1241 free_cmdlines(&lines_ga);
1242 }
1243 current_line = 0;
1244 }
1245
1246 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001247 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1248 * being restored at the ":endtry". Reset them here and set the
1249 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1250 * This includes the case where a missing ":endif", ":endwhile" or
1251 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001253 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001255 cstack.cs_lflags &= ~CSL_HAD_FINA;
1256 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1257 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 did_throw ? (void *)current_exception : NULL);
1259 did_emsg = got_int = did_throw = FALSE;
1260 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1261 }
1262
1263 /* Update global "trylevel" for recursive calls to do_cmdline() from
1264 * within this loop. */
1265 trylevel = initial_trylevel + cstack.cs_trylevel;
1266
1267 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001268 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 * files) is aborted because of an error, an interrupt, or an uncaught
1270 * exception, cancel everything. If it is left normally, reset
1271 * force_abort to get the non-EH compatible abortion behavior for
1272 * the rest of the script.
1273 */
1274 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1275 force_abort = FALSE;
1276
1277 /* Convert an interrupt to an exception if appropriate. */
1278 (void)do_intthrow(&cstack);
1279#endif /* FEAT_EVAL */
1280
1281 }
1282 /*
1283 * Continue executing command lines when:
1284 * - no CTRL-C typed, no aborting error, no exception thrown or try
1285 * conditionals need to be checked for executing finally clauses or
1286 * catching an interrupt exception
1287 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001288 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 * looping for ":source" command or function call.
1290 */
1291 while (!((got_int
1292#ifdef FEAT_EVAL
1293 || (did_emsg && force_abort) || did_throw
1294#endif
1295 )
1296#ifdef FEAT_EVAL
1297 && cstack.cs_trylevel == 0
1298#endif
1299 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001300 && !(did_emsg
1301#ifdef FEAT_EVAL
1302 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001303 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001304 * the :endtry to be missed. */
1305 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1306#endif
1307 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001308 && (getline_equal(fgetline, cookie, getexmodeline)
1309 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 && (next_cmdline != NULL
1311#ifdef FEAT_EVAL
1312 || cstack.cs_idx >= 0
1313#endif
1314 || (flags & DOCMD_REPEAT)));
1315
1316 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001317 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318#ifdef FEAT_EVAL
1319 free_cmdlines(&lines_ga);
1320 ga_clear(&lines_ga);
1321
1322 if (cstack.cs_idx >= 0)
1323 {
1324 /*
1325 * If a sourced file or executed function ran to its end, report the
1326 * unclosed conditional.
1327 */
1328 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001329 && ((getline_equal(fgetline, cookie, getsourceline)
1330 && !source_finished(fgetline, cookie))
1331 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 && !func_has_ended(real_cookie))))
1333 {
1334 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1335 EMSG(_(e_endtry));
1336 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1337 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001338 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1339 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 else
1341 EMSG(_(e_endif));
1342 }
1343
1344 /*
1345 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1346 * ":endtry" in a sourced file or executed function. If the try
1347 * conditional is in its finally clause, ignore anything pending.
1348 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001349 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 */
1351 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001352 {
1353 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1354
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001355 if (idx >= 0)
1356 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001357 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1358 &cstack.cs_looplevel);
1359 }
1360 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 trylevel = initial_trylevel;
1362 }
1363
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001364 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1365 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001367 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 ? (char_u *)"endfunction" : (char_u *)NULL);
1369
1370 if (trylevel == 0)
1371 {
1372 /*
1373 * When an exception is being thrown out of the outermost try
1374 * conditional, discard the uncaught exception, disable the conversion
1375 * of interrupts or errors to exceptions, and ensure that no more
1376 * commands are executed.
1377 */
1378 if (did_throw)
1379 {
1380 void *p = NULL;
1381 char_u *saved_sourcing_name;
1382 int saved_sourcing_lnum;
1383 struct msglist *messages = NULL, *next;
1384
1385 /*
1386 * If the uncaught exception is a user exception, report it as an
1387 * error. If it is an error exception, display the saved error
1388 * message now. For an interrupt exception, do nothing; the
1389 * interrupt message is given elsewhere.
1390 */
1391 switch (current_exception->type)
1392 {
1393 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001394 vim_snprintf((char *)IObuff, IOSIZE,
1395 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 current_exception->value);
1397 p = vim_strsave(IObuff);
1398 break;
1399 case ET_ERROR:
1400 messages = current_exception->messages;
1401 current_exception->messages = NULL;
1402 break;
1403 case ET_INTERRUPT:
1404 break;
1405 default:
1406 p = vim_strsave((char_u *)_(e_internal));
1407 }
1408
1409 saved_sourcing_name = sourcing_name;
1410 saved_sourcing_lnum = sourcing_lnum;
1411 sourcing_name = current_exception->throw_name;
1412 sourcing_lnum = current_exception->throw_lnum;
1413 current_exception->throw_name = NULL;
1414
1415 discard_current_exception(); /* uses IObuff if 'verbose' */
1416 suppress_errthrow = TRUE;
1417 force_abort = TRUE;
1418
1419 if (messages != NULL)
1420 {
1421 do
1422 {
1423 next = messages->next;
1424 emsg(messages->msg);
1425 vim_free(messages->msg);
1426 vim_free(messages);
1427 messages = next;
1428 }
1429 while (messages != NULL);
1430 }
1431 else if (p != NULL)
1432 {
1433 emsg(p);
1434 vim_free(p);
1435 }
1436 vim_free(sourcing_name);
1437 sourcing_name = saved_sourcing_name;
1438 sourcing_lnum = saved_sourcing_lnum;
1439 }
1440
1441 /*
1442 * On an interrupt or an aborting error not converted to an exception,
1443 * disable the conversion of errors to exceptions. (Interrupts are not
1444 * converted any more, here.) This enables also the interrupt message
1445 * when force_abort is set and did_emsg unset in case of an interrupt
1446 * from a finally clause after an error.
1447 */
1448 else if (got_int || (did_emsg && force_abort))
1449 suppress_errthrow = TRUE;
1450 }
1451
1452 /*
1453 * The current cstack will be freed when do_cmdline() returns. An uncaught
1454 * exception will have to be rethrown in the previous cstack. If a function
1455 * has just returned or a script file was just finished and the previous
1456 * cstack belongs to the same function or, respectively, script file, it
1457 * will have to be checked for finally clauses to be executed due to the
1458 * ":return" or ":finish". This is done in do_one_cmd().
1459 */
1460 if (did_throw)
1461 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001462 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001464 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 && ex_nesting_level > func_level(real_cookie) + 1))
1466 {
1467 if (!did_throw)
1468 check_cstack = TRUE;
1469 }
1470 else
1471 {
1472 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001473 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 --ex_nesting_level;
1475 /*
1476 * Go to debug mode when returning from a function in which we are
1477 * single-stepping.
1478 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001479 if ((getline_equal(fgetline, cookie, getsourceline)
1480 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001482 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 ? (char_u *)_("End of sourced file")
1484 : (char_u *)_("End of function"));
1485 }
1486
1487 /*
1488 * Restore the exception environment (done after returning from the
1489 * debugger).
1490 */
1491 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001492 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
1494 msg_list = saved_msg_list;
1495#endif /* FEAT_EVAL */
1496
1497 /*
1498 * If there was too much output to fit on the command line, ask the user to
1499 * hit return before redrawing the screen. With the ":global" command we do
1500 * this only once after the command is finished.
1501 */
1502 if (did_inc)
1503 {
1504 --RedrawingDisabled;
1505 --no_wait_return;
1506 msg_scroll = FALSE;
1507
1508 /*
1509 * When just finished an ":if"-":else" which was typed, no need to
1510 * wait for hit-return. Also for an error situation.
1511 */
1512 if (retval == FAIL
1513#ifdef FEAT_EVAL
1514 || (did_endif && KeyTyped && !did_emsg)
1515#endif
1516 )
1517 {
1518 need_wait_return = FALSE;
1519 msg_didany = FALSE; /* don't wait when restarting edit */
1520 }
1521 else if (need_wait_return)
1522 {
1523 /*
1524 * The msg_start() above clears msg_didout. The wait_return we do
1525 * here should not overwrite the command that may be shown before
1526 * doing that.
1527 */
1528 msg_didout |= msg_didout_before_start;
1529 wait_return(FALSE);
1530 }
1531 }
1532
Bram Moolenaar2a942252012-11-28 23:03:07 +01001533#ifdef FEAT_EVAL
1534 did_endif = FALSE; /* in case do_cmdline used recursively */
1535#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 /*
1537 * Reset if_level, in case a sourced script file contains more ":if" than
1538 * ":endif" (could be ":if x | foo | endif").
1539 */
1540 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001541#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 --call_depth;
1544 return retval;
1545}
1546
1547#ifdef FEAT_EVAL
1548/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001549 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 */
1551 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001552get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 int c;
1554 void *cookie;
1555 int indent;
1556{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001557 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 wcmd_T *wp;
1559 char_u *line;
1560
1561 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1562 {
1563 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001564 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001566 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 if (cp->getline == NULL)
1568 line = getcmdline(c, 0L, indent);
1569 else
1570 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001571 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 ++cp->current_line;
1573
1574 return line;
1575 }
1576
1577 KeyTyped = FALSE;
1578 ++cp->current_line;
1579 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1580 sourcing_lnum = wp->lnum;
1581 return vim_strsave(wp->line);
1582}
1583
1584/*
1585 * Store a line in "gap" so that a ":while" loop can execute it again.
1586 */
1587 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001588store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 garray_T *gap;
1590 char_u *line;
1591{
1592 if (ga_grow(gap, 1) == FAIL)
1593 return FAIL;
1594 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1595 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1596 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 return OK;
1598}
1599
1600/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001601 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 */
1603 static void
1604free_cmdlines(gap)
1605 garray_T *gap;
1606{
1607 while (gap->ga_len > 0)
1608 {
1609 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1610 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 }
1612}
1613#endif
1614
1615/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001616 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1617 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001620getline_equal(fgetline, cookie, func)
1621 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001622 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 char_u *(*func) __ARGS((int, void *, int));
1624{
1625#ifdef FEAT_EVAL
1626 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001627 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
Bram Moolenaar89d40322006-08-29 15:30:07 +00001629 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001630 * function that's originally used to obtain the lines. This may be
1631 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001632 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001633 cp = (struct loop_cookie *)cookie;
1634 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 {
1636 gp = cp->getline;
1637 cp = cp->cookie;
1638 }
1639 return gp == func;
1640#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001641 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642#endif
1643}
1644
1645#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1646/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001647 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 * getline function. Otherwise return "cookie".
1649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001651getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001652 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001653 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654{
1655# ifdef FEAT_EVAL
1656 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001657 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
Bram Moolenaar89d40322006-08-29 15:30:07 +00001659 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001660 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001662 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001663 cp = (struct loop_cookie *)cookie;
1664 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 {
1666 gp = cp->getline;
1667 cp = cp->cookie;
1668 }
1669 return cp;
1670# else
1671 return cookie;
1672# endif
1673}
1674#endif
1675
1676/*
1677 * Execute one Ex command.
1678 *
1679 * If 'sourcing' is TRUE, the command will be included in the error message.
1680 *
1681 * 1. skip comment lines and leading space
1682 * 2. handle command modifiers
1683 * 3. parse range
1684 * 4. parse command
1685 * 5. parse arguments
1686 * 6. switch on command name
1687 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001688 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 *
1690 * This function may be called recursively!
1691 */
1692#if (_MSC_VER == 1200)
1693/*
Bram Moolenaared203462004-06-16 11:19:22 +00001694 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001696 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697#endif
1698 static char_u *
1699do_one_cmd(cmdlinep, sourcing,
1700#ifdef FEAT_EVAL
1701 cstack,
1702#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001703 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 char_u **cmdlinep;
1705 int sourcing;
1706#ifdef FEAT_EVAL
1707 struct condstack *cstack;
1708#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001709 char_u *(*fgetline) __ARGS((int, void *, int));
1710 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711{
1712 char_u *p;
1713 linenr_T lnum;
1714 long n;
1715 char_u *errormsg = NULL; /* error message */
1716 exarg_T ea; /* Ex command arguments */
1717 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001718 int save_msg_scroll = msg_scroll;
1719 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001721#ifdef HAVE_SANDBOX
1722 int did_sandbox = FALSE;
1723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 cmdmod_T save_cmdmod;
1725 int ni; /* set when Not Implemented */
1726
1727 vim_memset(&ea, 0, sizeof(ea));
1728 ea.line1 = 1;
1729 ea.line2 = 1;
1730#ifdef FEAT_EVAL
1731 ++ex_nesting_level;
1732#endif
1733
Bram Moolenaar21691f82012-12-05 19:13:18 +01001734 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 if (quitmore
1736#ifdef FEAT_EVAL
1737 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001738 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001739#endif
1740#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001741 /* avoid that an autocommand, e.g. QuitPre, does this */
1742 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743#endif
1744 )
1745 --quitmore;
1746
1747 /*
1748 * Reset browse, confirm, etc.. They are restored when returning, for
1749 * recursive calls.
1750 */
1751 save_cmdmod = cmdmod;
1752 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1753
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001754 /* "#!anything" is handled like a comment. */
1755 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1756 goto doend;
1757
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 /*
1759 * Repeat until no more command modifiers are found.
1760 */
1761 ea.cmd = *cmdlinep;
1762 for (;;)
1763 {
1764/*
1765 * 1. skip comment lines and leading white space and colons
1766 */
1767 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1768 ++ea.cmd;
1769
1770 /* in ex mode, an empty line works like :+ */
1771 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001772 && (getline_equal(fgetline, cookie, getexmodeline)
1773 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001774 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 {
1776 ea.cmd = (char_u *)"+";
1777 ex_pressedreturn = TRUE;
1778 }
1779
1780 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001781 if (*ea.cmd == '"')
1782 goto doend;
1783 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001784 {
1785 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
1789/*
1790 * 2. handle command modifiers.
1791 */
1792 p = ea.cmd;
1793 if (VIM_ISDIGIT(*ea.cmd))
1794 p = skipwhite(skipdigits(ea.cmd));
1795 switch (*p)
1796 {
1797 /* When adding an entry, also modify cmd_exists(). */
1798 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1799 break;
1800#ifdef FEAT_WINDOWS
1801 cmdmod.split |= WSP_ABOVE;
1802#endif
1803 continue;
1804
1805 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1806 {
1807#ifdef FEAT_WINDOWS
1808 cmdmod.split |= WSP_BELOW;
1809#endif
1810 continue;
1811 }
1812 if (checkforcmd(&ea.cmd, "browse", 3))
1813 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001814#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 cmdmod.browse = TRUE;
1816#endif
1817 continue;
1818 }
1819 if (!checkforcmd(&ea.cmd, "botright", 2))
1820 break;
1821#ifdef FEAT_WINDOWS
1822 cmdmod.split |= WSP_BOT;
1823#endif
1824 continue;
1825
1826 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1827 break;
1828#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1829 cmdmod.confirm = TRUE;
1830#endif
1831 continue;
1832
1833 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1834 {
1835 cmdmod.keepmarks = TRUE;
1836 continue;
1837 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001838 if (checkforcmd(&ea.cmd, "keepalt", 5))
1839 {
1840 cmdmod.keepalt = TRUE;
1841 continue;
1842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1844 break;
1845 cmdmod.keepjumps = TRUE;
1846 continue;
1847
1848 /* ":hide" and ":hide | cmd" are not modifiers */
1849 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1850 || *p == NUL || ends_excmd(*p))
1851 break;
1852 ea.cmd = p;
1853 cmdmod.hide = TRUE;
1854 continue;
1855
1856 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1857 {
1858 cmdmod.lockmarks = TRUE;
1859 continue;
1860 }
1861
1862 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1863 break;
1864#ifdef FEAT_WINDOWS
1865 cmdmod.split |= WSP_ABOVE;
1866#endif
1867 continue;
1868
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001869 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1870 break;
1871#ifdef FEAT_AUTOCMD
1872 if (cmdmod.save_ei == NULL)
1873 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001874 /* Set 'eventignore' to "all". Restore the
1875 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001876 cmdmod.save_ei = vim_strsave(p_ei);
1877 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001878 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001879 }
1880#endif
1881 continue;
1882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1884 break;
1885#ifdef FEAT_WINDOWS
1886 cmdmod.split |= WSP_BELOW;
1887#endif
1888 continue;
1889
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001890 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1891 {
1892#ifdef HAVE_SANDBOX
1893 if (!did_sandbox)
1894 ++sandbox;
1895 did_sandbox = TRUE;
1896#endif
1897 continue;
1898 }
1899 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001901 if (save_msg_silent == -1)
1902 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1905 {
1906 /* ":silent!", but not "silent !cmd" */
1907 ea.cmd = skipwhite(ea.cmd + 1);
1908 ++emsg_silent;
1909 ++did_esilent;
1910 }
1911 continue;
1912
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001913 case 't': if (checkforcmd(&p, "tab", 3))
1914 {
1915#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001916 if (vim_isdigit(*ea.cmd))
1917 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1918 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001919 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001920 ea.cmd = p;
1921#endif
1922 continue;
1923 }
1924 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 break;
1926#ifdef FEAT_WINDOWS
1927 cmdmod.split |= WSP_TOP;
1928#endif
1929 continue;
1930
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001931 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1932 break;
1933 if (save_msg_silent == -1)
1934 save_msg_silent = msg_silent;
1935 msg_silent = 0;
1936 continue;
1937
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1939 {
1940#ifdef FEAT_VERTSPLIT
1941 cmdmod.split |= WSP_VERT;
1942#endif
1943 continue;
1944 }
1945 if (!checkforcmd(&p, "verbose", 4))
1946 break;
1947 if (verbose_save < 0)
1948 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001949 if (vim_isdigit(*ea.cmd))
1950 p_verbose = atoi((char *)ea.cmd);
1951 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 p_verbose = 1;
1953 ea.cmd = p;
1954 continue;
1955 }
1956 break;
1957 }
1958
1959#ifdef FEAT_EVAL
1960 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1961 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1962#else
1963 ea.skip = (if_level > 0);
1964#endif
1965
1966#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001967# ifdef FEAT_PROFILE
1968 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001969 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001970 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001971 if (getline_equal(fgetline, cookie, get_func_line))
1972 func_line_exec(getline_cookie(fgetline, cookie));
1973 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001974 script_line_exec();
1975 }
1976#endif
1977
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 /* May go to debug mode. If this happens and the ">quit" debug command is
1979 * used, throw an interrupt exception and skip the next command. */
1980 dbg_check_breakpoint(&ea);
1981 if (!ea.skip && got_int)
1982 {
1983 ea.skip = TRUE;
1984 (void)do_intthrow(cstack);
1985 }
1986#endif
1987
1988/*
1989 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1990 *
1991 * where 'addr' is:
1992 *
1993 * % (entire file)
1994 * $ [+-NUM]
1995 * 'x [+-NUM] (where x denotes a currently defined mark)
1996 * . [+-NUM]
1997 * [+-NUM]..
1998 * NUM
1999 *
2000 * The ea.cmd pointer is updated to point to the first character following the
2001 * range spec. If an initial address is found, but no second, the upper bound
2002 * is equal to the lower.
2003 */
2004
2005 /* repeat for all ',' or ';' separated addresses */
2006 for (;;)
2007 {
2008 ea.line1 = ea.line2;
2009 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
2010 ea.cmd = skipwhite(ea.cmd);
2011 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
2012 if (ea.cmd == NULL) /* error detected */
2013 goto doend;
2014 if (lnum == MAXLNUM)
2015 {
2016 if (*ea.cmd == '%') /* '%' - all lines */
2017 {
2018 ++ea.cmd;
2019 ea.line1 = 1;
2020 ea.line2 = curbuf->b_ml.ml_line_count;
2021 ++ea.addr_count;
2022 }
2023 /* '*' - visual area */
2024 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2025 {
2026 pos_T *fp;
2027
2028 ++ea.cmd;
2029 if (!ea.skip)
2030 {
2031 fp = getmark('<', FALSE);
2032 if (check_mark(fp) == FAIL)
2033 goto doend;
2034 ea.line1 = fp->lnum;
2035 fp = getmark('>', FALSE);
2036 if (check_mark(fp) == FAIL)
2037 goto doend;
2038 ea.line2 = fp->lnum;
2039 ++ea.addr_count;
2040 }
2041 }
2042 }
2043 else
2044 ea.line2 = lnum;
2045 ea.addr_count++;
2046
2047 if (*ea.cmd == ';')
2048 {
2049 if (!ea.skip)
2050 curwin->w_cursor.lnum = ea.line2;
2051 }
2052 else if (*ea.cmd != ',')
2053 break;
2054 ++ea.cmd;
2055 }
2056
2057 /* One address given: set start and end lines */
2058 if (ea.addr_count == 1)
2059 {
2060 ea.line1 = ea.line2;
2061 /* ... but only implicit: really no address given */
2062 if (lnum == MAXLNUM)
2063 ea.addr_count = 0;
2064 }
2065
2066 /* Don't leave the cursor on an illegal line (caused by ';') */
2067 check_cursor_lnum();
2068
2069/*
2070 * 4. parse command
2071 */
2072
2073 /*
2074 * Skip ':' and any white space
2075 */
2076 ea.cmd = skipwhite(ea.cmd);
2077 while (*ea.cmd == ':')
2078 ea.cmd = skipwhite(ea.cmd + 1);
2079
2080 /*
2081 * If we got a line, but no command, then go to the line.
2082 * If we find a '|' or '\n' we set ea.nextcmd.
2083 */
2084 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2085 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2086 {
2087 /*
2088 * strange vi behaviour:
2089 * ":3" jumps to line 3
2090 * ":3|..." prints line 3
2091 * ":|" prints current line
2092 */
2093 if (ea.skip) /* skip this if inside :if */
2094 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002095 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 {
2097 ea.cmdidx = CMD_print;
2098 ea.argt = RANGE+COUNT+TRLBAR;
2099 if ((errormsg = invalid_range(&ea)) == NULL)
2100 {
2101 correct_range(&ea);
2102 ex_print(&ea);
2103 }
2104 }
2105 else if (ea.addr_count != 0)
2106 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002107 if (ea.line2 > curbuf->b_ml.ml_line_count)
2108 {
2109 /* With '-' in 'cpoptions' a line number past the file is an
2110 * error, otherwise put it at the end of the file. */
2111 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2112 ea.line2 = -1;
2113 else
2114 ea.line2 = curbuf->b_ml.ml_line_count;
2115 }
2116
2117 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002118 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 else
2120 {
2121 if (ea.line2 == 0)
2122 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 else
2124 curwin->w_cursor.lnum = ea.line2;
2125 beginline(BL_SOL | BL_FIX);
2126 }
2127 }
2128 goto doend;
2129 }
2130
2131 /* Find the command and let "p" point to after it. */
2132 p = find_command(&ea, NULL);
2133
2134#ifdef FEAT_USR_CMDS
2135 if (p == NULL)
2136 {
2137 if (!ea.skip)
2138 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2139 goto doend;
2140 }
2141 /* Check for wrong commands. */
2142 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2143 {
2144 errormsg = uc_fun_cmd();
2145 goto doend;
2146 }
2147#endif
2148 if (ea.cmdidx == CMD_SIZE)
2149 {
2150 if (!ea.skip)
2151 {
2152 STRCPY(IObuff, _("E492: Not an editor command"));
2153 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002154 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002156 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
2158 goto doend;
2159 }
2160
2161 ni = (
2162#ifdef FEAT_USR_CMDS
2163 !USER_CMDIDX(ea.cmdidx) &&
2164#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002165 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002166#ifdef HAVE_EX_SCRIPT_NI
2167 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2168#endif
2169 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
2171#ifndef FEAT_EVAL
2172 /*
2173 * When the expression evaluation is disabled, recognize the ":if" and
2174 * ":endif" commands and ignore everything in between it.
2175 */
2176 if (ea.cmdidx == CMD_if)
2177 ++if_level;
2178 if (if_level)
2179 {
2180 if (ea.cmdidx == CMD_endif)
2181 --if_level;
2182 goto doend;
2183 }
2184
2185#endif
2186
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002187 /* forced commands */
2188 if (*p == '!' && ea.cmdidx != CMD_substitute
2189 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 {
2191 ++p;
2192 ea.forceit = TRUE;
2193 }
2194 else
2195 ea.forceit = FALSE;
2196
2197/*
2198 * 5. parse arguments
2199 */
2200#ifdef FEAT_USR_CMDS
2201 if (!USER_CMDIDX(ea.cmdidx))
2202#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002203 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204
2205 if (!ea.skip)
2206 {
2207#ifdef HAVE_SANDBOX
2208 if (sandbox != 0 && !(ea.argt & SBOXOK))
2209 {
2210 /* Command not allowed in sandbox. */
2211 errormsg = (char_u *)_(e_sandbox);
2212 goto doend;
2213 }
2214#endif
2215 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2216 {
2217 /* Command not allowed in non-'modifiable' buffer */
2218 errormsg = (char_u *)_(e_modifiable);
2219 goto doend;
2220 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002221
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002222 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223# ifdef FEAT_USR_CMDS
2224 && !USER_CMDIDX(ea.cmdidx)
2225# endif
2226 )
2227 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002228 /* Command not allowed when editing the command line. */
2229#ifdef FEAT_CMDWIN
2230 if (cmdwin_type != 0)
2231 errormsg = (char_u *)_(e_cmdwin);
2232 else
2233#endif
2234 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 goto doend;
2236 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002237#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002238 /* Disallow editing another buffer when "curbuf_lock" is set.
2239 * Do allow ":edit" (check for argument later).
2240 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002241 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002242 && ea.cmdidx != CMD_edit
2243 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002244# ifdef FEAT_USR_CMDS
2245 && !USER_CMDIDX(ea.cmdidx)
2246# endif
2247 && curbuf_locked())
2248 goto doend;
2249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250
2251 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2252 {
2253 /* no range allowed */
2254 errormsg = (char_u *)_(e_norange);
2255 goto doend;
2256 }
2257 }
2258
2259 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2260 {
2261 errormsg = (char_u *)_(e_nobang);
2262 goto doend;
2263 }
2264
2265 /*
2266 * Don't complain about the range if it is not used
2267 * (could happen if line_count is accidentally set to 0).
2268 */
2269 if (!ea.skip && !ni)
2270 {
2271 /*
2272 * If the range is backwards, ask for confirmation and, if given, swap
2273 * ea.line1 & ea.line2 so it's forwards again.
2274 * When global command is busy, don't ask, will fail below.
2275 */
2276 if (!global_busy && ea.line1 > ea.line2)
2277 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002278 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002280 if (sourcing || exmode_active)
2281 {
2282 errormsg = (char_u *)_("E493: Backwards range given");
2283 goto doend;
2284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (ask_yesno((char_u *)
2286 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002287 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 }
2289 lnum = ea.line1;
2290 ea.line1 = ea.line2;
2291 ea.line2 = lnum;
2292 }
2293 if ((errormsg = invalid_range(&ea)) != NULL)
2294 goto doend;
2295 }
2296
2297 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2298 ea.line2 = 1;
2299
2300 correct_range(&ea);
2301
2302#ifdef FEAT_FOLDING
2303 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2304 {
2305 /* Put the first line at the start of a closed fold, put the last line
2306 * at the end of a closed fold. */
2307 (void)hasFolding(ea.line1, &ea.line1, NULL);
2308 (void)hasFolding(ea.line2, NULL, &ea.line2);
2309 }
2310#endif
2311
2312#ifdef FEAT_QUICKFIX
2313 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002314 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 * option here, so things like % get expanded.
2316 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002317 p = replace_makeprg(&ea, p, cmdlinep);
2318 if (p == NULL)
2319 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320#endif
2321
2322 /*
2323 * Skip to start of argument.
2324 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2325 */
2326 if (ea.cmdidx == CMD_bang)
2327 ea.arg = p;
2328 else
2329 ea.arg = skipwhite(p);
2330
2331 /*
2332 * Check for "++opt=val" argument.
2333 * Must be first, allow ":w ++enc=utf8 !cmd"
2334 */
2335 if (ea.argt & ARGOPT)
2336 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2337 if (getargopt(&ea) == FAIL && !ni)
2338 {
2339 errormsg = (char_u *)_(e_invarg);
2340 goto doend;
2341 }
2342
2343 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2344 {
2345 if (*ea.arg == '>') /* append */
2346 {
2347 if (*++ea.arg != '>') /* typed wrong */
2348 {
2349 errormsg = (char_u *)_("E494: Use w or w>>");
2350 goto doend;
2351 }
2352 ea.arg = skipwhite(ea.arg + 1);
2353 ea.append = TRUE;
2354 }
2355 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2356 {
2357 ++ea.arg;
2358 ea.usefilter = TRUE;
2359 }
2360 }
2361
2362 if (ea.cmdidx == CMD_read)
2363 {
2364 if (ea.forceit)
2365 {
2366 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2367 ea.forceit = FALSE;
2368 }
2369 else if (*ea.arg == '!') /* :r !filter */
2370 {
2371 ++ea.arg;
2372 ea.usefilter = TRUE;
2373 }
2374 }
2375
2376 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2377 {
2378 ea.amount = 1;
2379 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2380 {
2381 ++ea.arg;
2382 ++ea.amount;
2383 }
2384 ea.arg = skipwhite(ea.arg);
2385 }
2386
2387 /*
2388 * Check for "+command" argument, before checking for next command.
2389 * Don't do this for ":read !cmd" and ":write !cmd".
2390 */
2391 if ((ea.argt & EDITCMD) && !ea.usefilter)
2392 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2393
2394 /*
2395 * Check for '|' to separate commands and '"' to start comments.
2396 * Don't do this for ":read !cmd" and ":write !cmd".
2397 */
2398 if ((ea.argt & TRLBAR) && !ea.usefilter)
2399 separate_nextcmd(&ea);
2400
2401 /*
2402 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002403 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2404 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002406 else if (ea.cmdidx == CMD_bang
2407 || ea.cmdidx == CMD_global
2408 || ea.cmdidx == CMD_vglobal
2409 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
2411 for (p = ea.arg; *p; ++p)
2412 {
2413 /* Remove one backslash before a newline, so that it's possible to
2414 * pass a newline to the shell and also a newline that is preceded
2415 * with a backslash. This makes it impossible to end a shell
2416 * command in a backslash, but that doesn't appear useful.
2417 * Halving the number of backslashes is incompatible with previous
2418 * versions. */
2419 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002420 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 else if (*p == '\n')
2422 {
2423 ea.nextcmd = p + 1;
2424 *p = NUL;
2425 break;
2426 }
2427 }
2428 }
2429
2430 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2431 {
2432 ea.line1 = 1;
2433 ea.line2 = curbuf->b_ml.ml_line_count;
2434 }
2435
2436 /* accept numbered register only when no count allowed (:put) */
2437 if ( (ea.argt & REGSTR)
2438 && *ea.arg != NUL
2439#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 /* Do not allow register = for user commands */
2441 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442#endif
2443 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2444 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002445#ifndef FEAT_CLIPBOARD
2446 /* check these explicitly for a more specific error message */
2447 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002449 errormsg = (char_u *)_(e_invalidreg);
2450 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 }
2452#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002453 if (
2454#ifdef FEAT_USR_CMDS
2455 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2456 && USER_CMDIDX(ea.cmdidx)))
2457#else
2458 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2459#endif
2460 )
2461 {
2462 ea.regname = *ea.arg++;
2463#ifdef FEAT_EVAL
2464 /* for '=' register: accept the rest of the line as an expression */
2465 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2466 {
2467 set_expr_line(vim_strsave(ea.arg));
2468 ea.arg += STRLEN(ea.arg);
2469 }
2470#endif
2471 ea.arg = skipwhite(ea.arg);
2472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 }
2474
2475 /*
2476 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2477 * count, it's a buffer name.
2478 */
2479 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2480 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2481 || vim_iswhite(*p)))
2482 {
2483 n = getdigits(&ea.arg);
2484 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002485 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
2487 errormsg = (char_u *)_(e_zerocount);
2488 goto doend;
2489 }
2490 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2491 {
2492 ea.line2 = n;
2493 if (ea.addr_count == 0)
2494 ea.addr_count = 1;
2495 }
2496 else
2497 {
2498 ea.line1 = ea.line2;
2499 ea.line2 += n - 1;
2500 ++ea.addr_count;
2501 /*
2502 * Be vi compatible: no error message for out of range.
2503 */
2504 if (ea.line2 > curbuf->b_ml.ml_line_count)
2505 ea.line2 = curbuf->b_ml.ml_line_count;
2506 }
2507 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002508
2509 /*
2510 * Check for flags: 'l', 'p' and '#'.
2511 */
2512 if (ea.argt & EXFLAGS)
2513 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002515 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002516 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 {
2518 errormsg = (char_u *)_(e_trailing);
2519 goto doend;
2520 }
2521
2522 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2523 {
2524 errormsg = (char_u *)_(e_argreq);
2525 goto doend;
2526 }
2527
2528#ifdef FEAT_EVAL
2529 /*
2530 * Skip the command when it's not going to be executed.
2531 * The commands like :if, :endif, etc. always need to be executed.
2532 * Also make an exception for commands that handle a trailing command
2533 * themselves.
2534 */
2535 if (ea.skip)
2536 {
2537 switch (ea.cmdidx)
2538 {
2539 /* commands that need evaluation */
2540 case CMD_while:
2541 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002542 case CMD_for:
2543 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 case CMD_if:
2545 case CMD_elseif:
2546 case CMD_else:
2547 case CMD_endif:
2548 case CMD_try:
2549 case CMD_catch:
2550 case CMD_finally:
2551 case CMD_endtry:
2552 case CMD_function:
2553 break;
2554
2555 /* Commands that handle '|' themselves. Check: A command should
2556 * either have the TRLBAR flag, appear in this list or appear in
2557 * the list at ":help :bar". */
2558 case CMD_aboveleft:
2559 case CMD_and:
2560 case CMD_belowright:
2561 case CMD_botright:
2562 case CMD_browse:
2563 case CMD_call:
2564 case CMD_confirm:
2565 case CMD_delfunction:
2566 case CMD_djump:
2567 case CMD_dlist:
2568 case CMD_dsearch:
2569 case CMD_dsplit:
2570 case CMD_echo:
2571 case CMD_echoerr:
2572 case CMD_echomsg:
2573 case CMD_echon:
2574 case CMD_execute:
2575 case CMD_help:
2576 case CMD_hide:
2577 case CMD_ijump:
2578 case CMD_ilist:
2579 case CMD_isearch:
2580 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002581 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 case CMD_keepjumps:
2583 case CMD_keepmarks:
2584 case CMD_leftabove:
2585 case CMD_let:
2586 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002587 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002589 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 case CMD_perl:
2591 case CMD_psearch:
2592 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002593 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002594 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 case CMD_return:
2596 case CMD_rightbelow:
2597 case CMD_ruby:
2598 case CMD_silent:
2599 case CMD_smagic:
2600 case CMD_snomagic:
2601 case CMD_substitute:
2602 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002603 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 case CMD_tcl:
2605 case CMD_throw:
2606 case CMD_tilde:
2607 case CMD_topleft:
2608 case CMD_unlet:
2609 case CMD_verbose:
2610 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002611 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 break;
2613
2614 default: goto doend;
2615 }
2616 }
2617#endif
2618
2619 if (ea.argt & XFILE)
2620 {
2621 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2622 goto doend;
2623 }
2624
2625#ifdef FEAT_LISTCMDS
2626 /*
2627 * Accept buffer name. Cannot be used at the same time with a buffer
2628 * number. Don't do this for a user command.
2629 */
2630 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2631# ifdef FEAT_USR_CMDS
2632 && !USER_CMDIDX(ea.cmdidx)
2633# endif
2634 )
2635 {
2636 /*
2637 * :bdelete, :bwipeout and :bunload take several arguments, separated
2638 * by spaces: find next space (skipping over escaped characters).
2639 * The others take one argument: ignore trailing spaces.
2640 */
2641 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2642 || ea.cmdidx == CMD_bunload)
2643 p = skiptowhite_esc(ea.arg);
2644 else
2645 {
2646 p = ea.arg + STRLEN(ea.arg);
2647 while (p > ea.arg && vim_iswhite(p[-1]))
2648 --p;
2649 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002650 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2651 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (ea.line2 < 0) /* failed */
2653 goto doend;
2654 ea.addr_count = 1;
2655 ea.arg = skipwhite(p);
2656 }
2657#endif
2658
2659/*
2660 * 6. switch on command name
2661 *
2662 * The "ea" structure holds the arguments that can be used.
2663 */
2664 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002665 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 ea.cookie = cookie;
2667#ifdef FEAT_EVAL
2668 ea.cstack = cstack;
2669#endif
2670
2671#ifdef FEAT_USR_CMDS
2672 if (USER_CMDIDX(ea.cmdidx))
2673 {
2674 /*
2675 * Execute a user-defined command.
2676 */
2677 do_ucmd(&ea);
2678 }
2679 else
2680#endif
2681 {
2682 /*
2683 * Call the function to execute the command.
2684 */
2685 ea.errmsg = NULL;
2686 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2687 if (ea.errmsg != NULL)
2688 errormsg = (char_u *)_(ea.errmsg);
2689 }
2690
2691#ifdef FEAT_EVAL
2692 /*
2693 * If the command just executed called do_cmdline(), any throw or ":return"
2694 * or ":finish" encountered there must also check the cstack of the still
2695 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2696 * exception, or reanimate a returned function or finished script file and
2697 * return or finish it again.
2698 */
2699 if (need_rethrow)
2700 do_throw(cstack);
2701 else if (check_cstack)
2702 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002703 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002705 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 && current_func_returned())
2707 do_return(&ea, TRUE, FALSE, NULL);
2708 }
2709 need_rethrow = check_cstack = FALSE;
2710#endif
2711
2712doend:
2713 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2714 curwin->w_cursor.lnum = 1;
2715
2716 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2717 {
2718 if (sourcing)
2719 {
2720 if (errormsg != IObuff)
2721 {
2722 STRCPY(IObuff, errormsg);
2723 errormsg = IObuff;
2724 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002725 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 }
2727 emsg(errormsg);
2728 }
2729#ifdef FEAT_EVAL
2730 do_errthrow(cstack,
2731 (ea.cmdidx != CMD_SIZE
2732# ifdef FEAT_USR_CMDS
2733 && !USER_CMDIDX(ea.cmdidx)
2734# endif
2735 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2736#endif
2737
2738 if (verbose_save >= 0)
2739 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002740#ifdef FEAT_AUTOCMD
2741 if (cmdmod.save_ei != NULL)
2742 {
2743 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002744 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2745 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002746 free_string_option(cmdmod.save_ei);
2747 }
2748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
2750 cmdmod = save_cmdmod;
2751
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002752 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 {
2754 /* messages could be enabled for a serious error, need to check if the
2755 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002756 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002757 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 emsg_silent -= did_esilent;
2759 if (emsg_silent < 0)
2760 emsg_silent = 0;
2761 /* Restore msg_scroll, it's set by file I/O commands, even when no
2762 * message is actually displayed. */
2763 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002764
2765 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2766 * somewhere in the line. Put it back in the first column. */
2767 if (redirecting())
2768 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
2770
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002771#ifdef HAVE_SANDBOX
2772 if (did_sandbox)
2773 --sandbox;
2774#endif
2775
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2777 ea.nextcmd = NULL;
2778
2779#ifdef FEAT_EVAL
2780 --ex_nesting_level;
2781#endif
2782
2783 return ea.nextcmd;
2784}
2785#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002786 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787#endif
2788
2789/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002790 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 * If there is a match advance "pp" to the argument and return TRUE.
2792 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002793 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002795 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 char *cmd; /* name of command */
2797 int len; /* required length */
2798{
2799 int i;
2800
2801 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002802 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 break;
2804 if (i >= len && !isalpha((*pp)[i]))
2805 {
2806 *pp = skipwhite(*pp + i);
2807 return TRUE;
2808 }
2809 return FALSE;
2810}
2811
2812/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002813 * Append "cmd" to the error message in IObuff.
2814 * Takes care of limiting the length and handling 0xa0, which would be
2815 * invisible otherwise.
2816 */
2817 static void
2818append_command(cmd)
2819 char_u *cmd;
2820{
2821 char_u *s = cmd;
2822 char_u *d;
2823
2824 STRCAT(IObuff, ": ");
2825 d = IObuff + STRLEN(IObuff);
2826 while (*s != NUL && d - IObuff < IOSIZE - 7)
2827 {
2828 if (
2829#ifdef FEAT_MBYTE
2830 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2831#endif
2832 *s == 0xa0)
2833 {
2834 s +=
2835#ifdef FEAT_MBYTE
2836 enc_utf8 ? 2 :
2837#endif
2838 1;
2839 STRCPY(d, "<a0>");
2840 d += 4;
2841 }
2842 else
2843 MB_COPY_CHAR(s, d);
2844 }
2845 *d = NUL;
2846}
2847
2848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002850 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002852 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 * Returns NULL for an ambiguous user command.
2854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 static char_u *
2856find_command(eap, full)
2857 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002858 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859{
2860 int len;
2861 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002862 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863
2864 /*
2865 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002866 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 * - the 'k' command can directly be followed by any character.
2868 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2869 * but :sre[wind] is another command, as are :scrip[tnames],
2870 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002871 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 */
2873 p = eap->cmd;
2874 if (*p == 'k')
2875 {
2876 eap->cmdidx = CMD_k;
2877 ++p;
2878 }
2879 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002880 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2881 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 || p[1] == 'g'
2883 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2884 || p[1] == 'I'
2885 || (p[1] == 'r' && p[2] != 'e')))
2886 {
2887 eap->cmdidx = CMD_substitute;
2888 ++p;
2889 }
2890 else
2891 {
2892 while (ASCII_ISALPHA(*p))
2893 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002894 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002895 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002896 while (ASCII_ISALNUM(*p))
2897 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002898
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 /* check for non-alpha command */
2900 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2901 ++p;
2902 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002903 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2904 {
2905 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2906 * :delete with the 'l' flag. Same for 'p'. */
2907 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002908 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002909 break;
2910 if (i == len - 1)
2911 {
2912 --len;
2913 if (p[-1] == 'l')
2914 eap->flags |= EXFLAG_LIST;
2915 else
2916 eap->flags |= EXFLAG_PRINT;
2917 }
2918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919
2920 if (ASCII_ISLOWER(*eap->cmd))
2921 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2922 else
2923 eap->cmdidx = cmdidxs[26];
2924
2925 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2926 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2927 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2928 (size_t)len) == 0)
2929 {
2930#ifdef FEAT_EVAL
2931 if (full != NULL
2932 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2933 *full = TRUE;
2934#endif
2935 break;
2936 }
2937
2938#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002939 /* Look for a user defined command as a last resort. Let ":Print" be
2940 * overruled by a user defined command. */
2941 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2942 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002944 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 while (ASCII_ISALNUM(*p))
2946 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002947 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 }
2949#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002950 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 eap->cmdidx = CMD_SIZE;
2952 }
2953
2954 return p;
2955}
2956
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002957#ifdef FEAT_USR_CMDS
2958/*
2959 * Search for a user command that matches "eap->cmd".
2960 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2961 * Return a pointer to just after the command.
2962 * Return NULL if there is no matching command.
2963 */
2964 static char_u *
2965find_ucmd(eap, p, full, xp, compl)
2966 exarg_T *eap;
2967 char_u *p; /* end of the command (possibly including count) */
2968 int *full; /* set to TRUE for a full match */
2969 expand_T *xp; /* used for completion, NULL otherwise */
2970 int *compl; /* completion flags or NULL */
2971{
2972 int len = (int)(p - eap->cmd);
2973 int j, k, matchlen = 0;
2974 ucmd_T *uc;
2975 int found = FALSE;
2976 int possible = FALSE;
2977 char_u *cp, *np; /* Point into typed cmd and test name */
2978 garray_T *gap;
2979 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2980 only full match global is accepted. */
2981
2982 /*
2983 * Look for buffer-local user commands first, then global ones.
2984 */
2985 gap = &curbuf->b_ucmds;
2986 for (;;)
2987 {
2988 for (j = 0; j < gap->ga_len; ++j)
2989 {
2990 uc = USER_CMD_GA(gap, j);
2991 cp = eap->cmd;
2992 np = uc->uc_name;
2993 k = 0;
2994 while (k < len && *np != NUL && *cp++ == *np++)
2995 k++;
2996 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2997 {
2998 /* If finding a second match, the command is ambiguous. But
2999 * not if a buffer-local command wasn't a full match and a
3000 * global command is a full match. */
3001 if (k == len && found && *np != NUL)
3002 {
3003 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003004 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003005 amb_local = TRUE;
3006 }
3007
3008 if (!found || (k == len && *np == NUL))
3009 {
3010 /* If we matched up to a digit, then there could
3011 * be another command including the digit that we
3012 * should use instead.
3013 */
3014 if (k == len)
3015 found = TRUE;
3016 else
3017 possible = TRUE;
3018
3019 if (gap == &ucmds)
3020 eap->cmdidx = CMD_USER;
3021 else
3022 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003023 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003024 eap->useridx = j;
3025
3026# ifdef FEAT_CMDL_COMPL
3027 if (compl != NULL)
3028 *compl = uc->uc_compl;
3029# ifdef FEAT_EVAL
3030 if (xp != NULL)
3031 {
3032 xp->xp_arg = uc->uc_compl_arg;
3033 xp->xp_scriptID = uc->uc_scriptID;
3034 }
3035# endif
3036# endif
3037 /* Do not search for further abbreviations
3038 * if this is an exact match. */
3039 matchlen = k;
3040 if (k == len && *np == NUL)
3041 {
3042 if (full != NULL)
3043 *full = TRUE;
3044 amb_local = FALSE;
3045 break;
3046 }
3047 }
3048 }
3049 }
3050
3051 /* Stop if we found a full match or searched all. */
3052 if (j < gap->ga_len || gap == &ucmds)
3053 break;
3054 gap = &ucmds;
3055 }
3056
3057 /* Only found ambiguous matches. */
3058 if (amb_local)
3059 {
3060 if (xp != NULL)
3061 xp->xp_context = EXPAND_UNSUCCESSFUL;
3062 return NULL;
3063 }
3064
3065 /* The match we found may be followed immediately by a number. Move "p"
3066 * back to point to it. */
3067 if (found || possible)
3068 return p + (matchlen - len);
3069 return p;
3070}
3071#endif
3072
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003074static struct cmdmod
3075{
3076 char *name;
3077 int minlen;
3078 int has_count; /* :123verbose :3tab */
3079} cmdmods[] = {
3080 {"aboveleft", 3, FALSE},
3081 {"belowright", 3, FALSE},
3082 {"botright", 2, FALSE},
3083 {"browse", 3, FALSE},
3084 {"confirm", 4, FALSE},
3085 {"hide", 3, FALSE},
3086 {"keepalt", 5, FALSE},
3087 {"keepjumps", 5, FALSE},
3088 {"keepmarks", 3, FALSE},
3089 {"leftabove", 5, FALSE},
3090 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003091 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003092 {"rightbelow", 6, FALSE},
3093 {"sandbox", 3, FALSE},
3094 {"silent", 3, FALSE},
3095 {"tab", 3, TRUE},
3096 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003097 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003098 {"verbose", 4, TRUE},
3099 {"vertical", 4, FALSE},
3100};
3101
3102/*
3103 * Return length of a command modifier (including optional count).
3104 * Return zero when it's not a modifier.
3105 */
3106 int
3107modifier_len(cmd)
3108 char_u *cmd;
3109{
3110 int i, j;
3111 char_u *p = cmd;
3112
3113 if (VIM_ISDIGIT(*cmd))
3114 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003115 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003116 {
3117 for (j = 0; p[j] != NUL; ++j)
3118 if (p[j] != cmdmods[i].name[j])
3119 break;
3120 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3121 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003122 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003123 }
3124 return 0;
3125}
3126
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127/*
3128 * Return > 0 if an Ex command "name" exists.
3129 * Return 2 if there is an exact match.
3130 * Return 3 if there is an ambiguous match.
3131 */
3132 int
3133cmd_exists(name)
3134 char_u *name;
3135{
3136 exarg_T ea;
3137 int full = FALSE;
3138 int i;
3139 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003140 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141
3142 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003143 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 {
3145 for (j = 0; name[j] != NUL; ++j)
3146 if (name[j] != cmdmods[i].name[j])
3147 break;
3148 if (name[j] == NUL && j >= cmdmods[i].minlen)
3149 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3150 }
3151
Bram Moolenaara9587612006-05-04 21:47:50 +00003152 /* Check built-in commands and user defined commands.
3153 * For ":2match" and ":3match" we need to skip the number. */
3154 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003156 p = find_command(&ea, &full);
3157 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003159 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3160 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003161 if (*skipwhite(p) != NUL)
3162 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3164}
3165#endif
3166
3167/*
3168 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3169 * we don't need/want deleted. Maybe this could be done better if we didn't
3170 * repeat all this stuff. The only problem is that they may not stay
3171 * perfectly compatible with each other, but then the command line syntax
3172 * probably won't change that much -- webb.
3173 */
3174 char_u *
3175set_one_cmd_context(xp, buff)
3176 expand_T *xp;
3177 char_u *buff; /* buffer for command string */
3178{
3179 char_u *p;
3180 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003181 int len = 0;
3182 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3184 int compl = EXPAND_NOTHING;
3185#endif
3186#ifdef FEAT_CMDL_COMPL
3187 int delim;
3188#endif
3189 int forceit = FALSE;
3190 int usefilter = FALSE; /* filter instead of file name */
3191
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003192 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 xp->xp_pattern = buff;
3194 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003195 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
3197/*
3198 * 2. skip comment lines and leading space, colons or bars
3199 */
3200 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3201 ;
3202 xp->xp_pattern = cmd;
3203
3204 if (*cmd == NUL)
3205 return NULL;
3206 if (*cmd == '"') /* ignore comment lines */
3207 {
3208 xp->xp_context = EXPAND_NOTHING;
3209 return NULL;
3210 }
3211
3212/*
3213 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3214 */
3215 cmd = skip_range(cmd, &xp->xp_context);
3216
3217/*
3218 * 4. parse command
3219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 xp->xp_pattern = cmd;
3221 if (*cmd == NUL)
3222 return NULL;
3223 if (*cmd == '"')
3224 {
3225 xp->xp_context = EXPAND_NOTHING;
3226 return NULL;
3227 }
3228
3229 if (*cmd == '|' || *cmd == '\n')
3230 return cmd + 1; /* There's another command */
3231
3232 /*
3233 * Isolate the command and search for it in the command table.
3234 * Exceptions:
3235 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003236 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3238 */
3239 if (*cmd == 'k' && cmd[1] != 'e')
3240 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003241 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 p = cmd + 1;
3243 }
3244 else
3245 {
3246 p = cmd;
3247 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3248 ++p;
3249 /* check for non-alpha command */
3250 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3251 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003252 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003254 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 {
3256 xp->xp_context = EXPAND_UNSUCCESSFUL;
3257 return NULL;
3258 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003259 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003260 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3261 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3262 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 break;
3264
3265#ifdef FEAT_USR_CMDS
3266 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3268 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269#endif
3270 }
3271
3272 /*
3273 * If the cursor is touching the command, and it ends in an alpha-numeric
3274 * character, complete the command name.
3275 */
3276 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3277 return NULL;
3278
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003279 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 {
3281 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3282 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003283 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 p = cmd + 1;
3285 }
3286#ifdef FEAT_USR_CMDS
3287 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3288 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003289 ea.cmd = cmd;
3290 p = find_ucmd(&ea, p, NULL, xp,
3291# if defined(FEAT_CMDL_COMPL)
3292 &compl
3293# else
3294 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003296 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003297 if (p == NULL)
3298 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 }
3300#endif
3301 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003302 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 {
3304 /* Not still touching the command and it was an illegal one */
3305 xp->xp_context = EXPAND_UNSUCCESSFUL;
3306 return NULL;
3307 }
3308
3309 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3310
3311 if (*p == '!') /* forced commands */
3312 {
3313 forceit = TRUE;
3314 ++p;
3315 }
3316
3317/*
3318 * 5. parse arguments
3319 */
3320#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003321 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003323 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324
3325 arg = skipwhite(p);
3326
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003327 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 {
3329 if (*arg == '>') /* append */
3330 {
3331 if (*++arg == '>')
3332 ++arg;
3333 arg = skipwhite(arg);
3334 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003335 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 {
3337 ++arg;
3338 usefilter = TRUE;
3339 }
3340 }
3341
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003342 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 {
3344 usefilter = forceit; /* :r! filter if forced */
3345 if (*arg == '!') /* :r !filter */
3346 {
3347 ++arg;
3348 usefilter = TRUE;
3349 }
3350 }
3351
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003352 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 {
3354 while (*arg == *cmd) /* allow any number of '>' or '<' */
3355 ++arg;
3356 arg = skipwhite(arg);
3357 }
3358
3359 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003360 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 {
3362 /* Check if we're in the +command */
3363 p = arg + 1;
3364 arg = skip_cmd_arg(arg, FALSE);
3365
3366 /* Still touching the command after '+'? */
3367 if (*arg == NUL)
3368 return p;
3369
3370 /* Skip space(s) after +command to get to the real argument */
3371 arg = skipwhite(arg);
3372 }
3373
3374 /*
3375 * Check for '|' to separate commands and '"' to start comments.
3376 * Don't do this for ":read !cmd" and ":write !cmd".
3377 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003378 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 {
3380 p = arg;
3381 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003382 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 p += 2;
3384 while (*p)
3385 {
3386 if (*p == Ctrl_V)
3387 {
3388 if (p[1] != NUL)
3389 ++p;
3390 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003391 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 || *p == '|' || *p == '\n')
3393 {
3394 if (*(p - 1) != '\\')
3395 {
3396 if (*p == '|' || *p == '\n')
3397 return p + 1;
3398 return NULL; /* It's a comment */
3399 }
3400 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003401 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 }
3403 }
3404
3405 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003406 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 vim_strchr((char_u *)"|\"", *arg) == NULL)
3408 return NULL;
3409
3410 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003411 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003413 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003414 while (*p && p < buff + len)
3415 {
3416 if (*p == ' ' || *p == TAB)
3417 {
3418 /* argument starts after a space */
3419 xp->xp_pattern = ++p;
3420 }
3421 else
3422 {
3423 if (*p == '\\' && *(p + 1) != NUL)
3424 ++p; /* skip over escaped character */
3425 mb_ptr_adv(p);
3426 }
3427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003429 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003431 int c;
3432 int in_quote = FALSE;
3433 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435 /*
3436 * Allow spaces within back-quotes to count as part of the argument
3437 * being expanded.
3438 */
3439 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003440 p = xp->xp_pattern;
3441 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003443#ifdef FEAT_MBYTE
3444 if (has_mbyte)
3445 c = mb_ptr2char(p);
3446 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003448 c = *p;
3449 if (c == '\\' && p[1] != NUL)
3450 ++p;
3451 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
3453 if (!in_quote)
3454 {
3455 xp->xp_pattern = p;
3456 bow = p + 1;
3457 }
3458 in_quote = !in_quote;
3459 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003460 /* An argument can contain just about everything, except
3461 * characters that end the command and white space. */
3462 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003463#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003464 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003465#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003466 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003467 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003468 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003469 while (*p != NUL)
3470 {
3471#ifdef FEAT_MBYTE
3472 if (has_mbyte)
3473 c = mb_ptr2char(p);
3474 else
3475#endif
3476 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003477 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003478 break;
3479#ifdef FEAT_MBYTE
3480 if (has_mbyte)
3481 len = (*mb_ptr2len)(p);
3482 else
3483#endif
3484 len = 1;
3485 mb_ptr_adv(p);
3486 }
3487 if (in_quote)
3488 bow = p;
3489 else
3490 xp->xp_pattern = p;
3491 p -= len;
3492 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003493 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 }
3495
3496 /*
3497 * If we are still inside the quotes, and we passed a space, just
3498 * expand from there.
3499 */
3500 if (bow != NULL && in_quote)
3501 xp->xp_pattern = bow;
3502 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003503
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003504 /* For a shell command more chars need to be escaped. */
3505 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003506 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003507#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003508 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003509#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003510 /* When still after the command name expand executables. */
3511 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003512 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
3515 /* Check for environment variable */
3516 if (*xp->xp_pattern == '$'
3517#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3518 || *xp->xp_pattern == '%'
3519#endif
3520 )
3521 {
3522 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3523 if (!vim_isIDc(*p))
3524 break;
3525 if (*p == NUL)
3526 {
3527 xp->xp_context = EXPAND_ENV_VARS;
3528 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003529#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3530 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003531 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003532 compl = EXPAND_ENV_VARS;
3533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 }
3535 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003536#if defined(FEAT_CMDL_COMPL)
3537 /* Check for user names */
3538 if (*xp->xp_pattern == '~')
3539 {
3540 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3541 ;
3542 /* Complete ~user only if it partially matches a user name.
3543 * A full match ~user<Tab> will be replaced by user's home
3544 * directory i.e. something like ~user<Tab> -> /home/user/ */
3545 if (*p == NUL && p > xp->xp_pattern + 1
3546 && match_user(xp->xp_pattern + 1) == 1)
3547 {
3548 xp->xp_context = EXPAND_USER;
3549 ++xp->xp_pattern;
3550 }
3551 }
3552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 }
3554
3555/*
3556 * 6. switch on command name
3557 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003558 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003560 case CMD_find:
3561 case CMD_sfind:
3562 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003563 if (xp->xp_context == EXPAND_FILES)
3564 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003565 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 case CMD_cd:
3567 case CMD_chdir:
3568 case CMD_lcd:
3569 case CMD_lchdir:
3570 if (xp->xp_context == EXPAND_FILES)
3571 xp->xp_context = EXPAND_DIRECTORIES;
3572 break;
3573 case CMD_help:
3574 xp->xp_context = EXPAND_HELP;
3575 xp->xp_pattern = arg;
3576 break;
3577
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003578 /* Command modifiers: return the argument.
3579 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003581 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 case CMD_belowright:
3583 case CMD_botright:
3584 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003585 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003587 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 case CMD_folddoclosed:
3589 case CMD_folddoopen:
3590 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003591 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 case CMD_keepjumps:
3593 case CMD_keepmarks:
3594 case CMD_leftabove:
3595 case CMD_lockmarks:
3596 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003597 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003599 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 case CMD_topleft:
3601 case CMD_verbose:
3602 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003603 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 return arg;
3605
Bram Moolenaar4f688582007-07-24 12:34:30 +00003606#ifdef FEAT_CMDL_COMPL
3607# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 case CMD_match:
3609 if (*arg == NUL || !ends_excmd(*arg))
3610 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003611 /* also complete "None" */
3612 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 arg = skipwhite(skiptowhite(arg));
3614 if (*arg != NUL)
3615 {
3616 xp->xp_context = EXPAND_NOTHING;
3617 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3618 }
3619 }
3620 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623/*
3624 * All completion for the +cmdline_compl feature goes here.
3625 */
3626
3627# ifdef FEAT_USR_CMDS
3628 case CMD_command:
3629 /* Check for attributes */
3630 while (*arg == '-')
3631 {
3632 arg++; /* Skip "-" */
3633 p = skiptowhite(arg);
3634 if (*p == NUL)
3635 {
3636 /* Cursor is still in the attribute */
3637 p = vim_strchr(arg, '=');
3638 if (p == NULL)
3639 {
3640 /* No "=", so complete attribute names */
3641 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3642 xp->xp_pattern = arg;
3643 return NULL;
3644 }
3645
3646 /* For the -complete and -nargs attributes, we complete
3647 * their arguments as well.
3648 */
3649 if (STRNICMP(arg, "complete", p - arg) == 0)
3650 {
3651 xp->xp_context = EXPAND_USER_COMPLETE;
3652 xp->xp_pattern = p + 1;
3653 return NULL;
3654 }
3655 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3656 {
3657 xp->xp_context = EXPAND_USER_NARGS;
3658 xp->xp_pattern = p + 1;
3659 return NULL;
3660 }
3661 return NULL;
3662 }
3663 arg = skipwhite(p);
3664 }
3665
3666 /* After the attributes comes the new command name */
3667 p = skiptowhite(arg);
3668 if (*p == NUL)
3669 {
3670 xp->xp_context = EXPAND_USER_COMMANDS;
3671 xp->xp_pattern = arg;
3672 break;
3673 }
3674
3675 /* And finally comes a normal command */
3676 return skipwhite(p);
3677
3678 case CMD_delcommand:
3679 xp->xp_context = EXPAND_USER_COMMANDS;
3680 xp->xp_pattern = arg;
3681 break;
3682# endif
3683
3684 case CMD_global:
3685 case CMD_vglobal:
3686 delim = *arg; /* get the delimiter */
3687 if (delim)
3688 ++arg; /* skip delimiter if there is one */
3689
3690 while (arg[0] != NUL && arg[0] != delim)
3691 {
3692 if (arg[0] == '\\' && arg[1] != NUL)
3693 ++arg;
3694 ++arg;
3695 }
3696 if (arg[0] != NUL)
3697 return arg + 1;
3698 break;
3699 case CMD_and:
3700 case CMD_substitute:
3701 delim = *arg;
3702 if (delim)
3703 {
3704 /* skip "from" part */
3705 ++arg;
3706 arg = skip_regexp(arg, delim, p_magic, NULL);
3707 }
3708 /* skip "to" part */
3709 while (arg[0] != NUL && arg[0] != delim)
3710 {
3711 if (arg[0] == '\\' && arg[1] != NUL)
3712 ++arg;
3713 ++arg;
3714 }
3715 if (arg[0] != NUL) /* skip delimiter */
3716 ++arg;
3717 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3718 ++arg;
3719 if (arg[0] != NUL)
3720 return arg;
3721 break;
3722 case CMD_isearch:
3723 case CMD_dsearch:
3724 case CMD_ilist:
3725 case CMD_dlist:
3726 case CMD_ijump:
3727 case CMD_psearch:
3728 case CMD_djump:
3729 case CMD_isplit:
3730 case CMD_dsplit:
3731 arg = skipwhite(skipdigits(arg)); /* skip count */
3732 if (*arg == '/') /* Match regexp, not just whole words */
3733 {
3734 for (++arg; *arg && *arg != '/'; arg++)
3735 if (*arg == '\\' && arg[1] != NUL)
3736 arg++;
3737 if (*arg)
3738 {
3739 arg = skipwhite(arg + 1);
3740
3741 /* Check for trailing illegal characters */
3742 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3743 xp->xp_context = EXPAND_NOTHING;
3744 else
3745 return arg;
3746 }
3747 }
3748 break;
3749#ifdef FEAT_AUTOCMD
3750 case CMD_autocmd:
3751 return set_context_in_autocmd(xp, arg, FALSE);
3752
3753 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003754 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 return set_context_in_autocmd(xp, arg, TRUE);
3756#endif
3757 case CMD_set:
3758 set_context_in_set_cmd(xp, arg, 0);
3759 break;
3760 case CMD_setglobal:
3761 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3762 break;
3763 case CMD_setlocal:
3764 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3765 break;
3766 case CMD_tag:
3767 case CMD_stag:
3768 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003769 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 case CMD_tselect:
3771 case CMD_stselect:
3772 case CMD_ptselect:
3773 case CMD_tjump:
3774 case CMD_stjump:
3775 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003776 if (*p_wop != NUL)
3777 xp->xp_context = EXPAND_TAGS_LISTFILES;
3778 else
3779 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 xp->xp_pattern = arg;
3781 break;
3782 case CMD_augroup:
3783 xp->xp_context = EXPAND_AUGROUP;
3784 xp->xp_pattern = arg;
3785 break;
3786#ifdef FEAT_SYN_HL
3787 case CMD_syntax:
3788 set_context_in_syntax_cmd(xp, arg);
3789 break;
3790#endif
3791#ifdef FEAT_EVAL
3792 case CMD_let:
3793 case CMD_if:
3794 case CMD_elseif:
3795 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003796 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 case CMD_echo:
3798 case CMD_echon:
3799 case CMD_execute:
3800 case CMD_echomsg:
3801 case CMD_echoerr:
3802 case CMD_call:
3803 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003804 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 break;
3806
3807 case CMD_unlet:
3808 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3809 arg = xp->xp_pattern + 1;
3810 xp->xp_context = EXPAND_USER_VARS;
3811 xp->xp_pattern = arg;
3812 break;
3813
3814 case CMD_function:
3815 case CMD_delfunction:
3816 xp->xp_context = EXPAND_USER_FUNC;
3817 xp->xp_pattern = arg;
3818 break;
3819
3820 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003821 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 break;
3823#endif
3824 case CMD_highlight:
3825 set_context_in_highlight_cmd(xp, arg);
3826 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003827#ifdef FEAT_CSCOPE
3828 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003829 case CMD_lcscope:
3830 case CMD_scscope:
3831 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003832 break;
3833#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003834#ifdef FEAT_SIGNS
3835 case CMD_sign:
3836 set_context_in_sign_cmd(xp, arg);
3837 break;
3838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839#ifdef FEAT_LISTCMDS
3840 case CMD_bdelete:
3841 case CMD_bwipeout:
3842 case CMD_bunload:
3843 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3844 arg = xp->xp_pattern + 1;
3845 /*FALLTHROUGH*/
3846 case CMD_buffer:
3847 case CMD_sbuffer:
3848 case CMD_checktime:
3849 xp->xp_context = EXPAND_BUFFERS;
3850 xp->xp_pattern = arg;
3851 break;
3852#endif
3853#ifdef FEAT_USR_CMDS
3854 case CMD_USER:
3855 case CMD_USER_BUF:
3856 if (compl != EXPAND_NOTHING)
3857 {
3858 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003859 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
3861# ifdef FEAT_MENU
3862 if (compl == EXPAND_MENUS)
3863 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3864# endif
3865 if (compl == EXPAND_COMMANDS)
3866 return arg;
3867 if (compl == EXPAND_MAPPINGS)
3868 return set_context_in_map_cmd(xp, (char_u *)"map",
3869 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003870 /* Find start of last argument. */
3871 p = arg;
3872 while (*p)
3873 {
3874 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003875 /* argument starts after a space */
3876 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003877 else if (*p == '\\' && *(p + 1) != NUL)
3878 ++p; /* skip over escaped character */
3879 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 xp->xp_pattern = arg;
3882 }
3883 xp->xp_context = compl;
3884 }
3885 break;
3886#endif
3887 case CMD_map: case CMD_noremap:
3888 case CMD_nmap: case CMD_nnoremap:
3889 case CMD_vmap: case CMD_vnoremap:
3890 case CMD_omap: case CMD_onoremap:
3891 case CMD_imap: case CMD_inoremap:
3892 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003893 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003894 case CMD_smap: case CMD_snoremap:
3895 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003897 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 case CMD_unmap:
3899 case CMD_nunmap:
3900 case CMD_vunmap:
3901 case CMD_ounmap:
3902 case CMD_iunmap:
3903 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003904 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003905 case CMD_sunmap:
3906 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003908 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 case CMD_abbreviate: case CMD_noreabbrev:
3910 case CMD_cabbrev: case CMD_cnoreabbrev:
3911 case CMD_iabbrev: case CMD_inoreabbrev:
3912 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003913 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 case CMD_unabbreviate:
3915 case CMD_cunabbrev:
3916 case CMD_iunabbrev:
3917 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003918 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919#ifdef FEAT_MENU
3920 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3921 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3922 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3923 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3924 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3925 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3926 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3927 case CMD_tmenu: case CMD_tunmenu:
3928 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3929 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3930#endif
3931
3932 case CMD_colorscheme:
3933 xp->xp_context = EXPAND_COLORS;
3934 xp->xp_pattern = arg;
3935 break;
3936
3937 case CMD_compiler:
3938 xp->xp_context = EXPAND_COMPILER;
3939 xp->xp_pattern = arg;
3940 break;
3941
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003942 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003943 xp->xp_context = EXPAND_OWNSYNTAX;
3944 xp->xp_pattern = arg;
3945 break;
3946
3947 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003948 xp->xp_context = EXPAND_FILETYPE;
3949 xp->xp_pattern = arg;
3950 break;
3951
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3953 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3954 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003955 p = skiptowhite(arg);
3956 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 {
3958 xp->xp_context = EXPAND_LANGUAGE;
3959 xp->xp_pattern = arg;
3960 }
3961 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003962 {
3963 if ( STRNCMP(arg, "messages", p - arg) == 0
3964 || STRNCMP(arg, "ctype", p - arg) == 0
3965 || STRNCMP(arg, "time", p - arg) == 0)
3966 {
3967 xp->xp_context = EXPAND_LOCALES;
3968 xp->xp_pattern = skipwhite(p);
3969 }
3970 else
3971 xp->xp_context = EXPAND_NOTHING;
3972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 break;
3974#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003975#if defined(FEAT_PROFILE)
3976 case CMD_profile:
3977 set_context_in_profile_cmd(xp, arg);
3978 break;
3979#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003980 case CMD_behave:
3981 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003982 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003983 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003985#if defined(FEAT_CMDHIST)
3986 case CMD_history:
3987 xp->xp_context = EXPAND_HISTORY;
3988 xp->xp_pattern = arg;
3989 break;
3990#endif
3991
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992#endif /* FEAT_CMDL_COMPL */
3993
3994 default:
3995 break;
3996 }
3997 return NULL;
3998}
3999
4000/*
4001 * skip a range specifier of the form: addr [,addr] [;addr] ..
4002 *
4003 * Backslashed delimiters after / or ? will be skipped, and commands will
4004 * not be expanded between /'s and ?'s or after "'".
4005 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004006 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 * Returns the "cmd" pointer advanced to beyond the range.
4008 */
4009 char_u *
4010skip_range(cmd, ctx)
4011 char_u *cmd;
4012 int *ctx; /* pointer to xp_context or NULL */
4013{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004014 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
Bram Moolenaardf177f62005-02-22 08:39:57 +00004016 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 {
4018 if (*cmd == '\'')
4019 {
4020 if (*++cmd == NUL && ctx != NULL)
4021 *ctx = EXPAND_NOTHING;
4022 }
4023 else if (*cmd == '/' || *cmd == '?')
4024 {
4025 delim = *cmd++;
4026 while (*cmd != NUL && *cmd != delim)
4027 if (*cmd++ == '\\' && *cmd != NUL)
4028 ++cmd;
4029 if (*cmd == NUL && ctx != NULL)
4030 *ctx = EXPAND_NOTHING;
4031 }
4032 if (*cmd != NUL)
4033 ++cmd;
4034 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004035
4036 /* Skip ":" and white space. */
4037 while (*cmd == ':')
4038 cmd = skipwhite(cmd + 1);
4039
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 return cmd;
4041}
4042
4043/*
4044 * get a single EX address
4045 *
4046 * Set ptr to the next character after the part that was interpreted.
4047 * Set ptr to NULL when an error is encountered.
4048 *
4049 * Return MAXLNUM when no Ex address was found.
4050 */
4051 static linenr_T
4052get_address(ptr, skip, to_other_file)
4053 char_u **ptr;
4054 int skip; /* only skip the address, don't use it */
4055 int to_other_file; /* flag: may jump to other file */
4056{
4057 int c;
4058 int i;
4059 long n;
4060 char_u *cmd;
4061 pos_T pos;
4062 pos_T *fp;
4063 linenr_T lnum;
4064
4065 cmd = skipwhite(*ptr);
4066 lnum = MAXLNUM;
4067 do
4068 {
4069 switch (*cmd)
4070 {
4071 case '.': /* '.' - Cursor position */
4072 ++cmd;
4073 lnum = curwin->w_cursor.lnum;
4074 break;
4075
4076 case '$': /* '$' - last line */
4077 ++cmd;
4078 lnum = curbuf->b_ml.ml_line_count;
4079 break;
4080
4081 case '\'': /* ''' - mark */
4082 if (*++cmd == NUL)
4083 {
4084 cmd = NULL;
4085 goto error;
4086 }
4087 if (skip)
4088 ++cmd;
4089 else
4090 {
4091 /* Only accept a mark in another file when it is
4092 * used by itself: ":'M". */
4093 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4094 ++cmd;
4095 if (fp == (pos_T *)-1)
4096 /* Jumped to another file. */
4097 lnum = curwin->w_cursor.lnum;
4098 else
4099 {
4100 if (check_mark(fp) == FAIL)
4101 {
4102 cmd = NULL;
4103 goto error;
4104 }
4105 lnum = fp->lnum;
4106 }
4107 }
4108 break;
4109
4110 case '/':
4111 case '?': /* '/' or '?' - search */
4112 c = *cmd++;
4113 if (skip) /* skip "/pat/" */
4114 {
4115 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4116 if (*cmd == c)
4117 ++cmd;
4118 }
4119 else
4120 {
4121 pos = curwin->w_cursor; /* save curwin->w_cursor */
4122 /*
4123 * When '/' or '?' follows another address, start
4124 * from there.
4125 */
4126 if (lnum != MAXLNUM)
4127 curwin->w_cursor.lnum = lnum;
4128 /*
4129 * Start a forward search at the end of the line.
4130 * Start a backward search at the start of the line.
4131 * This makes sure we never match in the current
4132 * line, and can match anywhere in the
4133 * next/previous line.
4134 */
4135 if (c == '/')
4136 curwin->w_cursor.col = MAXCOL;
4137 else
4138 curwin->w_cursor.col = 0;
4139 searchcmdlen = 0;
4140 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004141 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 {
4143 curwin->w_cursor = pos;
4144 cmd = NULL;
4145 goto error;
4146 }
4147 lnum = curwin->w_cursor.lnum;
4148 curwin->w_cursor = pos;
4149 /* adjust command string pointer */
4150 cmd += searchcmdlen;
4151 }
4152 break;
4153
4154 case '\\': /* "\?", "\/" or "\&", repeat search */
4155 ++cmd;
4156 if (*cmd == '&')
4157 i = RE_SUBST;
4158 else if (*cmd == '?' || *cmd == '/')
4159 i = RE_SEARCH;
4160 else
4161 {
4162 EMSG(_(e_backslash));
4163 cmd = NULL;
4164 goto error;
4165 }
4166
4167 if (!skip)
4168 {
4169 /*
4170 * When search follows another address, start from
4171 * there.
4172 */
4173 if (lnum != MAXLNUM)
4174 pos.lnum = lnum;
4175 else
4176 pos.lnum = curwin->w_cursor.lnum;
4177
4178 /*
4179 * Start the search just like for the above
4180 * do_search().
4181 */
4182 if (*cmd != '?')
4183 pos.col = MAXCOL;
4184 else
4185 pos.col = 0;
4186 if (searchit(curwin, curbuf, &pos,
4187 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004188 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004189 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 lnum = pos.lnum;
4191 else
4192 {
4193 cmd = NULL;
4194 goto error;
4195 }
4196 }
4197 ++cmd;
4198 break;
4199
4200 default:
4201 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4202 lnum = getdigits(&cmd);
4203 }
4204
4205 for (;;)
4206 {
4207 cmd = skipwhite(cmd);
4208 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4209 break;
4210
4211 if (lnum == MAXLNUM)
4212 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4213 if (VIM_ISDIGIT(*cmd))
4214 i = '+'; /* "number" is same as "+number" */
4215 else
4216 i = *cmd++;
4217 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4218 n = 1;
4219 else
4220 n = getdigits(&cmd);
4221 if (i == '-')
4222 lnum -= n;
4223 else
4224 lnum += n;
4225 }
4226 } while (*cmd == '/' || *cmd == '?');
4227
4228error:
4229 *ptr = cmd;
4230 return lnum;
4231}
4232
4233/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004234 * Get flags from an Ex command argument.
4235 */
4236 static void
4237get_flags(eap)
4238 exarg_T *eap;
4239{
4240 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4241 {
4242 if (*eap->arg == 'l')
4243 eap->flags |= EXFLAG_LIST;
4244 else if (*eap->arg == 'p')
4245 eap->flags |= EXFLAG_PRINT;
4246 else
4247 eap->flags |= EXFLAG_NR;
4248 eap->arg = skipwhite(eap->arg + 1);
4249 }
4250}
4251
4252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 * Function called for command which is Not Implemented. NI!
4254 */
4255 void
4256ex_ni(eap)
4257 exarg_T *eap;
4258{
4259 if (!eap->skip)
4260 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4261}
4262
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004263#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264/*
4265 * Function called for script command which is Not Implemented. NI!
4266 * Skips over ":perl <<EOF" constructs.
4267 */
4268 static void
4269ex_script_ni(eap)
4270 exarg_T *eap;
4271{
4272 if (!eap->skip)
4273 ex_ni(eap);
4274 else
4275 vim_free(script_get(eap, eap->arg));
4276}
4277#endif
4278
4279/*
4280 * Check range in Ex command for validity.
4281 * Return NULL when valid, error message when invalid.
4282 */
4283 static char_u *
4284invalid_range(eap)
4285 exarg_T *eap;
4286{
4287 if ( eap->line1 < 0
4288 || eap->line2 < 0
4289 || eap->line1 > eap->line2
4290 || ((eap->argt & RANGE)
4291 && !(eap->argt & NOTADR)
4292 && eap->line2 > curbuf->b_ml.ml_line_count
4293#ifdef FEAT_DIFF
4294 + (eap->cmdidx == CMD_diffget)
4295#endif
4296 ))
4297 return (char_u *)_(e_invrange);
4298 return NULL;
4299}
4300
4301/*
4302 * Correct the range for zero line number, if required.
4303 */
4304 static void
4305correct_range(eap)
4306 exarg_T *eap;
4307{
4308 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4309 {
4310 if (eap->line1 == 0)
4311 eap->line1 = 1;
4312 if (eap->line2 == 0)
4313 eap->line2 = 1;
4314 }
4315}
4316
Bram Moolenaar748bf032005-02-02 23:04:36 +00004317#ifdef FEAT_QUICKFIX
4318static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4319
4320/*
4321 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4322 * pattern. Otherwise return eap->arg.
4323 */
4324 static char_u *
4325skip_grep_pat(eap)
4326 exarg_T *eap;
4327{
4328 char_u *p = eap->arg;
4329
Bram Moolenaara37420f2006-02-04 22:37:47 +00004330 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4331 || eap->cmdidx == CMD_vimgrepadd
4332 || eap->cmdidx == CMD_lvimgrepadd
4333 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004334 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004335 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004336 if (p == NULL)
4337 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004338 }
4339 return p;
4340}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004341
4342/*
4343 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4344 * in the command line, so that things like % get expanded.
4345 */
4346 static char_u *
4347replace_makeprg(eap, p, cmdlinep)
4348 exarg_T *eap;
4349 char_u *p;
4350 char_u **cmdlinep;
4351{
4352 char_u *new_cmdline;
4353 char_u *program;
4354 char_u *pos;
4355 char_u *ptr;
4356 int len;
4357 int i;
4358
4359 /*
4360 * Don't do it when ":vimgrep" is used for ":grep".
4361 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004362 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4363 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4364 || eap->cmdidx == CMD_grepadd
4365 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004366 && !grep_internal(eap->cmdidx))
4367 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004368 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4369 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004370 {
4371 if (*curbuf->b_p_gp == NUL)
4372 program = p_gp;
4373 else
4374 program = curbuf->b_p_gp;
4375 }
4376 else
4377 {
4378 if (*curbuf->b_p_mp == NUL)
4379 program = p_mp;
4380 else
4381 program = curbuf->b_p_mp;
4382 }
4383
4384 p = skipwhite(p);
4385
4386 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4387 {
4388 /* replace $* by given arguments */
4389 i = 1;
4390 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4391 ++i;
4392 len = (int)STRLEN(p);
4393 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4394 if (new_cmdline == NULL)
4395 return NULL; /* out of memory */
4396 ptr = new_cmdline;
4397 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4398 {
4399 i = (int)(pos - program);
4400 STRNCPY(ptr, program, i);
4401 STRCPY(ptr += i, p);
4402 ptr += len;
4403 program = pos + 2;
4404 }
4405 STRCPY(ptr, program);
4406 }
4407 else
4408 {
4409 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4410 if (new_cmdline == NULL)
4411 return NULL; /* out of memory */
4412 STRCPY(new_cmdline, program);
4413 STRCAT(new_cmdline, " ");
4414 STRCAT(new_cmdline, p);
4415 }
4416 msg_make(p);
4417
4418 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4419 vim_free(*cmdlinep);
4420 *cmdlinep = new_cmdline;
4421 p = new_cmdline;
4422 }
4423 return p;
4424}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004425#endif
4426
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427/*
4428 * Expand file name in Ex command argument.
4429 * Return FAIL for failure, OK otherwise.
4430 */
4431 int
4432expand_filename(eap, cmdlinep, errormsgp)
4433 exarg_T *eap;
4434 char_u **cmdlinep;
4435 char_u **errormsgp;
4436{
4437 int has_wildcards; /* need to expand wildcards */
4438 char_u *repl;
4439 int srclen;
4440 char_u *p;
4441 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004442 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
Bram Moolenaar748bf032005-02-02 23:04:36 +00004444#ifdef FEAT_QUICKFIX
4445 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4446 p = skip_grep_pat(eap);
4447#else
4448 p = eap->arg;
4449#endif
4450
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 /*
4452 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4453 * the file name contains a wildcard it should not cause expanding.
4454 * (it will be expanded anyway if there is a wildcard before replacing).
4455 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004456 has_wildcards = mch_has_wildcard(p);
4457 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004459#ifdef FEAT_EVAL
4460 /* Skip over `=expr`, wildcards in it are not expanded. */
4461 if (p[0] == '`' && p[1] == '=')
4462 {
4463 p += 2;
4464 (void)skip_expr(&p);
4465 if (*p == '`')
4466 ++p;
4467 continue;
4468 }
4469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 /*
4471 * Quick check if this cannot be the start of a special string.
4472 * Also removes backslash before '%', '#' and '<'.
4473 */
4474 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4475 {
4476 ++p;
4477 continue;
4478 }
4479
4480 /*
4481 * Try to find a match at this position.
4482 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004483 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4484 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 if (*errormsgp != NULL) /* error detected */
4486 return FAIL;
4487 if (repl == NULL) /* no match found */
4488 {
4489 p += srclen;
4490 continue;
4491 }
4492
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004493 /* Wildcards won't be expanded below, the replacement is taken
4494 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4495 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4496 {
4497 char_u *l = repl;
4498
4499 repl = expand_env_save(repl);
4500 vim_free(l);
4501 }
4502
Bram Moolenaar63b92542007-03-27 14:57:09 +00004503 /* Need to escape white space et al. with a backslash.
4504 * Don't do this for:
4505 * - replacement that already has been escaped: "##"
4506 * - shell commands (may have to use quotes instead).
4507 * - non-unix systems when there is a single argument (spaces don't
4508 * separate arguments then).
4509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004511 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 && eap->cmdidx != CMD_bang
4513 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004514 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004516 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004518 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519#ifndef UNIX
4520 && !(eap->argt & NOSPC)
4521#endif
4522 )
4523 {
4524 char_u *l;
4525#ifdef BACKSLASH_IN_FILENAME
4526 /* Don't escape a backslash here, because rem_backslash() doesn't
4527 * remove it later. */
4528 static char_u *nobslash = (char_u *)" \t\"|";
4529# define ESCAPE_CHARS nobslash
4530#else
4531# define ESCAPE_CHARS escape_chars
4532#endif
4533
4534 for (l = repl; *l; ++l)
4535 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4536 {
4537 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4538 if (l != NULL)
4539 {
4540 vim_free(repl);
4541 repl = l;
4542 }
4543 break;
4544 }
4545 }
4546
4547 /* For a shell command a '!' must be escaped. */
4548 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004549 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 {
4551 char_u *l;
4552
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004553 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 if (l != NULL)
4555 {
4556 vim_free(repl);
4557 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004558 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 if (strstr((char *)p_sh, "sh") != NULL)
4560 {
4561 l = vim_strsave_escaped(repl, (char_u *)"!");
4562 if (l != NULL)
4563 {
4564 vim_free(repl);
4565 repl = l;
4566 }
4567 }
4568 }
4569 }
4570
4571 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4572 vim_free(repl);
4573 if (p == NULL)
4574 return FAIL;
4575 }
4576
4577 /*
4578 * One file argument: Expand wildcards.
4579 * Don't do this with ":r !command" or ":w !command".
4580 */
4581 if ((eap->argt & NOSPC) && !eap->usefilter)
4582 {
4583 /*
4584 * May do this twice:
4585 * 1. Replace environment variables.
4586 * 2. Replace any other wildcards, remove backslashes.
4587 */
4588 for (n = 1; n <= 2; ++n)
4589 {
4590 if (n == 2)
4591 {
4592#ifdef UNIX
4593 /*
4594 * Only for Unix we check for more than one file name.
4595 * For other systems spaces are considered to be part
4596 * of the file name.
4597 * Only check here if there is no wildcard, otherwise
4598 * ExpandOne() will check for errors. This allows
4599 * ":e `ls ve*.c`" on Unix.
4600 */
4601 if (!has_wildcards)
4602 for (p = eap->arg; *p; ++p)
4603 {
4604 /* skip escaped characters */
4605 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4606 ++p;
4607 else if (vim_iswhite(*p))
4608 {
4609 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4610 return FAIL;
4611 }
4612 }
4613#endif
4614
4615 /*
4616 * Halve the number of backslashes (this is Vi compatible).
4617 * For Unix and OS/2, when wildcards are expanded, this is
4618 * done by ExpandOne() below.
4619 */
4620#if defined(UNIX) || defined(OS2)
4621 if (!has_wildcards)
4622#endif
4623 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 }
4625
4626 if (has_wildcards)
4627 {
4628 if (n == 1)
4629 {
4630 /*
4631 * First loop: May expand environment variables. This
4632 * can be done much faster with expand_env() than with
4633 * something else (e.g., calling a shell).
4634 * After expanding environment variables, check again
4635 * if there are still wildcards present.
4636 */
4637 if (vim_strchr(eap->arg, '$') != NULL
4638 || vim_strchr(eap->arg, '~') != NULL)
4639 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004640 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004641 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 has_wildcards = mch_has_wildcard(NameBuff);
4643 p = NameBuff;
4644 }
4645 else
4646 p = NULL;
4647 }
4648 else /* n == 2 */
4649 {
4650 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004651 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652
4653 ExpandInit(&xpc);
4654 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004655 if (p_wic)
4656 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004658 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 if (p == NULL)
4660 return FAIL;
4661 }
4662 if (p != NULL)
4663 {
4664 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4665 p, cmdlinep);
4666 if (n == 2) /* p came from ExpandOne() */
4667 vim_free(p);
4668 }
4669 }
4670 }
4671 }
4672 return OK;
4673}
4674
4675/*
4676 * Replace part of the command line, keeping eap->cmd, eap->arg and
4677 * eap->nextcmd correct.
4678 * "src" points to the part that is to be replaced, of length "srclen".
4679 * "repl" is the replacement string.
4680 * Returns a pointer to the character after the replaced string.
4681 * Returns NULL for failure.
4682 */
4683 static char_u *
4684repl_cmdline(eap, src, srclen, repl, cmdlinep)
4685 exarg_T *eap;
4686 char_u *src;
4687 int srclen;
4688 char_u *repl;
4689 char_u **cmdlinep;
4690{
4691 int len;
4692 int i;
4693 char_u *new_cmdline;
4694
4695 /*
4696 * The new command line is build in new_cmdline[].
4697 * First allocate it.
4698 * Careful: a "+cmd" argument may have been NUL terminated.
4699 */
4700 len = (int)STRLEN(repl);
4701 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004702 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4704 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4705 return NULL; /* out of memory! */
4706
4707 /*
4708 * Copy the stuff before the expanded part.
4709 * Copy the expanded stuff.
4710 * Copy what came after the expanded part.
4711 * Copy the next commands, if there are any.
4712 */
4713 i = (int)(src - *cmdlinep); /* length of part before match */
4714 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004715
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 mch_memmove(new_cmdline + i, repl, (size_t)len);
4717 i += len; /* remember the end of the string */
4718 STRCPY(new_cmdline + i, src + srclen);
4719 src = new_cmdline + i; /* remember where to continue */
4720
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004721 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 {
4723 i = (int)STRLEN(new_cmdline) + 1;
4724 STRCPY(new_cmdline + i, eap->nextcmd);
4725 eap->nextcmd = new_cmdline + i;
4726 }
4727 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4728 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4729 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4730 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4731 vim_free(*cmdlinep);
4732 *cmdlinep = new_cmdline;
4733
4734 return src;
4735}
4736
4737/*
4738 * Check for '|' to separate commands and '"' to start comments.
4739 */
4740 void
4741separate_nextcmd(eap)
4742 exarg_T *eap;
4743{
4744 char_u *p;
4745
Bram Moolenaar86b68352004-12-27 21:59:20 +00004746#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004747 p = skip_grep_pat(eap);
4748#else
4749 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004750#endif
4751
4752 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 {
4754 if (*p == Ctrl_V)
4755 {
4756 if (eap->argt & (USECTRLV | XFILE))
4757 ++p; /* skip CTRL-V and next char */
4758 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004759 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004760 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 if (*p == NUL) /* stop at NUL after CTRL-V */
4762 break;
4763 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004764
4765#ifdef FEAT_EVAL
4766 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004767 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004768 {
4769 p += 2;
4770 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004771 }
4772#endif
4773
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 /* Check for '"': start of comment or '|': next command */
4775 /* :@" and :*" do not start a comment!
4776 * :redir @" doesn't either. */
4777 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4778 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4779 || p != eap->arg)
4780 && (eap->cmdidx != CMD_redir
4781 || p != eap->arg + 1 || p[-1] != '@'))
4782 || *p == '|' || *p == '\n')
4783 {
4784 /*
4785 * We remove the '\' before the '|', unless USECTRLV is used
4786 * AND 'b' is present in 'cpoptions'.
4787 */
4788 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4789 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4790 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004791 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 --p;
4793 }
4794 else
4795 {
4796 eap->nextcmd = check_nextcmd(p);
4797 *p = NUL;
4798 break;
4799 }
4800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004802
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4804 del_trailing_spaces(eap->arg);
4805}
4806
4807/*
4808 * get + command from ex argument
4809 */
4810 static char_u *
4811getargcmd(argp)
4812 char_u **argp;
4813{
4814 char_u *arg = *argp;
4815 char_u *command = NULL;
4816
4817 if (*arg == '+') /* +[command] */
4818 {
4819 ++arg;
4820 if (vim_isspace(*arg))
4821 command = dollar_command;
4822 else
4823 {
4824 command = arg;
4825 arg = skip_cmd_arg(command, TRUE);
4826 if (*arg != NUL)
4827 *arg++ = NUL; /* terminate command with NUL */
4828 }
4829
4830 arg = skipwhite(arg); /* skip over spaces */
4831 *argp = arg;
4832 }
4833 return command;
4834}
4835
4836/*
4837 * Find end of "+command" argument. Skip over "\ " and "\\".
4838 */
4839 static char_u *
4840skip_cmd_arg(p, rembs)
4841 char_u *p;
4842 int rembs; /* TRUE to halve the number of backslashes */
4843{
4844 while (*p && !vim_isspace(*p))
4845 {
4846 if (*p == '\\' && p[1] != NUL)
4847 {
4848 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004849 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 else
4851 ++p;
4852 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004853 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
4855 return p;
4856}
4857
4858/*
4859 * Get "++opt=arg" argument.
4860 * Return FAIL or OK.
4861 */
4862 static int
4863getargopt(eap)
4864 exarg_T *eap;
4865{
4866 char_u *arg = eap->arg + 2;
4867 int *pp = NULL;
4868#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004869 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 char_u *p;
4871#endif
4872
4873 /* ":edit ++[no]bin[ary] file" */
4874 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4875 {
4876 if (*arg == 'n')
4877 {
4878 arg += 2;
4879 eap->force_bin = FORCE_NOBIN;
4880 }
4881 else
4882 eap->force_bin = FORCE_BIN;
4883 if (!checkforcmd(&arg, "binary", 3))
4884 return FAIL;
4885 eap->arg = skipwhite(arg);
4886 return OK;
4887 }
4888
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004889 /* ":read ++edit file" */
4890 if (STRNCMP(arg, "edit", 4) == 0)
4891 {
4892 eap->read_edit = TRUE;
4893 eap->arg = skipwhite(arg + 4);
4894 return OK;
4895 }
4896
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 if (STRNCMP(arg, "ff", 2) == 0)
4898 {
4899 arg += 2;
4900 pp = &eap->force_ff;
4901 }
4902 else if (STRNCMP(arg, "fileformat", 10) == 0)
4903 {
4904 arg += 10;
4905 pp = &eap->force_ff;
4906 }
4907#ifdef FEAT_MBYTE
4908 else if (STRNCMP(arg, "enc", 3) == 0)
4909 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004910 if (STRNCMP(arg, "encoding", 8) == 0)
4911 arg += 8;
4912 else
4913 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 pp = &eap->force_enc;
4915 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004916 else if (STRNCMP(arg, "bad", 3) == 0)
4917 {
4918 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004919 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921#endif
4922
4923 if (pp == NULL || *arg != '=')
4924 return FAIL;
4925
4926 ++arg;
4927 *pp = (int)(arg - eap->cmd);
4928 arg = skip_cmd_arg(arg, FALSE);
4929 eap->arg = skipwhite(arg);
4930 *arg = NUL;
4931
4932#ifdef FEAT_MBYTE
4933 if (pp == &eap->force_ff)
4934 {
4935#endif
4936 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4937 return FAIL;
4938#ifdef FEAT_MBYTE
4939 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004940 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 {
4942 /* Make 'fileencoding' lower case. */
4943 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4944 *p = TOLOWER_ASC(*p);
4945 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004946 else
4947 {
4948 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4949 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004950 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004951 if (STRICMP(p, "keep") == 0)
4952 eap->bad_char = BAD_KEEP;
4953 else if (STRICMP(p, "drop") == 0)
4954 eap->bad_char = BAD_DROP;
4955 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4956 eap->bad_char = *p;
4957 else
4958 return FAIL;
4959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960#endif
4961
4962 return OK;
4963}
4964
4965/*
4966 * ":abbreviate" and friends.
4967 */
4968 static void
4969ex_abbreviate(eap)
4970 exarg_T *eap;
4971{
4972 do_exmap(eap, TRUE); /* almost the same as mapping */
4973}
4974
4975/*
4976 * ":map" and friends.
4977 */
4978 static void
4979ex_map(eap)
4980 exarg_T *eap;
4981{
4982 /*
4983 * If we are sourcing .exrc or .vimrc in current directory we
4984 * print the mappings for security reasons.
4985 */
4986 if (secure)
4987 {
4988 secure = 2;
4989 msg_outtrans(eap->cmd);
4990 msg_putchar('\n');
4991 }
4992 do_exmap(eap, FALSE);
4993}
4994
4995/*
4996 * ":unmap" and friends.
4997 */
4998 static void
4999ex_unmap(eap)
5000 exarg_T *eap;
5001{
5002 do_exmap(eap, FALSE);
5003}
5004
5005/*
5006 * ":mapclear" and friends.
5007 */
5008 static void
5009ex_mapclear(eap)
5010 exarg_T *eap;
5011{
5012 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5013}
5014
5015/*
5016 * ":abclear" and friends.
5017 */
5018 static void
5019ex_abclear(eap)
5020 exarg_T *eap;
5021{
5022 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5023}
5024
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005025#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 static void
5027ex_autocmd(eap)
5028 exarg_T *eap;
5029{
5030 /*
5031 * Disallow auto commands from .exrc and .vimrc in current
5032 * directory for security reasons.
5033 */
5034 if (secure)
5035 {
5036 secure = 2;
5037 eap->errmsg = e_curdir;
5038 }
5039 else if (eap->cmdidx == CMD_autocmd)
5040 do_autocmd(eap->arg, eap->forceit);
5041 else
5042 do_augroup(eap->arg, eap->forceit);
5043}
5044
5045/*
5046 * ":doautocmd": Apply the automatic commands to the current buffer.
5047 */
5048 static void
5049ex_doautocmd(eap)
5050 exarg_T *eap;
5051{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005052 char_u *arg = eap->arg;
5053 int call_do_modelines = check_nomodeline(&arg);
5054
5055 (void)do_doautocmd(arg, TRUE);
5056 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5057 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058}
5059#endif
5060
5061#ifdef FEAT_LISTCMDS
5062/*
5063 * :[N]bunload[!] [N] [bufname] unload buffer
5064 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5065 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5066 */
5067 static void
5068ex_bunload(eap)
5069 exarg_T *eap;
5070{
5071 eap->errmsg = do_bufdel(
5072 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5073 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5074 : DOBUF_UNLOAD, eap->arg,
5075 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5076}
5077
5078/*
5079 * :[N]buffer [N] to buffer N
5080 * :[N]sbuffer [N] to buffer N
5081 */
5082 static void
5083ex_buffer(eap)
5084 exarg_T *eap;
5085{
5086 if (*eap->arg)
5087 eap->errmsg = e_trailing;
5088 else
5089 {
5090 if (eap->addr_count == 0) /* default is current buffer */
5091 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5092 else
5093 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5094 }
5095}
5096
5097/*
5098 * :[N]bmodified [N] to next mod. buffer
5099 * :[N]sbmodified [N] to next mod. buffer
5100 */
5101 static void
5102ex_bmodified(eap)
5103 exarg_T *eap;
5104{
5105 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5106}
5107
5108/*
5109 * :[N]bnext [N] to next buffer
5110 * :[N]sbnext [N] split and to next buffer
5111 */
5112 static void
5113ex_bnext(eap)
5114 exarg_T *eap;
5115{
5116 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5117}
5118
5119/*
5120 * :[N]bNext [N] to previous buffer
5121 * :[N]bprevious [N] to previous buffer
5122 * :[N]sbNext [N] split and to previous buffer
5123 * :[N]sbprevious [N] split and to previous buffer
5124 */
5125 static void
5126ex_bprevious(eap)
5127 exarg_T *eap;
5128{
5129 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5130}
5131
5132/*
5133 * :brewind to first buffer
5134 * :bfirst to first buffer
5135 * :sbrewind split and to first buffer
5136 * :sbfirst split and to first buffer
5137 */
5138 static void
5139ex_brewind(eap)
5140 exarg_T *eap;
5141{
5142 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5143}
5144
5145/*
5146 * :blast to last buffer
5147 * :sblast split and to last buffer
5148 */
5149 static void
5150ex_blast(eap)
5151 exarg_T *eap;
5152{
5153 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5154}
5155#endif
5156
5157 int
5158ends_excmd(c)
5159 int c;
5160{
5161 return (c == NUL || c == '|' || c == '"' || c == '\n');
5162}
5163
5164#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5165 || defined(PROTO)
5166/*
5167 * Return the next command, after the first '|' or '\n'.
5168 * Return NULL if not found.
5169 */
5170 char_u *
5171find_nextcmd(p)
5172 char_u *p;
5173{
5174 while (*p != '|' && *p != '\n')
5175 {
5176 if (*p == NUL)
5177 return NULL;
5178 ++p;
5179 }
5180 return (p + 1);
5181}
5182#endif
5183
5184/*
5185 * Check if *p is a separator between Ex commands.
5186 * Return NULL if it isn't, (p + 1) if it is.
5187 */
5188 char_u *
5189check_nextcmd(p)
5190 char_u *p;
5191{
5192 p = skipwhite(p);
5193 if (*p == '|' || *p == '\n')
5194 return (p + 1);
5195 else
5196 return NULL;
5197}
5198
5199/*
5200 * - if there are more files to edit
5201 * - and this is the last window
5202 * - and forceit not used
5203 * - and not repeated twice on a row
5204 * return FAIL and give error message if 'message' TRUE
5205 * return OK otherwise
5206 */
5207 static int
5208check_more(message, forceit)
5209 int message; /* when FALSE check only, no messages */
5210 int forceit;
5211{
5212 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5213
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005214 if (!forceit && only_one_window()
5215 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 {
5217 if (message)
5218 {
5219#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5220 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5221 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005222 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223
5224 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005225 vim_strncpy(buff,
5226 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005227 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005229 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 _("%d more files to edit. Quit anyway?"), n);
5231 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5232 return OK;
5233 return FAIL;
5234 }
5235#endif
5236 if (n == 1)
5237 EMSG(_("E173: 1 more file to edit"));
5238 else
5239 EMSGN(_("E173: %ld more files to edit"), n);
5240 quitmore = 2; /* next try to quit is allowed */
5241 }
5242 return FAIL;
5243 }
5244 return OK;
5245}
5246
5247#ifdef FEAT_CMDL_COMPL
5248/*
5249 * Function given to ExpandGeneric() to obtain the list of command names.
5250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 char_u *
5252get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005253 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 int idx;
5255{
5256 if (idx >= (int)CMD_SIZE)
5257# ifdef FEAT_USR_CMDS
5258 return get_user_command_name(idx);
5259# else
5260 return NULL;
5261# endif
5262 return cmdnames[idx].cmd_name;
5263}
5264#endif
5265
5266#if defined(FEAT_USR_CMDS) || defined(PROTO)
5267static 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));
5268static void uc_list __ARGS((char_u *name, size_t name_len));
5269static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5270static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5271static 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));
5272
5273 static int
5274uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5275 char_u *name;
5276 size_t name_len;
5277 char_u *rep;
5278 long argt;
5279 long def;
5280 int flags;
5281 int compl;
5282 char_u *compl_arg;
5283 int force;
5284{
5285 ucmd_T *cmd = NULL;
5286 char_u *p;
5287 int i;
5288 int cmp = 1;
5289 char_u *rep_buf = NULL;
5290 garray_T *gap;
5291
Bram Moolenaar9c102382006-05-03 21:26:49 +00005292 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 if (rep_buf == NULL)
5294 {
5295 /* Can't replace termcodes - try using the string as is */
5296 rep_buf = vim_strsave(rep);
5297
5298 /* Give up if out of memory */
5299 if (rep_buf == NULL)
5300 return FAIL;
5301 }
5302
5303 /* get address of growarray: global or in curbuf */
5304 if (flags & UC_BUFFER)
5305 {
5306 gap = &curbuf->b_ucmds;
5307 if (gap->ga_itemsize == 0)
5308 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5309 }
5310 else
5311 gap = &ucmds;
5312
5313 /* Search for the command in the already defined commands. */
5314 for (i = 0; i < gap->ga_len; ++i)
5315 {
5316 size_t len;
5317
5318 cmd = USER_CMD_GA(gap, i);
5319 len = STRLEN(cmd->uc_name);
5320 cmp = STRNCMP(name, cmd->uc_name, name_len);
5321 if (cmp == 0)
5322 {
5323 if (name_len < len)
5324 cmp = -1;
5325 else if (name_len > len)
5326 cmp = 1;
5327 }
5328
5329 if (cmp == 0)
5330 {
5331 if (!force)
5332 {
5333 EMSG(_("E174: Command already exists: add ! to replace it"));
5334 goto fail;
5335 }
5336
5337 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005338 cmd->uc_rep = NULL;
5339#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5340 vim_free(cmd->uc_compl_arg);
5341 cmd->uc_compl_arg = NULL;
5342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 break;
5344 }
5345
5346 /* Stop as soon as we pass the name to add */
5347 if (cmp < 0)
5348 break;
5349 }
5350
5351 /* Extend the array unless we're replacing an existing command */
5352 if (cmp != 0)
5353 {
5354 if (ga_grow(gap, 1) != OK)
5355 goto fail;
5356 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5357 goto fail;
5358
5359 cmd = USER_CMD_GA(gap, i);
5360 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5361
5362 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363
5364 cmd->uc_name = p;
5365 }
5366
5367 cmd->uc_rep = rep_buf;
5368 cmd->uc_argt = argt;
5369 cmd->uc_def = def;
5370 cmd->uc_compl = compl;
5371#ifdef FEAT_EVAL
5372 cmd->uc_scriptID = current_SID;
5373# ifdef FEAT_CMDL_COMPL
5374 cmd->uc_compl_arg = compl_arg;
5375# endif
5376#endif
5377
5378 return OK;
5379
5380fail:
5381 vim_free(rep_buf);
5382#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5383 vim_free(compl_arg);
5384#endif
5385 return FAIL;
5386}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005389#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390/*
5391 * List of names for completion for ":command" with the EXPAND_ flag.
5392 * Must be alphabetical for completion.
5393 */
5394static struct
5395{
5396 int expand;
5397 char *name;
5398} command_complete[] =
5399{
5400 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005401 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005403 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005405 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005406#if defined(FEAT_CSCOPE)
5407 {EXPAND_CSCOPE, "cscope"},
5408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5410 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005411 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412#endif
5413 {EXPAND_DIRECTORIES, "dir"},
5414 {EXPAND_ENV_VARS, "environment"},
5415 {EXPAND_EVENTS, "event"},
5416 {EXPAND_EXPRESSION, "expression"},
5417 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005418 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005419 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 {EXPAND_FUNCTIONS, "function"},
5421 {EXPAND_HELP, "help"},
5422 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005423#if defined(FEAT_CMDHIST)
5424 {EXPAND_HISTORY, "history"},
5425#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005426#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005427 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005428 {EXPAND_LOCALES, "locale"},
5429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 {EXPAND_MAPPINGS, "mapping"},
5431 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005432 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005434 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005435#if defined(FEAT_SIGNS)
5436 {EXPAND_SIGN, "sign"},
5437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 {EXPAND_TAGS, "tag"},
5439 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005440 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 {EXPAND_USER_VARS, "var"},
5442 {0, NULL}
5443};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005446#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 static void
5448uc_list(name, name_len)
5449 char_u *name;
5450 size_t name_len;
5451{
5452 int i, j;
5453 int found = FALSE;
5454 ucmd_T *cmd;
5455 int len;
5456 long a;
5457 garray_T *gap;
5458
5459 gap = &curbuf->b_ucmds;
5460 for (;;)
5461 {
5462 for (i = 0; i < gap->ga_len; ++i)
5463 {
5464 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005465 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466
5467 /* Skip commands which don't match the requested prefix */
5468 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5469 continue;
5470
5471 /* Put out the title first time */
5472 if (!found)
5473 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5474 found = TRUE;
5475 msg_putchar('\n');
5476 if (got_int)
5477 break;
5478
5479 /* Special cases */
5480 msg_putchar(a & BANG ? '!' : ' ');
5481 msg_putchar(a & REGSTR ? '"' : ' ');
5482 msg_putchar(gap != &ucmds ? 'b' : ' ');
5483 msg_putchar(' ');
5484
5485 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5486 len = (int)STRLEN(cmd->uc_name) + 4;
5487
5488 do {
5489 msg_putchar(' ');
5490 ++len;
5491 } while (len < 16);
5492
5493 len = 0;
5494
5495 /* Arguments */
5496 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5497 {
5498 case 0: IObuff[len++] = '0'; break;
5499 case (EXTRA): IObuff[len++] = '*'; break;
5500 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5501 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5502 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5503 }
5504
5505 do {
5506 IObuff[len++] = ' ';
5507 } while (len < 5);
5508
5509 /* Range */
5510 if (a & (RANGE|COUNT))
5511 {
5512 if (a & COUNT)
5513 {
5514 /* -count=N */
5515 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5516 len += (int)STRLEN(IObuff + len);
5517 }
5518 else if (a & DFLALL)
5519 IObuff[len++] = '%';
5520 else if (cmd->uc_def >= 0)
5521 {
5522 /* -range=N */
5523 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5524 len += (int)STRLEN(IObuff + len);
5525 }
5526 else
5527 IObuff[len++] = '.';
5528 }
5529
5530 do {
5531 IObuff[len++] = ' ';
5532 } while (len < 11);
5533
5534 /* Completion */
5535 for (j = 0; command_complete[j].expand != 0; ++j)
5536 if (command_complete[j].expand == cmd->uc_compl)
5537 {
5538 STRCPY(IObuff + len, command_complete[j].name);
5539 len += (int)STRLEN(IObuff + len);
5540 break;
5541 }
5542
5543 do {
5544 IObuff[len++] = ' ';
5545 } while (len < 21);
5546
5547 IObuff[len] = '\0';
5548 msg_outtrans(IObuff);
5549
5550 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005551#ifdef FEAT_EVAL
5552 if (p_verbose > 0)
5553 last_set_msg(cmd->uc_scriptID);
5554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 out_flush();
5556 ui_breakcheck();
5557 if (got_int)
5558 break;
5559 }
5560 if (gap == &ucmds || i < gap->ga_len)
5561 break;
5562 gap = &ucmds;
5563 }
5564
5565 if (!found)
5566 MSG(_("No user-defined commands found"));
5567}
5568
5569 static char_u *
5570uc_fun_cmd()
5571{
5572 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5573 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5574 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5575 0xb9, 0x7f, 0};
5576 int i;
5577
5578 for (i = 0; fcmd[i]; ++i)
5579 IObuff[i] = fcmd[i] - 0x40;
5580 IObuff[i] = 0;
5581 return IObuff;
5582}
5583
5584 static int
5585uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5586 char_u *attr;
5587 size_t len;
5588 long *argt;
5589 long *def;
5590 int *flags;
5591 int *compl;
5592 char_u **compl_arg;
5593{
5594 char_u *p;
5595
5596 if (len == 0)
5597 {
5598 EMSG(_("E175: No attribute specified"));
5599 return FAIL;
5600 }
5601
5602 /* First, try the simple attributes (no arguments) */
5603 if (STRNICMP(attr, "bang", len) == 0)
5604 *argt |= BANG;
5605 else if (STRNICMP(attr, "buffer", len) == 0)
5606 *flags |= UC_BUFFER;
5607 else if (STRNICMP(attr, "register", len) == 0)
5608 *argt |= REGSTR;
5609 else if (STRNICMP(attr, "bar", len) == 0)
5610 *argt |= TRLBAR;
5611 else
5612 {
5613 int i;
5614 char_u *val = NULL;
5615 size_t vallen = 0;
5616 size_t attrlen = len;
5617
5618 /* Look for the attribute name - which is the part before any '=' */
5619 for (i = 0; i < (int)len; ++i)
5620 {
5621 if (attr[i] == '=')
5622 {
5623 val = &attr[i + 1];
5624 vallen = len - i - 1;
5625 attrlen = i;
5626 break;
5627 }
5628 }
5629
5630 if (STRNICMP(attr, "nargs", attrlen) == 0)
5631 {
5632 if (vallen == 1)
5633 {
5634 if (*val == '0')
5635 /* Do nothing - this is the default */;
5636 else if (*val == '1')
5637 *argt |= (EXTRA | NOSPC | NEEDARG);
5638 else if (*val == '*')
5639 *argt |= EXTRA;
5640 else if (*val == '?')
5641 *argt |= (EXTRA | NOSPC);
5642 else if (*val == '+')
5643 *argt |= (EXTRA | NEEDARG);
5644 else
5645 goto wrong_nargs;
5646 }
5647 else
5648 {
5649wrong_nargs:
5650 EMSG(_("E176: Invalid number of arguments"));
5651 return FAIL;
5652 }
5653 }
5654 else if (STRNICMP(attr, "range", attrlen) == 0)
5655 {
5656 *argt |= RANGE;
5657 if (vallen == 1 && *val == '%')
5658 *argt |= DFLALL;
5659 else if (val != NULL)
5660 {
5661 p = val;
5662 if (*def >= 0)
5663 {
5664two_count:
5665 EMSG(_("E177: Count cannot be specified twice"));
5666 return FAIL;
5667 }
5668
5669 *def = getdigits(&p);
5670 *argt |= (ZEROR | NOTADR);
5671
5672 if (p != val + vallen || vallen == 0)
5673 {
5674invalid_count:
5675 EMSG(_("E178: Invalid default value for count"));
5676 return FAIL;
5677 }
5678 }
5679 }
5680 else if (STRNICMP(attr, "count", attrlen) == 0)
5681 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005682 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683
5684 if (val != NULL)
5685 {
5686 p = val;
5687 if (*def >= 0)
5688 goto two_count;
5689
5690 *def = getdigits(&p);
5691
5692 if (p != val + vallen)
5693 goto invalid_count;
5694 }
5695
5696 if (*def < 0)
5697 *def = 0;
5698 }
5699 else if (STRNICMP(attr, "complete", attrlen) == 0)
5700 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 if (val == NULL)
5702 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005703 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 return FAIL;
5705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005707 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5708 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
5711 else
5712 {
5713 char_u ch = attr[len];
5714 attr[len] = '\0';
5715 EMSG2(_("E181: Invalid attribute: %s"), attr);
5716 attr[len] = ch;
5717 return FAIL;
5718 }
5719 }
5720
5721 return OK;
5722}
5723
Bram Moolenaara850a712009-01-28 14:42:59 +00005724/*
5725 * ":command ..."
5726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 static void
5728ex_command(eap)
5729 exarg_T *eap;
5730{
5731 char_u *name;
5732 char_u *end;
5733 char_u *p;
5734 long argt = 0;
5735 long def = -1;
5736 int flags = 0;
5737 int compl = EXPAND_NOTHING;
5738 char_u *compl_arg = NULL;
5739 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005740 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741
5742 p = eap->arg;
5743
5744 /* Check for attributes */
5745 while (*p == '-')
5746 {
5747 ++p;
5748 end = skiptowhite(p);
5749 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5750 == FAIL)
5751 return;
5752 p = skipwhite(end);
5753 }
5754
5755 /* Get the name (if any) and skip to the following argument */
5756 name = p;
5757 if (ASCII_ISALPHA(*p))
5758 while (ASCII_ISALNUM(*p))
5759 ++p;
5760 if (!ends_excmd(*p) && !vim_iswhite(*p))
5761 {
5762 EMSG(_("E182: Invalid command name"));
5763 return;
5764 }
5765 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005766 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
5768 /* If there is nothing after the name, and no attributes were specified,
5769 * we are listing commands
5770 */
5771 p = skipwhite(end);
5772 if (!has_attr && ends_excmd(*p))
5773 {
5774 uc_list(name, end - name);
5775 }
5776 else if (!ASCII_ISUPPER(*name))
5777 {
5778 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5779 return;
5780 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005781 else if ((name_len == 1 && *name == 'X')
5782 || (name_len <= 4
5783 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5784 {
5785 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5786 return;
5787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 else
5789 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5790 eap->forceit);
5791}
5792
5793/*
5794 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 * Clear all user commands, global and for current buffer.
5796 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005797 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005799 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800{
5801 uc_clear(&ucmds);
5802 uc_clear(&curbuf->b_ucmds);
5803}
5804
5805/*
5806 * Clear all user commands for "gap".
5807 */
5808 void
5809uc_clear(gap)
5810 garray_T *gap;
5811{
5812 int i;
5813 ucmd_T *cmd;
5814
5815 for (i = 0; i < gap->ga_len; ++i)
5816 {
5817 cmd = USER_CMD_GA(gap, i);
5818 vim_free(cmd->uc_name);
5819 vim_free(cmd->uc_rep);
5820# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5821 vim_free(cmd->uc_compl_arg);
5822# endif
5823 }
5824 ga_clear(gap);
5825}
5826
5827 static void
5828ex_delcommand(eap)
5829 exarg_T *eap;
5830{
5831 int i = 0;
5832 ucmd_T *cmd = NULL;
5833 int cmp = -1;
5834 garray_T *gap;
5835
5836 gap = &curbuf->b_ucmds;
5837 for (;;)
5838 {
5839 for (i = 0; i < gap->ga_len; ++i)
5840 {
5841 cmd = USER_CMD_GA(gap, i);
5842 cmp = STRCMP(eap->arg, cmd->uc_name);
5843 if (cmp <= 0)
5844 break;
5845 }
5846 if (gap == &ucmds || cmp == 0)
5847 break;
5848 gap = &ucmds;
5849 }
5850
5851 if (cmp != 0)
5852 {
5853 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5854 return;
5855 }
5856
5857 vim_free(cmd->uc_name);
5858 vim_free(cmd->uc_rep);
5859# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5860 vim_free(cmd->uc_compl_arg);
5861# endif
5862
5863 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864
5865 if (i < gap->ga_len)
5866 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5867}
5868
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005869/*
5870 * split and quote args for <f-args>
5871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 static char_u *
5873uc_split_args(arg, lenp)
5874 char_u *arg;
5875 size_t *lenp;
5876{
5877 char_u *buf;
5878 char_u *p;
5879 char_u *q;
5880 int len;
5881
5882 /* Precalculate length */
5883 p = arg;
5884 len = 2; /* Initial and final quotes */
5885
5886 while (*p)
5887 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005888 if (p[0] == '\\' && p[1] == '\\')
5889 {
5890 len += 2;
5891 p += 2;
5892 }
5893 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 {
5895 len += 1;
5896 p += 2;
5897 }
5898 else if (*p == '\\' || *p == '"')
5899 {
5900 len += 2;
5901 p += 1;
5902 }
5903 else if (vim_iswhite(*p))
5904 {
5905 p = skipwhite(p);
5906 if (*p == NUL)
5907 break;
5908 len += 3; /* "," */
5909 }
5910 else
5911 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005912#ifdef FEAT_MBYTE
5913 int charlen = (*mb_ptr2len)(p);
5914 len += charlen;
5915 p += charlen;
5916#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 ++len;
5918 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 }
5921 }
5922
5923 buf = alloc(len + 1);
5924 if (buf == NULL)
5925 {
5926 *lenp = 0;
5927 return buf;
5928 }
5929
5930 p = arg;
5931 q = buf;
5932 *q++ = '"';
5933 while (*p)
5934 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005935 if (p[0] == '\\' && p[1] == '\\')
5936 {
5937 *q++ = '\\';
5938 *q++ = '\\';
5939 p += 2;
5940 }
5941 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 {
5943 *q++ = p[1];
5944 p += 2;
5945 }
5946 else if (*p == '\\' || *p == '"')
5947 {
5948 *q++ = '\\';
5949 *q++ = *p++;
5950 }
5951 else if (vim_iswhite(*p))
5952 {
5953 p = skipwhite(p);
5954 if (*p == NUL)
5955 break;
5956 *q++ = '"';
5957 *q++ = ',';
5958 *q++ = '"';
5959 }
5960 else
5961 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005962 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 }
5964 }
5965 *q++ = '"';
5966 *q = 0;
5967
5968 *lenp = len;
5969 return buf;
5970}
5971
5972/*
5973 * Check for a <> code in a user command.
5974 * "code" points to the '<'. "len" the length of the <> (inclusive).
5975 * "buf" is where the result is to be added.
5976 * "split_buf" points to a buffer used for splitting, caller should free it.
5977 * "split_len" is the length of what "split_buf" contains.
5978 * Returns the length of the replacement, which has been added to "buf".
5979 * Returns -1 if there was no match, and only the "<" has been copied.
5980 */
5981 static size_t
5982uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5983 char_u *code;
5984 size_t len;
5985 char_u *buf;
5986 ucmd_T *cmd; /* the user command we're expanding */
5987 exarg_T *eap; /* ex arguments */
5988 char_u **split_buf;
5989 size_t *split_len;
5990{
5991 size_t result = 0;
5992 char_u *p = code + 1;
5993 size_t l = len - 2;
5994 int quote = 0;
5995 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5996 ct_LT, ct_NONE } type = ct_NONE;
5997
5998 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5999 {
6000 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6001 p += 2;
6002 l -= 2;
6003 }
6004
Bram Moolenaar371d5402006-03-20 21:47:49 +00006005 ++l;
6006 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006008 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006010 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006012 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006014 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006016 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006018 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006020 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 type = ct_REGISTER;
6022
6023 switch (type)
6024 {
6025 case ct_ARGS:
6026 /* Simple case first */
6027 if (*eap->arg == NUL)
6028 {
6029 if (quote == 1)
6030 {
6031 result = 2;
6032 if (buf != NULL)
6033 STRCPY(buf, "''");
6034 }
6035 else
6036 result = 0;
6037 break;
6038 }
6039
6040 /* When specified there is a single argument don't split it.
6041 * Works for ":Cmd %" when % is "a b c". */
6042 if ((eap->argt & NOSPC) && quote == 2)
6043 quote = 1;
6044
6045 switch (quote)
6046 {
6047 case 0: /* No quoting, no splitting */
6048 result = STRLEN(eap->arg);
6049 if (buf != NULL)
6050 STRCPY(buf, eap->arg);
6051 break;
6052 case 1: /* Quote, but don't split */
6053 result = STRLEN(eap->arg) + 2;
6054 for (p = eap->arg; *p; ++p)
6055 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006056#ifdef FEAT_MBYTE
6057 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6058 /* DBCS can contain \ in a trail byte, skip the
6059 * double-byte character. */
6060 ++p;
6061 else
6062#endif
6063 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 ++result;
6065 }
6066
6067 if (buf != NULL)
6068 {
6069 *buf++ = '"';
6070 for (p = eap->arg; *p; ++p)
6071 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006072#ifdef FEAT_MBYTE
6073 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6074 /* DBCS can contain \ in a trail byte, copy the
6075 * double-byte character to avoid escaping. */
6076 *buf++ = *p++;
6077 else
6078#endif
6079 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 *buf++ = '\\';
6081 *buf++ = *p;
6082 }
6083 *buf = '"';
6084 }
6085
6086 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006087 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 /* This is hard, so only do it once, and cache the result */
6089 if (*split_buf == NULL)
6090 *split_buf = uc_split_args(eap->arg, split_len);
6091
6092 result = *split_len;
6093 if (buf != NULL && result != 0)
6094 STRCPY(buf, *split_buf);
6095
6096 break;
6097 }
6098 break;
6099
6100 case ct_BANG:
6101 result = eap->forceit ? 1 : 0;
6102 if (quote)
6103 result += 2;
6104 if (buf != NULL)
6105 {
6106 if (quote)
6107 *buf++ = '"';
6108 if (eap->forceit)
6109 *buf++ = '!';
6110 if (quote)
6111 *buf = '"';
6112 }
6113 break;
6114
6115 case ct_LINE1:
6116 case ct_LINE2:
6117 case ct_COUNT:
6118 {
6119 char num_buf[20];
6120 long num = (type == ct_LINE1) ? eap->line1 :
6121 (type == ct_LINE2) ? eap->line2 :
6122 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6123 size_t num_len;
6124
6125 sprintf(num_buf, "%ld", num);
6126 num_len = STRLEN(num_buf);
6127 result = num_len;
6128
6129 if (quote)
6130 result += 2;
6131
6132 if (buf != NULL)
6133 {
6134 if (quote)
6135 *buf++ = '"';
6136 STRCPY(buf, num_buf);
6137 buf += num_len;
6138 if (quote)
6139 *buf = '"';
6140 }
6141
6142 break;
6143 }
6144
6145 case ct_REGISTER:
6146 result = eap->regname ? 1 : 0;
6147 if (quote)
6148 result += 2;
6149 if (buf != NULL)
6150 {
6151 if (quote)
6152 *buf++ = '\'';
6153 if (eap->regname)
6154 *buf++ = eap->regname;
6155 if (quote)
6156 *buf = '\'';
6157 }
6158 break;
6159
6160 case ct_LT:
6161 result = 1;
6162 if (buf != NULL)
6163 *buf = '<';
6164 break;
6165
6166 default:
6167 /* Not recognized: just copy the '<' and return -1. */
6168 result = (size_t)-1;
6169 if (buf != NULL)
6170 *buf = '<';
6171 break;
6172 }
6173
6174 return result;
6175}
6176
6177 static void
6178do_ucmd(eap)
6179 exarg_T *eap;
6180{
6181 char_u *buf;
6182 char_u *p;
6183 char_u *q;
6184
6185 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006186 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006187 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 size_t len, totlen;
6189
6190 size_t split_len = 0;
6191 char_u *split_buf = NULL;
6192 ucmd_T *cmd;
6193#ifdef FEAT_EVAL
6194 scid_T save_current_SID = current_SID;
6195#endif
6196
6197 if (eap->cmdidx == CMD_USER)
6198 cmd = USER_CMD(eap->useridx);
6199 else
6200 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6201
6202 /*
6203 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006204 * First round: "buf" is NULL, compute length, allocate "buf".
6205 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 */
6207 buf = NULL;
6208 for (;;)
6209 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006210 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006211 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006213
6214 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006216 start = vim_strchr(p, '<');
6217 if (start != NULL)
6218 end = vim_strchr(start + 1, '>');
6219 if (buf != NULL)
6220 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006221 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6222 ;
6223 if (*ksp == K_SPECIAL
6224 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006225 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6226# ifdef FEAT_GUI
6227 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6228# endif
6229 ))
6230 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006231 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006232 * KS_SPECIAL KE_FILLER, like for mappings, but
6233 * do_cmdline() doesn't handle that, so convert it back.
6234 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6235 len = ksp - p;
6236 if (len > 0)
6237 {
6238 mch_memmove(q, p, len);
6239 q += len;
6240 }
6241 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6242 p = ksp + 3;
6243 continue;
6244 }
6245 }
6246
6247 /* break if there no <item> is found */
6248 if (start == NULL || end == NULL)
6249 break;
6250
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 /* Include the '>' */
6252 ++end;
6253
6254 /* Take everything up to the '<' */
6255 len = start - p;
6256 if (buf == NULL)
6257 totlen += len;
6258 else
6259 {
6260 mch_memmove(q, p, len);
6261 q += len;
6262 }
6263
6264 len = uc_check_code(start, end - start, q, cmd, eap,
6265 &split_buf, &split_len);
6266 if (len == (size_t)-1)
6267 {
6268 /* no match, continue after '<' */
6269 p = start + 1;
6270 len = 1;
6271 }
6272 else
6273 p = end;
6274 if (buf == NULL)
6275 totlen += len;
6276 else
6277 q += len;
6278 }
6279 if (buf != NULL) /* second time here, finished */
6280 {
6281 STRCPY(q, p);
6282 break;
6283 }
6284
6285 totlen += STRLEN(p); /* Add on the trailing characters */
6286 buf = alloc((unsigned)(totlen + 1));
6287 if (buf == NULL)
6288 {
6289 vim_free(split_buf);
6290 return;
6291 }
6292 }
6293
6294#ifdef FEAT_EVAL
6295 current_SID = cmd->uc_scriptID;
6296#endif
6297 (void)do_cmdline(buf, eap->getline, eap->cookie,
6298 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6299#ifdef FEAT_EVAL
6300 current_SID = save_current_SID;
6301#endif
6302 vim_free(buf);
6303 vim_free(split_buf);
6304}
6305
6306# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6307 static char_u *
6308get_user_command_name(idx)
6309 int idx;
6310{
6311 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6312}
6313
6314/*
6315 * Function given to ExpandGeneric() to obtain the list of user command names.
6316 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 char_u *
6318get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006319 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 int idx;
6321{
6322 if (idx < curbuf->b_ucmds.ga_len)
6323 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6324 idx -= curbuf->b_ucmds.ga_len;
6325 if (idx < ucmds.ga_len)
6326 return USER_CMD(idx)->uc_name;
6327 return NULL;
6328}
6329
6330/*
6331 * Function given to ExpandGeneric() to obtain the list of user command
6332 * attributes.
6333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 char_u *
6335get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006336 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 int idx;
6338{
6339 static char *user_cmd_flags[] =
6340 {"bang", "bar", "buffer", "complete", "count",
6341 "nargs", "range", "register"};
6342
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006343 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 return NULL;
6345 return (char_u *)user_cmd_flags[idx];
6346}
6347
6348/*
6349 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 char_u *
6352get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006353 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 int idx;
6355{
6356 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6357
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006358 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 return NULL;
6360 return (char_u *)user_cmd_nargs[idx];
6361}
6362
6363/*
6364 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 char_u *
6367get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006368 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 int idx;
6370{
6371 return (char_u *)command_complete[idx].name;
6372}
6373# endif /* FEAT_CMDL_COMPL */
6374
6375#endif /* FEAT_USR_CMDS */
6376
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006377#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6378/*
6379 * Parse a completion argument "value[vallen]".
6380 * The detected completion goes in "*complp", argument type in "*argt".
6381 * When there is an argument, for function and user defined completion, it's
6382 * copied to allocated memory and stored in "*compl_arg".
6383 * Returns FAIL if something is wrong.
6384 */
6385 int
6386parse_compl_arg(value, vallen, complp, argt, compl_arg)
6387 char_u *value;
6388 int vallen;
6389 int *complp;
6390 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006391 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006392{
6393 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006394# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006395 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006396# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006397 int i;
6398 int valend = vallen;
6399
6400 /* Look for any argument part - which is the part after any ',' */
6401 for (i = 0; i < vallen; ++i)
6402 {
6403 if (value[i] == ',')
6404 {
6405 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006406# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006407 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006408# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006409 valend = i;
6410 break;
6411 }
6412 }
6413
6414 for (i = 0; command_complete[i].expand != 0; ++i)
6415 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006416 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006417 && STRNCMP(value, command_complete[i].name, valend) == 0)
6418 {
6419 *complp = command_complete[i].expand;
6420 if (command_complete[i].expand == EXPAND_BUFFERS)
6421 *argt |= BUFNAME;
6422 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6423 || command_complete[i].expand == EXPAND_FILES)
6424 *argt |= XFILE;
6425 break;
6426 }
6427 }
6428
6429 if (command_complete[i].expand == 0)
6430 {
6431 EMSG2(_("E180: Invalid complete value: %s"), value);
6432 return FAIL;
6433 }
6434
6435# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6436 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6437 && arg != NULL)
6438# else
6439 if (arg != NULL)
6440# endif
6441 {
6442 EMSG(_("E468: Completion argument only allowed for custom completion"));
6443 return FAIL;
6444 }
6445
6446# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6447 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6448 && arg == NULL)
6449 {
6450 EMSG(_("E467: Custom completion requires a function argument"));
6451 return FAIL;
6452 }
6453
6454 if (arg != NULL)
6455 *compl_arg = vim_strnsave(arg, (int)arglen);
6456# endif
6457 return OK;
6458}
6459#endif
6460
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 static void
6462ex_colorscheme(eap)
6463 exarg_T *eap;
6464{
Bram Moolenaare6850792010-05-14 15:28:44 +02006465 if (*eap->arg == NUL)
6466 {
6467#ifdef FEAT_EVAL
6468 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6469 char_u *p = NULL;
6470
6471 if (expr != NULL)
6472 {
6473 ++emsg_off;
6474 p = eval_to_string(expr, NULL, FALSE);
6475 --emsg_off;
6476 vim_free(expr);
6477 }
6478 if (p != NULL)
6479 {
6480 MSG(p);
6481 vim_free(p);
6482 }
6483 else
6484 MSG("default");
6485#else
6486 MSG(_("unknown"));
6487#endif
6488 }
6489 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006490 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491}
6492
6493 static void
6494ex_highlight(eap)
6495 exarg_T *eap;
6496{
6497 if (*eap->arg == NUL && eap->cmd[2] == '!')
6498 MSG(_("Greetings, Vim user!"));
6499 do_highlight(eap->arg, eap->forceit, FALSE);
6500}
6501
6502
6503/*
6504 * Call this function if we thought we were going to exit, but we won't
6505 * (because of an error). May need to restore the terminal mode.
6506 */
6507 void
6508not_exiting()
6509{
6510 exiting = FALSE;
6511 settmode(TMODE_RAW);
6512}
6513
6514/*
6515 * ":quit": quit current window, quit Vim if closed the last window.
6516 */
6517 static void
6518ex_quit(eap)
6519 exarg_T *eap;
6520{
6521#ifdef FEAT_CMDWIN
6522 if (cmdwin_type != 0)
6523 {
6524 cmdwin_result = Ctrl_C;
6525 return;
6526 }
6527#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006528 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006529 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006530 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006531 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006532 return;
6533 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006534#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006535 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006536 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006537 * being closed (can only happen in autocommands). */
6538 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006539 return;
6540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541
6542#ifdef FEAT_NETBEANS_INTG
6543 netbeansForcedQuit = eap->forceit;
6544#endif
6545
6546 /*
6547 * If there are more files or windows we won't exit.
6548 */
6549 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6550 exiting = TRUE;
6551 if ((!P_HID(curbuf)
6552 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6553 || check_more(TRUE, eap->forceit) == FAIL
6554 || (only_one_window() && check_changed_any(eap->forceit)))
6555 {
6556 not_exiting();
6557 }
6558 else
6559 {
6560#ifdef FEAT_WINDOWS
6561 if (only_one_window()) /* quit last window */
6562#endif
6563 getout(0);
6564#ifdef FEAT_WINDOWS
6565# ifdef FEAT_GUI
6566 need_mouse_correct = TRUE;
6567# endif
6568 /* close window; may free buffer */
6569 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6570#endif
6571 }
6572}
6573
6574/*
6575 * ":cquit".
6576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 static void
6578ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006579 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580{
6581 getout(1); /* this does not always pass on the exit code to the Manx
6582 compiler. why? */
6583}
6584
6585/*
6586 * ":qall": try to quit all windows
6587 */
6588 static void
6589ex_quit_all(eap)
6590 exarg_T *eap;
6591{
6592# ifdef FEAT_CMDWIN
6593 if (cmdwin_type != 0)
6594 {
6595 if (eap->forceit)
6596 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6597 else
6598 cmdwin_result = K_XF2;
6599 return;
6600 }
6601# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006602
6603 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006604 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006605 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006606 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006607 return;
6608 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006609#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006610 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6611 /* Refuse to quit when locked or when the buffer in the last window is
6612 * being closed (can only happen in autocommands). */
6613 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006614 return;
6615#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006616
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 exiting = TRUE;
6618 if (eap->forceit || !check_changed_any(FALSE))
6619 getout(0);
6620 not_exiting();
6621}
6622
Bram Moolenaard9967712006-03-11 21:18:15 +00006623#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624/*
6625 * ":close": close current window, unless it is the last one
6626 */
6627 static void
6628ex_close(eap)
6629 exarg_T *eap;
6630{
6631# ifdef FEAT_CMDWIN
6632 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006633 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 else
6635# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006636 if (!text_locked()
6637#ifdef FEAT_AUTOCMD
6638 && !curbuf_locked()
6639#endif
6640 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006641 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642}
6643
Bram Moolenaard9967712006-03-11 21:18:15 +00006644# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006645/*
6646 * ":pclose": Close any preview window.
6647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006649ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006651{
6652 win_T *win;
6653
6654 for (win = firstwin; win != NULL; win = win->w_next)
6655 if (win->w_p_pvw)
6656 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006657 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006658 break;
6659 }
6660}
Bram Moolenaard9967712006-03-11 21:18:15 +00006661# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006662
Bram Moolenaarf740b292006-02-16 22:11:02 +00006663/*
6664 * Close window "win" and take care of handling closing the last window for a
6665 * modified buffer.
6666 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006667 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006668ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006669 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006671 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672{
6673 int need_hide;
6674 buf_T *buf = win->w_buffer;
6675
6676 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006677 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006679# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 if ((p_confirm || cmdmod.confirm) && p_write)
6681 {
6682 dialog_changed(buf, FALSE);
6683 if (buf_valid(buf) && bufIsChanged(buf))
6684 return;
6685 need_hide = FALSE;
6686 }
6687 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006688# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 {
6690 EMSG(_(e_nowrtmsg));
6691 return;
6692 }
6693 }
6694
Bram Moolenaard9967712006-03-11 21:18:15 +00006695# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006697# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006698
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006700 if (tp == NULL)
6701 win_close(win, !need_hide && !P_HID(buf));
6702 else
6703 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704}
6705
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006707 * ":tabclose": close current tab page, unless it is the last one.
6708 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 */
6710 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006711ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 exarg_T *eap;
6713{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006714 tabpage_T *tp;
6715
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006716# ifdef FEAT_CMDWIN
6717 if (cmdwin_type != 0)
6718 cmdwin_result = K_IGNORE;
6719 else
6720# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006721 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006722 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006723 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006725 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006726 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006727 tp = find_tabpage((int)eap->line2);
6728 if (tp == NULL)
6729 {
6730 beep_flush();
6731 return;
6732 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006733 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006734 {
6735 tabpage_close_other(tp, eap->forceit);
6736 return;
6737 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006738 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006739 if (!text_locked()
6740#ifdef FEAT_AUTOCMD
6741 && !curbuf_locked()
6742#endif
6743 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006744 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006746}
6747
6748/*
6749 * ":tabonly": close all tab pages except the current one
6750 */
6751 static void
6752ex_tabonly(eap)
6753 exarg_T *eap;
6754{
6755 tabpage_T *tp;
6756 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006757
6758# ifdef FEAT_CMDWIN
6759 if (cmdwin_type != 0)
6760 cmdwin_result = K_IGNORE;
6761 else
6762# endif
6763 if (first_tabpage->tp_next == NULL)
6764 MSG(_("Already only one tab page"));
6765 else
6766 {
6767 /* Repeat this up to a 1000 times, because autocommands may mess
6768 * up the lists. */
6769 for (done = 0; done < 1000; ++done)
6770 {
6771 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6772 if (tp->tp_topframe != topframe)
6773 {
6774 tabpage_close_other(tp, eap->forceit);
6775 /* if we failed to close it quit */
6776 if (valid_tabpage(tp))
6777 done = 1000;
6778 /* start over, "tp" is now invalid */
6779 break;
6780 }
6781 if (first_tabpage->tp_next == NULL)
6782 break;
6783 }
6784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786
6787/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006788 * Close the current tab page.
6789 */
6790 void
6791tabpage_close(forceit)
6792 int forceit;
6793{
6794 /* First close all the windows but the current one. If that worked then
6795 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006796 if (lastwin != firstwin)
6797 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006798 if (lastwin == firstwin)
6799 ex_win_close(forceit, curwin, NULL);
6800# ifdef FEAT_GUI
6801 need_mouse_correct = TRUE;
6802# endif
6803}
6804
6805/*
6806 * Close tab page "tp", which is not the current tab page.
6807 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006808 * Also takes care of the tab pages line disappearing when closing the
6809 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006810 */
6811 void
6812tabpage_close_other(tp, forceit)
6813 tabpage_T *tp;
6814 int forceit;
6815{
6816 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006817 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006818 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006819
6820 /* Limit to 1000 windows, autocommands may add a window while we close
6821 * one. OK, so I'm paranoid... */
6822 while (++done < 1000)
6823 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006824 wp = tp->tp_firstwin;
6825 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006826
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006827 /* Autocommands may delete the tab page under our fingers and we may
6828 * fail to close a window with a modified buffer. */
6829 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006830 break;
6831 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006832
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006833 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006834 if (h != tabline_height())
6835 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006836}
6837
6838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 * ":only".
6840 */
6841 static void
6842ex_only(eap)
6843 exarg_T *eap;
6844{
6845# ifdef FEAT_GUI
6846 need_mouse_correct = TRUE;
6847# endif
6848 close_others(TRUE, eap->forceit);
6849}
6850
6851/*
6852 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006853 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006855 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856ex_all(eap)
6857 exarg_T *eap;
6858{
6859 if (eap->addr_count == 0)
6860 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006861 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862}
6863#endif /* FEAT_WINDOWS */
6864
6865 static void
6866ex_hide(eap)
6867 exarg_T *eap;
6868{
6869 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6870 eap->errmsg = e_invarg;
6871 else
6872 {
6873 /* ":hide" or ":hide | cmd": hide current window */
6874 eap->nextcmd = check_nextcmd(eap->arg);
6875#ifdef FEAT_WINDOWS
6876 if (!eap->skip)
6877 {
6878# ifdef FEAT_GUI
6879 need_mouse_correct = TRUE;
6880# endif
6881 win_close(curwin, FALSE); /* don't free buffer */
6882 }
6883#endif
6884 }
6885}
6886
6887/*
6888 * ":stop" and ":suspend": Suspend Vim.
6889 */
6890 static void
6891ex_stop(eap)
6892 exarg_T *eap;
6893{
6894 /*
6895 * Disallow suspending for "rvim".
6896 */
6897 if (!check_restricted()
6898#ifdef WIN3264
6899 /*
6900 * Check if external commands are allowed now.
6901 */
6902 && can_end_termcap_mode(TRUE)
6903#endif
6904 )
6905 {
6906 if (!eap->forceit)
6907 autowrite_all();
6908 windgoto((int)Rows - 1, 0);
6909 out_char('\n');
6910 out_flush();
6911 stoptermcap();
6912 out_flush(); /* needed for SUN to restore xterm buffer */
6913#ifdef FEAT_TITLE
6914 mch_restore_title(3); /* restore window titles */
6915#endif
6916 ui_suspend(); /* call machine specific function */
6917#ifdef FEAT_TITLE
6918 maketitle();
6919 resettitle(); /* force updating the title */
6920#endif
6921 starttermcap();
6922 scroll_start(); /* scroll screen before redrawing */
6923 redraw_later_clear();
6924 shell_resized(); /* may have resized window */
6925 }
6926}
6927
6928/*
6929 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6930 */
6931 static void
6932ex_exit(eap)
6933 exarg_T *eap;
6934{
6935#ifdef FEAT_CMDWIN
6936 if (cmdwin_type != 0)
6937 {
6938 cmdwin_result = Ctrl_C;
6939 return;
6940 }
6941#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006942 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006943 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006944 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006945 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006946 return;
6947 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006948#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006949 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6950 /* Refuse to quit when locked or when the buffer in the last window is
6951 * being closed (can only happen in autocommands). */
6952 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006953 return;
6954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
6956 /*
6957 * if more files or windows we won't exit
6958 */
6959 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6960 exiting = TRUE;
6961 if ( ((eap->cmdidx == CMD_wq
6962 || curbufIsChanged())
6963 && do_write(eap) == FAIL)
6964 || check_more(TRUE, eap->forceit) == FAIL
6965 || (only_one_window() && check_changed_any(eap->forceit)))
6966 {
6967 not_exiting();
6968 }
6969 else
6970 {
6971#ifdef FEAT_WINDOWS
6972 if (only_one_window()) /* quit last window, exit Vim */
6973#endif
6974 getout(0);
6975#ifdef FEAT_WINDOWS
6976# ifdef FEAT_GUI
6977 need_mouse_correct = TRUE;
6978# endif
6979 /* quit current window, may free buffer */
6980 win_close(curwin, !P_HID(curwin->w_buffer));
6981#endif
6982 }
6983}
6984
6985/*
6986 * ":print", ":list", ":number".
6987 */
6988 static void
6989ex_print(eap)
6990 exarg_T *eap;
6991{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006992 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6993 EMSG(_(e_emptybuf));
6994 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006996 for ( ;!got_int; ui_breakcheck())
6997 {
6998 print_line(eap->line1,
6999 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7000 || (eap->flags & EXFLAG_NR)),
7001 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7002 if (++eap->line1 > eap->line2)
7003 break;
7004 out_flush(); /* show one line at a time */
7005 }
7006 setpcmark();
7007 /* put cursor at last line */
7008 curwin->w_cursor.lnum = eap->line2;
7009 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 }
7011
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013}
7014
7015#ifdef FEAT_BYTEOFF
7016 static void
7017ex_goto(eap)
7018 exarg_T *eap;
7019{
7020 goto_byte(eap->line2);
7021}
7022#endif
7023
7024/*
7025 * ":shell".
7026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 static void
7028ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007029 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030{
7031 do_shell(NULL, 0);
7032}
7033
7034#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7035 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007036 || defined(FEAT_GUI_MSWIN) \
7037 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 || defined(PROTO)
7039
7040/*
7041 * Handle a file drop. The code is here because a drop is *nearly* like an
7042 * :args command, but not quite (we have a list of exact filenames, so we
7043 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7044 * code is very similar to :args and hence needs access to a lot of the static
7045 * functions in this file.
7046 *
7047 * The list should be allocated using alloc(), as should each item in the
7048 * list. This function takes over responsibility for freeing the list.
7049 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007050 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7052 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7053 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7054 * file functionality is (currently) not in EMX this is not presently a
7055 * problem.
7056 */
7057 void
7058handle_drop(filec, filev, split)
7059 int filec; /* the number of files dropped */
7060 char_u **filev; /* the list of files dropped */
7061 int split; /* force splitting the window */
7062{
7063 exarg_T ea;
7064 int save_msg_scroll = msg_scroll;
7065
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007066 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007067 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007069#ifdef FEAT_AUTOCMD
7070 if (curbuf_locked())
7071 return;
7072#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007073 /* When the screen is being updated we should not change buffers and
7074 * windows structures, it may cause freed memory to be used. */
7075 if (updating_screen)
7076 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077
7078 /* Check whether the current buffer is changed. If so, we will need
7079 * to split the current window or data could be lost.
7080 * We don't need to check if the 'hidden' option is set, as in this
7081 * case the buffer won't be lost.
7082 */
7083 if (!P_HID(curbuf) && !split)
7084 {
7085 ++emsg_off;
7086 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
7087 --emsg_off;
7088 }
7089 if (split)
7090 {
7091# ifdef FEAT_WINDOWS
7092 if (win_split(0, 0) == FAIL)
7093 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007094 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095
7096 /* When splitting the window, create a new alist. Otherwise the
7097 * existing one is overwritten. */
7098 alist_unlink(curwin->w_alist);
7099 alist_new();
7100# else
7101 return; /* can't split, always fail */
7102# endif
7103 }
7104
7105 /*
7106 * Set up the new argument list.
7107 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007108 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109
7110 /*
7111 * Move to the first file.
7112 */
7113 /* Fake up a minimal "next" command for do_argfile() */
7114 vim_memset(&ea, 0, sizeof(ea));
7115 ea.cmd = (char_u *)"next";
7116 do_argfile(&ea, 0);
7117
7118 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7119 * mode that is not needed here. */
7120 need_start_insertmode = FALSE;
7121
7122 /* Restore msg_scroll, otherwise a following command may cause scrolling
7123 * unexpectedly. The screen will be redrawn by the caller, thus
7124 * msg_scroll being set by displaying a message is irrelevant. */
7125 msg_scroll = save_msg_scroll;
7126}
7127#endif
7128
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129/*
7130 * Clear an argument list: free all file names and reset it to zero entries.
7131 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007132 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133alist_clear(al)
7134 alist_T *al;
7135{
7136 while (--al->al_ga.ga_len >= 0)
7137 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7138 ga_clear(&al->al_ga);
7139}
7140
7141/*
7142 * Init an argument list.
7143 */
7144 void
7145alist_init(al)
7146 alist_T *al;
7147{
7148 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7149}
7150
7151#if defined(FEAT_WINDOWS) || defined(PROTO)
7152
7153/*
7154 * Remove a reference from an argument list.
7155 * Ignored when the argument list is the global one.
7156 * If the argument list is no longer used by any window, free it.
7157 */
7158 void
7159alist_unlink(al)
7160 alist_T *al;
7161{
7162 if (al != &global_alist && --al->al_refcount <= 0)
7163 {
7164 alist_clear(al);
7165 vim_free(al);
7166 }
7167}
7168
7169# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7170/*
7171 * Create a new argument list and use it for the current window.
7172 */
7173 void
7174alist_new()
7175{
7176 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7177 if (curwin->w_alist == NULL)
7178 {
7179 curwin->w_alist = &global_alist;
7180 ++global_alist.al_refcount;
7181 }
7182 else
7183 {
7184 curwin->w_alist->al_refcount = 1;
7185 alist_init(curwin->w_alist);
7186 }
7187}
7188# endif
7189#endif
7190
7191#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7192/*
7193 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007194 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7195 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 */
7197 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007198alist_expand(fnum_list, fnum_len)
7199 int *fnum_list;
7200 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201{
7202 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007203 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 char_u **new_arg_files;
7205 int new_arg_file_count;
7206 char_u *save_p_su = p_su;
7207 int i;
7208
7209 /* Don't use 'suffixes' here. This should work like the shell did the
7210 * expansion. Also, the vimrc file isn't read yet, thus the user
7211 * can't set the options. */
7212 p_su = empty_option;
7213 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7214 if (old_arg_files != NULL)
7215 {
7216 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007217 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7218 old_arg_count = GARGCOUNT;
7219 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007221 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 && new_arg_file_count > 0)
7223 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007224 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7225 TRUE, fnum_list, fnum_len);
7226 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 }
7229 p_su = save_p_su;
7230}
7231#endif
7232
7233/*
7234 * Set the argument list for the current window.
7235 * Takes over the allocated files[] and the allocated fnames in it.
7236 */
7237 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007238alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 alist_T *al;
7240 int count;
7241 char_u **files;
7242 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007243 int *fnum_list;
7244 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245{
7246 int i;
7247
7248 alist_clear(al);
7249 if (ga_grow(&al->al_ga, count) == OK)
7250 {
7251 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007252 {
7253 if (got_int)
7254 {
7255 /* When adding many buffers this can take a long time. Allow
7256 * interrupting here. */
7257 while (i < count)
7258 vim_free(files[i++]);
7259 break;
7260 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007261
7262 /* May set buffer name of a buffer previously used for the
7263 * argument list, so that it's re-used by alist_add. */
7264 if (fnum_list != NULL && i < fnum_len)
7265 buf_set_name(fnum_list[i], files[i]);
7266
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007268 ui_breakcheck();
7269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 vim_free(files);
7271 }
7272 else
7273 FreeWild(count, files);
7274#ifdef FEAT_WINDOWS
7275 if (al == &global_alist)
7276#endif
7277 arg_had_last = FALSE;
7278}
7279
7280/*
7281 * Add file "fname" to argument list "al".
7282 * "fname" must have been allocated and "al" must have been checked for room.
7283 */
7284 void
7285alist_add(al, fname, set_fnum)
7286 alist_T *al;
7287 char_u *fname;
7288 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7289{
7290 if (fname == NULL) /* don't add NULL file names */
7291 return;
7292#ifdef BACKSLASH_IN_FILENAME
7293 slash_adjust(fname);
7294#endif
7295 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7296 if (set_fnum > 0)
7297 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7298 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7299 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300}
7301
7302#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7303/*
7304 * Adjust slashes in file names. Called after 'shellslash' was set.
7305 */
7306 void
7307alist_slash_adjust()
7308{
7309 int i;
7310# ifdef FEAT_WINDOWS
7311 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007312 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313# endif
7314
7315 for (i = 0; i < GARGCOUNT; ++i)
7316 if (GARGLIST[i].ae_fname != NULL)
7317 slash_adjust(GARGLIST[i].ae_fname);
7318# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007319 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 if (wp->w_alist != &global_alist)
7321 for (i = 0; i < WARGCOUNT(wp); ++i)
7322 if (WARGLIST(wp)[i].ae_fname != NULL)
7323 slash_adjust(WARGLIST(wp)[i].ae_fname);
7324# endif
7325}
7326#endif
7327
7328/*
7329 * ":preserve".
7330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 static void
7332ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007333 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007335 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 ml_preserve(curbuf, TRUE);
7337}
7338
7339/*
7340 * ":recover".
7341 */
7342 static void
7343ex_recover(eap)
7344 exarg_T *eap;
7345{
7346 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7347 recoverymode = TRUE;
7348 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7349 && (*eap->arg == NUL
7350 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7351 ml_recover();
7352 recoverymode = FALSE;
7353}
7354
7355/*
7356 * Command modifier used in a wrong way.
7357 */
7358 static void
7359ex_wrongmodifier(eap)
7360 exarg_T *eap;
7361{
7362 eap->errmsg = e_invcmd;
7363}
7364
7365#ifdef FEAT_WINDOWS
7366/*
7367 * :sview [+command] file split window with new file, read-only
7368 * :split [[+command] file] split window with current or new file
7369 * :vsplit [[+command] file] split window vertically with current or new file
7370 * :new [[+command] file] split window with no or new file
7371 * :vnew [[+command] file] split vertically window with no or new file
7372 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007373 *
7374 * :tabedit open new Tab page with empty window
7375 * :tabedit [+command] file open new Tab page and edit "file"
7376 * :tabnew [[+command] file] just like :tabedit
7377 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 */
7379 void
7380ex_splitview(eap)
7381 exarg_T *eap;
7382{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007383 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007384# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007386# endif
7387# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007389# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007391# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7393 {
7394 ex_ni(eap);
7395 return;
7396 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007397# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007399# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007403# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007405 * quickfix windows. But it's OK when doing ":tab split". */
7406 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 {
7408 if (eap->cmdidx == CMD_split)
7409 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007410# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 if (eap->cmdidx == CMD_vsplit)
7412 eap->cmdidx = CMD_vnew;
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# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007417# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007418 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 {
7420 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7421 FNAME_MESS, TRUE, curbuf->b_ffname);
7422 if (fname == NULL)
7423 goto theend;
7424 eap->arg = fname;
7425 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007426# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007428# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007430# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007432# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007434# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 && eap->cmdidx != CMD_new)
7436 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007437# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007438 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007439# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007440 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007441# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007442 au_has_group((char_u *)"FileExplorer"))
7443 {
7444 /* No browsing supported but we do have the file explorer:
7445 * Edit the directory. */
7446 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7447 eap->arg = (char_u *)".";
7448 }
7449 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007450# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007451 {
7452 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007454 if (fname == NULL)
7455 goto theend;
7456 eap->arg = fname;
7457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 }
7459 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007460# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007462 /*
7463 * Either open new tab page or split the window.
7464 */
7465 if (eap->cmdidx == CMD_tabedit
7466 || eap->cmdidx == CMD_tabfind
7467 || eap->cmdidx == CMD_tabnew)
7468 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007469 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7470 : eap->addr_count == 0 ? 0
7471 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007472 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007473 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007474
7475 /* set the alternate buffer for the window we came from */
7476 if (curwin != old_curwin
7477 && win_valid(old_curwin)
7478 && old_curwin->w_buffer != curbuf
7479 && !cmdmod.keepalt)
7480 old_curwin->w_alt_fnum = curbuf->b_fnum;
7481 }
7482 }
7483 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7485 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007486# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 /* Reset 'scrollbind' when editing another file, but keep it when
7488 * doing ":split" without arguments. */
7489 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007490# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007492# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007494 {
7495 RESET_BINDING(curwin);
7496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 else
7498 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007499# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 do_exedit(eap, old_curwin);
7501 }
7502
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007503# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007505# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007507# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508theend:
7509 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007510# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007512
7513/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007514 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007515 */
7516 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007517tabpage_new()
7518{
7519 exarg_T ea;
7520
7521 vim_memset(&ea, 0, sizeof(ea));
7522 ea.cmdidx = CMD_tabnew;
7523 ea.cmd = (char_u *)"tabn";
7524 ea.arg = (char_u *)"";
7525 ex_splitview(&ea);
7526}
7527
7528/*
7529 * :tabnext command
7530 */
7531 static void
7532ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007533 exarg_T *eap;
7534{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007535 switch (eap->cmdidx)
7536 {
7537 case CMD_tabfirst:
7538 case CMD_tabrewind:
7539 goto_tabpage(1);
7540 break;
7541 case CMD_tablast:
7542 goto_tabpage(9999);
7543 break;
7544 case CMD_tabprevious:
7545 case CMD_tabNext:
7546 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7547 break;
7548 default: /* CMD_tabnext */
7549 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7550 break;
7551 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007552}
7553
7554/*
7555 * :tabmove command
7556 */
7557 static void
7558ex_tabmove(eap)
7559 exarg_T *eap;
7560{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007561 int tab_number = 9999;
7562
7563 if (eap->arg && *eap->arg != NUL)
7564 {
7565 char_u *p = eap->arg;
7566 int relative = 0; /* argument +N/-N means: move N places to the
7567 * right/left relative to the current position. */
7568
7569 if (*eap->arg == '-')
7570 {
7571 relative = -1;
7572 p = eap->arg + 1;
7573 }
7574 else if (*eap->arg == '+')
7575 {
7576 relative = 1;
7577 p = eap->arg + 1;
7578 }
7579 else
7580 p = eap->arg;
7581
7582 if (p == skipdigits(p))
7583 {
7584 /* No numbers as argument. */
7585 eap->errmsg = e_invarg;
7586 return;
7587 }
7588
7589 tab_number = getdigits(&p);
7590 if (relative != 0)
7591 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7592 }
7593 else if (eap->addr_count != 0)
7594 tab_number = eap->line2;
7595
7596 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007597}
7598
7599/*
7600 * :tabs command: List tabs and their contents.
7601 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007602 static void
7603ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007604 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007605{
7606 tabpage_T *tp;
7607 win_T *wp;
7608 int tabcount = 1;
7609
7610 msg_start();
7611 msg_scroll = TRUE;
7612 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7613 {
7614 msg_putchar('\n');
7615 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7616 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7617 out_flush(); /* output one line at a time */
7618 ui_breakcheck();
7619
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007620 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007621 wp = firstwin;
7622 else
7623 wp = tp->tp_firstwin;
7624 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7625 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007626 msg_putchar('\n');
7627 msg_putchar(wp == curwin ? '>' : ' ');
7628 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007629 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7630 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007631 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007632 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007633 else
7634 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7635 IObuff, IOSIZE, TRUE);
7636 msg_outtrans(IObuff);
7637 out_flush(); /* output one line at a time */
7638 ui_breakcheck();
7639 }
7640 }
7641}
7642
7643#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644
7645/*
7646 * ":mode": Set screen mode.
7647 * If no argument given, just get the screen size and redraw.
7648 */
7649 static void
7650ex_mode(eap)
7651 exarg_T *eap;
7652{
7653 if (*eap->arg == NUL)
7654 shell_resized();
7655 else
7656 mch_screenmode(eap->arg);
7657}
7658
7659#ifdef FEAT_WINDOWS
7660/*
7661 * ":resize".
7662 * set, increment or decrement current window height
7663 */
7664 static void
7665ex_resize(eap)
7666 exarg_T *eap;
7667{
7668 int n;
7669 win_T *wp = curwin;
7670
7671 if (eap->addr_count > 0)
7672 {
7673 n = eap->line2;
7674 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7675 ;
7676 }
7677
7678#ifdef FEAT_GUI
7679 need_mouse_correct = TRUE;
7680#endif
7681 n = atol((char *)eap->arg);
7682#ifdef FEAT_VERTSPLIT
7683 if (cmdmod.split & WSP_VERT)
7684 {
7685 if (*eap->arg == '-' || *eap->arg == '+')
7686 n += W_WIDTH(curwin);
7687 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7688 n = 9999;
7689 win_setwidth_win((int)n, wp);
7690 }
7691 else
7692#endif
7693 {
7694 if (*eap->arg == '-' || *eap->arg == '+')
7695 n += curwin->w_height;
7696 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7697 n = 9999;
7698 win_setheight_win((int)n, wp);
7699 }
7700}
7701#endif
7702
7703/*
7704 * ":find [+command] <file>" command.
7705 */
7706 static void
7707ex_find(eap)
7708 exarg_T *eap;
7709{
7710#ifdef FEAT_SEARCHPATH
7711 char_u *fname;
7712 int count;
7713
7714 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7715 TRUE, curbuf->b_ffname);
7716 if (eap->addr_count > 0)
7717 {
7718 /* Repeat finding the file "count" times. This matters when it
7719 * appears several times in the path. */
7720 count = eap->line2;
7721 while (fname != NULL && --count > 0)
7722 {
7723 vim_free(fname);
7724 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7725 FALSE, curbuf->b_ffname);
7726 }
7727 }
7728
7729 if (fname != NULL)
7730 {
7731 eap->arg = fname;
7732#endif
7733 do_exedit(eap, NULL);
7734#ifdef FEAT_SEARCHPATH
7735 vim_free(fname);
7736 }
7737#endif
7738}
7739
7740/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007741 * ":open" simulation: for now just work like ":visual".
7742 */
7743 static void
7744ex_open(eap)
7745 exarg_T *eap;
7746{
7747 regmatch_T regmatch;
7748 char_u *p;
7749
7750 curwin->w_cursor.lnum = eap->line2;
7751 beginline(BL_SOL | BL_FIX);
7752 if (*eap->arg == '/')
7753 {
7754 /* ":open /pattern/": put cursor in column found with pattern */
7755 ++eap->arg;
7756 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7757 *p = NUL;
7758 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7759 if (regmatch.regprog != NULL)
7760 {
7761 regmatch.rm_ic = p_ic;
7762 p = ml_get_curline();
7763 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007764 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007765 else
7766 EMSG(_(e_nomatch));
7767 vim_free(regmatch.regprog);
7768 }
7769 /* Move to the NUL, ignore any other arguments. */
7770 eap->arg += STRLEN(eap->arg);
7771 }
7772 check_cursor();
7773
7774 eap->cmdidx = CMD_visual;
7775 do_exedit(eap, NULL);
7776}
7777
7778/*
7779 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 */
7781 static void
7782ex_edit(eap)
7783 exarg_T *eap;
7784{
7785 do_exedit(eap, NULL);
7786}
7787
7788/*
7789 * ":edit <file>" command and alikes.
7790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 void
7792do_exedit(eap, old_curwin)
7793 exarg_T *eap;
7794 win_T *old_curwin; /* curwin before doing a split or NULL */
7795{
7796 int n;
7797#ifdef FEAT_WINDOWS
7798 int need_hide;
7799#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007800 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801
7802 /*
7803 * ":vi" command ends Ex mode.
7804 */
7805 if (exmode_active && (eap->cmdidx == CMD_visual
7806 || eap->cmdidx == CMD_view))
7807 {
7808 exmode_active = FALSE;
7809 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007810 {
7811 /* Special case: ":global/pat/visual\NLvi-commands" */
7812 if (global_busy)
7813 {
7814 int rd = RedrawingDisabled;
7815 int nwr = no_wait_return;
7816 int ms = msg_scroll;
7817#ifdef FEAT_GUI
7818 int he = hold_gui_events;
7819#endif
7820
7821 if (eap->nextcmd != NULL)
7822 {
7823 stuffReadbuff(eap->nextcmd);
7824 eap->nextcmd = NULL;
7825 }
7826
7827 if (exmode_was != EXMODE_VIM)
7828 settmode(TMODE_RAW);
7829 RedrawingDisabled = 0;
7830 no_wait_return = 0;
7831 need_wait_return = FALSE;
7832 msg_scroll = 0;
7833#ifdef FEAT_GUI
7834 hold_gui_events = 0;
7835#endif
7836 must_redraw = CLEAR;
7837
7838 main_loop(FALSE, TRUE);
7839
7840 RedrawingDisabled = rd;
7841 no_wait_return = nwr;
7842 msg_scroll = ms;
7843#ifdef FEAT_GUI
7844 hold_gui_events = he;
7845#endif
7846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 }
7850
7851 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007852 || eap->cmdidx == CMD_tabnew
7853 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854#ifdef FEAT_VERTSPLIT
7855 || eap->cmdidx == CMD_vnew
7856#endif
7857 ) && *eap->arg == NUL)
7858 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007859 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 setpcmark();
7861 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007862 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7863 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 }
7865 else if ((eap->cmdidx != CMD_split
7866#ifdef FEAT_VERTSPLIT
7867 && eap->cmdidx != CMD_vsplit
7868#endif
7869 )
7870 || *eap->arg != NUL
7871#ifdef FEAT_BROWSE
7872 || cmdmod.browse
7873#endif
7874 )
7875 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007876#ifdef FEAT_AUTOCMD
7877 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7878 * can bring us here, others are stopped earlier. */
7879 if (*eap->arg != NUL && curbuf_locked())
7880 return;
7881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 n = readonlymode;
7883 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7884 readonlymode = TRUE;
7885 else if (eap->cmdidx == CMD_enew)
7886 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7887 empty buffer */
7888 setpcmark();
7889 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7890 NULL, eap,
7891 /* ":edit" goes to first line if Vi compatible */
7892 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7893 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7894 ? ECMD_ONE : eap->do_ecmd_lnum,
7895 (P_HID(curbuf) ? ECMD_HIDE : 0)
7896 + (eap->forceit ? ECMD_FORCEIT : 0)
7897#ifdef FEAT_LISTCMDS
7898 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7899#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007900 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 {
7902 /* Editing the file failed. If the window was split, close it. */
7903#ifdef FEAT_WINDOWS
7904 if (old_curwin != NULL)
7905 {
7906 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7907 if (!need_hide || P_HID(curbuf))
7908 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007909# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7910 cleanup_T cs;
7911
7912 /* Reset the error/interrupt/exception state here so that
7913 * aborting() returns FALSE when closing a window. */
7914 enter_cleanup(&cs);
7915# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916# ifdef FEAT_GUI
7917 need_mouse_correct = TRUE;
7918# endif
7919 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007920
7921# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7922 /* Restore the error/interrupt/exception state if not
7923 * discarded by a new aborting error, interrupt, or
7924 * uncaught exception. */
7925 leave_cleanup(&cs);
7926# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 }
7928 }
7929#endif
7930 }
7931 else if (readonlymode && curbuf->b_nwindows == 1)
7932 {
7933 /* When editing an already visited buffer, 'readonly' won't be set
7934 * but the previous value is kept. With ":view" and ":sview" we
7935 * want the file to be readonly, except when another window is
7936 * editing the same buffer. */
7937 curbuf->b_p_ro = TRUE;
7938 }
7939 readonlymode = n;
7940 }
7941 else
7942 {
7943 if (eap->do_ecmd_cmd != NULL)
7944 do_cmdline_cmd(eap->do_ecmd_cmd);
7945#ifdef FEAT_TITLE
7946 n = curwin->w_arg_idx_invalid;
7947#endif
7948 check_arg_idx(curwin);
7949#ifdef FEAT_TITLE
7950 if (n != curwin->w_arg_idx_invalid)
7951 maketitle();
7952#endif
7953 }
7954
7955#ifdef FEAT_WINDOWS
7956 /*
7957 * if ":split file" worked, set alternate file name in old window to new
7958 * file
7959 */
7960 if (old_curwin != NULL
7961 && *eap->arg != NUL
7962 && curwin != old_curwin
7963 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007964 && old_curwin->w_buffer != curbuf
7965 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 old_curwin->w_alt_fnum = curbuf->b_fnum;
7967#endif
7968
7969 ex_no_reprint = TRUE;
7970}
7971
7972#ifndef FEAT_GUI
7973/*
7974 * ":gui" and ":gvim" when there is no GUI.
7975 */
7976 static void
7977ex_nogui(eap)
7978 exarg_T *eap;
7979{
7980 eap->errmsg = e_nogvim;
7981}
7982#endif
7983
7984#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7985 static void
7986ex_tearoff(eap)
7987 exarg_T *eap;
7988{
7989 gui_make_tearoff(eap->arg);
7990}
7991#endif
7992
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007993#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 static void
7995ex_popup(eap)
7996 exarg_T *eap;
7997{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007998 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999}
8000#endif
8001
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 static void
8003ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008004 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005{
8006 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8007 MSG(_("No swap file"));
8008 else
8009 msg(curbuf->b_ml.ml_mfp->mf_fname);
8010}
8011
8012/*
8013 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8014 * offset.
8015 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 static void
8018ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008019 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020{
8021#ifdef FEAT_SCROLLBIND
8022 win_T *wp;
8023 long topline;
8024 long y;
8025 linenr_T old_linenr = curwin->w_cursor.lnum;
8026
8027 setpcmark();
8028
8029 /*
8030 * determine max topline
8031 */
8032 if (curwin->w_p_scb)
8033 {
8034 topline = curwin->w_topline;
8035 for (wp = firstwin; wp; wp = wp->w_next)
8036 {
8037 if (wp->w_p_scb && wp->w_buffer)
8038 {
8039 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8040 if (topline > y)
8041 topline = y;
8042 }
8043 }
8044 if (topline < 1)
8045 topline = 1;
8046 }
8047 else
8048 {
8049 topline = 1;
8050 }
8051
8052
8053 /*
8054 * set all scrollbind windows to the same topline
8055 */
8056 wp = curwin;
8057 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8058 {
8059 if (curwin->w_p_scb)
8060 {
8061 y = topline - curwin->w_topline;
8062 if (y > 0)
8063 scrollup(y, TRUE);
8064 else
8065 scrolldown(-y, TRUE);
8066 curwin->w_scbind_pos = topline;
8067 redraw_later(VALID);
8068 cursor_correct();
8069#ifdef FEAT_WINDOWS
8070 curwin->w_redr_status = TRUE;
8071#endif
8072 }
8073 }
8074 curwin = wp;
8075 if (curwin->w_p_scb)
8076 {
8077 did_syncbind = TRUE;
8078 checkpcmark();
8079 if (old_linenr != curwin->w_cursor.lnum)
8080 {
8081 char_u ctrl_o[2];
8082
8083 ctrl_o[0] = Ctrl_O;
8084 ctrl_o[1] = 0;
8085 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8086 }
8087 }
8088#endif
8089}
8090
8091
8092 static void
8093ex_read(eap)
8094 exarg_T *eap;
8095{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008096 int i;
8097 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8098 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099
8100 if (eap->usefilter) /* :r!cmd */
8101 do_bang(1, eap, FALSE, FALSE, TRUE);
8102 else
8103 {
8104 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8105 return;
8106
8107#ifdef FEAT_BROWSE
8108 if (cmdmod.browse)
8109 {
8110 char_u *browseFile;
8111
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008112 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 NULL, NULL, NULL, curbuf);
8114 if (browseFile != NULL)
8115 {
8116 i = readfile(browseFile, NULL,
8117 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8118 vim_free(browseFile);
8119 }
8120 else
8121 i = OK;
8122 }
8123 else
8124#endif
8125 if (*eap->arg == NUL)
8126 {
8127 if (check_fname() == FAIL) /* check for no file name */
8128 return;
8129 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8130 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8131 }
8132 else
8133 {
8134 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8135 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8136 i = readfile(eap->arg, NULL,
8137 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8138
8139 }
8140 if (i == FAIL)
8141 {
8142#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8143 if (!aborting())
8144#endif
8145 EMSG2(_(e_notopen), eap->arg);
8146 }
8147 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008148 {
8149 if (empty && exmode_active)
8150 {
8151 /* Delete the empty line that remains. Historically ex does
8152 * this but vi doesn't. */
8153 if (eap->line2 == 0)
8154 lnum = curbuf->b_ml.ml_line_count;
8155 else
8156 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008157 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008158 {
8159 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008160 if (curwin->w_cursor.lnum > 1
8161 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008162 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008163 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008164 }
8165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 }
8169}
8170
Bram Moolenaarea408852005-06-25 22:49:46 +00008171static char_u *prev_dir = NULL;
8172
8173#if defined(EXITFREE) || defined(PROTO)
8174 void
8175free_cd_dir()
8176{
8177 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008178 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008179
8180 vim_free(globaldir);
8181 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008182}
8183#endif
8184
Bram Moolenaarf4258302013-06-02 18:20:17 +02008185/*
8186 * Deal with the side effects of changing the current directory.
8187 * When "local" is TRUE then this was after an ":lcd" command.
8188 */
8189 void
8190post_chdir(local)
8191 int local;
8192{
8193 vim_free(curwin->w_localdir);
8194 if (local)
8195 {
8196 /* If still in global directory, need to remember current
8197 * directory as global directory. */
8198 if (globaldir == NULL && prev_dir != NULL)
8199 globaldir = vim_strsave(prev_dir);
8200 /* Remember this local directory for the window. */
8201 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8202 curwin->w_localdir = vim_strsave(NameBuff);
8203 }
8204 else
8205 {
8206 /* We are now in the global directory, no need to remember its
8207 * name. */
8208 vim_free(globaldir);
8209 globaldir = NULL;
8210 curwin->w_localdir = NULL;
8211 }
8212
8213 shorten_fnames(TRUE);
8214}
8215
Bram Moolenaarea408852005-06-25 22:49:46 +00008216
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217/*
8218 * ":cd", ":lcd", ":chdir" and ":lchdir".
8219 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008220 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221ex_cd(eap)
8222 exarg_T *eap;
8223{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 char_u *new_dir;
8225 char_u *tofree;
8226
8227 new_dir = eap->arg;
8228#if !defined(UNIX) && !defined(VMS)
8229 /* for non-UNIX ":cd" means: print current directory */
8230 if (*new_dir == NUL)
8231 ex_pwd(NULL);
8232 else
8233#endif
8234 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008235#ifdef FEAT_AUTOCMD
8236 if (allbuf_locked())
8237 return;
8238#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008239 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8240 && !eap->forceit)
8241 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008242 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008243 return;
8244 }
8245
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 /* ":cd -": Change to previous directory */
8247 if (STRCMP(new_dir, "-") == 0)
8248 {
8249 if (prev_dir == NULL)
8250 {
8251 EMSG(_("E186: No previous directory"));
8252 return;
8253 }
8254 new_dir = prev_dir;
8255 }
8256
8257 /* Save current directory for next ":cd -" */
8258 tofree = prev_dir;
8259 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8260 prev_dir = vim_strsave(NameBuff);
8261 else
8262 prev_dir = NULL;
8263
8264#if defined(UNIX) || defined(VMS)
8265 /* for UNIX ":cd" means: go to home directory */
8266 if (*new_dir == NUL)
8267 {
8268 /* use NameBuff for home directory name */
8269# ifdef VMS
8270 char_u *p;
8271
8272 p = mch_getenv((char_u *)"SYS$LOGIN");
8273 if (p == NULL || *p == NUL) /* empty is the same as not set */
8274 NameBuff[0] = NUL;
8275 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008276 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277# else
8278 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8279# endif
8280 new_dir = NameBuff;
8281 }
8282#endif
8283 if (new_dir == NULL || vim_chdir(new_dir))
8284 EMSG(_(e_failed));
8285 else
8286 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008287 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288
8289 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008290 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 ex_pwd(eap);
8292 }
8293 vim_free(tofree);
8294 }
8295}
8296
8297/*
8298 * ":pwd".
8299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 static void
8301ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008302 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303{
8304 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8305 {
8306#ifdef BACKSLASH_IN_FILENAME
8307 slash_adjust(NameBuff);
8308#endif
8309 msg(NameBuff);
8310 }
8311 else
8312 EMSG(_("E187: Unknown"));
8313}
8314
8315/*
8316 * ":=".
8317 */
8318 static void
8319ex_equal(eap)
8320 exarg_T *eap;
8321{
8322 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008323 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324}
8325
8326 static void
8327ex_sleep(eap)
8328 exarg_T *eap;
8329{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008330 int n;
8331 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332
8333 if (cursor_valid())
8334 {
8335 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8336 if (n >= 0)
8337 windgoto((int)n, curwin->w_wcol);
8338 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008339
8340 len = eap->line2;
8341 switch (*eap->arg)
8342 {
8343 case 'm': break;
8344 case NUL: len *= 1000L; break;
8345 default: EMSG2(_(e_invarg2), eap->arg); return;
8346 }
8347 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348}
8349
8350/*
8351 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8352 */
8353 void
8354do_sleep(msec)
8355 long msec;
8356{
8357 long done;
8358
8359 cursor_on();
8360 out_flush();
8361 for (done = 0; !got_int && done < msec; done += 1000L)
8362 {
8363 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8364 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008365#ifdef FEAT_NETBEANS_INTG
8366 /* Process the netbeans messages that may have been received in the
8367 * call to ui_breakcheck() when the GUI is in use. This may occur when
8368 * running a test case. */
8369 netbeans_parse_messages();
8370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 }
8372}
8373
8374 static void
8375do_exmap(eap, isabbrev)
8376 exarg_T *eap;
8377 int isabbrev;
8378{
8379 int mode;
8380 char_u *cmdp;
8381
8382 cmdp = eap->cmd;
8383 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8384
8385 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8386 eap->arg, mode, isabbrev))
8387 {
8388 case 1: EMSG(_(e_invarg));
8389 break;
8390 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8391 break;
8392 }
8393}
8394
8395/*
8396 * ":winsize" command (obsolete).
8397 */
8398 static void
8399ex_winsize(eap)
8400 exarg_T *eap;
8401{
8402 int w, h;
8403 char_u *arg = eap->arg;
8404 char_u *p;
8405
8406 w = getdigits(&arg);
8407 arg = skipwhite(arg);
8408 p = arg;
8409 h = getdigits(&arg);
8410 if (*p != NUL && *arg == NUL)
8411 set_shellsize(w, h, TRUE);
8412 else
8413 EMSG(_("E465: :winsize requires two number arguments"));
8414}
8415
8416#ifdef FEAT_WINDOWS
8417 static void
8418ex_wincmd(eap)
8419 exarg_T *eap;
8420{
8421 int xchar = NUL;
8422 char_u *p;
8423
8424 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8425 {
8426 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8427 if (eap->arg[1] == NUL)
8428 {
8429 EMSG(_(e_invarg));
8430 return;
8431 }
8432 xchar = eap->arg[1];
8433 p = eap->arg + 2;
8434 }
8435 else
8436 p = eap->arg + 1;
8437
8438 eap->nextcmd = check_nextcmd(p);
8439 p = skipwhite(p);
8440 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8441 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008442 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 {
8444 /* Pass flags on for ":vertical wincmd ]". */
8445 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008446 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8448 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008449 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 }
8451}
8452#endif
8453
Bram Moolenaar843ee412004-06-30 16:16:41 +00008454#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455/*
8456 * ":winpos".
8457 */
8458 static void
8459ex_winpos(eap)
8460 exarg_T *eap;
8461{
8462 int x, y;
8463 char_u *arg = eap->arg;
8464 char_u *p;
8465
8466 if (*arg == NUL)
8467 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008468# if defined(FEAT_GUI) || defined(MSWIN)
8469# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008471# else
8472 if (mch_get_winpos(&x, &y) != FAIL)
8473# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 {
8475 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8476 msg(IObuff);
8477 }
8478 else
8479# endif
8480 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8481 }
8482 else
8483 {
8484 x = getdigits(&arg);
8485 arg = skipwhite(arg);
8486 p = arg;
8487 y = getdigits(&arg);
8488 if (*p == NUL || *arg != NUL)
8489 {
8490 EMSG(_("E466: :winpos requires two number arguments"));
8491 return;
8492 }
8493# ifdef FEAT_GUI
8494 if (gui.in_use)
8495 gui_mch_set_winpos(x, y);
8496 else if (gui.starting)
8497 {
8498 /* Remember the coordinates for when the window is opened. */
8499 gui_win_x = x;
8500 gui_win_y = y;
8501 }
8502# ifdef HAVE_TGETENT
8503 else
8504# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008505# else
8506# ifdef MSWIN
8507 mch_set_winpos(x, y);
8508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509# endif
8510# ifdef HAVE_TGETENT
8511 if (*T_CWP)
8512 term_set_winpos(x, y);
8513# endif
8514 }
8515}
8516#endif
8517
8518/*
8519 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8520 */
8521 static void
8522ex_operators(eap)
8523 exarg_T *eap;
8524{
8525 oparg_T oa;
8526
8527 clear_oparg(&oa);
8528 oa.regname = eap->regname;
8529 oa.start.lnum = eap->line1;
8530 oa.end.lnum = eap->line2;
8531 oa.line_count = eap->line2 - eap->line1 + 1;
8532 oa.motion_type = MLINE;
8533#ifdef FEAT_VIRTUALEDIT
8534 virtual_op = FALSE;
8535#endif
8536 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8537 {
8538 setpcmark();
8539 curwin->w_cursor.lnum = eap->line1;
8540 beginline(BL_SOL | BL_FIX);
8541 }
8542
8543 switch (eap->cmdidx)
8544 {
8545 case CMD_delete:
8546 oa.op_type = OP_DELETE;
8547 op_delete(&oa);
8548 break;
8549
8550 case CMD_yank:
8551 oa.op_type = OP_YANK;
8552 (void)op_yank(&oa, FALSE, TRUE);
8553 break;
8554
8555 default: /* CMD_rshift or CMD_lshift */
8556 if ((eap->cmdidx == CMD_rshift)
8557#ifdef FEAT_RIGHTLEFT
8558 ^ curwin->w_p_rl
8559#endif
8560 )
8561 oa.op_type = OP_RSHIFT;
8562 else
8563 oa.op_type = OP_LSHIFT;
8564 op_shift(&oa, FALSE, eap->amount);
8565 break;
8566 }
8567#ifdef FEAT_VIRTUALEDIT
8568 virtual_op = MAYBE;
8569#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008570 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571}
8572
8573/*
8574 * ":put".
8575 */
8576 static void
8577ex_put(eap)
8578 exarg_T *eap;
8579{
8580 /* ":0put" works like ":1put!". */
8581 if (eap->line2 == 0)
8582 {
8583 eap->line2 = 1;
8584 eap->forceit = TRUE;
8585 }
8586 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008587 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8588 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589}
8590
8591/*
8592 * Handle ":copy" and ":move".
8593 */
8594 static void
8595ex_copymove(eap)
8596 exarg_T *eap;
8597{
8598 long n;
8599
8600 n = get_address(&eap->arg, FALSE, FALSE);
8601 if (eap->arg == NULL) /* error detected */
8602 {
8603 eap->nextcmd = NULL;
8604 return;
8605 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008606 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607
8608 /*
8609 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8610 */
8611 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8612 {
8613 EMSG(_(e_invaddr));
8614 return;
8615 }
8616
8617 if (eap->cmdidx == CMD_move)
8618 {
8619 if (do_move(eap->line1, eap->line2, n) == FAIL)
8620 return;
8621 }
8622 else
8623 ex_copy(eap->line1, eap->line2, n);
8624 u_clearline();
8625 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008626 ex_may_print(eap);
8627}
8628
8629/*
8630 * Print the current line if flags were given to the Ex command.
8631 */
8632 static void
8633ex_may_print(eap)
8634 exarg_T *eap;
8635{
8636 if (eap->flags != 0)
8637 {
8638 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8639 (eap->flags & EXFLAG_LIST));
8640 ex_no_reprint = TRUE;
8641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642}
8643
8644/*
8645 * ":smagic" and ":snomagic".
8646 */
8647 static void
8648ex_submagic(eap)
8649 exarg_T *eap;
8650{
8651 int magic_save = p_magic;
8652
8653 p_magic = (eap->cmdidx == CMD_smagic);
8654 do_sub(eap);
8655 p_magic = magic_save;
8656}
8657
8658/*
8659 * ":join".
8660 */
8661 static void
8662ex_join(eap)
8663 exarg_T *eap;
8664{
8665 curwin->w_cursor.lnum = eap->line1;
8666 if (eap->line1 == eap->line2)
8667 {
8668 if (eap->addr_count >= 2) /* :2,2join does nothing */
8669 return;
8670 if (eap->line2 == curbuf->b_ml.ml_line_count)
8671 {
8672 beep_flush();
8673 return;
8674 }
8675 ++eap->line2;
8676 }
Bram Moolenaar81340392012-06-06 16:12:59 +02008677 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008679 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680}
8681
8682/*
8683 * ":[addr]@r" or ":[addr]*r": execute register
8684 */
8685 static void
8686ex_at(eap)
8687 exarg_T *eap;
8688{
8689 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008690 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691
8692 curwin->w_cursor.lnum = eap->line2;
8693
8694#ifdef USE_ON_FLY_SCROLL
8695 dont_scroll = TRUE; /* disallow scrolling here */
8696#endif
8697
8698 /* get the register name. No name means to use the previous one */
8699 c = *eap->arg;
8700 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8701 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008702 /* Put the register in the typeahead buffer with the "silent" flag. */
8703 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8704 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008705 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 else
8709 {
8710 int save_efr = exec_from_reg;
8711
8712 exec_from_reg = TRUE;
8713
8714 /*
8715 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008716 * Continue until the stuff buffer is empty and all added characters
8717 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008719 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8721
8722 exec_from_reg = save_efr;
8723 }
8724}
8725
8726/*
8727 * ":!".
8728 */
8729 static void
8730ex_bang(eap)
8731 exarg_T *eap;
8732{
8733 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8734}
8735
8736/*
8737 * ":undo".
8738 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 static void
8740ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008741 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008743 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008744 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008745 else
8746 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747}
8748
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008749#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008750 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008751ex_wundo(eap)
8752 exarg_T *eap;
8753{
8754 char_u hash[UNDO_HASH_SIZE];
8755
8756 u_compute_hash(hash);
8757 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8758}
8759
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008760 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008761ex_rundo(eap)
8762 exarg_T *eap;
8763{
8764 char_u hash[UNDO_HASH_SIZE];
8765
8766 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008767 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008768}
8769#endif
8770
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771/*
8772 * ":redo".
8773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 static void
8775ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008776 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777{
8778 u_redo(1);
8779}
8780
8781/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008782 * ":earlier" and ":later".
8783 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008784 static void
8785ex_later(eap)
8786 exarg_T *eap;
8787{
8788 long count = 0;
8789 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008790 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008791 char_u *p = eap->arg;
8792
8793 if (*p == NUL)
8794 count = 1;
8795 else if (isdigit(*p))
8796 {
8797 count = getdigits(&p);
8798 switch (*p)
8799 {
8800 case 's': ++p; sec = TRUE; break;
8801 case 'm': ++p; sec = TRUE; count *= 60; break;
8802 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008803 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8804 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008805 }
8806 }
8807
8808 if (*p != NUL)
8809 EMSG2(_(e_invarg2), eap->arg);
8810 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008811 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8812 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008813}
8814
8815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 * ":redir": start/stop redirection.
8817 */
8818 static void
8819ex_redir(eap)
8820 exarg_T *eap;
8821{
8822 char *mode;
8823 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008824 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825
8826 if (STRICMP(eap->arg, "END") == 0)
8827 close_redir();
8828 else
8829 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008830 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008832 ++arg;
8833 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008835 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 mode = "a";
8837 }
8838 else
8839 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008840 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841
8842 close_redir();
8843
8844 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008845 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 if (fname == NULL)
8847 return;
8848#ifdef FEAT_BROWSE
8849 if (cmdmod.browse)
8850 {
8851 char_u *browseFile;
8852
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008853 browseFile = do_browse(BROWSE_SAVE,
8854 (char_u *)_("Save Redirection"),
8855 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 if (browseFile == NULL)
8857 return; /* operation cancelled */
8858 vim_free(fname);
8859 fname = browseFile;
8860 eap->forceit = TRUE; /* since dialog already asked */
8861 }
8862#endif
8863
8864 redir_fd = open_exfile(fname, eap->forceit, mode);
8865 vim_free(fname);
8866 }
8867#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008868 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 {
8870 /* redirect to a register a-z (resp. A-Z for appending) */
8871 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008872 ++arg;
8873 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008875 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008876 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008878 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008880 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008881 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008882 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008883 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008885 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008886 if (*arg == '>')
8887 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008888 /* Make register empty when not using @A-@Z and the
8889 * command is valid. */
8890 if (*arg == NUL && !isupper(redir_reg))
8891 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008893 }
8894 if (*arg != NUL)
8895 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008896 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008897 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008898 }
8899 }
8900 else if (*arg == '=' && arg[1] == '>')
8901 {
8902 int append;
8903
8904 /* redirect to a variable */
8905 close_redir();
8906 arg += 2;
8907
8908 if (*arg == '>')
8909 {
8910 ++arg;
8911 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 }
8913 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008914 append = FALSE;
8915
8916 if (var_redir_start(skipwhite(arg), append) == OK)
8917 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 }
8919#endif
8920
8921 /* TODO: redirect to a buffer */
8922
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008924 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008926
8927 /* Make sure redirection is not off. Can happen for cmdline completion
8928 * that indirectly invokes a command to catch its output. */
8929 if (redir_fd != NULL
8930#ifdef FEAT_EVAL
8931 || redir_reg || redir_vname
8932#endif
8933 )
8934 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935}
8936
8937/*
8938 * ":redraw": force redraw
8939 */
8940 static void
8941ex_redraw(eap)
8942 exarg_T *eap;
8943{
8944 int r = RedrawingDisabled;
8945 int p = p_lz;
8946
8947 RedrawingDisabled = 0;
8948 p_lz = FALSE;
8949 update_topline();
8950 update_screen(eap->forceit ? CLEAR :
8951#ifdef FEAT_VISUAL
8952 VIsual_active ? INVERTED :
8953#endif
8954 0);
8955#ifdef FEAT_TITLE
8956 if (need_maketitle)
8957 maketitle();
8958#endif
8959 RedrawingDisabled = r;
8960 p_lz = p;
8961
8962 /* Reset msg_didout, so that a message that's there is overwritten. */
8963 msg_didout = FALSE;
8964 msg_col = 0;
8965
8966 out_flush();
8967}
8968
8969/*
8970 * ":redrawstatus": force redraw of status line(s)
8971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 static void
8973ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008974 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975{
8976#if defined(FEAT_WINDOWS)
8977 int r = RedrawingDisabled;
8978 int p = p_lz;
8979
8980 RedrawingDisabled = 0;
8981 p_lz = FALSE;
8982 if (eap->forceit)
8983 status_redraw_all();
8984 else
8985 status_redraw_curbuf();
8986 update_screen(
8987# ifdef FEAT_VISUAL
8988 VIsual_active ? INVERTED :
8989# endif
8990 0);
8991 RedrawingDisabled = r;
8992 p_lz = p;
8993 out_flush();
8994#endif
8995}
8996
8997 static void
8998close_redir()
8999{
9000 if (redir_fd != NULL)
9001 {
9002 fclose(redir_fd);
9003 redir_fd = NULL;
9004 }
9005#ifdef FEAT_EVAL
9006 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009007 if (redir_vname)
9008 {
9009 var_redir_stop();
9010 redir_vname = 0;
9011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012#endif
9013}
9014
9015#if defined(FEAT_SESSION) && defined(USE_CRNL)
9016# define MKSESSION_NL
9017static int mksession_nl = FALSE; /* use NL only in put_eol() */
9018#endif
9019
9020/*
9021 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9022 */
9023 static void
9024ex_mkrc(eap)
9025 exarg_T *eap;
9026{
9027 FILE *fd;
9028 int failed = FALSE;
9029 char_u *fname;
9030#ifdef FEAT_BROWSE
9031 char_u *browseFile = NULL;
9032#endif
9033#ifdef FEAT_SESSION
9034 int view_session = FALSE;
9035 int using_vdir = FALSE; /* using 'viewdir'? */
9036 char_u *viewFile = NULL;
9037 unsigned *flagp;
9038#endif
9039
9040 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9041 {
9042#ifdef FEAT_SESSION
9043 view_session = TRUE;
9044#else
9045 ex_ni(eap);
9046 return;
9047#endif
9048 }
9049
9050#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009051 /* Use the short file name until ":lcd" is used. We also don't use the
9052 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009053 did_lcd = FALSE;
9054
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9056 if (eap->cmdidx == CMD_mkview
9057 && (*eap->arg == NUL
9058 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9059 {
9060 eap->forceit = TRUE;
9061 fname = get_view_file(*eap->arg);
9062 if (fname == NULL)
9063 return;
9064 viewFile = fname;
9065 using_vdir = TRUE;
9066 }
9067 else
9068#endif
9069 if (*eap->arg != NUL)
9070 fname = eap->arg;
9071 else if (eap->cmdidx == CMD_mkvimrc)
9072 fname = (char_u *)VIMRC_FILE;
9073#ifdef FEAT_SESSION
9074 else if (eap->cmdidx == CMD_mksession)
9075 fname = (char_u *)SESSION_FILE;
9076#endif
9077 else
9078 fname = (char_u *)EXRC_FILE;
9079
9080#ifdef FEAT_BROWSE
9081 if (cmdmod.browse)
9082 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009083 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084# ifdef FEAT_SESSION
9085 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9086 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9087# endif
9088 (char_u *)_("Save Setup"),
9089 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9090 if (browseFile == NULL)
9091 goto theend;
9092 fname = browseFile;
9093 eap->forceit = TRUE; /* since dialog already asked */
9094 }
9095#endif
9096
9097#if defined(FEAT_SESSION) && defined(vim_mkdir)
9098 /* When using 'viewdir' may have to create the directory. */
9099 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009100 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101#endif
9102
9103 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9104 if (fd != NULL)
9105 {
9106#ifdef FEAT_SESSION
9107 if (eap->cmdidx == CMD_mkview)
9108 flagp = &vop_flags;
9109 else
9110 flagp = &ssop_flags;
9111#endif
9112
9113#ifdef MKSESSION_NL
9114 /* "unix" in 'sessionoptions': use NL line separator */
9115 if (view_session && (*flagp & SSOP_UNIX))
9116 mksession_nl = TRUE;
9117#endif
9118
9119 /* Write the version command for :mkvimrc */
9120 if (eap->cmdidx == CMD_mkvimrc)
9121 (void)put_line(fd, "version 6.0");
9122
9123#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009124 if (eap->cmdidx == CMD_mksession)
9125 {
9126 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9127 failed = TRUE;
9128 }
9129
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 if (eap->cmdidx != CMD_mkview)
9131#endif
9132 {
9133 /* Write setting 'compatible' first, because it has side effects.
9134 * For that same reason only do it when needed. */
9135 if (p_cp)
9136 (void)put_line(fd, "if !&cp | set cp | endif");
9137 else
9138 (void)put_line(fd, "if &cp | set nocp | endif");
9139 }
9140
9141#ifdef FEAT_SESSION
9142 if (!view_session
9143 || (eap->cmdidx == CMD_mksession
9144 && (*flagp & SSOP_OPTIONS)))
9145#endif
9146 failed |= (makemap(fd, NULL) == FAIL
9147 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9148
9149#ifdef FEAT_SESSION
9150 if (!failed && view_session)
9151 {
9152 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9153 failed = TRUE;
9154 if (eap->cmdidx == CMD_mksession)
9155 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009156 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157
Bram Moolenaard9462e32011-04-11 21:35:11 +02009158 dirnow = alloc(MAXPATHL);
9159 if (dirnow == NULL)
9160 failed = TRUE;
9161 else
9162 {
9163 /*
9164 * Change to session file's dir.
9165 */
9166 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009168 *dirnow = NUL;
9169 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9170 {
9171 if (vim_chdirfile(fname) == OK)
9172 shorten_fnames(TRUE);
9173 }
9174 else if (*dirnow != NUL
9175 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9176 {
9177 if (mch_chdir((char *)globaldir) == 0)
9178 shorten_fnames(TRUE);
9179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180
Bram Moolenaard9462e32011-04-11 21:35:11 +02009181 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182
Bram Moolenaard9462e32011-04-11 21:35:11 +02009183 /* restore original dir */
9184 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009186 {
9187 if (mch_chdir((char *)dirnow) != 0)
9188 EMSG(_(e_prev_dir));
9189 shorten_fnames(TRUE);
9190 }
9191 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 }
9193 }
9194 else
9195 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009196 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9197 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 }
9199 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9200 == FAIL)
9201 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009202 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9203 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009204 if (eap->cmdidx == CMD_mksession)
9205 {
9206 if (put_line(fd, "unlet SessionLoad") == FAIL)
9207 failed = TRUE;
9208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 }
9210#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009211 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9212 failed = TRUE;
9213
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 failed |= fclose(fd);
9215
9216 if (failed)
9217 EMSG(_(e_write));
9218#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9219 else if (eap->cmdidx == CMD_mksession)
9220 {
9221 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009222 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223
Bram Moolenaard9462e32011-04-11 21:35:11 +02009224 tbuf = alloc(MAXPATHL);
9225 if (tbuf != NULL)
9226 {
9227 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9228 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9229 vim_free(tbuf);
9230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 }
9232#endif
9233#ifdef MKSESSION_NL
9234 mksession_nl = FALSE;
9235#endif
9236 }
9237
9238#ifdef FEAT_BROWSE
9239theend:
9240 vim_free(browseFile);
9241#endif
9242#ifdef FEAT_SESSION
9243 vim_free(viewFile);
9244#endif
9245}
9246
Bram Moolenaardf177f62005-02-22 08:39:57 +00009247#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9248 || defined(PROTO)
9249 int
9250vim_mkdir_emsg(name, prot)
9251 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009252 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009253{
9254 if (vim_mkdir(name, prot) != 0)
9255 {
9256 EMSG2(_("E739: Cannot create directory: %s"), name);
9257 return FAIL;
9258 }
9259 return OK;
9260}
9261#endif
9262
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263/*
9264 * Open a file for writing for an Ex command, with some checks.
9265 * Return file descriptor, or NULL on failure.
9266 */
9267 FILE *
9268open_exfile(fname, forceit, mode)
9269 char_u *fname;
9270 int forceit;
9271 char *mode; /* "w" for create new file or "a" for append */
9272{
9273 FILE *fd;
9274
9275#ifdef UNIX
9276 /* with Unix it is possible to open a directory */
9277 if (mch_isdir(fname))
9278 {
9279 EMSG2(_(e_isadir2), fname);
9280 return NULL;
9281 }
9282#endif
9283 if (!forceit && *mode != 'a' && vim_fexists(fname))
9284 {
9285 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9286 return NULL;
9287 }
9288
9289 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9290 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9291
9292 return fd;
9293}
9294
9295/*
9296 * ":mark" and ":k".
9297 */
9298 static void
9299ex_mark(eap)
9300 exarg_T *eap;
9301{
9302 pos_T pos;
9303
9304 if (*eap->arg == NUL) /* No argument? */
9305 EMSG(_(e_argreq));
9306 else if (eap->arg[1] != NUL) /* more than one character? */
9307 EMSG(_(e_trailing));
9308 else
9309 {
9310 pos = curwin->w_cursor; /* save curwin->w_cursor */
9311 curwin->w_cursor.lnum = eap->line2;
9312 beginline(BL_WHITE | BL_FIX);
9313 if (setmark(*eap->arg) == FAIL) /* set mark */
9314 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9315 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9316 }
9317}
9318
9319/*
9320 * Update w_topline, w_leftcol and the cursor position.
9321 */
9322 void
9323update_topline_cursor()
9324{
9325 check_cursor(); /* put cursor on valid line */
9326 update_topline();
9327 if (!curwin->w_p_wrap)
9328 validate_cursor();
9329 update_curswant();
9330}
9331
9332#ifdef FEAT_EX_EXTRA
9333/*
9334 * ":normal[!] {commands}": Execute normal mode commands.
9335 */
9336 static void
9337ex_normal(eap)
9338 exarg_T *eap;
9339{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 int save_msg_scroll = msg_scroll;
9341 int save_restart_edit = restart_edit;
9342 int save_msg_didout = msg_didout;
9343 int save_State = State;
9344 tasave_T tabuf;
9345 int save_insertmode = p_im;
9346 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009347 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348#ifdef FEAT_MBYTE
9349 char_u *arg = NULL;
9350 int l;
9351 char_u *p;
9352#endif
9353
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009354 if (ex_normal_lock > 0)
9355 {
9356 EMSG(_(e_secure));
9357 return;
9358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 if (ex_normal_busy >= p_mmd)
9360 {
9361 EMSG(_("E192: Recursive use of :normal too deep"));
9362 return;
9363 }
9364 ++ex_normal_busy;
9365
9366 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9367 restart_edit = 0; /* don't go to Insert mode */
9368 p_im = FALSE; /* don't use 'insertmode' */
9369
9370#ifdef FEAT_MBYTE
9371 /*
9372 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9373 * this for the K_SPECIAL leading byte, otherwise special keys will not
9374 * work.
9375 */
9376 if (has_mbyte)
9377 {
9378 int len = 0;
9379
9380 /* Count the number of characters to be escaped. */
9381 for (p = eap->arg; *p != NUL; ++p)
9382 {
9383# ifdef FEAT_GUI
9384 if (*p == CSI) /* leadbyte CSI */
9385 len += 2;
9386# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009387 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9389# ifdef FEAT_GUI
9390 || *p == CSI
9391# endif
9392 )
9393 len += 2;
9394 }
9395 if (len > 0)
9396 {
9397 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9398 if (arg != NULL)
9399 {
9400 len = 0;
9401 for (p = eap->arg; *p != NUL; ++p)
9402 {
9403 arg[len++] = *p;
9404# ifdef FEAT_GUI
9405 if (*p == CSI)
9406 {
9407 arg[len++] = KS_EXTRA;
9408 arg[len++] = (int)KE_CSI;
9409 }
9410# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009411 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 {
9413 arg[len++] = *++p;
9414 if (*p == K_SPECIAL)
9415 {
9416 arg[len++] = KS_SPECIAL;
9417 arg[len++] = KE_FILLER;
9418 }
9419# ifdef FEAT_GUI
9420 else if (*p == CSI)
9421 {
9422 arg[len++] = KS_EXTRA;
9423 arg[len++] = (int)KE_CSI;
9424 }
9425# endif
9426 }
9427 arg[len] = NUL;
9428 }
9429 }
9430 }
9431 }
9432#endif
9433
9434 /*
9435 * Save the current typeahead. This is required to allow using ":normal"
9436 * from an event handler and makes sure we don't hang when the argument
9437 * ends with half a command.
9438 */
9439 save_typeahead(&tabuf);
9440 if (tabuf.typebuf_valid)
9441 {
9442 /*
9443 * Repeat the :normal command for each line in the range. When no
9444 * range given, execute it just once, without positioning the cursor
9445 * first.
9446 */
9447 do
9448 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 if (eap->addr_count != 0)
9450 {
9451 curwin->w_cursor.lnum = eap->line1++;
9452 curwin->w_cursor.col = 0;
9453 }
9454
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009455 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456#ifdef FEAT_MBYTE
9457 arg != NULL ? arg :
9458#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009459 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 }
9461 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9462 }
9463
9464 /* Might not return to the main loop when in an event handler. */
9465 update_topline_cursor();
9466
9467 /* Restore the previous typeahead. */
9468 restore_typeahead(&tabuf);
9469
9470 --ex_normal_busy;
9471 msg_scroll = save_msg_scroll;
9472 restart_edit = save_restart_edit;
9473 p_im = save_insertmode;
9474 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009475 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9477
9478 /* Restore the state (needed when called from a function executed for
9479 * 'indentexpr'). */
9480 State = save_State;
9481#ifdef FEAT_MBYTE
9482 vim_free(arg);
9483#endif
9484}
9485
9486/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009487 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 */
9489 static void
9490ex_startinsert(eap)
9491 exarg_T *eap;
9492{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009493 if (eap->forceit)
9494 {
9495 coladvance((colnr_T)MAXCOL);
9496 curwin->w_curswant = MAXCOL;
9497 curwin->w_set_curswant = FALSE;
9498 }
9499
Bram Moolenaara40c5002005-01-09 21:16:21 +00009500 /* Ignore the command when already in Insert mode. Inserting an
9501 * expression register that invokes a function can do this. */
9502 if (State & INSERT)
9503 return;
9504
Bram Moolenaar64969662005-12-14 21:59:55 +00009505 if (eap->cmdidx == CMD_startinsert)
9506 restart_edit = 'a';
9507 else if (eap->cmdidx == CMD_startreplace)
9508 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009510 restart_edit = 'V';
9511
9512 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009514 if (eap->cmdidx == CMD_startinsert)
9515 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 curwin->w_curswant = 0; /* avoid MAXCOL */
9517 }
9518}
9519
9520/*
9521 * ":stopinsert"
9522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 static void
9524ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009525 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526{
9527 restart_edit = 0;
9528 stop_insert_mode = TRUE;
9529}
9530#endif
9531
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009532#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9533/*
9534 * Execute normal mode command "cmd".
9535 * "remap" can be REMAP_NONE or REMAP_YES.
9536 */
9537 void
9538exec_normal_cmd(cmd, remap, silent)
9539 char_u *cmd;
9540 int remap;
9541 int silent;
9542{
9543 oparg_T oa;
9544
9545 /*
9546 * Stuff the argument into the typeahead buffer.
9547 * Execute normal_cmd() until there is no typeahead left.
9548 */
9549 clear_oparg(&oa);
9550 finish_op = FALSE;
9551 ins_typebuf(cmd, remap, 0, TRUE, silent);
9552 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9553 && !got_int)
9554 {
9555 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009556 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009557 }
9558}
9559#endif
9560
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561#ifdef FEAT_FIND_ID
9562 static void
9563ex_checkpath(eap)
9564 exarg_T *eap;
9565{
9566 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9567 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9568 (linenr_T)1, (linenr_T)MAXLNUM);
9569}
9570
9571#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9572/*
9573 * ":psearch"
9574 */
9575 static void
9576ex_psearch(eap)
9577 exarg_T *eap;
9578{
9579 g_do_tagpreview = p_pvh;
9580 ex_findpat(eap);
9581 g_do_tagpreview = 0;
9582}
9583#endif
9584
9585 static void
9586ex_findpat(eap)
9587 exarg_T *eap;
9588{
9589 int whole = TRUE;
9590 long n;
9591 char_u *p;
9592 int action;
9593
9594 switch (cmdnames[eap->cmdidx].cmd_name[2])
9595 {
9596 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9597 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9598 action = ACTION_GOTO;
9599 else
9600 action = ACTION_SHOW;
9601 break;
9602 case 'i': /* ":ilist" and ":dlist" */
9603 action = ACTION_SHOW_ALL;
9604 break;
9605 case 'u': /* ":ijump" and ":djump" */
9606 action = ACTION_GOTO;
9607 break;
9608 default: /* ":isplit" and ":dsplit" */
9609 action = ACTION_SPLIT;
9610 break;
9611 }
9612
9613 n = 1;
9614 if (vim_isdigit(*eap->arg)) /* get count */
9615 {
9616 n = getdigits(&eap->arg);
9617 eap->arg = skipwhite(eap->arg);
9618 }
9619 if (*eap->arg == '/') /* Match regexp, not just whole words */
9620 {
9621 whole = FALSE;
9622 ++eap->arg;
9623 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9624 if (*p)
9625 {
9626 *p++ = NUL;
9627 p = skipwhite(p);
9628
9629 /* Check for trailing illegal characters */
9630 if (!ends_excmd(*p))
9631 eap->errmsg = e_trailing;
9632 else
9633 eap->nextcmd = check_nextcmd(p);
9634 }
9635 }
9636 if (!eap->skip)
9637 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9638 whole, !eap->forceit,
9639 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9640 n, action, eap->line1, eap->line2);
9641}
9642#endif
9643
9644#ifdef FEAT_WINDOWS
9645
9646# ifdef FEAT_QUICKFIX
9647/*
9648 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9649 */
9650 static void
9651ex_ptag(eap)
9652 exarg_T *eap;
9653{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009654 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9656}
9657
9658/*
9659 * ":pedit"
9660 */
9661 static void
9662ex_pedit(eap)
9663 exarg_T *eap;
9664{
9665 win_T *curwin_save = curwin;
9666
9667 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009668 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 keep_help_flag = curwin_save->w_buffer->b_help;
9670 do_exedit(eap, NULL);
9671 keep_help_flag = FALSE;
9672 if (curwin != curwin_save && win_valid(curwin_save))
9673 {
9674 /* Return cursor to where we were */
9675 validate_cursor();
9676 redraw_later(VALID);
9677 win_enter(curwin_save, TRUE);
9678 }
9679 g_do_tagpreview = 0;
9680}
9681# endif
9682
9683/*
9684 * ":stag", ":stselect" and ":stjump".
9685 */
9686 static void
9687ex_stag(eap)
9688 exarg_T *eap;
9689{
9690 postponed_split = -1;
9691 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009692 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9694 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009695 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696}
9697#endif
9698
9699/*
9700 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9701 */
9702 static void
9703ex_tag(eap)
9704 exarg_T *eap;
9705{
9706 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9707}
9708
9709 static void
9710ex_tag_cmd(eap, name)
9711 exarg_T *eap;
9712 char_u *name;
9713{
9714 int cmd;
9715
9716 switch (name[1])
9717 {
9718 case 'j': cmd = DT_JUMP; /* ":tjump" */
9719 break;
9720 case 's': cmd = DT_SELECT; /* ":tselect" */
9721 break;
9722 case 'p': cmd = DT_PREV; /* ":tprevious" */
9723 break;
9724 case 'N': cmd = DT_PREV; /* ":tNext" */
9725 break;
9726 case 'n': cmd = DT_NEXT; /* ":tnext" */
9727 break;
9728 case 'o': cmd = DT_POP; /* ":pop" */
9729 break;
9730 case 'f': /* ":tfirst" */
9731 case 'r': cmd = DT_FIRST; /* ":trewind" */
9732 break;
9733 case 'l': cmd = DT_LAST; /* ":tlast" */
9734 break;
9735 default: /* ":tag" */
9736#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009737 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 {
9739 do_cstag(eap);
9740 return;
9741 }
9742#endif
9743 cmd = DT_TAG;
9744 break;
9745 }
9746
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009747 if (name[0] == 'l')
9748 {
9749#ifndef FEAT_QUICKFIX
9750 ex_ni(eap);
9751 return;
9752#else
9753 cmd = DT_LTAG;
9754#endif
9755 }
9756
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9758 eap->forceit, TRUE);
9759}
9760
9761/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009762 * Check "str" for starting with a special cmdline variable.
9763 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9764 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9765 */
9766 int
9767find_cmdline_var(src, usedlen)
9768 char_u *src;
9769 int *usedlen;
9770{
9771 int len;
9772 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009773 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009774 "%",
9775#define SPEC_PERC 0
9776 "#",
9777#define SPEC_HASH 1
9778 "<cword>", /* cursor word */
9779#define SPEC_CWORD 2
9780 "<cWORD>", /* cursor WORD */
9781#define SPEC_CCWORD 3
9782 "<cfile>", /* cursor path name */
9783#define SPEC_CFILE 4
9784 "<sfile>", /* ":so" file name */
9785#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009786 "<slnum>", /* ":so" file line number */
9787#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009788#ifdef FEAT_AUTOCMD
9789 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009790# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009791 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009792# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009793 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009794# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009795#endif
9796#ifdef FEAT_CLIENTSERVER
9797 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009798# ifdef FEAT_AUTOCMD
9799# define SPEC_CLIENT 10
9800# else
9801# define SPEC_CLIENT 7
9802# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009803#endif
9804 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009805
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009806 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009807 {
9808 len = (int)STRLEN(spec_str[i]);
9809 if (STRNCMP(src, spec_str[i], len) == 0)
9810 {
9811 *usedlen = len;
9812 return i;
9813 }
9814 }
9815 return -1;
9816}
9817
9818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 * Evaluate cmdline variables.
9820 *
9821 * change '%' to curbuf->b_ffname
9822 * '#' to curwin->w_altfile
9823 * '<cword>' to word under the cursor
9824 * '<cWORD>' to WORD under the cursor
9825 * '<cfile>' to path name under the cursor
9826 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009827 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 * '<afile>' to file name for autocommand
9829 * '<abuf>' to buffer number for autocommand
9830 * '<amatch>' to matching name for autocommand
9831 *
9832 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9833 * "" for error without a message) and NULL is returned.
9834 * Returns an allocated string if a valid match was found.
9835 * Returns NULL if no match was found. "usedlen" then still contains the
9836 * number of characters to skip.
9837 */
9838 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009839eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009841 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 int *usedlen; /* characters after src that are used */
9843 linenr_T *lnump; /* line number for :e command, or NULL */
9844 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009845 int *escaped; /* return value has escaped white space (can
9846 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847{
9848 int i;
9849 char_u *s;
9850 char_u *result;
9851 char_u *resultbuf = NULL;
9852 int resultlen;
9853 buf_T *buf;
9854 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9855 int spec_idx;
9856#ifdef FEAT_MODIFY_FNAME
9857 int skip_mod = FALSE;
9858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860
9861 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009862 if (escaped != NULL)
9863 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864
9865 /*
9866 * Check if there is something to do.
9867 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009868 spec_idx = find_cmdline_var(src, usedlen);
9869 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 {
9871 *usedlen = 1;
9872 return NULL;
9873 }
9874
9875 /*
9876 * Skip when preceded with a backslash "\%" and "\#".
9877 * Note: In "\\%" the % is also not recognized!
9878 */
9879 if (src > srcstart && src[-1] == '\\')
9880 {
9881 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009882 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 return NULL;
9884 }
9885
9886 /*
9887 * word or WORD under cursor
9888 */
9889 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9890 {
9891 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9892 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9893 if (resultlen == 0)
9894 {
9895 *errormsg = (char_u *)"";
9896 return NULL;
9897 }
9898 }
9899
9900 /*
9901 * '#': Alternate file name
9902 * '%': Current file name
9903 * File name under the cursor
9904 * File name for autocommand
9905 * and following modifiers
9906 */
9907 else
9908 {
9909 switch (spec_idx)
9910 {
9911 case SPEC_PERC: /* '%': current file */
9912 if (curbuf->b_fname == NULL)
9913 {
9914 result = (char_u *)"";
9915 valid = 0; /* Must have ":p:h" to be valid */
9916 }
9917 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 break;
9920
9921 case SPEC_HASH: /* '#' or "#99": alternate file */
9922 if (src[1] == '#') /* "##": the argument list */
9923 {
9924 result = arg_all();
9925 resultbuf = result;
9926 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009927 if (escaped != NULL)
9928 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929#ifdef FEAT_MODIFY_FNAME
9930 skip_mod = TRUE;
9931#endif
9932 break;
9933 }
9934 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009935 if (*s == '<') /* "#<99" uses v:oldfiles */
9936 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 i = (int)getdigits(&s);
9938 *usedlen = (int)(s - src); /* length of what we expand */
9939
Bram Moolenaard812df62008-11-09 12:46:09 +00009940 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009942 if (*usedlen < 2)
9943 {
9944 /* Should we give an error message for #<text? */
9945 *usedlen = 1;
9946 return NULL;
9947 }
9948#ifdef FEAT_EVAL
9949 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9950 (long)i);
9951 if (result == NULL)
9952 {
9953 *errormsg = (char_u *)"";
9954 return NULL;
9955 }
9956#else
9957 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 }
9961 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009962 {
9963 buf = buflist_findnr(i);
9964 if (buf == NULL)
9965 {
9966 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9967 return NULL;
9968 }
9969 if (lnump != NULL)
9970 *lnump = ECMD_LAST;
9971 if (buf->b_fname == NULL)
9972 {
9973 result = (char_u *)"";
9974 valid = 0; /* Must have ":p:h" to be valid */
9975 }
9976 else
9977 result = buf->b_fname;
9978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 break;
9980
9981#ifdef FEAT_SEARCHPATH
9982 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009983 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 if (result == NULL)
9985 {
9986 *errormsg = (char_u *)"";
9987 return NULL;
9988 }
9989 resultbuf = result; /* remember allocated string */
9990 break;
9991#endif
9992
9993#ifdef FEAT_AUTOCMD
9994 case SPEC_AFILE: /* file name for autocommand */
9995 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009996 if (result != NULL && !autocmd_fname_full)
9997 {
9998 /* Still need to turn the fname into a full path. It is
9999 * postponed to avoid a delay when <afile> is not used. */
10000 autocmd_fname_full = TRUE;
10001 result = FullName_save(autocmd_fname, FALSE);
10002 vim_free(autocmd_fname);
10003 autocmd_fname = result;
10004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 if (result == NULL)
10006 {
10007 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10008 return NULL;
10009 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010010 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 break;
10012
10013 case SPEC_ABUF: /* buffer number for autocommand */
10014 if (autocmd_bufnr <= 0)
10015 {
10016 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10017 return NULL;
10018 }
10019 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10020 result = strbuf;
10021 break;
10022
10023 case SPEC_AMATCH: /* match name for autocommand */
10024 result = autocmd_match;
10025 if (result == NULL)
10026 {
10027 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10028 return NULL;
10029 }
10030 break;
10031
10032#endif
10033 case SPEC_SFILE: /* file name for ":so" command */
10034 result = sourcing_name;
10035 if (result == NULL)
10036 {
10037 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10038 return NULL;
10039 }
10040 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010041 case SPEC_SLNUM: /* line in file for ":so" command */
10042 if (sourcing_name == NULL || sourcing_lnum == 0)
10043 {
10044 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10045 return NULL;
10046 }
10047 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10048 result = strbuf;
10049 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050#if defined(FEAT_CLIENTSERVER)
10051 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010052 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10053 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 result = strbuf;
10055 break;
10056#endif
10057 }
10058
10059 resultlen = (int)STRLEN(result); /* length of new string */
10060 if (src[*usedlen] == '<') /* remove the file name extension */
10061 {
10062 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 resultlen = (int)(s - result);
10065 }
10066#ifdef FEAT_MODIFY_FNAME
10067 else if (!skip_mod)
10068 {
10069 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10070 &resultlen);
10071 if (result == NULL)
10072 {
10073 *errormsg = (char_u *)"";
10074 return NULL;
10075 }
10076 }
10077#endif
10078 }
10079
10080 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10081 {
10082 if (valid != VALID_HEAD + VALID_PATH)
10083 /* xgettext:no-c-format */
10084 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10085 else
10086 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10087 result = NULL;
10088 }
10089 else
10090 result = vim_strnsave(result, resultlen);
10091 vim_free(resultbuf);
10092 return result;
10093}
10094
10095/*
10096 * Concatenate all files in the argument list, separated by spaces, and return
10097 * it in one allocated string.
10098 * Spaces and backslashes in the file names are escaped with a backslash.
10099 * Returns NULL when out of memory.
10100 */
10101 static char_u *
10102arg_all()
10103{
10104 int len;
10105 int idx;
10106 char_u *retval = NULL;
10107 char_u *p;
10108
10109 /*
10110 * Do this loop two times:
10111 * first time: compute the total length
10112 * second time: concatenate the names
10113 */
10114 for (;;)
10115 {
10116 len = 0;
10117 for (idx = 0; idx < ARGCOUNT; ++idx)
10118 {
10119 p = alist_name(&ARGLIST[idx]);
10120 if (p != NULL)
10121 {
10122 if (len > 0)
10123 {
10124 /* insert a space in between names */
10125 if (retval != NULL)
10126 retval[len] = ' ';
10127 ++len;
10128 }
10129 for ( ; *p != NUL; ++p)
10130 {
10131 if (*p == ' ' || *p == '\\')
10132 {
10133 /* insert a backslash */
10134 if (retval != NULL)
10135 retval[len] = '\\';
10136 ++len;
10137 }
10138 if (retval != NULL)
10139 retval[len] = *p;
10140 ++len;
10141 }
10142 }
10143 }
10144
10145 /* second time: break here */
10146 if (retval != NULL)
10147 {
10148 retval[len] = NUL;
10149 break;
10150 }
10151
10152 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010153 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 if (retval == NULL)
10155 break;
10156 }
10157
10158 return retval;
10159}
10160
10161#if defined(FEAT_AUTOCMD) || defined(PROTO)
10162/*
10163 * Expand the <sfile> string in "arg".
10164 *
10165 * Returns an allocated string, or NULL for any error.
10166 */
10167 char_u *
10168expand_sfile(arg)
10169 char_u *arg;
10170{
10171 char_u *errormsg;
10172 int len;
10173 char_u *result;
10174 char_u *newres;
10175 char_u *repl;
10176 int srclen;
10177 char_u *p;
10178
10179 result = vim_strsave(arg);
10180 if (result == NULL)
10181 return NULL;
10182
10183 for (p = result; *p; )
10184 {
10185 if (STRNCMP(p, "<sfile>", 7) != 0)
10186 ++p;
10187 else
10188 {
10189 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010190 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 if (errormsg != NULL)
10192 {
10193 if (*errormsg)
10194 emsg(errormsg);
10195 vim_free(result);
10196 return NULL;
10197 }
10198 if (repl == NULL) /* no match (cannot happen) */
10199 {
10200 p += srclen;
10201 continue;
10202 }
10203 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10204 newres = alloc(len);
10205 if (newres == NULL)
10206 {
10207 vim_free(repl);
10208 vim_free(result);
10209 return NULL;
10210 }
10211 mch_memmove(newres, result, (size_t)(p - result));
10212 STRCPY(newres + (p - result), repl);
10213 len = (int)STRLEN(newres);
10214 STRCAT(newres, p + srclen);
10215 vim_free(repl);
10216 vim_free(result);
10217 result = newres;
10218 p = newres + len; /* continue after the match */
10219 }
10220 }
10221
10222 return result;
10223}
10224#endif
10225
10226#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010227static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10228 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10230static frame_T *ses_skipframe __ARGS((frame_T *fr));
10231static int ses_do_frame __ARGS((frame_T *fr));
10232static int ses_do_win __ARGS((win_T *wp));
10233static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10234static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10235static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10236
10237/*
10238 * Write openfile commands for the current buffers to an .exrc file.
10239 * Return FAIL on error, OK otherwise.
10240 */
10241 static int
10242makeopens(fd, dirnow)
10243 FILE *fd;
10244 char_u *dirnow; /* Current directory name */
10245{
10246 buf_T *buf;
10247 int only_save_windows = TRUE;
10248 int nr;
10249 int cnr = 1;
10250 int restore_size = TRUE;
10251 win_T *wp;
10252 char_u *sname;
10253 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010254 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010255 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010256 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010257 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010258 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259
10260 if (ssop_flags & SSOP_BUFFERS)
10261 only_save_windows = FALSE; /* Save ALL buffers */
10262
10263 /*
10264 * Begin by setting the this_session variable, and then other
10265 * sessionable variables.
10266 */
10267#ifdef FEAT_EVAL
10268 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10269 return FAIL;
10270 if (ssop_flags & SSOP_GLOBALS)
10271 if (store_session_globals(fd) == FAIL)
10272 return FAIL;
10273#endif
10274
10275 /*
10276 * Close all windows but one.
10277 */
10278 if (put_line(fd, "silent only") == FAIL)
10279 return FAIL;
10280
10281 /*
10282 * Now a :cd command to the session directory or the current directory
10283 */
10284 if (ssop_flags & SSOP_SESDIR)
10285 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010286 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10287 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 return FAIL;
10289 }
10290 else if (ssop_flags & SSOP_CURDIR)
10291 {
10292 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10293 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010294 || fputs("cd ", fd) < 0
10295 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10296 || put_eol(fd) == FAIL)
10297 {
10298 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 vim_free(sname);
10302 }
10303
10304 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010305 * If there is an empty, unnamed buffer we will wipe it out later.
10306 * Remember the buffer number.
10307 */
10308 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10309 return FAIL;
10310 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10311 return FAIL;
10312 if (put_line(fd, "endif") == FAIL)
10313 return FAIL;
10314
10315 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 * Now save the current files, current buffer first.
10317 */
10318 if (put_line(fd, "set shortmess=aoO") == FAIL)
10319 return FAIL;
10320
10321 /* Now put the other buffers into the buffer list */
10322 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10323 {
10324 if (!(only_save_windows && buf->b_nwindows == 0)
10325 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10326 && buf->b_fname != NULL
10327 && buf->b_p_bl)
10328 {
10329 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10330 : buf->b_wininfo->wi_fpos.lnum) < 0
10331 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10332 return FAIL;
10333 }
10334 }
10335
10336 /* the global argument list */
10337 if (ses_arglist(fd, "args", &global_alist.al_ga,
10338 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10339 return FAIL;
10340
10341 if (ssop_flags & SSOP_RESIZE)
10342 {
10343 /* Note: after the restore we still check it worked!*/
10344 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10345 || put_eol(fd) == FAIL)
10346 return FAIL;
10347 }
10348
10349#ifdef FEAT_GUI
10350 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10351 {
10352 int x, y;
10353
10354 if (gui_mch_get_winpos(&x, &y) == OK)
10355 {
10356 /* Note: after the restore we still check it worked!*/
10357 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10358 return FAIL;
10359 }
10360 }
10361#endif
10362
10363 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010364 * May repeat putting Windows for each tab, when "tabpages" is in
10365 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010366 * Don't use goto_tabpage(), it may change directory and trigger
10367 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010369 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010370 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010371 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010373 int need_tabnew = FALSE;
10374
Bram Moolenaar18144c82006-04-12 21:52:12 +000010375 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010377 tabpage_T *tp = find_tabpage(tabnr);
10378
10379 if (tp == NULL)
10380 break; /* done all tab pages */
10381 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010382 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010383 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010384 tab_topframe = topframe;
10385 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010386 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010387 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010388 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010389 tab_topframe = tp->tp_topframe;
10390 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010391 if (tabnr > 1)
10392 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394
Bram Moolenaar18144c82006-04-12 21:52:12 +000010395 /*
10396 * Before creating the window layout, try loading one file. If this
10397 * is aborted we don't end up with a number of useless windows.
10398 * This may have side effects! (e.g., compressed or network file).
10399 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010400 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010401 {
10402 if (ses_do_win(wp)
10403 && wp->w_buffer->b_ffname != NULL
10404 && !wp->w_buffer->b_help
10405#ifdef FEAT_QUICKFIX
10406 && !bt_nofile(wp->w_buffer)
10407#endif
10408 )
10409 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010410 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010411 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10412 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010413 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010414 if (!wp->w_arg_idx_invalid)
10415 edited_win = wp;
10416 break;
10417 }
10418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010420 /* If no file got edited create an empty tab page. */
10421 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10422 return FAIL;
10423
Bram Moolenaar18144c82006-04-12 21:52:12 +000010424 /*
10425 * Save current window layout.
10426 */
10427 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010429 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010431 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10432 return FAIL;
10433 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10434 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435
Bram Moolenaar18144c82006-04-12 21:52:12 +000010436 /*
10437 * Check if window sizes can be restored (no windows omitted).
10438 * Remember the window number of the current window after restoring.
10439 */
10440 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010441 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010442 {
10443 if (ses_do_win(wp))
10444 ++nr;
10445 else
10446 restore_size = FALSE;
10447 if (curwin == wp)
10448 cnr = nr;
10449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450
Bram Moolenaar18144c82006-04-12 21:52:12 +000010451 /* Go to the first window. */
10452 if (put_line(fd, "wincmd t") == FAIL)
10453 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010454
Bram Moolenaar18144c82006-04-12 21:52:12 +000010455 /*
10456 * If more than one window, see if sizes can be restored.
10457 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10458 * resized when moving between windows.
10459 * Do this before restoring the view, so that the topline and the
10460 * cursor can be set. This is done again below.
10461 */
10462 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10463 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010464 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010465 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466
Bram Moolenaar18144c82006-04-12 21:52:12 +000010467 /*
10468 * Restore the view of the window (options, file, cursor, etc.).
10469 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010470 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010471 {
10472 if (!ses_do_win(wp))
10473 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010474 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10475 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010476 return FAIL;
10477 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10478 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010479 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010480 }
10481
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010482 /* The argument index in the first tab page is zero, need to set it in
10483 * each window. For further tab pages it's the window where we do
10484 * "tabedit". */
10485 cur_arg_idx = next_arg_idx;
10486
Bram Moolenaar18144c82006-04-12 21:52:12 +000010487 /*
10488 * Restore cursor to the current window if it's not the first one.
10489 */
10490 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10491 || put_eol(fd) == FAIL))
10492 return FAIL;
10493
10494 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010495 * Restore window sizes again after jumping around in windows, because
10496 * the current window has a minimum size while others may not.
10497 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010498 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010499 return FAIL;
10500
Bram Moolenaar18144c82006-04-12 21:52:12 +000010501 /* Don't continue in another tab page when doing only the current one
10502 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010503 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010504 break;
10505 }
10506
10507 if (ssop_flags & SSOP_TABPAGES)
10508 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010509 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10510 || put_eol(fd) == FAIL)
10511 return FAIL;
10512 }
10513
Bram Moolenaar9c102382006-05-03 21:26:49 +000010514 /*
10515 * Wipe out an empty unnamed buffer we started in.
10516 */
10517 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10518 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010519 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010520 return FAIL;
10521 if (put_line(fd, "endif") == FAIL)
10522 return FAIL;
10523 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10524 return FAIL;
10525
10526 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10527 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10528 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10529 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530
10531 /*
10532 * Lastly, execute the x.vim file if it exists.
10533 */
10534 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10535 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010536 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 || put_line(fd, "endif") == FAIL)
10538 return FAIL;
10539
10540 return OK;
10541}
10542
10543 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010544ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 FILE *fd;
10546 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010547 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548{
10549 int n = 0;
10550 win_T *wp;
10551
10552 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10553 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010554 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 {
10556 if (!ses_do_win(wp))
10557 continue;
10558 ++n;
10559
10560 /* restore height when not full height */
10561 if (wp->w_height + wp->w_status_height < topframe->fr_height
10562 && (fprintf(fd,
10563 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10564 n, (long)wp->w_height, Rows / 2, Rows) < 0
10565 || put_eol(fd) == FAIL))
10566 return FAIL;
10567
10568 /* restore width when not full width */
10569 if (wp->w_width < Columns && (fprintf(fd,
10570 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10571 n, (long)wp->w_width, Columns / 2, Columns) < 0
10572 || put_eol(fd) == FAIL))
10573 return FAIL;
10574 }
10575 }
10576 else
10577 {
10578 /* Just equalise window sizes */
10579 if (put_line(fd, "wincmd =") == FAIL)
10580 return FAIL;
10581 }
10582 return OK;
10583}
10584
10585/*
10586 * Write commands to "fd" to recursively create windows for frame "fr",
10587 * horizontally and vertically split.
10588 * After the commands the last window in the frame is the current window.
10589 * Returns FAIL when writing the commands to "fd" fails.
10590 */
10591 static int
10592ses_win_rec(fd, fr)
10593 FILE *fd;
10594 frame_T *fr;
10595{
10596 frame_T *frc;
10597 int count = 0;
10598
10599 if (fr->fr_layout != FR_LEAF)
10600 {
10601 /* Find first frame that's not skipped and then create a window for
10602 * each following one (first frame is already there). */
10603 frc = ses_skipframe(fr->fr_child);
10604 if (frc != NULL)
10605 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10606 {
10607 /* Make window as big as possible so that we have lots of room
10608 * to split. */
10609 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10610 || put_line(fd, fr->fr_layout == FR_COL
10611 ? "split" : "vsplit") == FAIL)
10612 return FAIL;
10613 ++count;
10614 }
10615
10616 /* Go back to the first window. */
10617 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10618 ? "%dwincmd k" : "%dwincmd h", count) < 0
10619 || put_eol(fd) == FAIL))
10620 return FAIL;
10621
10622 /* Recursively create frames/windows in each window of this column or
10623 * row. */
10624 frc = ses_skipframe(fr->fr_child);
10625 while (frc != NULL)
10626 {
10627 ses_win_rec(fd, frc);
10628 frc = ses_skipframe(frc->fr_next);
10629 /* Go to next window. */
10630 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10631 return FAIL;
10632 }
10633 }
10634 return OK;
10635}
10636
10637/*
10638 * Skip frames that don't contain windows we want to save in the Session.
10639 * Returns NULL when there none.
10640 */
10641 static frame_T *
10642ses_skipframe(fr)
10643 frame_T *fr;
10644{
10645 frame_T *frc;
10646
10647 for (frc = fr; frc != NULL; frc = frc->fr_next)
10648 if (ses_do_frame(frc))
10649 break;
10650 return frc;
10651}
10652
10653/*
10654 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10655 * the Session.
10656 */
10657 static int
10658ses_do_frame(fr)
10659 frame_T *fr;
10660{
10661 frame_T *frc;
10662
10663 if (fr->fr_layout == FR_LEAF)
10664 return ses_do_win(fr->fr_win);
10665 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10666 if (ses_do_frame(frc))
10667 return TRUE;
10668 return FALSE;
10669}
10670
10671/*
10672 * Return non-zero if window "wp" is to be stored in the Session.
10673 */
10674 static int
10675ses_do_win(wp)
10676 win_T *wp;
10677{
10678 if (wp->w_buffer->b_fname == NULL
10679#ifdef FEAT_QUICKFIX
10680 /* When 'buftype' is "nofile" can't restore the window contents. */
10681 || bt_nofile(wp->w_buffer)
10682#endif
10683 )
10684 return (ssop_flags & SSOP_BLANK);
10685 if (wp->w_buffer->b_help)
10686 return (ssop_flags & SSOP_HELP);
10687 return TRUE;
10688}
10689
10690/*
10691 * Write commands to "fd" to restore the view of a window.
10692 * Caller must make sure 'scrolloff' is zero.
10693 */
10694 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010695put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 FILE *fd;
10697 win_T *wp;
10698 int add_edit; /* add ":edit" command to view */
10699 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010700 int current_arg_idx; /* current argument index of the window, use
10701 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702{
10703 win_T *save_curwin;
10704 int f;
10705 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010706 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707
10708 /* Always restore cursor position for ":mksession". For ":mkview" only
10709 * when 'viewoptions' contains "cursor". */
10710 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10711
10712 /*
10713 * Local argument list.
10714 */
10715 if (wp->w_alist == &global_alist)
10716 {
10717 if (put_line(fd, "argglobal") == FAIL)
10718 return FAIL;
10719 }
10720 else
10721 {
10722 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10723 flagp == &vop_flags
10724 || !(*flagp & SSOP_CURDIR)
10725 || wp->w_localdir != NULL, flagp) == FAIL)
10726 return FAIL;
10727 }
10728
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010729 /* Only when part of a session: restore the argument index. Some
10730 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010731 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010732 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010734 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735 || put_eol(fd) == FAIL)
10736 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010737 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 }
10739
10740 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010741 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 {
10743 /*
10744 * Load the file.
10745 */
10746 if (wp->w_buffer->b_ffname != NULL
10747#ifdef FEAT_QUICKFIX
10748 && !bt_nofile(wp->w_buffer)
10749#endif
10750 )
10751 {
10752 /*
10753 * Editing a file in this buffer: use ":edit file".
10754 * This may have side effects! (e.g., compressed or network file).
10755 */
10756 if (fputs("edit ", fd) < 0
10757 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10758 return FAIL;
10759 }
10760 else
10761 {
10762 /* No file in this buffer, just make it empty. */
10763 if (put_line(fd, "enew") == FAIL)
10764 return FAIL;
10765#ifdef FEAT_QUICKFIX
10766 if (wp->w_buffer->b_ffname != NULL)
10767 {
10768 /* The buffer does have a name, but it's not a file name. */
10769 if (fputs("file ", fd) < 0
10770 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10771 return FAIL;
10772 }
10773#endif
10774 do_cursor = FALSE;
10775 }
10776 }
10777
10778 /*
10779 * Local mappings and abbreviations.
10780 */
10781 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10782 && makemap(fd, wp->w_buffer) == FAIL)
10783 return FAIL;
10784
10785 /*
10786 * Local options. Need to go to the window temporarily.
10787 * Store only local values when using ":mkview" and when ":mksession" is
10788 * used and 'sessionoptions' doesn't include "options".
10789 * Some folding options are always stored when "folds" is included,
10790 * otherwise the folds would not be restored correctly.
10791 */
10792 save_curwin = curwin;
10793 curwin = wp;
10794 curbuf = curwin->w_buffer;
10795 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10796 f = makeset(fd, OPT_LOCAL,
10797 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10798#ifdef FEAT_FOLDING
10799 else if (*flagp & SSOP_FOLDS)
10800 f = makefoldset(fd);
10801#endif
10802 else
10803 f = OK;
10804 curwin = save_curwin;
10805 curbuf = curwin->w_buffer;
10806 if (f == FAIL)
10807 return FAIL;
10808
10809#ifdef FEAT_FOLDING
10810 /*
10811 * Save Folds when 'buftype' is empty and for help files.
10812 */
10813 if ((*flagp & SSOP_FOLDS)
10814 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010815# ifdef FEAT_QUICKFIX
10816 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10817# endif
10818 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 {
10820 if (put_folds(fd, wp) == FAIL)
10821 return FAIL;
10822 }
10823#endif
10824
10825 /*
10826 * Set the cursor after creating folds, since that moves the cursor.
10827 */
10828 if (do_cursor)
10829 {
10830
10831 /* Restore the cursor line in the file and relatively in the
10832 * window. Don't use "G", it changes the jumplist. */
10833 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10834 (long)wp->w_cursor.lnum,
10835 (long)(wp->w_cursor.lnum - wp->w_topline),
10836 (long)wp->w_height / 2, (long)wp->w_height) < 0
10837 || put_eol(fd) == FAIL
10838 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10839 || put_line(fd, "exe s:l") == FAIL
10840 || put_line(fd, "normal! zt") == FAIL
10841 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10842 || put_eol(fd) == FAIL)
10843 return FAIL;
10844 /* Restore the cursor column and left offset when not wrapping. */
10845 if (wp->w_cursor.col == 0)
10846 {
10847 if (put_line(fd, "normal! 0") == FAIL)
10848 return FAIL;
10849 }
10850 else
10851 {
10852 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10853 {
10854 if (fprintf(fd,
10855 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010856 (long)wp->w_virtcol + 1,
10857 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 (long)wp->w_width / 2, (long)wp->w_width) < 0
10859 || put_eol(fd) == FAIL
10860 || put_line(fd, "if s:c > 0") == FAIL
10861 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010862 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10863 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 || put_eol(fd) == FAIL
10865 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010010866 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 || put_eol(fd) == FAIL
10868 || put_line(fd, "endif") == FAIL)
10869 return FAIL;
10870 }
10871 else
10872 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010873 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 || put_eol(fd) == FAIL)
10875 return FAIL;
10876 }
10877 }
10878 }
10879
10880 /*
10881 * Local directory.
10882 */
10883 if (wp->w_localdir != NULL)
10884 {
10885 if (fputs("lcd ", fd) < 0
10886 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10887 || put_eol(fd) == FAIL)
10888 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010889 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 }
10891
10892 return OK;
10893}
10894
10895/*
10896 * Write an argument list to the session file.
10897 * Returns FAIL if writing fails.
10898 */
10899 static int
10900ses_arglist(fd, cmd, gap, fullname, flagp)
10901 FILE *fd;
10902 char *cmd;
10903 garray_T *gap;
10904 int fullname; /* TRUE: use full path name */
10905 unsigned *flagp;
10906{
10907 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010908 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 char_u *s;
10910
10911 if (gap->ga_len == 0)
10912 return put_line(fd, "silent! argdel *");
10913 if (fputs(cmd, fd) < 0)
10914 return FAIL;
10915 for (i = 0; i < gap->ga_len; ++i)
10916 {
10917 /* NULL file names are skipped (only happens when out of memory). */
10918 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10919 if (s != NULL)
10920 {
10921 if (fullname)
10922 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010923 buf = alloc(MAXPATHL);
10924 if (buf != NULL)
10925 {
10926 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10927 s = buf;
10928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 }
10930 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010931 {
10932 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010934 }
10935 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 }
10937 }
10938 return put_eol(fd);
10939}
10940
10941/*
10942 * Write a buffer name to the session file.
10943 * Also ends the line.
10944 * Returns FAIL if writing fails.
10945 */
10946 static int
10947ses_fname(fd, buf, flagp)
10948 FILE *fd;
10949 buf_T *buf;
10950 unsigned *flagp;
10951{
10952 char_u *name;
10953
10954 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010955 * the session file will be sourced.
10956 * Don't do this for ":mkview", we don't know the current directory.
10957 * Don't do this after ":lcd", we don't keep track of what the current
10958 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 if (buf->b_sfname != NULL
10960 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010961 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010962#ifdef FEAT_AUTOCHDIR
10963 && !p_acd
10964#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010965 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 name = buf->b_sfname;
10967 else
10968 name = buf->b_ffname;
10969 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10970 return FAIL;
10971 return OK;
10972}
10973
10974/*
10975 * Write a file name to the session file.
10976 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10977 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010978 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 */
10980 static int
10981ses_put_fname(fd, name, flagp)
10982 FILE *fd;
10983 char_u *name;
10984 unsigned *flagp;
10985{
10986 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010987 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989
10990 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010991 if (sname == NULL)
10992 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010994 if (*flagp & SSOP_SLASH)
10995 {
10996 /* change all backslashes to forward slashes */
10997 for (p = sname; *p != NUL; mb_ptr_adv(p))
10998 if (*p == '\\')
10999 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011001
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011002 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011003 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011005 if (p == NULL)
11006 return FAIL;
11007
11008 /* write the result */
11009 if (fputs((char *)p, fd) < 0)
11010 retval = FAIL;
11011
11012 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 return retval;
11014}
11015
11016/*
11017 * ":loadview [nr]"
11018 */
11019 static void
11020ex_loadview(eap)
11021 exarg_T *eap;
11022{
11023 char_u *fname;
11024
11025 fname = get_view_file(*eap->arg);
11026 if (fname != NULL)
11027 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011028 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 vim_free(fname);
11030 }
11031}
11032
11033/*
11034 * Get the name of the view file for the current buffer.
11035 */
11036 static char_u *
11037get_view_file(c)
11038 int c;
11039{
11040 int len = 0;
11041 char_u *p, *s;
11042 char_u *retval;
11043 char_u *sname;
11044
11045 if (curbuf->b_ffname == NULL)
11046 {
11047 EMSG(_(e_noname));
11048 return NULL;
11049 }
11050 sname = home_replace_save(NULL, curbuf->b_ffname);
11051 if (sname == NULL)
11052 return NULL;
11053
11054 /*
11055 * We want a file name without separators, because we're not going to make
11056 * a directory.
11057 * "normal" path separator -> "=+"
11058 * "=" -> "=="
11059 * ":" path separator -> "=-"
11060 */
11061 for (p = sname; *p; ++p)
11062 if (*p == '=' || vim_ispathsep(*p))
11063 ++len;
11064 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11065 if (retval != NULL)
11066 {
11067 STRCPY(retval, p_vdir);
11068 add_pathsep(retval);
11069 s = retval + STRLEN(retval);
11070 for (p = sname; *p; ++p)
11071 {
11072 if (*p == '=')
11073 {
11074 *s++ = '=';
11075 *s++ = '=';
11076 }
11077 else if (vim_ispathsep(*p))
11078 {
11079 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011080#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 if (*p == ':')
11082 *s++ = '-';
11083 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011085 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 }
11087 else
11088 *s++ = *p;
11089 }
11090 *s++ = '=';
11091 *s++ = c;
11092 STRCPY(s, ".vim");
11093 }
11094
11095 vim_free(sname);
11096 return retval;
11097}
11098
11099#endif /* FEAT_SESSION */
11100
11101/*
11102 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11103 * Return FAIL for a write error.
11104 */
11105 int
11106put_eol(fd)
11107 FILE *fd;
11108{
11109 if (
11110#ifdef USE_CRNL
11111 (
11112# ifdef MKSESSION_NL
11113 !mksession_nl &&
11114# endif
11115 (putc('\r', fd) < 0)) ||
11116#endif
11117 (putc('\n', fd) < 0))
11118 return FAIL;
11119 return OK;
11120}
11121
11122/*
11123 * Write a line to "fd".
11124 * Return FAIL for a write error.
11125 */
11126 int
11127put_line(fd, s)
11128 FILE *fd;
11129 char *s;
11130{
11131 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11132 return FAIL;
11133 return OK;
11134}
11135
11136#ifdef FEAT_VIMINFO
11137/*
11138 * ":rviminfo" and ":wviminfo".
11139 */
11140 static void
11141ex_viminfo(eap)
11142 exarg_T *eap;
11143{
11144 char_u *save_viminfo;
11145
11146 save_viminfo = p_viminfo;
11147 if (*p_viminfo == NUL)
11148 p_viminfo = (char_u *)"'100";
11149 if (eap->cmdidx == CMD_rviminfo)
11150 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011151 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11152 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 EMSG(_("E195: Cannot open viminfo file for reading"));
11154 }
11155 else
11156 write_viminfo(eap->arg, eap->forceit);
11157 p_viminfo = save_viminfo;
11158}
11159#endif
11160
11161#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011162/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011163 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011164 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011165 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 void
11167dialog_msg(buff, format, fname)
11168 char_u *buff;
11169 char *format;
11170 char_u *fname;
11171{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 if (fname == NULL)
11173 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011174 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175}
11176#endif
11177
11178/*
11179 * ":behave {mswin,xterm}"
11180 */
11181 static void
11182ex_behave(eap)
11183 exarg_T *eap;
11184{
11185 if (STRCMP(eap->arg, "mswin") == 0)
11186 {
11187 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11188 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11189 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11190 set_option_value((char_u *)"keymodel", 0L,
11191 (char_u *)"startsel,stopsel", 0);
11192 }
11193 else if (STRCMP(eap->arg, "xterm") == 0)
11194 {
11195 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11196 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11197 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11198 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11199 }
11200 else
11201 EMSG2(_(e_invarg2), eap->arg);
11202}
11203
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011204#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11205/*
11206 * Function given to ExpandGeneric() to obtain the possible arguments of the
11207 * ":behave {mswin,xterm}" command.
11208 */
11209 char_u *
11210get_behave_arg(xp, idx)
11211 expand_T *xp UNUSED;
11212 int idx;
11213{
11214 if (idx == 0)
11215 return (char_u *)"mswin";
11216 if (idx == 1)
11217 return (char_u *)"xterm";
11218 return NULL;
11219}
11220#endif
11221
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222#ifdef FEAT_AUTOCMD
11223static int filetype_detect = FALSE;
11224static int filetype_plugin = FALSE;
11225static int filetype_indent = FALSE;
11226
11227/*
11228 * ":filetype [plugin] [indent] {on,off,detect}"
11229 * on: Load the filetype.vim file to install autocommands for file types.
11230 * off: Load the ftoff.vim file to remove all autocommands for file types.
11231 * plugin on: load filetype.vim and ftplugin.vim
11232 * plugin off: load ftplugof.vim
11233 * indent on: load filetype.vim and indent.vim
11234 * indent off: load indoff.vim
11235 */
11236 static void
11237ex_filetype(eap)
11238 exarg_T *eap;
11239{
11240 char_u *arg = eap->arg;
11241 int plugin = FALSE;
11242 int indent = FALSE;
11243
11244 if (*eap->arg == NUL)
11245 {
11246 /* Print current status. */
11247 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11248 filetype_detect ? "ON" : "OFF",
11249 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11250 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11251 return;
11252 }
11253
11254 /* Accept "plugin" and "indent" in any order. */
11255 for (;;)
11256 {
11257 if (STRNCMP(arg, "plugin", 6) == 0)
11258 {
11259 plugin = TRUE;
11260 arg = skipwhite(arg + 6);
11261 continue;
11262 }
11263 if (STRNCMP(arg, "indent", 6) == 0)
11264 {
11265 indent = TRUE;
11266 arg = skipwhite(arg + 6);
11267 continue;
11268 }
11269 break;
11270 }
11271 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11272 {
11273 if (*arg == 'o' || !filetype_detect)
11274 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011275 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276 filetype_detect = TRUE;
11277 if (plugin)
11278 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011279 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 filetype_plugin = TRUE;
11281 }
11282 if (indent)
11283 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011284 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 filetype_indent = TRUE;
11286 }
11287 }
11288 if (*arg == 'd')
11289 {
11290 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011291 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 }
11293 }
11294 else if (STRCMP(arg, "off") == 0)
11295 {
11296 if (plugin || indent)
11297 {
11298 if (plugin)
11299 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011300 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301 filetype_plugin = FALSE;
11302 }
11303 if (indent)
11304 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011305 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306 filetype_indent = FALSE;
11307 }
11308 }
11309 else
11310 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011311 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 filetype_detect = FALSE;
11313 }
11314 }
11315 else
11316 EMSG2(_(e_invarg2), arg);
11317}
11318
11319/*
11320 * ":setfiletype {name}"
11321 */
11322 static void
11323ex_setfiletype(eap)
11324 exarg_T *eap;
11325{
11326 if (!did_filetype)
11327 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11328}
11329#endif
11330
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331 static void
11332ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011333 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334{
11335#ifdef FEAT_DIGRAPHS
11336 if (*eap->arg != NUL)
11337 putdigraph(eap->arg);
11338 else
11339 listdigraphs();
11340#else
11341 EMSG(_("E196: No digraphs in this version"));
11342#endif
11343}
11344
11345 static void
11346ex_set(eap)
11347 exarg_T *eap;
11348{
11349 int flags = 0;
11350
11351 if (eap->cmdidx == CMD_setlocal)
11352 flags = OPT_LOCAL;
11353 else if (eap->cmdidx == CMD_setglobal)
11354 flags = OPT_GLOBAL;
11355#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11356 if (cmdmod.browse && flags == 0)
11357 ex_options(eap);
11358 else
11359#endif
11360 (void)do_set(eap->arg, flags);
11361}
11362
11363#ifdef FEAT_SEARCH_EXTRA
11364/*
11365 * ":nohlsearch"
11366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 static void
11368ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011369 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370{
11371 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011372 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373}
11374
11375/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011376 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377 * Sets nextcmd to the start of the next command, if any. Also called when
11378 * skipping commands to find the next command.
11379 */
11380 static void
11381ex_match(eap)
11382 exarg_T *eap;
11383{
11384 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011385 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386 char_u *end;
11387 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011388 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011389
11390 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011391 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011392 else
11393 {
11394 EMSG(e_invcmd);
11395 return;
11396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397
11398 /* First clear any old pattern. */
11399 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011400 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401
11402 if (ends_excmd(*eap->arg))
11403 end = eap->arg;
11404 else if ((STRNICMP(eap->arg, "none", 4) == 0
11405 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11406 end = eap->arg + 4;
11407 else
11408 {
11409 p = skiptowhite(eap->arg);
11410 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011411 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 p = skipwhite(p);
11413 if (*p == NUL)
11414 {
11415 /* There must be two arguments. */
11416 EMSG2(_(e_invarg2), eap->arg);
11417 return;
11418 }
11419 end = skip_regexp(p + 1, *p, TRUE, NULL);
11420 if (!eap->skip)
11421 {
11422 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11423 {
11424 eap->errmsg = e_trailing;
11425 return;
11426 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011427 if (*end != *p)
11428 {
11429 EMSG2(_(e_invarg2), p);
11430 return;
11431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432
11433 c = *end;
11434 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011435 match_add(curwin, g, p + 1, 10, id);
11436 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011437 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438 }
11439 }
11440 eap->nextcmd = find_nextcmd(end);
11441}
11442#endif
11443
11444#ifdef FEAT_CRYPT
11445/*
11446 * ":X": Get crypt key
11447 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448 static void
11449ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011450 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011452 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011453 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454}
11455#endif
11456
11457#ifdef FEAT_FOLDING
11458 static void
11459ex_fold(eap)
11460 exarg_T *eap;
11461{
11462 if (foldManualAllowed(TRUE))
11463 foldCreate(eap->line1, eap->line2);
11464}
11465
11466 static void
11467ex_foldopen(eap)
11468 exarg_T *eap;
11469{
11470 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11471 eap->forceit, FALSE);
11472}
11473
11474 static void
11475ex_folddo(eap)
11476 exarg_T *eap;
11477{
11478 linenr_T lnum;
11479
11480 /* First set the marks for all lines closed/open. */
11481 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11482 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11483 ml_setmarked(lnum);
11484
11485 /* Execute the command on the marked lines. */
11486 global_exe(eap->arg);
11487 ml_clearmarked(); /* clear rest of the marks */
11488}
11489#endif