blob: b926027f26aaccce2e7cb6e2a983d1e7a443bddd [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
Bram Moolenaar958636c2014-10-21 20:01:58 +020052/* Wether a command index indicates a user command. */
53# define IS_USER_CMDIDX(idx) ((int)(idx) < 0)
54
Bram Moolenaar071d4272004-06-13 20:20:40 +000055#else
56# define ex_command ex_ni
57# define ex_comclear ex_ni
58# define ex_delcommand ex_ni
Bram Moolenaar958636c2014-10-21 20:01:58 +020059/* Wether a command index indicates a user command. */
60# define IS_USER_CMDIDX(idx) (FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000061#endif
62
Bram Moolenaarb96a7f32014-11-27 16:22:48 +010063static int compute_buffer_local_count __ARGS((int addr_type, int lnum, int local));
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#ifdef FEAT_EVAL
Bram Moolenaar89d40322006-08-29 15:30:07 +000065static 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 +000066#else
Bram Moolenaar89d40322006-08-29 15:30:07 +000067static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000068static int if_level = 0; /* depth in :if */
69#endif
Bram Moolenaara6f4d612011-09-21 19:10:46 +020070static void append_command __ARGS((char_u *cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +000071static char_u *find_command __ARGS((exarg_T *eap, int *full));
72
73static void ex_abbreviate __ARGS((exarg_T *eap));
74static void ex_map __ARGS((exarg_T *eap));
75static void ex_unmap __ARGS((exarg_T *eap));
76static void ex_mapclear __ARGS((exarg_T *eap));
77static void ex_abclear __ARGS((exarg_T *eap));
78#ifndef FEAT_MENU
79# define ex_emenu ex_ni
80# define ex_menu ex_ni
81# define ex_menutranslate ex_ni
82#endif
83#ifdef FEAT_AUTOCMD
84static void ex_autocmd __ARGS((exarg_T *eap));
85static void ex_doautocmd __ARGS((exarg_T *eap));
86#else
87# define ex_autocmd ex_ni
88# define ex_doautocmd ex_ni
89# define ex_doautoall ex_ni
90#endif
91#ifdef FEAT_LISTCMDS
92static void ex_bunload __ARGS((exarg_T *eap));
93static void ex_buffer __ARGS((exarg_T *eap));
94static void ex_bmodified __ARGS((exarg_T *eap));
95static void ex_bnext __ARGS((exarg_T *eap));
96static void ex_bprevious __ARGS((exarg_T *eap));
97static void ex_brewind __ARGS((exarg_T *eap));
98static void ex_blast __ARGS((exarg_T *eap));
99#else
100# define ex_bunload ex_ni
101# define ex_buffer ex_ni
102# define ex_bmodified ex_ni
103# define ex_bnext ex_ni
104# define ex_bprevious ex_ni
105# define ex_brewind ex_ni
106# define ex_blast ex_ni
107# define buflist_list ex_ni
108# define ex_checktime ex_ni
109#endif
110#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
111# define ex_buffer_all ex_ni
112#endif
113static char_u *getargcmd __ARGS((char_u **));
114static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
115static int getargopt __ARGS((exarg_T *eap));
116#ifndef FEAT_QUICKFIX
117# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000118# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119# define ex_cc ex_ni
120# define ex_cnext ex_ni
121# define ex_cfile ex_ni
122# define qf_list ex_ni
123# define qf_age ex_ni
124# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000125# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
127#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
128# define ex_cclose ex_ni
129# define ex_copen ex_ni
130# define ex_cwindow ex_ni
131#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000132#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
133# define ex_cexpr ex_ni
134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
136static int check_more __ARGS((int, int));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100137static linenr_T get_address __ARGS((char_u **, int addr_type, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000138static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200139#if !defined(FEAT_PERL) \
140 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
141 || !defined(FEAT_TCL) \
142 || !defined(FEAT_RUBY) \
143 || !defined(FEAT_LUA) \
144 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000145# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146static void ex_script_ni __ARGS((exarg_T *eap));
147#endif
148static char_u *invalid_range __ARGS((exarg_T *eap));
149static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000150#ifdef FEAT_QUICKFIX
151static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
154static void ex_highlight __ARGS((exarg_T *eap));
155static void ex_colorscheme __ARGS((exarg_T *eap));
156static void ex_quit __ARGS((exarg_T *eap));
157static void ex_cquit __ARGS((exarg_T *eap));
158static void ex_quit_all __ARGS((exarg_T *eap));
159#ifdef FEAT_WINDOWS
160static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000161static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163static void ex_resize __ARGS((exarg_T *eap));
164static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000165static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000166static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000167static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000168static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000169static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#else
171# define ex_close ex_ni
172# define ex_only ex_ni
173# define ex_all ex_ni
174# define ex_resize ex_ni
175# define ex_splitview ex_ni
176# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000177# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000178# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000179# define ex_tabs ex_ni
180# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000181# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#endif
183#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
184static void ex_pclose __ARGS((exarg_T *eap));
185static void ex_ptag __ARGS((exarg_T *eap));
186static void ex_pedit __ARGS((exarg_T *eap));
187#else
188# define ex_pclose ex_ni
189# define ex_ptag ex_ni
190# define ex_pedit ex_ni
191#endif
192static void ex_hide __ARGS((exarg_T *eap));
193static void ex_stop __ARGS((exarg_T *eap));
194static void ex_exit __ARGS((exarg_T *eap));
195static void ex_print __ARGS((exarg_T *eap));
196#ifdef FEAT_BYTEOFF
197static void ex_goto __ARGS((exarg_T *eap));
198#else
199# define ex_goto ex_ni
200#endif
201static void ex_shell __ARGS((exarg_T *eap));
202static void ex_preserve __ARGS((exarg_T *eap));
203static void ex_recover __ARGS((exarg_T *eap));
204#ifndef FEAT_LISTCMDS
205# define ex_argedit ex_ni
206# define ex_argadd ex_ni
207# define ex_argdelete ex_ni
208# define ex_listdo ex_ni
209#endif
210static void ex_mode __ARGS((exarg_T *eap));
211static void ex_wrongmodifier __ARGS((exarg_T *eap));
212static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000213static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214static void ex_edit __ARGS((exarg_T *eap));
215#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
216# define ex_drop ex_ni
217#endif
218#ifndef FEAT_GUI
219# define ex_gui ex_nogui
220static void ex_nogui __ARGS((exarg_T *eap));
221#endif
222#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
223static void ex_tearoff __ARGS((exarg_T *eap));
224#else
225# define ex_tearoff ex_ni
226#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000227#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228static void ex_popup __ARGS((exarg_T *eap));
229#else
230# define ex_popup ex_ni
231#endif
232#ifndef FEAT_GUI_MSWIN
233# define ex_simalt ex_ni
234#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000235#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236# define gui_mch_find_dialog ex_ni
237# define gui_mch_replace_dialog ex_ni
238#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000239#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240# define ex_helpfind ex_ni
241#endif
242#ifndef FEAT_CSCOPE
243# define do_cscope ex_ni
244# define do_scscope ex_ni
245# define do_cstag ex_ni
246#endif
247#ifndef FEAT_SYN_HL
248# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200249# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000250#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200251#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200252# define ex_syntime ex_ni
253#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000254#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000255# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000256# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000257# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000258# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000259# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000260#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200261#ifndef FEAT_PERSISTENT_UNDO
262# define ex_rundo ex_ni
263# define ex_wundo ex_ni
264#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200265#ifndef FEAT_LUA
266# define ex_lua ex_script_ni
267# define ex_luado ex_ni
268# define ex_luafile ex_ni
269#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000270#ifndef FEAT_MZSCHEME
271# define ex_mzscheme ex_script_ni
272# define ex_mzfile ex_ni
273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274#ifndef FEAT_PERL
275# define ex_perl ex_script_ni
276# define ex_perldo ex_ni
277#endif
278#ifndef FEAT_PYTHON
279# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200280# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281# define ex_pyfile ex_ni
282#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200283#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200284# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200285# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200286# define ex_py3file ex_ni
287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288#ifndef FEAT_TCL
289# define ex_tcl ex_script_ni
290# define ex_tcldo ex_ni
291# define ex_tclfile ex_ni
292#endif
293#ifndef FEAT_RUBY
294# define ex_ruby ex_script_ni
295# define ex_rubydo ex_ni
296# define ex_rubyfile ex_ni
297#endif
298#ifndef FEAT_SNIFF
299# define ex_sniff ex_ni
300#endif
301#ifndef FEAT_KEYMAP
302# define ex_loadkeymap ex_ni
303#endif
304static void ex_swapname __ARGS((exarg_T *eap));
305static void ex_syncbind __ARGS((exarg_T *eap));
306static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307static void ex_pwd __ARGS((exarg_T *eap));
308static void ex_equal __ARGS((exarg_T *eap));
309static void ex_sleep __ARGS((exarg_T *eap));
310static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
311static void ex_winsize __ARGS((exarg_T *eap));
312#ifdef FEAT_WINDOWS
313static void ex_wincmd __ARGS((exarg_T *eap));
314#else
315# define ex_wincmd ex_ni
316#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000317#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318static void ex_winpos __ARGS((exarg_T *eap));
319#else
320# define ex_winpos ex_ni
321#endif
322static void ex_operators __ARGS((exarg_T *eap));
323static void ex_put __ARGS((exarg_T *eap));
324static void ex_copymove __ARGS((exarg_T *eap));
325static void ex_submagic __ARGS((exarg_T *eap));
326static void ex_join __ARGS((exarg_T *eap));
327static void ex_at __ARGS((exarg_T *eap));
328static void ex_bang __ARGS((exarg_T *eap));
329static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200330#ifdef FEAT_PERSISTENT_UNDO
331static void ex_wundo __ARGS((exarg_T *eap));
332static void ex_rundo __ARGS((exarg_T *eap));
333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000335static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336static void ex_redir __ARGS((exarg_T *eap));
337static void ex_redraw __ARGS((exarg_T *eap));
338static void ex_redrawstatus __ARGS((exarg_T *eap));
339static void close_redir __ARGS((void));
340static void ex_mkrc __ARGS((exarg_T *eap));
341static void ex_mark __ARGS((exarg_T *eap));
342#ifdef FEAT_USR_CMDS
343static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000344static 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 +0000345#endif
346#ifdef FEAT_EX_EXTRA
347static void ex_normal __ARGS((exarg_T *eap));
348static void ex_startinsert __ARGS((exarg_T *eap));
349static void ex_stopinsert __ARGS((exarg_T *eap));
350#else
351# define ex_normal ex_ni
352# define ex_align ex_ni
353# define ex_retab ex_ni
354# define ex_startinsert ex_ni
355# define ex_stopinsert ex_ni
356# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000357# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#endif
359#ifdef FEAT_FIND_ID
360static void ex_checkpath __ARGS((exarg_T *eap));
361static void ex_findpat __ARGS((exarg_T *eap));
362#else
363# define ex_findpat ex_ni
364# define ex_checkpath ex_ni
365#endif
366#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
367static void ex_psearch __ARGS((exarg_T *eap));
368#else
369# define ex_psearch ex_ni
370#endif
371static void ex_tag __ARGS((exarg_T *eap));
372static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
373#ifndef FEAT_EVAL
374# define ex_scriptnames ex_ni
375# define ex_finish ex_ni
376# define ex_echo ex_ni
377# define ex_echohl ex_ni
378# define ex_execute ex_ni
379# define ex_call ex_ni
380# define ex_if ex_ni
381# define ex_endif ex_ni
382# define ex_else ex_ni
383# define ex_while ex_ni
384# define ex_continue ex_ni
385# define ex_break ex_ni
386# define ex_endwhile ex_ni
387# define ex_throw ex_ni
388# define ex_try ex_ni
389# define ex_catch ex_ni
390# define ex_finally ex_ni
391# define ex_endtry ex_ni
392# define ex_endfunction ex_ni
393# define ex_let ex_ni
394# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000395# define ex_lockvar ex_ni
396# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397# define ex_function ex_ni
398# define ex_delfunction ex_ni
399# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000400# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401#endif
402static char_u *arg_all __ARGS((void));
403#ifdef FEAT_SESSION
404static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000405static 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 +0000406static void ex_loadview __ARGS((exarg_T *eap));
407static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000408static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409#else
410# define ex_loadview ex_ni
411#endif
412#ifndef FEAT_EVAL
413# define ex_compiler ex_ni
414#endif
415#ifdef FEAT_VIMINFO
416static void ex_viminfo __ARGS((exarg_T *eap));
417#else
418# define ex_viminfo ex_ni
419#endif
420static void ex_behave __ARGS((exarg_T *eap));
421#ifdef FEAT_AUTOCMD
422static void ex_filetype __ARGS((exarg_T *eap));
423static void ex_setfiletype __ARGS((exarg_T *eap));
424#else
425# define ex_filetype ex_ni
426# define ex_setfiletype ex_ni
427#endif
428#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000429# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430# define ex_diffpatch ex_ni
431# define ex_diffgetput ex_ni
432# define ex_diffsplit ex_ni
433# define ex_diffthis ex_ni
434# define ex_diffupdate ex_ni
435#endif
436static void ex_digraphs __ARGS((exarg_T *eap));
437static void ex_set __ARGS((exarg_T *eap));
438#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
439# define ex_options ex_ni
440#endif
441#ifdef FEAT_SEARCH_EXTRA
442static void ex_nohlsearch __ARGS((exarg_T *eap));
443static void ex_match __ARGS((exarg_T *eap));
444#else
445# define ex_nohlsearch ex_ni
446# define ex_match ex_ni
447#endif
448#ifdef FEAT_CRYPT
449static void ex_X __ARGS((exarg_T *eap));
450#else
451# define ex_X ex_ni
452#endif
453#ifdef FEAT_FOLDING
454static void ex_fold __ARGS((exarg_T *eap));
455static void ex_foldopen __ARGS((exarg_T *eap));
456static void ex_folddo __ARGS((exarg_T *eap));
457#else
458# define ex_fold ex_ni
459# define ex_foldopen ex_ni
460# define ex_folddo ex_ni
461#endif
462#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
463 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
464# define ex_language ex_ni
465#endif
466#ifndef FEAT_SIGNS
467# define ex_sign ex_ni
468#endif
469#ifndef FEAT_SUN_WORKSHOP
470# define ex_wsverb ex_ni
471#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000472#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200473# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000474# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200475# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477
478#ifndef FEAT_EVAL
479# define ex_debug ex_ni
480# define ex_breakadd ex_ni
481# define ex_debuggreedy ex_ni
482# define ex_breakdel ex_ni
483# define ex_breaklist ex_ni
484#endif
485
486#ifndef FEAT_CMDHIST
487# define ex_history ex_ni
488#endif
489#ifndef FEAT_JUMPLIST
490# define ex_jumps ex_ni
491# define ex_changes ex_ni
492#endif
493
Bram Moolenaar05159a02005-02-26 23:04:13 +0000494#ifndef FEAT_PROFILE
495# define ex_profile ex_ni
496#endif
497
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498/*
499 * Declare cmdnames[].
500 */
501#define DO_DECLARE_EXCMD
502#include "ex_cmds.h"
503
504/*
505 * Table used to quickly search for a command, based on its first character.
506 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000507static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508{
509 CMD_append,
510 CMD_buffer,
511 CMD_change,
512 CMD_delete,
513 CMD_edit,
514 CMD_file,
515 CMD_global,
516 CMD_help,
517 CMD_insert,
518 CMD_join,
519 CMD_k,
520 CMD_list,
521 CMD_move,
522 CMD_next,
523 CMD_open,
524 CMD_print,
525 CMD_quit,
526 CMD_read,
527 CMD_substitute,
528 CMD_t,
529 CMD_undo,
530 CMD_vglobal,
531 CMD_write,
532 CMD_xit,
533 CMD_yank,
534 CMD_z,
535 CMD_bang
536};
537
538static char_u dollar_command[2] = {'$', 0};
539
540
541#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000542/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543typedef struct
544{
545 char_u *line; /* command line */
546 linenr_T lnum; /* sourcing_lnum of the line */
547} wcmd_T;
548
549/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000550 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000552 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000554struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555{
556 garray_T *lines_gap; /* growarray with line info */
557 int current_line; /* last read line from growarray */
558 int repeating; /* TRUE when looping a second time */
559 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
560 char_u *(*getline) __ARGS((int, void *, int));
561 void *cookie;
562};
563
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000564static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
565static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000567
568/* Struct to save a few things while debugging. Used in do_cmdline() only. */
569struct dbg_stuff
570{
571 int trylevel;
572 int force_abort;
573 except_T *caught_stack;
574 char_u *vv_exception;
575 char_u *vv_throwpoint;
576 int did_emsg;
577 int got_int;
578 int did_throw;
579 int need_rethrow;
580 int check_cstack;
581 except_T *current_exception;
582};
583
584static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
585static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
586
587 static void
588save_dbg_stuff(dsp)
589 struct dbg_stuff *dsp;
590{
591 dsp->trylevel = trylevel; trylevel = 0;
592 dsp->force_abort = force_abort; force_abort = FALSE;
593 dsp->caught_stack = caught_stack; caught_stack = NULL;
594 dsp->vv_exception = v_exception(NULL);
595 dsp->vv_throwpoint = v_throwpoint(NULL);
596
597 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
598 dsp->did_emsg = did_emsg; did_emsg = FALSE;
599 dsp->got_int = got_int; got_int = FALSE;
600 dsp->did_throw = did_throw; did_throw = FALSE;
601 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
602 dsp->check_cstack = check_cstack; check_cstack = FALSE;
603 dsp->current_exception = current_exception; current_exception = NULL;
604}
605
606 static void
607restore_dbg_stuff(dsp)
608 struct dbg_stuff *dsp;
609{
610 suppress_errthrow = FALSE;
611 trylevel = dsp->trylevel;
612 force_abort = dsp->force_abort;
613 caught_stack = dsp->caught_stack;
614 (void)v_exception(dsp->vv_exception);
615 (void)v_throwpoint(dsp->vv_throwpoint);
616 did_emsg = dsp->did_emsg;
617 got_int = dsp->got_int;
618 did_throw = dsp->did_throw;
619 need_rethrow = dsp->need_rethrow;
620 check_cstack = dsp->check_cstack;
621 current_exception = dsp->current_exception;
622}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623#endif
624
625
626/*
627 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
628 * command is given.
629 */
630 void
631do_exmode(improved)
632 int improved; /* TRUE for "improved Ex" mode */
633{
634 int save_msg_scroll;
635 int prev_msg_row;
636 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000637 int changedtick;
638
639 if (improved)
640 exmode_active = EXMODE_VIM;
641 else
642 exmode_active = EXMODE_NORMAL;
643 State = NORMAL;
644
645 /* When using ":global /pat/ visual" and then "Q" we return to continue
646 * the :global command. */
647 if (global_busy)
648 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
650 save_msg_scroll = msg_scroll;
651 ++RedrawingDisabled; /* don't redisplay the window */
652 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653#ifdef FEAT_GUI
654 /* Ignore scrollbar and mouse events in Ex mode */
655 ++hold_gui_events;
656#endif
657#ifdef FEAT_SNIFF
658 want_sniff_request = 0; /* No K_SNIFF wanted */
659#endif
660
661 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
662 while (exmode_active)
663 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000664#ifdef FEAT_EX_EXTRA
665 /* Check for a ":normal" command and no more characters left. */
666 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
667 {
668 exmode_active = FALSE;
669 break;
670 }
671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 msg_scroll = TRUE;
673 need_wait_return = FALSE;
674 ex_pressedreturn = FALSE;
675 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000676 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 prev_msg_row = msg_row;
678 prev_line = curwin->w_cursor.lnum;
679#ifdef FEAT_SNIFF
680 ProcessSniffRequests();
681#endif
682 if (improved)
683 {
684 cmdline_row = msg_row;
685 do_cmdline(NULL, getexline, NULL, 0);
686 }
687 else
688 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
689 lines_left = Rows - 1;
690
Bram Moolenaardf177f62005-02-22 08:39:57 +0000691 if ((prev_line != curwin->w_cursor.lnum
692 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000694 if (curbuf->b_ml.ml_flags & ML_EMPTY)
695 EMSG(_(e_emptybuf));
696 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000698 if (ex_pressedreturn)
699 {
700 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000701 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000702 msg_row = prev_msg_row;
703 if (prev_msg_row == Rows - 1)
704 msg_row--;
705 }
706 msg_col = 0;
707 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
708 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000711 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
712 {
713 if (curbuf->b_ml.ml_flags & ML_EMPTY)
714 EMSG(_(e_emptybuf));
715 else
716 EMSG(_("E501: At end-of-file"));
717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 }
719
720#ifdef FEAT_GUI
721 --hold_gui_events;
722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 --RedrawingDisabled;
724 --no_wait_return;
725 update_screen(CLEAR);
726 need_wait_return = FALSE;
727 msg_scroll = save_msg_scroll;
728}
729
730/*
731 * Execute a simple command line. Used for translated commands like "*".
732 */
733 int
734do_cmdline_cmd(cmd)
735 char_u *cmd;
736{
737 return do_cmdline(cmd, NULL, NULL,
738 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
739}
740
741/*
742 * do_cmdline(): execute one Ex command line
743 *
744 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100745 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 * 2. Split up in parts separated with '|'.
747 *
748 * This function can be called recursively!
749 *
750 * flags:
751 * DOCMD_VERBOSE - The command will be included in the error message.
752 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100753 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 * DOCMD_KEYTYPED - Don't reset KeyTyped.
755 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
756 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
757 *
758 * return FAIL if cmdline could not be executed, OK otherwise
759 */
760 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100761do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100763 char_u *(*fgetline) __ARGS((int, void *, int));
764 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 int flags;
766{
767 char_u *next_cmdline; /* next cmd to execute */
768 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100769 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 static int recursive = 0; /* recursive depth */
771 int msg_didout_before_start = 0;
772 int count = 0; /* line number count */
773 int did_inc = FALSE; /* incremented RedrawingDisabled */
774 int retval = OK;
775#ifdef FEAT_EVAL
776 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000777 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 int current_line = 0; /* active line in lines_ga */
779 char_u *fname = NULL; /* function or script name */
780 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
781 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000782 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 int initial_trylevel;
784 struct msglist **saved_msg_list = NULL;
785 struct msglist *private_msg_list;
786
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100787 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 char_u *(*cmd_getline) __ARGS((int, void *, int));
789 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000790 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000792 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100794# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795# define cmd_cookie cookie
796#endif
797 static int call_depth = 0; /* recursiveness */
798
799#ifdef FEAT_EVAL
800 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
801 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000802 * This ensures that the do_errthrow() call in do_one_cmd() does not
803 * combine the messages stored by an earlier invocation of do_one_cmd()
804 * with the command name of the later one. This would happen when
805 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 saved_msg_list = msg_list;
807 msg_list = &private_msg_list;
808 private_msg_list = NULL;
809#endif
810
811 /* It's possible to create an endless loop with ":execute", catch that
812 * here. The value of 200 allows nested function calls, ":source", etc. */
813 if (call_depth == 200)
814 {
815 EMSG(_("E169: Command too recursive"));
816#ifdef FEAT_EVAL
817 /* When converting to an exception, we do not include the command name
818 * since this is not an error of the specific command. */
819 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
820 msg_list = saved_msg_list;
821#endif
822 return FAIL;
823 }
824 ++call_depth;
825
826#ifdef FEAT_EVAL
827 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000828 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 cstack.cs_trylevel = 0;
830 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000831 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
833
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100834 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835
836 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100837 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000838 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 ++ex_nesting_level;
840
841 /* Get the function or script name and the address where the next breakpoint
842 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000843 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 {
845 fname = func_name(real_cookie);
846 breakpoint = func_breakpoint(real_cookie);
847 dbg_tick = func_dbg_tick(real_cookie);
848 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100849 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 {
851 fname = sourcing_name;
852 breakpoint = source_breakpoint(real_cookie);
853 dbg_tick = source_dbg_tick(real_cookie);
854 }
855
856 /*
857 * Initialize "force_abort" and "suppress_errthrow" at the top level.
858 */
859 if (!recursive)
860 {
861 force_abort = FALSE;
862 suppress_errthrow = FALSE;
863 }
864
865 /*
866 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000867 * exception handling (used when debugging). Otherwise clear it to avoid
868 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000870 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000871 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000872 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200873 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
875 initial_trylevel = trylevel;
876
877 /*
878 * "did_throw" will be set to TRUE when an exception is being thrown.
879 */
880 did_throw = FALSE;
881#endif
882 /*
883 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000884 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 * If force_abort is set, we cancel everything.
886 */
887 did_emsg = FALSE;
888
889 /*
890 * KeyTyped is only set when calling vgetc(). Reset it here when not
891 * calling vgetc() (sourced command lines).
892 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100893 if (!(flags & DOCMD_KEYTYPED)
894 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 KeyTyped = FALSE;
896
897 /*
898 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000899 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 * - for multiple commands on one line, separated with '|'
901 * - when repeating until there are no more lines (for ":source")
902 */
903 next_cmdline = cmdline;
904 do
905 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000906#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100907 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000908#endif
909
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000910 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if (next_cmdline == NULL
912#ifdef FEAT_EVAL
913 && !force_abort
914 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000915 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916#endif
917 )
918 did_emsg = FALSE;
919
920 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000921 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100922 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 * 3. If a line is given: Make a copy, so we can mess with it.
924 */
925
926#ifdef FEAT_EVAL
927 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000928 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 {
930 /* Each '|' separated command is stored separately in lines_ga, to
931 * be able to jump to it. Don't use next_cmdline now. */
932 vim_free(cmdline_copy);
933 cmdline_copy = NULL;
934
935 /* Check if a function has returned or, unless it has an unclosed
936 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000937 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000939# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000940 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000941 func_line_end(real_cookie);
942# endif
943 if (func_has_ended(real_cookie))
944 {
945 retval = FAIL;
946 break;
947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000949#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000950 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100951 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000952 script_line_end();
953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954
955 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100956 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 {
958 retval = FAIL;
959 break;
960 }
961
962 /* If breakpoints have been added/deleted need to check for it. */
963 if (breakpoint != NULL && dbg_tick != NULL
964 && *dbg_tick != debug_tick)
965 {
966 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100967 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 fname, sourcing_lnum);
969 *dbg_tick = debug_tick;
970 }
971
972 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
973 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
974
975 /* Did we encounter a breakpoint? */
976 if (breakpoint != NULL && *breakpoint != 0
977 && *breakpoint <= sourcing_lnum)
978 {
979 dbg_breakpoint(fname, sourcing_lnum);
980 /* Find next breakpoint. */
981 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100982 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 fname, sourcing_lnum);
984 *dbg_tick = debug_tick;
985 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000986# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000987 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000988 {
989 if (getline_is_func)
990 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100991 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000992 script_line_start();
993 }
994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 }
996
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000997 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000999 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001000 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 * below, so that it stores lines in or reads them from
1002 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001003 * while/for loop. */
1004 cmd_getline = get_loop_line;
1005 cmd_cookie = (void *)&cmd_loop_cookie;
1006 cmd_loop_cookie.lines_gap = &lines_ga;
1007 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001008 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001009 cmd_loop_cookie.cookie = cookie;
1010 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 }
1012 else
1013 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001014 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 cmd_cookie = cookie;
1016 }
1017#endif
1018
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001019 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 if (next_cmdline == NULL)
1021 {
1022 /*
1023 * Need to set msg_didout for the first line after an ":if",
1024 * otherwise the ":if" will be overwritten.
1025 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001026 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001028 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029#ifdef FEAT_EVAL
1030 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1031#else
1032 0
1033#endif
1034 )) == NULL)
1035 {
1036 /* Don't call wait_return for aborted command line. The NULL
1037 * returned for the end of a sourced file or executed function
1038 * doesn't do this. */
1039 if (KeyTyped && !(flags & DOCMD_REPEAT))
1040 need_wait_return = FALSE;
1041 retval = FAIL;
1042 break;
1043 }
1044 used_getline = TRUE;
1045
1046 /*
1047 * Keep the first typed line. Clear it when more lines are typed.
1048 */
1049 if (flags & DOCMD_KEEPLINE)
1050 {
1051 vim_free(repeat_cmdline);
1052 if (count == 0)
1053 repeat_cmdline = vim_strsave(next_cmdline);
1054 else
1055 repeat_cmdline = NULL;
1056 }
1057 }
1058
1059 /* 3. Make a copy of the command so we can mess with it. */
1060 else if (cmdline_copy == NULL)
1061 {
1062 next_cmdline = vim_strsave(next_cmdline);
1063 if (next_cmdline == NULL)
1064 {
1065 EMSG(_(e_outofmem));
1066 retval = FAIL;
1067 break;
1068 }
1069 }
1070 cmdline_copy = next_cmdline;
1071
1072#ifdef FEAT_EVAL
1073 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001074 * Save the current line when inside a ":while" or ":for", and when
1075 * the command looks like a ":while" or ":for", because we may need it
1076 * later. When there is a '|' and another command, it is stored
1077 * separately, because we need to be able to jump back to it from an
1078 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001080 if (current_line == lines_ga.ga_len
1081 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001083 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {
1085 retval = FAIL;
1086 break;
1087 }
1088 }
1089 did_endif = FALSE;
1090#endif
1091
1092 if (count++ == 0)
1093 {
1094 /*
1095 * All output from the commands is put below each other, without
1096 * waiting for a return. Don't do this when executing commands
1097 * from a script or when being called recursive (e.g. for ":e
1098 * +command file").
1099 */
1100 if (!(flags & DOCMD_NOWAIT) && !recursive)
1101 {
1102 msg_didout_before_start = msg_didout;
1103 msg_didany = FALSE; /* no output yet */
1104 msg_start();
1105 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001106 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 ++RedrawingDisabled;
1108 did_inc = TRUE;
1109 }
1110 }
1111
1112 if (p_verbose >= 15 && sourcing_name != NULL)
1113 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001115 verbose_enter_scroll();
1116
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 smsg((char_u *)_("line %ld: %s"),
1118 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001119 if (msg_silent == 0)
1120 msg_puts((char_u *)"\n"); /* don't overwrite this */
1121
1122 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 --no_wait_return;
1124 }
1125
1126 /*
1127 * 2. Execute one '|' separated command.
1128 * do_one_cmd() will return NULL if there is no trailing '|'.
1129 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1130 */
1131 ++recursive;
1132 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1133#ifdef FEAT_EVAL
1134 &cstack,
1135#endif
1136 cmd_getline, cmd_cookie);
1137 --recursive;
1138
1139#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001140 if (cmd_cookie == (void *)&cmd_loop_cookie)
1141 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001143 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144#endif
1145
1146 if (next_cmdline == NULL)
1147 {
1148 vim_free(cmdline_copy);
1149 cmdline_copy = NULL;
1150#ifdef FEAT_CMDHIST
1151 /*
1152 * If the command was typed, remember it for the ':' register.
1153 * Do this AFTER executing the command to make :@: work.
1154 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001155 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 && new_last_cmdline != NULL)
1157 {
1158 vim_free(last_cmdline);
1159 last_cmdline = new_last_cmdline;
1160 new_last_cmdline = NULL;
1161 }
1162#endif
1163 }
1164 else
1165 {
1166 /* need to copy the command after the '|' to cmdline_copy, for the
1167 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001168 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 next_cmdline = cmdline_copy;
1170 }
1171
1172
1173#ifdef FEAT_EVAL
1174 /* reset did_emsg for a function that is not aborted by an error */
1175 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001176 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 && !func_has_abort(real_cookie))
1178 did_emsg = FALSE;
1179
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
1182 ++current_line;
1183
1184 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001185 * An ":endwhile", ":endfor" and ":continue" is handled here.
1186 * If we were executing commands, jump back to the ":while" or
1187 * ":for".
1188 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001190 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001192 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001194 /* Jump back to the matching ":while" or ":for". Be careful
1195 * not to use a cs_line[] from an entry that isn't a ":while"
1196 * or ":for": It would make "current_line" invalid and can
1197 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 if (!did_emsg && !got_int && !did_throw
1199 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 && (cstack.cs_flags[cstack.cs_idx]
1201 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 && cstack.cs_line[cstack.cs_idx] >= 0
1203 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1204 {
1205 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001206 /* remember we jumped there */
1207 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 line_breakcheck(); /* check if CTRL-C typed */
1209
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001210 /* Check for the next breakpoint at or after the ":while"
1211 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 if (breakpoint != NULL)
1213 {
1214 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001215 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 fname,
1217 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1218 *dbg_tick = debug_tick;
1219 }
1220 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001225 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1226 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 }
1228 }
1229
1230 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001231 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001233 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001235 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1237 }
1238 }
1239
1240 /*
1241 * When not inside any ":while" loop, clear remembered lines.
1242 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001243 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 {
1245 if (lines_ga.ga_len > 0)
1246 {
1247 sourcing_lnum =
1248 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1249 free_cmdlines(&lines_ga);
1250 }
1251 current_line = 0;
1252 }
1253
1254 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001255 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1256 * being restored at the ":endtry". Reset them here and set the
1257 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1258 * This includes the case where a missing ":endif", ":endwhile" or
1259 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001261 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001263 cstack.cs_lflags &= ~CSL_HAD_FINA;
1264 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1265 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 did_throw ? (void *)current_exception : NULL);
1267 did_emsg = got_int = did_throw = FALSE;
1268 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1269 }
1270
1271 /* Update global "trylevel" for recursive calls to do_cmdline() from
1272 * within this loop. */
1273 trylevel = initial_trylevel + cstack.cs_trylevel;
1274
1275 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001276 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 * files) is aborted because of an error, an interrupt, or an uncaught
1278 * exception, cancel everything. If it is left normally, reset
1279 * force_abort to get the non-EH compatible abortion behavior for
1280 * the rest of the script.
1281 */
1282 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1283 force_abort = FALSE;
1284
1285 /* Convert an interrupt to an exception if appropriate. */
1286 (void)do_intthrow(&cstack);
1287#endif /* FEAT_EVAL */
1288
1289 }
1290 /*
1291 * Continue executing command lines when:
1292 * - no CTRL-C typed, no aborting error, no exception thrown or try
1293 * conditionals need to be checked for executing finally clauses or
1294 * catching an interrupt exception
1295 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001296 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 * looping for ":source" command or function call.
1298 */
1299 while (!((got_int
1300#ifdef FEAT_EVAL
1301 || (did_emsg && force_abort) || did_throw
1302#endif
1303 )
1304#ifdef FEAT_EVAL
1305 && cstack.cs_trylevel == 0
1306#endif
1307 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001308 && !(did_emsg
1309#ifdef FEAT_EVAL
1310 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001311 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001312 * the :endtry to be missed. */
1313 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1314#endif
1315 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001316 && (getline_equal(fgetline, cookie, getexmodeline)
1317 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 && (next_cmdline != NULL
1319#ifdef FEAT_EVAL
1320 || cstack.cs_idx >= 0
1321#endif
1322 || (flags & DOCMD_REPEAT)));
1323
1324 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001325 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326#ifdef FEAT_EVAL
1327 free_cmdlines(&lines_ga);
1328 ga_clear(&lines_ga);
1329
1330 if (cstack.cs_idx >= 0)
1331 {
1332 /*
1333 * If a sourced file or executed function ran to its end, report the
1334 * unclosed conditional.
1335 */
1336 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001337 && ((getline_equal(fgetline, cookie, getsourceline)
1338 && !source_finished(fgetline, cookie))
1339 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 && !func_has_ended(real_cookie))))
1341 {
1342 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1343 EMSG(_(e_endtry));
1344 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1345 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001346 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1347 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 else
1349 EMSG(_(e_endif));
1350 }
1351
1352 /*
1353 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1354 * ":endtry" in a sourced file or executed function. If the try
1355 * conditional is in its finally clause, ignore anything pending.
1356 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001357 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 */
1359 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001360 {
1361 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1362
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001363 if (idx >= 0)
1364 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001365 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1366 &cstack.cs_looplevel);
1367 }
1368 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 trylevel = initial_trylevel;
1370 }
1371
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001372 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1373 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001375 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 ? (char_u *)"endfunction" : (char_u *)NULL);
1377
1378 if (trylevel == 0)
1379 {
1380 /*
1381 * When an exception is being thrown out of the outermost try
1382 * conditional, discard the uncaught exception, disable the conversion
1383 * of interrupts or errors to exceptions, and ensure that no more
1384 * commands are executed.
1385 */
1386 if (did_throw)
1387 {
1388 void *p = NULL;
1389 char_u *saved_sourcing_name;
1390 int saved_sourcing_lnum;
1391 struct msglist *messages = NULL, *next;
1392
1393 /*
1394 * If the uncaught exception is a user exception, report it as an
1395 * error. If it is an error exception, display the saved error
1396 * message now. For an interrupt exception, do nothing; the
1397 * interrupt message is given elsewhere.
1398 */
1399 switch (current_exception->type)
1400 {
1401 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001402 vim_snprintf((char *)IObuff, IOSIZE,
1403 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 current_exception->value);
1405 p = vim_strsave(IObuff);
1406 break;
1407 case ET_ERROR:
1408 messages = current_exception->messages;
1409 current_exception->messages = NULL;
1410 break;
1411 case ET_INTERRUPT:
1412 break;
1413 default:
1414 p = vim_strsave((char_u *)_(e_internal));
1415 }
1416
1417 saved_sourcing_name = sourcing_name;
1418 saved_sourcing_lnum = sourcing_lnum;
1419 sourcing_name = current_exception->throw_name;
1420 sourcing_lnum = current_exception->throw_lnum;
1421 current_exception->throw_name = NULL;
1422
1423 discard_current_exception(); /* uses IObuff if 'verbose' */
1424 suppress_errthrow = TRUE;
1425 force_abort = TRUE;
1426
1427 if (messages != NULL)
1428 {
1429 do
1430 {
1431 next = messages->next;
1432 emsg(messages->msg);
1433 vim_free(messages->msg);
1434 vim_free(messages);
1435 messages = next;
1436 }
1437 while (messages != NULL);
1438 }
1439 else if (p != NULL)
1440 {
1441 emsg(p);
1442 vim_free(p);
1443 }
1444 vim_free(sourcing_name);
1445 sourcing_name = saved_sourcing_name;
1446 sourcing_lnum = saved_sourcing_lnum;
1447 }
1448
1449 /*
1450 * On an interrupt or an aborting error not converted to an exception,
1451 * disable the conversion of errors to exceptions. (Interrupts are not
1452 * converted any more, here.) This enables also the interrupt message
1453 * when force_abort is set and did_emsg unset in case of an interrupt
1454 * from a finally clause after an error.
1455 */
1456 else if (got_int || (did_emsg && force_abort))
1457 suppress_errthrow = TRUE;
1458 }
1459
1460 /*
1461 * The current cstack will be freed when do_cmdline() returns. An uncaught
1462 * exception will have to be rethrown in the previous cstack. If a function
1463 * has just returned or a script file was just finished and the previous
1464 * cstack belongs to the same function or, respectively, script file, it
1465 * will have to be checked for finally clauses to be executed due to the
1466 * ":return" or ":finish". This is done in do_one_cmd().
1467 */
1468 if (did_throw)
1469 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001470 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001472 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 && ex_nesting_level > func_level(real_cookie) + 1))
1474 {
1475 if (!did_throw)
1476 check_cstack = TRUE;
1477 }
1478 else
1479 {
1480 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001481 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 --ex_nesting_level;
1483 /*
1484 * Go to debug mode when returning from a function in which we are
1485 * single-stepping.
1486 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001487 if ((getline_equal(fgetline, cookie, getsourceline)
1488 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001490 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 ? (char_u *)_("End of sourced file")
1492 : (char_u *)_("End of function"));
1493 }
1494
1495 /*
1496 * Restore the exception environment (done after returning from the
1497 * debugger).
1498 */
1499 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001500 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501
1502 msg_list = saved_msg_list;
1503#endif /* FEAT_EVAL */
1504
1505 /*
1506 * If there was too much output to fit on the command line, ask the user to
1507 * hit return before redrawing the screen. With the ":global" command we do
1508 * this only once after the command is finished.
1509 */
1510 if (did_inc)
1511 {
1512 --RedrawingDisabled;
1513 --no_wait_return;
1514 msg_scroll = FALSE;
1515
1516 /*
1517 * When just finished an ":if"-":else" which was typed, no need to
1518 * wait for hit-return. Also for an error situation.
1519 */
1520 if (retval == FAIL
1521#ifdef FEAT_EVAL
1522 || (did_endif && KeyTyped && !did_emsg)
1523#endif
1524 )
1525 {
1526 need_wait_return = FALSE;
1527 msg_didany = FALSE; /* don't wait when restarting edit */
1528 }
1529 else if (need_wait_return)
1530 {
1531 /*
1532 * The msg_start() above clears msg_didout. The wait_return we do
1533 * here should not overwrite the command that may be shown before
1534 * doing that.
1535 */
1536 msg_didout |= msg_didout_before_start;
1537 wait_return(FALSE);
1538 }
1539 }
1540
Bram Moolenaar2a942252012-11-28 23:03:07 +01001541#ifdef FEAT_EVAL
1542 did_endif = FALSE; /* in case do_cmdline used recursively */
1543#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 /*
1545 * Reset if_level, in case a sourced script file contains more ":if" than
1546 * ":endif" (could be ":if x | foo | endif").
1547 */
1548 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001549#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001550
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 --call_depth;
1552 return retval;
1553}
1554
1555#ifdef FEAT_EVAL
1556/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001557 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 */
1559 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001560get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 int c;
1562 void *cookie;
1563 int indent;
1564{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001565 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 wcmd_T *wp;
1567 char_u *line;
1568
1569 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1570 {
1571 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001572 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001574 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 if (cp->getline == NULL)
1576 line = getcmdline(c, 0L, indent);
1577 else
1578 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001579 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 ++cp->current_line;
1581
1582 return line;
1583 }
1584
1585 KeyTyped = FALSE;
1586 ++cp->current_line;
1587 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1588 sourcing_lnum = wp->lnum;
1589 return vim_strsave(wp->line);
1590}
1591
1592/*
1593 * Store a line in "gap" so that a ":while" loop can execute it again.
1594 */
1595 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001596store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 garray_T *gap;
1598 char_u *line;
1599{
1600 if (ga_grow(gap, 1) == FAIL)
1601 return FAIL;
1602 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1603 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1604 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 return OK;
1606}
1607
1608/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001609 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 */
1611 static void
1612free_cmdlines(gap)
1613 garray_T *gap;
1614{
1615 while (gap->ga_len > 0)
1616 {
1617 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1618 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 }
1620}
1621#endif
1622
1623/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001624 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1625 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001628getline_equal(fgetline, cookie, func)
1629 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001630 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 char_u *(*func) __ARGS((int, void *, int));
1632{
1633#ifdef FEAT_EVAL
1634 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001635 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaar89d40322006-08-29 15:30:07 +00001637 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001638 * function that's originally used to obtain the lines. This may be
1639 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001640 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001641 cp = (struct loop_cookie *)cookie;
1642 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 {
1644 gp = cp->getline;
1645 cp = cp->cookie;
1646 }
1647 return gp == func;
1648#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001649 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650#endif
1651}
1652
1653#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1654/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001655 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 * getline function. Otherwise return "cookie".
1657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001659getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001660 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001661 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662{
1663# ifdef FEAT_EVAL
1664 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001665 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666
Bram Moolenaar89d40322006-08-29 15:30:07 +00001667 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001668 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001670 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001671 cp = (struct loop_cookie *)cookie;
1672 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 {
1674 gp = cp->getline;
1675 cp = cp->cookie;
1676 }
1677 return cp;
1678# else
1679 return cookie;
1680# endif
1681}
1682#endif
1683
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001684
1685/*
1686 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1687 * ":bwipeout", etc.
1688 * Returns the buffer number.
1689 */
1690 static int
1691compute_buffer_local_count(addr_type, lnum, offset)
1692 int addr_type;
1693 int lnum;
1694 int offset;
1695{
1696 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001697 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001698 int count = offset;
1699
1700 buf = firstbuf;
1701 while (buf->b_next != NULL && buf->b_fnum < lnum)
1702 buf = buf->b_next;
1703 while (count != 0)
1704 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001705 count += (offset < 0) ? 1 : -1;
1706 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1707 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001708 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001709 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001710 if (addr_type == ADDR_LOADED_BUFFERS)
1711 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001712 while (buf->b_ml.ml_mfp == NULL)
1713 {
1714 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1715 if (nextbuf == NULL)
1716 break;
1717 buf = nextbuf;
1718 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001719 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001720 /* we might have gone too far, last buffer is not loadedd */
1721 if (addr_type == ADDR_LOADED_BUFFERS)
1722 while (buf->b_ml.ml_mfp == NULL)
1723 {
1724 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1725 if (nextbuf == NULL)
1726 break;
1727 buf = nextbuf;
1728 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001729 return buf->b_fnum;
1730}
1731
Bram Moolenaarf240e182014-11-27 18:33:02 +01001732#ifdef FEAT_WINDOWS
1733static int current_win_nr __ARGS((win_T *win));
1734static int current_tab_nr __ARGS((tabpage_T *tab));
1735
1736 static int
1737current_win_nr(win)
1738 win_T *win;
1739{
1740 win_T *wp;
1741 int nr = 0;
1742
1743 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1744 {
1745 ++nr;
1746 if (wp == win)
1747 break;
1748 }
1749 return nr;
1750}
1751
1752 static int
1753current_tab_nr(tab)
1754 tabpage_T *tab;
1755{
1756 tabpage_T *tp;
1757 int nr = 0;
1758
1759 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
1760 {
1761 ++nr;
1762 if (tp == tab)
1763 break;
1764 }
1765 return nr;
1766}
1767
1768# define CURRENT_WIN_NR current_win_nr(curwin)
1769# define LAST_WIN_NR current_win_nr(NULL)
1770# define CURRENT_TAB_NR current_tab_nr(curtab)
1771# define LAST_TAB_NR current_tab_nr(NULL)
1772#else
1773# define CURRENT_WIN_NR 1
1774# define LAST_WIN_NR 1
1775# define CURRENT_TAB_NR 1
1776# define LAST_TAB_NR 1
1777#endif
1778
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780/*
1781 * Execute one Ex command.
1782 *
1783 * If 'sourcing' is TRUE, the command will be included in the error message.
1784 *
1785 * 1. skip comment lines and leading space
1786 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001787 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001788 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001789 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001790 * 6. parse arguments
1791 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001793 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 *
1795 * This function may be called recursively!
1796 */
1797#if (_MSC_VER == 1200)
1798/*
Bram Moolenaared203462004-06-16 11:19:22 +00001799 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001801 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802#endif
1803 static char_u *
1804do_one_cmd(cmdlinep, sourcing,
1805#ifdef FEAT_EVAL
1806 cstack,
1807#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001808 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 char_u **cmdlinep;
1810 int sourcing;
1811#ifdef FEAT_EVAL
1812 struct condstack *cstack;
1813#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001814 char_u *(*fgetline) __ARGS((int, void *, int));
1815 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816{
1817 char_u *p;
1818 linenr_T lnum;
1819 long n;
1820 char_u *errormsg = NULL; /* error message */
1821 exarg_T ea; /* Ex command arguments */
1822 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001823 int save_msg_scroll = msg_scroll;
1824 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001826#ifdef HAVE_SANDBOX
1827 int did_sandbox = FALSE;
1828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 cmdmod_T save_cmdmod;
1830 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001831 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
1833 vim_memset(&ea, 0, sizeof(ea));
1834 ea.line1 = 1;
1835 ea.line2 = 1;
1836#ifdef FEAT_EVAL
1837 ++ex_nesting_level;
1838#endif
1839
Bram Moolenaar21691f82012-12-05 19:13:18 +01001840 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 if (quitmore
1842#ifdef FEAT_EVAL
1843 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001844 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001845#endif
1846#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001847 /* avoid that an autocommand, e.g. QuitPre, does this */
1848 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849#endif
1850 )
1851 --quitmore;
1852
1853 /*
1854 * Reset browse, confirm, etc.. They are restored when returning, for
1855 * recursive calls.
1856 */
1857 save_cmdmod = cmdmod;
1858 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1859
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001860 /* "#!anything" is handled like a comment. */
1861 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1862 goto doend;
1863
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 /*
1865 * Repeat until no more command modifiers are found.
1866 */
1867 ea.cmd = *cmdlinep;
1868 for (;;)
1869 {
1870/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001871 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 */
1873 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1874 ++ea.cmd;
1875
1876 /* in ex mode, an empty line works like :+ */
1877 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001878 && (getline_equal(fgetline, cookie, getexmodeline)
1879 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001880 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 {
1882 ea.cmd = (char_u *)"+";
1883 ex_pressedreturn = TRUE;
1884 }
1885
1886 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001887 if (*ea.cmd == '"')
1888 goto doend;
1889 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001890 {
1891 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894
1895/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001896 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 */
1898 p = ea.cmd;
1899 if (VIM_ISDIGIT(*ea.cmd))
1900 p = skipwhite(skipdigits(ea.cmd));
1901 switch (*p)
1902 {
1903 /* When adding an entry, also modify cmd_exists(). */
1904 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1905 break;
1906#ifdef FEAT_WINDOWS
1907 cmdmod.split |= WSP_ABOVE;
1908#endif
1909 continue;
1910
1911 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1912 {
1913#ifdef FEAT_WINDOWS
1914 cmdmod.split |= WSP_BELOW;
1915#endif
1916 continue;
1917 }
1918 if (checkforcmd(&ea.cmd, "browse", 3))
1919 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001920#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 cmdmod.browse = TRUE;
1922#endif
1923 continue;
1924 }
1925 if (!checkforcmd(&ea.cmd, "botright", 2))
1926 break;
1927#ifdef FEAT_WINDOWS
1928 cmdmod.split |= WSP_BOT;
1929#endif
1930 continue;
1931
1932 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1933 break;
1934#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1935 cmdmod.confirm = TRUE;
1936#endif
1937 continue;
1938
1939 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1940 {
1941 cmdmod.keepmarks = TRUE;
1942 continue;
1943 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001944 if (checkforcmd(&ea.cmd, "keepalt", 5))
1945 {
1946 cmdmod.keepalt = TRUE;
1947 continue;
1948 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001949 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1950 {
1951 cmdmod.keeppatterns = TRUE;
1952 continue;
1953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1955 break;
1956 cmdmod.keepjumps = TRUE;
1957 continue;
1958
1959 /* ":hide" and ":hide | cmd" are not modifiers */
1960 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1961 || *p == NUL || ends_excmd(*p))
1962 break;
1963 ea.cmd = p;
1964 cmdmod.hide = TRUE;
1965 continue;
1966
1967 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1968 {
1969 cmdmod.lockmarks = TRUE;
1970 continue;
1971 }
1972
1973 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1974 break;
1975#ifdef FEAT_WINDOWS
1976 cmdmod.split |= WSP_ABOVE;
1977#endif
1978 continue;
1979
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001980 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001981 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001982#ifdef FEAT_AUTOCMD
1983 if (cmdmod.save_ei == NULL)
1984 {
1985 /* Set 'eventignore' to "all". Restore the
1986 * existing option value later. */
1987 cmdmod.save_ei = vim_strsave(p_ei);
1988 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001989 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001990 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001991#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001992 continue;
1993 }
1994 if (!checkforcmd(&ea.cmd, "noswapfile", 6))
1995 break;
1996 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001997 continue;
1998
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
2000 break;
2001#ifdef FEAT_WINDOWS
2002 cmdmod.split |= WSP_BELOW;
2003#endif
2004 continue;
2005
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002006 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
2007 {
2008#ifdef HAVE_SANDBOX
2009 if (!did_sandbox)
2010 ++sandbox;
2011 did_sandbox = TRUE;
2012#endif
2013 continue;
2014 }
2015 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002017 if (save_msg_silent == -1)
2018 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
2021 {
2022 /* ":silent!", but not "silent !cmd" */
2023 ea.cmd = skipwhite(ea.cmd + 1);
2024 ++emsg_silent;
2025 ++did_esilent;
2026 }
2027 continue;
2028
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002029 case 't': if (checkforcmd(&p, "tab", 3))
2030 {
2031#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002032 if (vim_isdigit(*ea.cmd))
2033 cmdmod.tab = atoi((char *)ea.cmd) + 1;
2034 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002035 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002036 ea.cmd = p;
2037#endif
2038 continue;
2039 }
2040 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 break;
2042#ifdef FEAT_WINDOWS
2043 cmdmod.split |= WSP_TOP;
2044#endif
2045 continue;
2046
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002047 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2048 break;
2049 if (save_msg_silent == -1)
2050 save_msg_silent = msg_silent;
2051 msg_silent = 0;
2052 continue;
2053
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2055 {
2056#ifdef FEAT_VERTSPLIT
2057 cmdmod.split |= WSP_VERT;
2058#endif
2059 continue;
2060 }
2061 if (!checkforcmd(&p, "verbose", 4))
2062 break;
2063 if (verbose_save < 0)
2064 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002065 if (vim_isdigit(*ea.cmd))
2066 p_verbose = atoi((char *)ea.cmd);
2067 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 p_verbose = 1;
2069 ea.cmd = p;
2070 continue;
2071 }
2072 break;
2073 }
2074
2075#ifdef FEAT_EVAL
2076 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2077 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2078#else
2079 ea.skip = (if_level > 0);
2080#endif
2081
2082#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002083# ifdef FEAT_PROFILE
2084 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002085 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002086 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002087 if (getline_equal(fgetline, cookie, get_func_line))
2088 func_line_exec(getline_cookie(fgetline, cookie));
2089 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002090 script_line_exec();
2091 }
2092#endif
2093
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 /* May go to debug mode. If this happens and the ">quit" debug command is
2095 * used, throw an interrupt exception and skip the next command. */
2096 dbg_check_breakpoint(&ea);
2097 if (!ea.skip && got_int)
2098 {
2099 ea.skip = TRUE;
2100 (void)do_intthrow(cstack);
2101 }
2102#endif
2103
2104/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002105 * 3. Skip over the range to find the command. Let "p" point to after it.
2106 *
2107 * We need the command to know what kind of range it uses.
2108 */
2109 cmd = ea.cmd;
2110 ea.cmd = skip_range(ea.cmd, NULL);
2111 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2112 ea.cmd = skipwhite(ea.cmd + 1);
2113 p = find_command(&ea, NULL);
2114
2115/*
2116 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 *
2118 * where 'addr' is:
2119 *
2120 * % (entire file)
2121 * $ [+-NUM]
2122 * 'x [+-NUM] (where x denotes a currently defined mark)
2123 * . [+-NUM]
2124 * [+-NUM]..
2125 * NUM
2126 *
2127 * The ea.cmd pointer is updated to point to the first character following the
2128 * range spec. If an initial address is found, but no second, the upper bound
2129 * is equal to the lower.
2130 */
2131
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002132 if (ea.cmdidx != CMD_USER && ea.cmdidx != CMD_SIZE)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002133 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2134 else
2135 ea.addr_type = ADDR_LINES;
2136 ea.cmd = cmd;
2137
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 /* repeat for all ',' or ';' separated addresses */
2139 for (;;)
2140 {
2141 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002142 switch (ea.addr_type)
2143 {
2144 case ADDR_LINES:
2145 /* default is current line number */
2146 ea.line2 = curwin->w_cursor.lnum;
2147 break;
2148 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002149 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002150 ea.line2 = lnum;
2151 break;
2152 case ADDR_ARGUMENTS:
2153 ea.line2 = curwin->w_arg_idx + 1;
2154 break;
2155 case ADDR_LOADED_BUFFERS:
2156 case ADDR_UNLOADED_BUFFERS:
2157 ea.line2 = curbuf->b_fnum;
2158 break;
2159 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002160 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002161 ea.line2 = lnum;
2162 break;
2163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002165 lnum = get_address(&ea.cmd, ea.addr_type, ea.skip, ea.addr_count == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 if (ea.cmd == NULL) /* error detected */
2167 goto doend;
2168 if (lnum == MAXLNUM)
2169 {
2170 if (*ea.cmd == '%') /* '%' - all lines */
2171 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002172 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002174 switch (ea.addr_type)
2175 {
2176 case ADDR_LINES:
2177 ea.line1 = 1;
2178 ea.line2 = curbuf->b_ml.ml_line_count;
2179 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002180 case ADDR_LOADED_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002181 buf = firstbuf;
2182 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2183 buf = buf->b_next;
2184 ea.line1 = buf->b_fnum;
2185 buf = lastbuf;
2186 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2187 buf = buf->b_prev;
2188 ea.line2 = buf->b_fnum;
2189 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002190 case ADDR_UNLOADED_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002191 ea.line1 = firstbuf->b_fnum;
2192 ea.line2 = lastbuf->b_fnum;
2193 break;
2194 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002195 case ADDR_TABS:
2196 errormsg = (char_u *)_(e_invrange);
2197 goto doend;
2198 break;
2199 case ADDR_ARGUMENTS:
2200 ea.line1 = 1;
2201 ea.line2 = ARGCOUNT;
2202 break;
2203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 ++ea.addr_count;
2205 }
2206 /* '*' - visual area */
2207 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2208 {
2209 pos_T *fp;
2210
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002211 if (ea.addr_type != ADDR_LINES)
2212 {
2213 errormsg = (char_u *)_(e_invrange);
2214 goto doend;
2215 }
2216
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 ++ea.cmd;
2218 if (!ea.skip)
2219 {
2220 fp = getmark('<', FALSE);
2221 if (check_mark(fp) == FAIL)
2222 goto doend;
2223 ea.line1 = fp->lnum;
2224 fp = getmark('>', FALSE);
2225 if (check_mark(fp) == FAIL)
2226 goto doend;
2227 ea.line2 = fp->lnum;
2228 ++ea.addr_count;
2229 }
2230 }
2231 }
2232 else
2233 ea.line2 = lnum;
2234 ea.addr_count++;
2235
2236 if (*ea.cmd == ';')
2237 {
2238 if (!ea.skip)
2239 curwin->w_cursor.lnum = ea.line2;
2240 }
2241 else if (*ea.cmd != ',')
2242 break;
2243 ++ea.cmd;
2244 }
2245
2246 /* One address given: set start and end lines */
2247 if (ea.addr_count == 1)
2248 {
2249 ea.line1 = ea.line2;
2250 /* ... but only implicit: really no address given */
2251 if (lnum == MAXLNUM)
2252 ea.addr_count = 0;
2253 }
2254
2255 /* Don't leave the cursor on an illegal line (caused by ';') */
2256 check_cursor_lnum();
2257
2258/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002259 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 */
2261
2262 /*
2263 * Skip ':' and any white space
2264 */
2265 ea.cmd = skipwhite(ea.cmd);
2266 while (*ea.cmd == ':')
2267 ea.cmd = skipwhite(ea.cmd + 1);
2268
2269 /*
2270 * If we got a line, but no command, then go to the line.
2271 * If we find a '|' or '\n' we set ea.nextcmd.
2272 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002273 if (*ea.cmd == NUL || *ea.cmd == '"'
2274 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 {
2276 /*
2277 * strange vi behaviour:
2278 * ":3" jumps to line 3
2279 * ":3|..." prints line 3
2280 * ":|" prints current line
2281 */
2282 if (ea.skip) /* skip this if inside :if */
2283 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002284 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
2286 ea.cmdidx = CMD_print;
2287 ea.argt = RANGE+COUNT+TRLBAR;
2288 if ((errormsg = invalid_range(&ea)) == NULL)
2289 {
2290 correct_range(&ea);
2291 ex_print(&ea);
2292 }
2293 }
2294 else if (ea.addr_count != 0)
2295 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002296 if (ea.line2 > curbuf->b_ml.ml_line_count)
2297 {
2298 /* With '-' in 'cpoptions' a line number past the file is an
2299 * error, otherwise put it at the end of the file. */
2300 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2301 ea.line2 = -1;
2302 else
2303 ea.line2 = curbuf->b_ml.ml_line_count;
2304 }
2305
2306 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002307 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 else
2309 {
2310 if (ea.line2 == 0)
2311 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 else
2313 curwin->w_cursor.lnum = ea.line2;
2314 beginline(BL_SOL | BL_FIX);
2315 }
2316 }
2317 goto doend;
2318 }
2319
Bram Moolenaard5005162014-08-22 23:05:54 +02002320#ifdef FEAT_AUTOCMD
2321 /* If this looks like an undefined user command and there are CmdUndefined
2322 * autocommands defined, trigger the matching autocommands. */
2323 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2324 && ASCII_ISUPPER(*ea.cmd)
2325 && has_cmdundefined())
2326 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002327 int ret;
2328
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002329 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002330 while (ASCII_ISALNUM(*p))
2331 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002332 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002333 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2334 vim_free(p);
2335 if (ret && !aborting())
2336 p = find_command(&ea, NULL);
2337 }
2338#endif
2339
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340#ifdef FEAT_USR_CMDS
2341 if (p == NULL)
2342 {
2343 if (!ea.skip)
2344 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2345 goto doend;
2346 }
2347 /* Check for wrong commands. */
2348 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2349 {
2350 errormsg = uc_fun_cmd();
2351 goto doend;
2352 }
2353#endif
2354 if (ea.cmdidx == CMD_SIZE)
2355 {
2356 if (!ea.skip)
2357 {
2358 STRCPY(IObuff, _("E492: Not an editor command"));
2359 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002360 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002362 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 }
2364 goto doend;
2365 }
2366
Bram Moolenaar958636c2014-10-21 20:01:58 +02002367 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2368 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002369#ifdef HAVE_EX_SCRIPT_NI
2370 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2371#endif
2372 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
2374#ifndef FEAT_EVAL
2375 /*
2376 * When the expression evaluation is disabled, recognize the ":if" and
2377 * ":endif" commands and ignore everything in between it.
2378 */
2379 if (ea.cmdidx == CMD_if)
2380 ++if_level;
2381 if (if_level)
2382 {
2383 if (ea.cmdidx == CMD_endif)
2384 --if_level;
2385 goto doend;
2386 }
2387
2388#endif
2389
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002390 /* forced commands */
2391 if (*p == '!' && ea.cmdidx != CMD_substitute
2392 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 {
2394 ++p;
2395 ea.forceit = TRUE;
2396 }
2397 else
2398 ea.forceit = FALSE;
2399
2400/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002401 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002403 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002404 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406 if (!ea.skip)
2407 {
2408#ifdef HAVE_SANDBOX
2409 if (sandbox != 0 && !(ea.argt & SBOXOK))
2410 {
2411 /* Command not allowed in sandbox. */
2412 errormsg = (char_u *)_(e_sandbox);
2413 goto doend;
2414 }
2415#endif
2416 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2417 {
2418 /* Command not allowed in non-'modifiable' buffer */
2419 errormsg = (char_u *)_(e_modifiable);
2420 goto doend;
2421 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002422
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002423 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002424 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002426 /* Command not allowed when editing the command line. */
2427#ifdef FEAT_CMDWIN
2428 if (cmdwin_type != 0)
2429 errormsg = (char_u *)_(e_cmdwin);
2430 else
2431#endif
2432 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 goto doend;
2434 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002435#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002436 /* Disallow editing another buffer when "curbuf_lock" is set.
2437 * Do allow ":edit" (check for argument later).
2438 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002439 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002440 && ea.cmdidx != CMD_edit
2441 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002442 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002443 && curbuf_locked())
2444 goto doend;
2445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446
2447 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2448 {
2449 /* no range allowed */
2450 errormsg = (char_u *)_(e_norange);
2451 goto doend;
2452 }
2453 }
2454
2455 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2456 {
2457 errormsg = (char_u *)_(e_nobang);
2458 goto doend;
2459 }
2460
2461 /*
2462 * Don't complain about the range if it is not used
2463 * (could happen if line_count is accidentally set to 0).
2464 */
2465 if (!ea.skip && !ni)
2466 {
2467 /*
2468 * If the range is backwards, ask for confirmation and, if given, swap
2469 * ea.line1 & ea.line2 so it's forwards again.
2470 * When global command is busy, don't ask, will fail below.
2471 */
2472 if (!global_busy && ea.line1 > ea.line2)
2473 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002474 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002476 if (sourcing || exmode_active)
2477 {
2478 errormsg = (char_u *)_("E493: Backwards range given");
2479 goto doend;
2480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 if (ask_yesno((char_u *)
2482 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002483 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 }
2485 lnum = ea.line1;
2486 ea.line1 = ea.line2;
2487 ea.line2 = lnum;
2488 }
2489 if ((errormsg = invalid_range(&ea)) != NULL)
2490 goto doend;
2491 }
2492
2493 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2494 ea.line2 = 1;
2495
2496 correct_range(&ea);
2497
2498#ifdef FEAT_FOLDING
2499 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2500 {
2501 /* Put the first line at the start of a closed fold, put the last line
2502 * at the end of a closed fold. */
2503 (void)hasFolding(ea.line1, &ea.line1, NULL);
2504 (void)hasFolding(ea.line2, NULL, &ea.line2);
2505 }
2506#endif
2507
2508#ifdef FEAT_QUICKFIX
2509 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002510 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 * option here, so things like % get expanded.
2512 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002513 p = replace_makeprg(&ea, p, cmdlinep);
2514 if (p == NULL)
2515 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516#endif
2517
2518 /*
2519 * Skip to start of argument.
2520 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2521 */
2522 if (ea.cmdidx == CMD_bang)
2523 ea.arg = p;
2524 else
2525 ea.arg = skipwhite(p);
2526
2527 /*
2528 * Check for "++opt=val" argument.
2529 * Must be first, allow ":w ++enc=utf8 !cmd"
2530 */
2531 if (ea.argt & ARGOPT)
2532 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2533 if (getargopt(&ea) == FAIL && !ni)
2534 {
2535 errormsg = (char_u *)_(e_invarg);
2536 goto doend;
2537 }
2538
2539 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2540 {
2541 if (*ea.arg == '>') /* append */
2542 {
2543 if (*++ea.arg != '>') /* typed wrong */
2544 {
2545 errormsg = (char_u *)_("E494: Use w or w>>");
2546 goto doend;
2547 }
2548 ea.arg = skipwhite(ea.arg + 1);
2549 ea.append = TRUE;
2550 }
2551 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2552 {
2553 ++ea.arg;
2554 ea.usefilter = TRUE;
2555 }
2556 }
2557
2558 if (ea.cmdidx == CMD_read)
2559 {
2560 if (ea.forceit)
2561 {
2562 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2563 ea.forceit = FALSE;
2564 }
2565 else if (*ea.arg == '!') /* :r !filter */
2566 {
2567 ++ea.arg;
2568 ea.usefilter = TRUE;
2569 }
2570 }
2571
2572 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2573 {
2574 ea.amount = 1;
2575 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2576 {
2577 ++ea.arg;
2578 ++ea.amount;
2579 }
2580 ea.arg = skipwhite(ea.arg);
2581 }
2582
2583 /*
2584 * Check for "+command" argument, before checking for next command.
2585 * Don't do this for ":read !cmd" and ":write !cmd".
2586 */
2587 if ((ea.argt & EDITCMD) && !ea.usefilter)
2588 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2589
2590 /*
2591 * Check for '|' to separate commands and '"' to start comments.
2592 * Don't do this for ":read !cmd" and ":write !cmd".
2593 */
2594 if ((ea.argt & TRLBAR) && !ea.usefilter)
2595 separate_nextcmd(&ea);
2596
2597 /*
2598 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002599 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2600 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002602 else if (ea.cmdidx == CMD_bang
2603 || ea.cmdidx == CMD_global
2604 || ea.cmdidx == CMD_vglobal
2605 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 {
2607 for (p = ea.arg; *p; ++p)
2608 {
2609 /* Remove one backslash before a newline, so that it's possible to
2610 * pass a newline to the shell and also a newline that is preceded
2611 * with a backslash. This makes it impossible to end a shell
2612 * command in a backslash, but that doesn't appear useful.
2613 * Halving the number of backslashes is incompatible with previous
2614 * versions. */
2615 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002616 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 else if (*p == '\n')
2618 {
2619 ea.nextcmd = p + 1;
2620 *p = NUL;
2621 break;
2622 }
2623 }
2624 }
2625
2626 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2627 {
2628 ea.line1 = 1;
2629 ea.line2 = curbuf->b_ml.ml_line_count;
2630 }
2631
2632 /* accept numbered register only when no count allowed (:put) */
2633 if ( (ea.argt & REGSTR)
2634 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002635 /* Do not allow register = for user commands */
2636 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2638 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002639#ifndef FEAT_CLIPBOARD
2640 /* check these explicitly for a more specific error message */
2641 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002643 errormsg = (char_u *)_(e_invalidreg);
2644 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
2646#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002647 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2648 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002649 {
2650 ea.regname = *ea.arg++;
2651#ifdef FEAT_EVAL
2652 /* for '=' register: accept the rest of the line as an expression */
2653 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2654 {
2655 set_expr_line(vim_strsave(ea.arg));
2656 ea.arg += STRLEN(ea.arg);
2657 }
2658#endif
2659 ea.arg = skipwhite(ea.arg);
2660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 }
2662
2663 /*
2664 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2665 * count, it's a buffer name.
2666 */
2667 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2668 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2669 || vim_iswhite(*p)))
2670 {
2671 n = getdigits(&ea.arg);
2672 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002673 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 {
2675 errormsg = (char_u *)_(e_zerocount);
2676 goto doend;
2677 }
2678 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2679 {
2680 ea.line2 = n;
2681 if (ea.addr_count == 0)
2682 ea.addr_count = 1;
2683 }
2684 else
2685 {
2686 ea.line1 = ea.line2;
2687 ea.line2 += n - 1;
2688 ++ea.addr_count;
2689 /*
2690 * Be vi compatible: no error message for out of range.
2691 */
2692 if (ea.line2 > curbuf->b_ml.ml_line_count)
2693 ea.line2 = curbuf->b_ml.ml_line_count;
2694 }
2695 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002696
2697 /*
2698 * Check for flags: 'l', 'p' and '#'.
2699 */
2700 if (ea.argt & EXFLAGS)
2701 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002703 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002704 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 {
2706 errormsg = (char_u *)_(e_trailing);
2707 goto doend;
2708 }
2709
2710 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2711 {
2712 errormsg = (char_u *)_(e_argreq);
2713 goto doend;
2714 }
2715
2716#ifdef FEAT_EVAL
2717 /*
2718 * Skip the command when it's not going to be executed.
2719 * The commands like :if, :endif, etc. always need to be executed.
2720 * Also make an exception for commands that handle a trailing command
2721 * themselves.
2722 */
2723 if (ea.skip)
2724 {
2725 switch (ea.cmdidx)
2726 {
2727 /* commands that need evaluation */
2728 case CMD_while:
2729 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002730 case CMD_for:
2731 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 case CMD_if:
2733 case CMD_elseif:
2734 case CMD_else:
2735 case CMD_endif:
2736 case CMD_try:
2737 case CMD_catch:
2738 case CMD_finally:
2739 case CMD_endtry:
2740 case CMD_function:
2741 break;
2742
2743 /* Commands that handle '|' themselves. Check: A command should
2744 * either have the TRLBAR flag, appear in this list or appear in
2745 * the list at ":help :bar". */
2746 case CMD_aboveleft:
2747 case CMD_and:
2748 case CMD_belowright:
2749 case CMD_botright:
2750 case CMD_browse:
2751 case CMD_call:
2752 case CMD_confirm:
2753 case CMD_delfunction:
2754 case CMD_djump:
2755 case CMD_dlist:
2756 case CMD_dsearch:
2757 case CMD_dsplit:
2758 case CMD_echo:
2759 case CMD_echoerr:
2760 case CMD_echomsg:
2761 case CMD_echon:
2762 case CMD_execute:
2763 case CMD_help:
2764 case CMD_hide:
2765 case CMD_ijump:
2766 case CMD_ilist:
2767 case CMD_isearch:
2768 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002769 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 case CMD_keepjumps:
2771 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002772 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 case CMD_leftabove:
2774 case CMD_let:
2775 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002776 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002778 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002779 case CMD_noautocmd:
2780 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 case CMD_perl:
2782 case CMD_psearch:
2783 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002784 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002785 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 case CMD_return:
2787 case CMD_rightbelow:
2788 case CMD_ruby:
2789 case CMD_silent:
2790 case CMD_smagic:
2791 case CMD_snomagic:
2792 case CMD_substitute:
2793 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002794 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 case CMD_tcl:
2796 case CMD_throw:
2797 case CMD_tilde:
2798 case CMD_topleft:
2799 case CMD_unlet:
2800 case CMD_verbose:
2801 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002802 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 break;
2804
2805 default: goto doend;
2806 }
2807 }
2808#endif
2809
2810 if (ea.argt & XFILE)
2811 {
2812 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2813 goto doend;
2814 }
2815
2816#ifdef FEAT_LISTCMDS
2817 /*
2818 * Accept buffer name. Cannot be used at the same time with a buffer
2819 * number. Don't do this for a user command.
2820 */
2821 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002822 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 {
2824 /*
2825 * :bdelete, :bwipeout and :bunload take several arguments, separated
2826 * by spaces: find next space (skipping over escaped characters).
2827 * The others take one argument: ignore trailing spaces.
2828 */
2829 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2830 || ea.cmdidx == CMD_bunload)
2831 p = skiptowhite_esc(ea.arg);
2832 else
2833 {
2834 p = ea.arg + STRLEN(ea.arg);
2835 while (p > ea.arg && vim_iswhite(p[-1]))
2836 --p;
2837 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002838 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2839 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 if (ea.line2 < 0) /* failed */
2841 goto doend;
2842 ea.addr_count = 1;
2843 ea.arg = skipwhite(p);
2844 }
2845#endif
2846
2847/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002848 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 *
2850 * The "ea" structure holds the arguments that can be used.
2851 */
2852 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002853 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 ea.cookie = cookie;
2855#ifdef FEAT_EVAL
2856 ea.cstack = cstack;
2857#endif
2858
2859#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002860 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 {
2862 /*
2863 * Execute a user-defined command.
2864 */
2865 do_ucmd(&ea);
2866 }
2867 else
2868#endif
2869 {
2870 /*
2871 * Call the function to execute the command.
2872 */
2873 ea.errmsg = NULL;
2874 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2875 if (ea.errmsg != NULL)
2876 errormsg = (char_u *)_(ea.errmsg);
2877 }
2878
2879#ifdef FEAT_EVAL
2880 /*
2881 * If the command just executed called do_cmdline(), any throw or ":return"
2882 * or ":finish" encountered there must also check the cstack of the still
2883 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2884 * exception, or reanimate a returned function or finished script file and
2885 * return or finish it again.
2886 */
2887 if (need_rethrow)
2888 do_throw(cstack);
2889 else if (check_cstack)
2890 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002891 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002893 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 && current_func_returned())
2895 do_return(&ea, TRUE, FALSE, NULL);
2896 }
2897 need_rethrow = check_cstack = FALSE;
2898#endif
2899
2900doend:
2901 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2902 curwin->w_cursor.lnum = 1;
2903
2904 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2905 {
2906 if (sourcing)
2907 {
2908 if (errormsg != IObuff)
2909 {
2910 STRCPY(IObuff, errormsg);
2911 errormsg = IObuff;
2912 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002913 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
2915 emsg(errormsg);
2916 }
2917#ifdef FEAT_EVAL
2918 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002919 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2920 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921#endif
2922
2923 if (verbose_save >= 0)
2924 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002925#ifdef FEAT_AUTOCMD
2926 if (cmdmod.save_ei != NULL)
2927 {
2928 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002929 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2930 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002931 free_string_option(cmdmod.save_ei);
2932 }
2933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934
2935 cmdmod = save_cmdmod;
2936
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002937 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 {
2939 /* messages could be enabled for a serious error, need to check if the
2940 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002941 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002942 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 emsg_silent -= did_esilent;
2944 if (emsg_silent < 0)
2945 emsg_silent = 0;
2946 /* Restore msg_scroll, it's set by file I/O commands, even when no
2947 * message is actually displayed. */
2948 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002949
2950 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2951 * somewhere in the line. Put it back in the first column. */
2952 if (redirecting())
2953 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 }
2955
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002956#ifdef HAVE_SANDBOX
2957 if (did_sandbox)
2958 --sandbox;
2959#endif
2960
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2962 ea.nextcmd = NULL;
2963
2964#ifdef FEAT_EVAL
2965 --ex_nesting_level;
2966#endif
2967
2968 return ea.nextcmd;
2969}
2970#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002971 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972#endif
2973
2974/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002975 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 * If there is a match advance "pp" to the argument and return TRUE.
2977 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002978 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002980 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 char *cmd; /* name of command */
2982 int len; /* required length */
2983{
2984 int i;
2985
2986 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002987 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 break;
2989 if (i >= len && !isalpha((*pp)[i]))
2990 {
2991 *pp = skipwhite(*pp + i);
2992 return TRUE;
2993 }
2994 return FALSE;
2995}
2996
2997/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002998 * Append "cmd" to the error message in IObuff.
2999 * Takes care of limiting the length and handling 0xa0, which would be
3000 * invisible otherwise.
3001 */
3002 static void
3003append_command(cmd)
3004 char_u *cmd;
3005{
3006 char_u *s = cmd;
3007 char_u *d;
3008
3009 STRCAT(IObuff, ": ");
3010 d = IObuff + STRLEN(IObuff);
3011 while (*s != NUL && d - IObuff < IOSIZE - 7)
3012 {
3013 if (
3014#ifdef FEAT_MBYTE
3015 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3016#endif
3017 *s == 0xa0)
3018 {
3019 s +=
3020#ifdef FEAT_MBYTE
3021 enc_utf8 ? 2 :
3022#endif
3023 1;
3024 STRCPY(d, "<a0>");
3025 d += 4;
3026 }
3027 else
3028 MB_COPY_CHAR(s, d);
3029 }
3030 *d = NUL;
3031}
3032
3033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003035 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003037 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 * Returns NULL for an ambiguous user command.
3039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 static char_u *
3041find_command(eap, full)
3042 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003043 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044{
3045 int len;
3046 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003047 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048
3049 /*
3050 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003051 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 * - the 'k' command can directly be followed by any character.
3053 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3054 * but :sre[wind] is another command, as are :scrip[tnames],
3055 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003056 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 */
3058 p = eap->cmd;
3059 if (*p == 'k')
3060 {
3061 eap->cmdidx = CMD_k;
3062 ++p;
3063 }
3064 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003065 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
3066 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 || p[1] == 'g'
3068 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3069 || p[1] == 'I'
3070 || (p[1] == 'r' && p[2] != 'e')))
3071 {
3072 eap->cmdidx = CMD_substitute;
3073 ++p;
3074 }
3075 else
3076 {
3077 while (ASCII_ISALPHA(*p))
3078 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003079 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003080 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003081 while (ASCII_ISALNUM(*p))
3082 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003083
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 /* check for non-alpha command */
3085 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3086 ++p;
3087 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003088 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3089 {
3090 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3091 * :delete with the 'l' flag. Same for 'p'. */
3092 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003093 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003094 break;
3095 if (i == len - 1)
3096 {
3097 --len;
3098 if (p[-1] == 'l')
3099 eap->flags |= EXFLAG_LIST;
3100 else
3101 eap->flags |= EXFLAG_PRINT;
3102 }
3103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104
3105 if (ASCII_ISLOWER(*eap->cmd))
3106 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3107 else
3108 eap->cmdidx = cmdidxs[26];
3109
3110 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3111 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3112 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3113 (size_t)len) == 0)
3114 {
3115#ifdef FEAT_EVAL
3116 if (full != NULL
3117 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3118 *full = TRUE;
3119#endif
3120 break;
3121 }
3122
3123#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003124 /* Look for a user defined command as a last resort. Let ":Print" be
3125 * overruled by a user defined command. */
3126 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3127 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003129 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 while (ASCII_ISALNUM(*p))
3131 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003132 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 }
3134#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003135 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 eap->cmdidx = CMD_SIZE;
3137 }
3138
3139 return p;
3140}
3141
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003142#ifdef FEAT_USR_CMDS
3143/*
3144 * Search for a user command that matches "eap->cmd".
3145 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3146 * Return a pointer to just after the command.
3147 * Return NULL if there is no matching command.
3148 */
3149 static char_u *
3150find_ucmd(eap, p, full, xp, compl)
3151 exarg_T *eap;
3152 char_u *p; /* end of the command (possibly including count) */
3153 int *full; /* set to TRUE for a full match */
3154 expand_T *xp; /* used for completion, NULL otherwise */
3155 int *compl; /* completion flags or NULL */
3156{
3157 int len = (int)(p - eap->cmd);
3158 int j, k, matchlen = 0;
3159 ucmd_T *uc;
3160 int found = FALSE;
3161 int possible = FALSE;
3162 char_u *cp, *np; /* Point into typed cmd and test name */
3163 garray_T *gap;
3164 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3165 only full match global is accepted. */
3166
3167 /*
3168 * Look for buffer-local user commands first, then global ones.
3169 */
3170 gap = &curbuf->b_ucmds;
3171 for (;;)
3172 {
3173 for (j = 0; j < gap->ga_len; ++j)
3174 {
3175 uc = USER_CMD_GA(gap, j);
3176 cp = eap->cmd;
3177 np = uc->uc_name;
3178 k = 0;
3179 while (k < len && *np != NUL && *cp++ == *np++)
3180 k++;
3181 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3182 {
3183 /* If finding a second match, the command is ambiguous. But
3184 * not if a buffer-local command wasn't a full match and a
3185 * global command is a full match. */
3186 if (k == len && found && *np != NUL)
3187 {
3188 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003189 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003190 amb_local = TRUE;
3191 }
3192
3193 if (!found || (k == len && *np == NUL))
3194 {
3195 /* If we matched up to a digit, then there could
3196 * be another command including the digit that we
3197 * should use instead.
3198 */
3199 if (k == len)
3200 found = TRUE;
3201 else
3202 possible = TRUE;
3203
3204 if (gap == &ucmds)
3205 eap->cmdidx = CMD_USER;
3206 else
3207 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003208 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003209 eap->useridx = j;
3210
3211# ifdef FEAT_CMDL_COMPL
3212 if (compl != NULL)
3213 *compl = uc->uc_compl;
3214# ifdef FEAT_EVAL
3215 if (xp != NULL)
3216 {
3217 xp->xp_arg = uc->uc_compl_arg;
3218 xp->xp_scriptID = uc->uc_scriptID;
3219 }
3220# endif
3221# endif
3222 /* Do not search for further abbreviations
3223 * if this is an exact match. */
3224 matchlen = k;
3225 if (k == len && *np == NUL)
3226 {
3227 if (full != NULL)
3228 *full = TRUE;
3229 amb_local = FALSE;
3230 break;
3231 }
3232 }
3233 }
3234 }
3235
3236 /* Stop if we found a full match or searched all. */
3237 if (j < gap->ga_len || gap == &ucmds)
3238 break;
3239 gap = &ucmds;
3240 }
3241
3242 /* Only found ambiguous matches. */
3243 if (amb_local)
3244 {
3245 if (xp != NULL)
3246 xp->xp_context = EXPAND_UNSUCCESSFUL;
3247 return NULL;
3248 }
3249
3250 /* The match we found may be followed immediately by a number. Move "p"
3251 * back to point to it. */
3252 if (found || possible)
3253 return p + (matchlen - len);
3254 return p;
3255}
3256#endif
3257
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003259static struct cmdmod
3260{
3261 char *name;
3262 int minlen;
3263 int has_count; /* :123verbose :3tab */
3264} cmdmods[] = {
3265 {"aboveleft", 3, FALSE},
3266 {"belowright", 3, FALSE},
3267 {"botright", 2, FALSE},
3268 {"browse", 3, FALSE},
3269 {"confirm", 4, FALSE},
3270 {"hide", 3, FALSE},
3271 {"keepalt", 5, FALSE},
3272 {"keepjumps", 5, FALSE},
3273 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003274 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003275 {"leftabove", 5, FALSE},
3276 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003277 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003278 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003279 {"rightbelow", 6, FALSE},
3280 {"sandbox", 3, FALSE},
3281 {"silent", 3, FALSE},
3282 {"tab", 3, TRUE},
3283 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003284 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003285 {"verbose", 4, TRUE},
3286 {"vertical", 4, FALSE},
3287};
3288
3289/*
3290 * Return length of a command modifier (including optional count).
3291 * Return zero when it's not a modifier.
3292 */
3293 int
3294modifier_len(cmd)
3295 char_u *cmd;
3296{
3297 int i, j;
3298 char_u *p = cmd;
3299
3300 if (VIM_ISDIGIT(*cmd))
3301 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003302 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003303 {
3304 for (j = 0; p[j] != NUL; ++j)
3305 if (p[j] != cmdmods[i].name[j])
3306 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003307 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003308 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003309 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003310 }
3311 return 0;
3312}
3313
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314/*
3315 * Return > 0 if an Ex command "name" exists.
3316 * Return 2 if there is an exact match.
3317 * Return 3 if there is an ambiguous match.
3318 */
3319 int
3320cmd_exists(name)
3321 char_u *name;
3322{
3323 exarg_T ea;
3324 int full = FALSE;
3325 int i;
3326 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003327 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
3329 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003330 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
3332 for (j = 0; name[j] != NUL; ++j)
3333 if (name[j] != cmdmods[i].name[j])
3334 break;
3335 if (name[j] == NUL && j >= cmdmods[i].minlen)
3336 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3337 }
3338
Bram Moolenaara9587612006-05-04 21:47:50 +00003339 /* Check built-in commands and user defined commands.
3340 * For ":2match" and ":3match" we need to skip the number. */
3341 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003343 p = find_command(&ea, &full);
3344 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003346 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3347 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003348 if (*skipwhite(p) != NUL)
3349 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3351}
3352#endif
3353
3354/*
3355 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3356 * we don't need/want deleted. Maybe this could be done better if we didn't
3357 * repeat all this stuff. The only problem is that they may not stay
3358 * perfectly compatible with each other, but then the command line syntax
3359 * probably won't change that much -- webb.
3360 */
3361 char_u *
3362set_one_cmd_context(xp, buff)
3363 expand_T *xp;
3364 char_u *buff; /* buffer for command string */
3365{
3366 char_u *p;
3367 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003368 int len = 0;
3369 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3371 int compl = EXPAND_NOTHING;
3372#endif
3373#ifdef FEAT_CMDL_COMPL
3374 int delim;
3375#endif
3376 int forceit = FALSE;
3377 int usefilter = FALSE; /* filter instead of file name */
3378
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003379 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 xp->xp_pattern = buff;
3381 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003382 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
3384/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003385 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 */
3387 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3388 ;
3389 xp->xp_pattern = cmd;
3390
3391 if (*cmd == NUL)
3392 return NULL;
3393 if (*cmd == '"') /* ignore comment lines */
3394 {
3395 xp->xp_context = EXPAND_NOTHING;
3396 return NULL;
3397 }
3398
3399/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003400 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 */
3402 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 xp->xp_pattern = cmd;
3404 if (*cmd == NUL)
3405 return NULL;
3406 if (*cmd == '"')
3407 {
3408 xp->xp_context = EXPAND_NOTHING;
3409 return NULL;
3410 }
3411
3412 if (*cmd == '|' || *cmd == '\n')
3413 return cmd + 1; /* There's another command */
3414
3415 /*
3416 * Isolate the command and search for it in the command table.
3417 * Exceptions:
3418 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003419 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3421 */
3422 if (*cmd == 'k' && cmd[1] != 'e')
3423 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003424 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 p = cmd + 1;
3426 }
3427 else
3428 {
3429 p = cmd;
3430 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3431 ++p;
3432 /* check for non-alpha command */
3433 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3434 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003435 /* for python 3.x: ":py3*" commands completion */
3436 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003437 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003438 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003439 while (ASCII_ISALPHA(*p) || *p == '*')
3440 ++p;
3441 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003442 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003444 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
3446 xp->xp_context = EXPAND_UNSUCCESSFUL;
3447 return NULL;
3448 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003449 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003450 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3451 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3452 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 break;
3454
3455#ifdef FEAT_USR_CMDS
3456 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3458 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459#endif
3460 }
3461
3462 /*
3463 * If the cursor is touching the command, and it ends in an alpha-numeric
3464 * character, complete the command name.
3465 */
3466 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3467 return NULL;
3468
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003469 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 {
3471 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3472 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003473 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 p = cmd + 1;
3475 }
3476#ifdef FEAT_USR_CMDS
3477 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3478 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003479 ea.cmd = cmd;
3480 p = find_ucmd(&ea, p, NULL, xp,
3481# if defined(FEAT_CMDL_COMPL)
3482 &compl
3483# else
3484 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003486 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003487 if (p == NULL)
3488 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 }
3490#endif
3491 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003492 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 {
3494 /* Not still touching the command and it was an illegal one */
3495 xp->xp_context = EXPAND_UNSUCCESSFUL;
3496 return NULL;
3497 }
3498
3499 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3500
3501 if (*p == '!') /* forced commands */
3502 {
3503 forceit = TRUE;
3504 ++p;
3505 }
3506
3507/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003508 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003510 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003511 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
3513 arg = skipwhite(p);
3514
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003515 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
3517 if (*arg == '>') /* append */
3518 {
3519 if (*++arg == '>')
3520 ++arg;
3521 arg = skipwhite(arg);
3522 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003523 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 {
3525 ++arg;
3526 usefilter = TRUE;
3527 }
3528 }
3529
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003530 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
3532 usefilter = forceit; /* :r! filter if forced */
3533 if (*arg == '!') /* :r !filter */
3534 {
3535 ++arg;
3536 usefilter = TRUE;
3537 }
3538 }
3539
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003540 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
3542 while (*arg == *cmd) /* allow any number of '>' or '<' */
3543 ++arg;
3544 arg = skipwhite(arg);
3545 }
3546
3547 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003548 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
3550 /* Check if we're in the +command */
3551 p = arg + 1;
3552 arg = skip_cmd_arg(arg, FALSE);
3553
3554 /* Still touching the command after '+'? */
3555 if (*arg == NUL)
3556 return p;
3557
3558 /* Skip space(s) after +command to get to the real argument */
3559 arg = skipwhite(arg);
3560 }
3561
3562 /*
3563 * Check for '|' to separate commands and '"' to start comments.
3564 * Don't do this for ":read !cmd" and ":write !cmd".
3565 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003566 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
3568 p = arg;
3569 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003570 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 p += 2;
3572 while (*p)
3573 {
3574 if (*p == Ctrl_V)
3575 {
3576 if (p[1] != NUL)
3577 ++p;
3578 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003579 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 || *p == '|' || *p == '\n')
3581 {
3582 if (*(p - 1) != '\\')
3583 {
3584 if (*p == '|' || *p == '\n')
3585 return p + 1;
3586 return NULL; /* It's a comment */
3587 }
3588 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003589 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 }
3591 }
3592
3593 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003594 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 vim_strchr((char_u *)"|\"", *arg) == NULL)
3596 return NULL;
3597
3598 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003599 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003601 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003602 while (*p && p < buff + len)
3603 {
3604 if (*p == ' ' || *p == TAB)
3605 {
3606 /* argument starts after a space */
3607 xp->xp_pattern = ++p;
3608 }
3609 else
3610 {
3611 if (*p == '\\' && *(p + 1) != NUL)
3612 ++p; /* skip over escaped character */
3613 mb_ptr_adv(p);
3614 }
3615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003617 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003619 int c;
3620 int in_quote = FALSE;
3621 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
3623 /*
3624 * Allow spaces within back-quotes to count as part of the argument
3625 * being expanded.
3626 */
3627 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003628 p = xp->xp_pattern;
3629 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003631#ifdef FEAT_MBYTE
3632 if (has_mbyte)
3633 c = mb_ptr2char(p);
3634 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003636 c = *p;
3637 if (c == '\\' && p[1] != NUL)
3638 ++p;
3639 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 {
3641 if (!in_quote)
3642 {
3643 xp->xp_pattern = p;
3644 bow = p + 1;
3645 }
3646 in_quote = !in_quote;
3647 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003648 /* An argument can contain just about everything, except
3649 * characters that end the command and white space. */
3650 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003651#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003652 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003653#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003654 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003655 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003656 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003657 while (*p != NUL)
3658 {
3659#ifdef FEAT_MBYTE
3660 if (has_mbyte)
3661 c = mb_ptr2char(p);
3662 else
3663#endif
3664 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003665 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003666 break;
3667#ifdef FEAT_MBYTE
3668 if (has_mbyte)
3669 len = (*mb_ptr2len)(p);
3670 else
3671#endif
3672 len = 1;
3673 mb_ptr_adv(p);
3674 }
3675 if (in_quote)
3676 bow = p;
3677 else
3678 xp->xp_pattern = p;
3679 p -= len;
3680 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003681 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 }
3683
3684 /*
3685 * If we are still inside the quotes, and we passed a space, just
3686 * expand from there.
3687 */
3688 if (bow != NULL && in_quote)
3689 xp->xp_pattern = bow;
3690 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003691
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003692 /* For a shell command more chars need to be escaped. */
3693 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003694 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003695#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003696 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003697#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003698 /* When still after the command name expand executables. */
3699 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003700 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702
3703 /* Check for environment variable */
3704 if (*xp->xp_pattern == '$'
3705#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3706 || *xp->xp_pattern == '%'
3707#endif
3708 )
3709 {
3710 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3711 if (!vim_isIDc(*p))
3712 break;
3713 if (*p == NUL)
3714 {
3715 xp->xp_context = EXPAND_ENV_VARS;
3716 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003717#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3718 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003719 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003720 compl = EXPAND_ENV_VARS;
3721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 }
3723 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003724#if defined(FEAT_CMDL_COMPL)
3725 /* Check for user names */
3726 if (*xp->xp_pattern == '~')
3727 {
3728 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3729 ;
3730 /* Complete ~user only if it partially matches a user name.
3731 * A full match ~user<Tab> will be replaced by user's home
3732 * directory i.e. something like ~user<Tab> -> /home/user/ */
3733 if (*p == NUL && p > xp->xp_pattern + 1
3734 && match_user(xp->xp_pattern + 1) == 1)
3735 {
3736 xp->xp_context = EXPAND_USER;
3737 ++xp->xp_pattern;
3738 }
3739 }
3740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 }
3742
3743/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003744 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003746 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003748 case CMD_find:
3749 case CMD_sfind:
3750 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003751 if (xp->xp_context == EXPAND_FILES)
3752 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003753 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 case CMD_cd:
3755 case CMD_chdir:
3756 case CMD_lcd:
3757 case CMD_lchdir:
3758 if (xp->xp_context == EXPAND_FILES)
3759 xp->xp_context = EXPAND_DIRECTORIES;
3760 break;
3761 case CMD_help:
3762 xp->xp_context = EXPAND_HELP;
3763 xp->xp_pattern = arg;
3764 break;
3765
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003766 /* Command modifiers: return the argument.
3767 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003769 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 case CMD_belowright:
3771 case CMD_botright:
3772 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003773 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003775 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 case CMD_folddoclosed:
3777 case CMD_folddoopen:
3778 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003779 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 case CMD_keepjumps:
3781 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003782 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 case CMD_leftabove:
3784 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003785 case CMD_noautocmd:
3786 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003788 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003790 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003791 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 case CMD_topleft:
3793 case CMD_verbose:
3794 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003795 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 return arg;
3797
Bram Moolenaar4f688582007-07-24 12:34:30 +00003798#ifdef FEAT_CMDL_COMPL
3799# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 case CMD_match:
3801 if (*arg == NUL || !ends_excmd(*arg))
3802 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003803 /* also complete "None" */
3804 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 arg = skipwhite(skiptowhite(arg));
3806 if (*arg != NUL)
3807 {
3808 xp->xp_context = EXPAND_NOTHING;
3809 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3810 }
3811 }
3812 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003813# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815/*
3816 * All completion for the +cmdline_compl feature goes here.
3817 */
3818
3819# ifdef FEAT_USR_CMDS
3820 case CMD_command:
3821 /* Check for attributes */
3822 while (*arg == '-')
3823 {
3824 arg++; /* Skip "-" */
3825 p = skiptowhite(arg);
3826 if (*p == NUL)
3827 {
3828 /* Cursor is still in the attribute */
3829 p = vim_strchr(arg, '=');
3830 if (p == NULL)
3831 {
3832 /* No "=", so complete attribute names */
3833 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3834 xp->xp_pattern = arg;
3835 return NULL;
3836 }
3837
3838 /* For the -complete and -nargs attributes, we complete
3839 * their arguments as well.
3840 */
3841 if (STRNICMP(arg, "complete", p - arg) == 0)
3842 {
3843 xp->xp_context = EXPAND_USER_COMPLETE;
3844 xp->xp_pattern = p + 1;
3845 return NULL;
3846 }
3847 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3848 {
3849 xp->xp_context = EXPAND_USER_NARGS;
3850 xp->xp_pattern = p + 1;
3851 return NULL;
3852 }
3853 return NULL;
3854 }
3855 arg = skipwhite(p);
3856 }
3857
3858 /* After the attributes comes the new command name */
3859 p = skiptowhite(arg);
3860 if (*p == NUL)
3861 {
3862 xp->xp_context = EXPAND_USER_COMMANDS;
3863 xp->xp_pattern = arg;
3864 break;
3865 }
3866
3867 /* And finally comes a normal command */
3868 return skipwhite(p);
3869
3870 case CMD_delcommand:
3871 xp->xp_context = EXPAND_USER_COMMANDS;
3872 xp->xp_pattern = arg;
3873 break;
3874# endif
3875
3876 case CMD_global:
3877 case CMD_vglobal:
3878 delim = *arg; /* get the delimiter */
3879 if (delim)
3880 ++arg; /* skip delimiter if there is one */
3881
3882 while (arg[0] != NUL && arg[0] != delim)
3883 {
3884 if (arg[0] == '\\' && arg[1] != NUL)
3885 ++arg;
3886 ++arg;
3887 }
3888 if (arg[0] != NUL)
3889 return arg + 1;
3890 break;
3891 case CMD_and:
3892 case CMD_substitute:
3893 delim = *arg;
3894 if (delim)
3895 {
3896 /* skip "from" part */
3897 ++arg;
3898 arg = skip_regexp(arg, delim, p_magic, NULL);
3899 }
3900 /* skip "to" part */
3901 while (arg[0] != NUL && arg[0] != delim)
3902 {
3903 if (arg[0] == '\\' && arg[1] != NUL)
3904 ++arg;
3905 ++arg;
3906 }
3907 if (arg[0] != NUL) /* skip delimiter */
3908 ++arg;
3909 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3910 ++arg;
3911 if (arg[0] != NUL)
3912 return arg;
3913 break;
3914 case CMD_isearch:
3915 case CMD_dsearch:
3916 case CMD_ilist:
3917 case CMD_dlist:
3918 case CMD_ijump:
3919 case CMD_psearch:
3920 case CMD_djump:
3921 case CMD_isplit:
3922 case CMD_dsplit:
3923 arg = skipwhite(skipdigits(arg)); /* skip count */
3924 if (*arg == '/') /* Match regexp, not just whole words */
3925 {
3926 for (++arg; *arg && *arg != '/'; arg++)
3927 if (*arg == '\\' && arg[1] != NUL)
3928 arg++;
3929 if (*arg)
3930 {
3931 arg = skipwhite(arg + 1);
3932
3933 /* Check for trailing illegal characters */
3934 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3935 xp->xp_context = EXPAND_NOTHING;
3936 else
3937 return arg;
3938 }
3939 }
3940 break;
3941#ifdef FEAT_AUTOCMD
3942 case CMD_autocmd:
3943 return set_context_in_autocmd(xp, arg, FALSE);
3944
3945 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003946 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 return set_context_in_autocmd(xp, arg, TRUE);
3948#endif
3949 case CMD_set:
3950 set_context_in_set_cmd(xp, arg, 0);
3951 break;
3952 case CMD_setglobal:
3953 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3954 break;
3955 case CMD_setlocal:
3956 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3957 break;
3958 case CMD_tag:
3959 case CMD_stag:
3960 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003961 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 case CMD_tselect:
3963 case CMD_stselect:
3964 case CMD_ptselect:
3965 case CMD_tjump:
3966 case CMD_stjump:
3967 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003968 if (*p_wop != NUL)
3969 xp->xp_context = EXPAND_TAGS_LISTFILES;
3970 else
3971 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 xp->xp_pattern = arg;
3973 break;
3974 case CMD_augroup:
3975 xp->xp_context = EXPAND_AUGROUP;
3976 xp->xp_pattern = arg;
3977 break;
3978#ifdef FEAT_SYN_HL
3979 case CMD_syntax:
3980 set_context_in_syntax_cmd(xp, arg);
3981 break;
3982#endif
3983#ifdef FEAT_EVAL
3984 case CMD_let:
3985 case CMD_if:
3986 case CMD_elseif:
3987 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003988 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 case CMD_echo:
3990 case CMD_echon:
3991 case CMD_execute:
3992 case CMD_echomsg:
3993 case CMD_echoerr:
3994 case CMD_call:
3995 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003996 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 break;
3998
3999 case CMD_unlet:
4000 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4001 arg = xp->xp_pattern + 1;
4002 xp->xp_context = EXPAND_USER_VARS;
4003 xp->xp_pattern = arg;
4004 break;
4005
4006 case CMD_function:
4007 case CMD_delfunction:
4008 xp->xp_context = EXPAND_USER_FUNC;
4009 xp->xp_pattern = arg;
4010 break;
4011
4012 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004013 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 break;
4015#endif
4016 case CMD_highlight:
4017 set_context_in_highlight_cmd(xp, arg);
4018 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004019#ifdef FEAT_CSCOPE
4020 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004021 case CMD_lcscope:
4022 case CMD_scscope:
4023 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004024 break;
4025#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004026#ifdef FEAT_SIGNS
4027 case CMD_sign:
4028 set_context_in_sign_cmd(xp, arg);
4029 break;
4030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031#ifdef FEAT_LISTCMDS
4032 case CMD_bdelete:
4033 case CMD_bwipeout:
4034 case CMD_bunload:
4035 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4036 arg = xp->xp_pattern + 1;
4037 /*FALLTHROUGH*/
4038 case CMD_buffer:
4039 case CMD_sbuffer:
4040 case CMD_checktime:
4041 xp->xp_context = EXPAND_BUFFERS;
4042 xp->xp_pattern = arg;
4043 break;
4044#endif
4045#ifdef FEAT_USR_CMDS
4046 case CMD_USER:
4047 case CMD_USER_BUF:
4048 if (compl != EXPAND_NOTHING)
4049 {
4050 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004051 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
4053# ifdef FEAT_MENU
4054 if (compl == EXPAND_MENUS)
4055 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4056# endif
4057 if (compl == EXPAND_COMMANDS)
4058 return arg;
4059 if (compl == EXPAND_MAPPINGS)
4060 return set_context_in_map_cmd(xp, (char_u *)"map",
4061 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004062 /* Find start of last argument. */
4063 p = arg;
4064 while (*p)
4065 {
4066 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004067 /* argument starts after a space */
4068 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004069 else if (*p == '\\' && *(p + 1) != NUL)
4070 ++p; /* skip over escaped character */
4071 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 xp->xp_pattern = arg;
4074 }
4075 xp->xp_context = compl;
4076 }
4077 break;
4078#endif
4079 case CMD_map: case CMD_noremap:
4080 case CMD_nmap: case CMD_nnoremap:
4081 case CMD_vmap: case CMD_vnoremap:
4082 case CMD_omap: case CMD_onoremap:
4083 case CMD_imap: case CMD_inoremap:
4084 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004085 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004086 case CMD_smap: case CMD_snoremap:
4087 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004089 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 case CMD_unmap:
4091 case CMD_nunmap:
4092 case CMD_vunmap:
4093 case CMD_ounmap:
4094 case CMD_iunmap:
4095 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004096 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004097 case CMD_sunmap:
4098 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004100 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 case CMD_abbreviate: case CMD_noreabbrev:
4102 case CMD_cabbrev: case CMD_cnoreabbrev:
4103 case CMD_iabbrev: case CMD_inoreabbrev:
4104 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004105 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 case CMD_unabbreviate:
4107 case CMD_cunabbrev:
4108 case CMD_iunabbrev:
4109 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004110 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111#ifdef FEAT_MENU
4112 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4113 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4114 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4115 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4116 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4117 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4118 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4119 case CMD_tmenu: case CMD_tunmenu:
4120 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4121 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4122#endif
4123
4124 case CMD_colorscheme:
4125 xp->xp_context = EXPAND_COLORS;
4126 xp->xp_pattern = arg;
4127 break;
4128
4129 case CMD_compiler:
4130 xp->xp_context = EXPAND_COMPILER;
4131 xp->xp_pattern = arg;
4132 break;
4133
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004134 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004135 xp->xp_context = EXPAND_OWNSYNTAX;
4136 xp->xp_pattern = arg;
4137 break;
4138
4139 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004140 xp->xp_context = EXPAND_FILETYPE;
4141 xp->xp_pattern = arg;
4142 break;
4143
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4145 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4146 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004147 p = skiptowhite(arg);
4148 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 {
4150 xp->xp_context = EXPAND_LANGUAGE;
4151 xp->xp_pattern = arg;
4152 }
4153 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004154 {
4155 if ( STRNCMP(arg, "messages", p - arg) == 0
4156 || STRNCMP(arg, "ctype", p - arg) == 0
4157 || STRNCMP(arg, "time", p - arg) == 0)
4158 {
4159 xp->xp_context = EXPAND_LOCALES;
4160 xp->xp_pattern = skipwhite(p);
4161 }
4162 else
4163 xp->xp_context = EXPAND_NOTHING;
4164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 break;
4166#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004167#if defined(FEAT_PROFILE)
4168 case CMD_profile:
4169 set_context_in_profile_cmd(xp, arg);
4170 break;
4171#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004172 case CMD_behave:
4173 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004174 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004175 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004177#if defined(FEAT_CMDHIST)
4178 case CMD_history:
4179 xp->xp_context = EXPAND_HISTORY;
4180 xp->xp_pattern = arg;
4181 break;
4182#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004183#if defined(FEAT_PROFILE)
4184 case CMD_syntime:
4185 xp->xp_context = EXPAND_SYNTIME;
4186 xp->xp_pattern = arg;
4187 break;
4188#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004189
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190#endif /* FEAT_CMDL_COMPL */
4191
4192 default:
4193 break;
4194 }
4195 return NULL;
4196}
4197
4198/*
4199 * skip a range specifier of the form: addr [,addr] [;addr] ..
4200 *
4201 * Backslashed delimiters after / or ? will be skipped, and commands will
4202 * not be expanded between /'s and ?'s or after "'".
4203 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004204 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 * Returns the "cmd" pointer advanced to beyond the range.
4206 */
4207 char_u *
4208skip_range(cmd, ctx)
4209 char_u *cmd;
4210 int *ctx; /* pointer to xp_context or NULL */
4211{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004212 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213
Bram Moolenaardf177f62005-02-22 08:39:57 +00004214 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 {
4216 if (*cmd == '\'')
4217 {
4218 if (*++cmd == NUL && ctx != NULL)
4219 *ctx = EXPAND_NOTHING;
4220 }
4221 else if (*cmd == '/' || *cmd == '?')
4222 {
4223 delim = *cmd++;
4224 while (*cmd != NUL && *cmd != delim)
4225 if (*cmd++ == '\\' && *cmd != NUL)
4226 ++cmd;
4227 if (*cmd == NUL && ctx != NULL)
4228 *ctx = EXPAND_NOTHING;
4229 }
4230 if (*cmd != NUL)
4231 ++cmd;
4232 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004233
4234 /* Skip ":" and white space. */
4235 while (*cmd == ':')
4236 cmd = skipwhite(cmd + 1);
4237
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 return cmd;
4239}
4240
4241/*
4242 * get a single EX address
4243 *
4244 * Set ptr to the next character after the part that was interpreted.
4245 * Set ptr to NULL when an error is encountered.
4246 *
4247 * Return MAXLNUM when no Ex address was found.
4248 */
4249 static linenr_T
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004250get_address(ptr, addr_type, skip, to_other_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 char_u **ptr;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004252 int addr_type; /* flag: one of ADDR_LINES, ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 int skip; /* only skip the address, don't use it */
4254 int to_other_file; /* flag: may jump to other file */
4255{
4256 int c;
4257 int i;
4258 long n;
4259 char_u *cmd;
4260 pos_T pos;
4261 pos_T *fp;
4262 linenr_T lnum;
4263
4264 cmd = skipwhite(*ptr);
4265 lnum = MAXLNUM;
4266 do
4267 {
4268 switch (*cmd)
4269 {
4270 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004271 ++cmd;
4272 switch (addr_type)
4273 {
4274 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 lnum = curwin->w_cursor.lnum;
4276 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004277 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004278 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004279 break;
4280 case ADDR_ARGUMENTS:
4281 lnum = curwin->w_arg_idx + 1;
4282 break;
4283 case ADDR_LOADED_BUFFERS:
4284 case ADDR_UNLOADED_BUFFERS:
4285 lnum = curbuf->b_fnum;
4286 break;
4287 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004288 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004289 break;
4290 }
4291 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
4293 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004294 ++cmd;
4295 switch (addr_type)
4296 {
4297 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 lnum = curbuf->b_ml.ml_line_count;
4299 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004300 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004301 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004302 break;
4303 case ADDR_ARGUMENTS:
4304 lnum = ARGCOUNT;
4305 break;
4306 case ADDR_LOADED_BUFFERS:
4307 case ADDR_UNLOADED_BUFFERS:
4308 lnum = lastbuf->b_fnum;
4309 break;
4310 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004311 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004312 break;
4313 }
4314 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315
4316 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004317 if (*++cmd == NUL)
4318 {
4319 cmd = NULL;
4320 goto error;
4321 }
4322 if (addr_type != ADDR_LINES)
4323 {
4324 EMSG(_(e_invaddr));
4325 goto error;
4326 }
4327 if (skip)
4328 ++cmd;
4329 else
4330 {
4331 /* Only accept a mark in another file when it is
4332 * used by itself: ":'M". */
4333 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4334 ++cmd;
4335 if (fp == (pos_T *)-1)
4336 /* Jumped to another file. */
4337 lnum = curwin->w_cursor.lnum;
4338 else
4339 {
4340 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 {
4342 cmd = NULL;
4343 goto error;
4344 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004345 lnum = fp->lnum;
4346 }
4347 }
4348 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349
4350 case '/':
4351 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004352 c = *cmd++;
4353 if (addr_type != ADDR_LINES)
4354 {
4355 EMSG(_(e_invaddr));
4356 goto error;
4357 }
4358 if (skip) /* skip "/pat/" */
4359 {
4360 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4361 if (*cmd == c)
4362 ++cmd;
4363 }
4364 else
4365 {
4366 pos = curwin->w_cursor; /* save curwin->w_cursor */
4367 /*
4368 * When '/' or '?' follows another address, start
4369 * from there.
4370 */
4371 if (lnum != MAXLNUM)
4372 curwin->w_cursor.lnum = lnum;
4373 /*
4374 * Start a forward search at the end of the line.
4375 * Start a backward search at the start of the line.
4376 * This makes sure we never match in the current
4377 * line, and can match anywhere in the
4378 * next/previous line.
4379 */
4380 if (c == '/')
4381 curwin->w_cursor.col = MAXCOL;
4382 else
4383 curwin->w_cursor.col = 0;
4384 searchcmdlen = 0;
4385 if (!do_search(NULL, c, cmd, 1L,
4386 SEARCH_HIS | SEARCH_MSG, NULL))
4387 {
4388 curwin->w_cursor = pos;
4389 cmd = NULL;
4390 goto error;
4391 }
4392 lnum = curwin->w_cursor.lnum;
4393 curwin->w_cursor = pos;
4394 /* adjust command string pointer */
4395 cmd += searchcmdlen;
4396 }
4397 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
4399 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004400 ++cmd;
4401 if (addr_type != ADDR_LINES)
4402 {
4403 EMSG(_(e_invaddr));
4404 goto error;
4405 }
4406 if (*cmd == '&')
4407 i = RE_SUBST;
4408 else if (*cmd == '?' || *cmd == '/')
4409 i = RE_SEARCH;
4410 else
4411 {
4412 EMSG(_(e_backslash));
4413 cmd = NULL;
4414 goto error;
4415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004417 if (!skip)
4418 {
4419 /*
4420 * When search follows another address, start from
4421 * there.
4422 */
4423 if (lnum != MAXLNUM)
4424 pos.lnum = lnum;
4425 else
4426 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004428 /*
4429 * Start the search just like for the above
4430 * do_search().
4431 */
4432 if (*cmd != '?')
4433 pos.col = MAXCOL;
4434 else
4435 pos.col = 0;
4436 if (searchit(curwin, curbuf, &pos,
4437 *cmd == '?' ? BACKWARD : FORWARD,
4438 (char_u *)"", 1L, SEARCH_MSG,
4439 i, (linenr_T)0, NULL) != FAIL)
4440 lnum = pos.lnum;
4441 else
4442 {
4443 cmd = NULL;
4444 goto error;
4445 }
4446 }
4447 ++cmd;
4448 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
4450 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004451 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4452 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 }
4454
4455 for (;;)
4456 {
4457 cmd = skipwhite(cmd);
4458 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4459 break;
4460
4461 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004462 {
4463 switch (addr_type)
4464 {
4465 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004466 /* "+1" is same as ".+1" */
4467 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004468 break;
4469 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004470 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004471 break;
4472 case ADDR_ARGUMENTS:
4473 lnum = curwin->w_arg_idx + 1;
4474 break;
4475 case ADDR_LOADED_BUFFERS:
4476 case ADDR_UNLOADED_BUFFERS:
4477 lnum = curbuf->b_fnum;
4478 break;
4479 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004480 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004481 break;
4482 }
4483 }
4484
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 if (VIM_ISDIGIT(*cmd))
4486 i = '+'; /* "number" is same as "+number" */
4487 else
4488 i = *cmd++;
4489 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4490 n = 1;
4491 else
4492 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004493 if (addr_type == ADDR_LOADED_BUFFERS
4494 || addr_type == ADDR_UNLOADED_BUFFERS)
Bram Moolenaar4d84d932014-11-30 14:50:16 +01004495 lnum = compute_buffer_local_count(addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004496 else if (i == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 lnum -= n;
4498 else
4499 lnum += n;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004500
4501 switch (addr_type)
4502 {
4503 case ADDR_LINES:
4504 break;
4505 case ADDR_ARGUMENTS:
4506 if (lnum < 0)
4507 lnum = 0;
4508 else if (lnum >= ARGCOUNT)
4509 lnum = ARGCOUNT;
4510 break;
4511 case ADDR_TABS:
4512 if (lnum < 0)
4513 {
4514 lnum = 0;
4515 break;
4516 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01004517 if (lnum >= LAST_TAB_NR)
4518 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004519 break;
4520 case ADDR_WINDOWS:
4521 if (lnum < 0)
4522 {
4523 lnum = 0;
4524 break;
4525 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01004526 if (lnum >= LAST_WIN_NR)
4527 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004528 break;
4529 case ADDR_LOADED_BUFFERS:
4530 case ADDR_UNLOADED_BUFFERS:
4531 if (lnum < firstbuf->b_fnum)
4532 {
4533 lnum = firstbuf->b_fnum;
4534 break;
4535 }
4536 if (lnum > lastbuf->b_fnum)
4537 lnum = lastbuf->b_fnum;
4538 break;
4539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 }
4541 } while (*cmd == '/' || *cmd == '?');
4542
4543error:
4544 *ptr = cmd;
4545 return lnum;
4546}
4547
4548/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004549 * Get flags from an Ex command argument.
4550 */
4551 static void
4552get_flags(eap)
4553 exarg_T *eap;
4554{
4555 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4556 {
4557 if (*eap->arg == 'l')
4558 eap->flags |= EXFLAG_LIST;
4559 else if (*eap->arg == 'p')
4560 eap->flags |= EXFLAG_PRINT;
4561 else
4562 eap->flags |= EXFLAG_NR;
4563 eap->arg = skipwhite(eap->arg + 1);
4564 }
4565}
4566
4567/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 * Function called for command which is Not Implemented. NI!
4569 */
4570 void
4571ex_ni(eap)
4572 exarg_T *eap;
4573{
4574 if (!eap->skip)
4575 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4576}
4577
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004578#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579/*
4580 * Function called for script command which is Not Implemented. NI!
4581 * Skips over ":perl <<EOF" constructs.
4582 */
4583 static void
4584ex_script_ni(eap)
4585 exarg_T *eap;
4586{
4587 if (!eap->skip)
4588 ex_ni(eap);
4589 else
4590 vim_free(script_get(eap, eap->arg));
4591}
4592#endif
4593
4594/*
4595 * Check range in Ex command for validity.
4596 * Return NULL when valid, error message when invalid.
4597 */
4598 static char_u *
4599invalid_range(eap)
4600 exarg_T *eap;
4601{
4602 if ( eap->line1 < 0
4603 || eap->line2 < 0
4604 || eap->line1 > eap->line2
4605 || ((eap->argt & RANGE)
4606 && !(eap->argt & NOTADR)
4607 && eap->line2 > curbuf->b_ml.ml_line_count
4608#ifdef FEAT_DIFF
4609 + (eap->cmdidx == CMD_diffget)
4610#endif
4611 ))
4612 return (char_u *)_(e_invrange);
4613 return NULL;
4614}
4615
4616/*
4617 * Correct the range for zero line number, if required.
4618 */
4619 static void
4620correct_range(eap)
4621 exarg_T *eap;
4622{
4623 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4624 {
4625 if (eap->line1 == 0)
4626 eap->line1 = 1;
4627 if (eap->line2 == 0)
4628 eap->line2 = 1;
4629 }
4630}
4631
Bram Moolenaar748bf032005-02-02 23:04:36 +00004632#ifdef FEAT_QUICKFIX
4633static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4634
4635/*
4636 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4637 * pattern. Otherwise return eap->arg.
4638 */
4639 static char_u *
4640skip_grep_pat(eap)
4641 exarg_T *eap;
4642{
4643 char_u *p = eap->arg;
4644
Bram Moolenaara37420f2006-02-04 22:37:47 +00004645 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4646 || eap->cmdidx == CMD_vimgrepadd
4647 || eap->cmdidx == CMD_lvimgrepadd
4648 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004649 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004650 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004651 if (p == NULL)
4652 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004653 }
4654 return p;
4655}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004656
4657/*
4658 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4659 * in the command line, so that things like % get expanded.
4660 */
4661 static char_u *
4662replace_makeprg(eap, p, cmdlinep)
4663 exarg_T *eap;
4664 char_u *p;
4665 char_u **cmdlinep;
4666{
4667 char_u *new_cmdline;
4668 char_u *program;
4669 char_u *pos;
4670 char_u *ptr;
4671 int len;
4672 int i;
4673
4674 /*
4675 * Don't do it when ":vimgrep" is used for ":grep".
4676 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004677 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4678 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4679 || eap->cmdidx == CMD_grepadd
4680 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004681 && !grep_internal(eap->cmdidx))
4682 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004683 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4684 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004685 {
4686 if (*curbuf->b_p_gp == NUL)
4687 program = p_gp;
4688 else
4689 program = curbuf->b_p_gp;
4690 }
4691 else
4692 {
4693 if (*curbuf->b_p_mp == NUL)
4694 program = p_mp;
4695 else
4696 program = curbuf->b_p_mp;
4697 }
4698
4699 p = skipwhite(p);
4700
4701 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4702 {
4703 /* replace $* by given arguments */
4704 i = 1;
4705 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4706 ++i;
4707 len = (int)STRLEN(p);
4708 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4709 if (new_cmdline == NULL)
4710 return NULL; /* out of memory */
4711 ptr = new_cmdline;
4712 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4713 {
4714 i = (int)(pos - program);
4715 STRNCPY(ptr, program, i);
4716 STRCPY(ptr += i, p);
4717 ptr += len;
4718 program = pos + 2;
4719 }
4720 STRCPY(ptr, program);
4721 }
4722 else
4723 {
4724 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4725 if (new_cmdline == NULL)
4726 return NULL; /* out of memory */
4727 STRCPY(new_cmdline, program);
4728 STRCAT(new_cmdline, " ");
4729 STRCAT(new_cmdline, p);
4730 }
4731 msg_make(p);
4732
4733 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4734 vim_free(*cmdlinep);
4735 *cmdlinep = new_cmdline;
4736 p = new_cmdline;
4737 }
4738 return p;
4739}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004740#endif
4741
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742/*
4743 * Expand file name in Ex command argument.
4744 * Return FAIL for failure, OK otherwise.
4745 */
4746 int
4747expand_filename(eap, cmdlinep, errormsgp)
4748 exarg_T *eap;
4749 char_u **cmdlinep;
4750 char_u **errormsgp;
4751{
4752 int has_wildcards; /* need to expand wildcards */
4753 char_u *repl;
4754 int srclen;
4755 char_u *p;
4756 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004757 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758
Bram Moolenaar748bf032005-02-02 23:04:36 +00004759#ifdef FEAT_QUICKFIX
4760 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4761 p = skip_grep_pat(eap);
4762#else
4763 p = eap->arg;
4764#endif
4765
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 /*
4767 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4768 * the file name contains a wildcard it should not cause expanding.
4769 * (it will be expanded anyway if there is a wildcard before replacing).
4770 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004771 has_wildcards = mch_has_wildcard(p);
4772 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004774#ifdef FEAT_EVAL
4775 /* Skip over `=expr`, wildcards in it are not expanded. */
4776 if (p[0] == '`' && p[1] == '=')
4777 {
4778 p += 2;
4779 (void)skip_expr(&p);
4780 if (*p == '`')
4781 ++p;
4782 continue;
4783 }
4784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 /*
4786 * Quick check if this cannot be the start of a special string.
4787 * Also removes backslash before '%', '#' and '<'.
4788 */
4789 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4790 {
4791 ++p;
4792 continue;
4793 }
4794
4795 /*
4796 * Try to find a match at this position.
4797 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004798 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4799 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 if (*errormsgp != NULL) /* error detected */
4801 return FAIL;
4802 if (repl == NULL) /* no match found */
4803 {
4804 p += srclen;
4805 continue;
4806 }
4807
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004808 /* Wildcards won't be expanded below, the replacement is taken
4809 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4810 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4811 {
4812 char_u *l = repl;
4813
4814 repl = expand_env_save(repl);
4815 vim_free(l);
4816 }
4817
Bram Moolenaar63b92542007-03-27 14:57:09 +00004818 /* Need to escape white space et al. with a backslash.
4819 * Don't do this for:
4820 * - replacement that already has been escaped: "##"
4821 * - shell commands (may have to use quotes instead).
4822 * - non-unix systems when there is a single argument (spaces don't
4823 * separate arguments then).
4824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004826 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 && eap->cmdidx != CMD_bang
4828 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004829 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004831 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004833 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834#ifndef UNIX
4835 && !(eap->argt & NOSPC)
4836#endif
4837 )
4838 {
4839 char_u *l;
4840#ifdef BACKSLASH_IN_FILENAME
4841 /* Don't escape a backslash here, because rem_backslash() doesn't
4842 * remove it later. */
4843 static char_u *nobslash = (char_u *)" \t\"|";
4844# define ESCAPE_CHARS nobslash
4845#else
4846# define ESCAPE_CHARS escape_chars
4847#endif
4848
4849 for (l = repl; *l; ++l)
4850 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4851 {
4852 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4853 if (l != NULL)
4854 {
4855 vim_free(repl);
4856 repl = l;
4857 }
4858 break;
4859 }
4860 }
4861
4862 /* For a shell command a '!' must be escaped. */
4863 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004864 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 {
4866 char_u *l;
4867
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004868 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 if (l != NULL)
4870 {
4871 vim_free(repl);
4872 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
4874 }
4875
4876 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4877 vim_free(repl);
4878 if (p == NULL)
4879 return FAIL;
4880 }
4881
4882 /*
4883 * One file argument: Expand wildcards.
4884 * Don't do this with ":r !command" or ":w !command".
4885 */
4886 if ((eap->argt & NOSPC) && !eap->usefilter)
4887 {
4888 /*
4889 * May do this twice:
4890 * 1. Replace environment variables.
4891 * 2. Replace any other wildcards, remove backslashes.
4892 */
4893 for (n = 1; n <= 2; ++n)
4894 {
4895 if (n == 2)
4896 {
4897#ifdef UNIX
4898 /*
4899 * Only for Unix we check for more than one file name.
4900 * For other systems spaces are considered to be part
4901 * of the file name.
4902 * Only check here if there is no wildcard, otherwise
4903 * ExpandOne() will check for errors. This allows
4904 * ":e `ls ve*.c`" on Unix.
4905 */
4906 if (!has_wildcards)
4907 for (p = eap->arg; *p; ++p)
4908 {
4909 /* skip escaped characters */
4910 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4911 ++p;
4912 else if (vim_iswhite(*p))
4913 {
4914 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4915 return FAIL;
4916 }
4917 }
4918#endif
4919
4920 /*
4921 * Halve the number of backslashes (this is Vi compatible).
4922 * For Unix and OS/2, when wildcards are expanded, this is
4923 * done by ExpandOne() below.
4924 */
4925#if defined(UNIX) || defined(OS2)
4926 if (!has_wildcards)
4927#endif
4928 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 }
4930
4931 if (has_wildcards)
4932 {
4933 if (n == 1)
4934 {
4935 /*
4936 * First loop: May expand environment variables. This
4937 * can be done much faster with expand_env() than with
4938 * something else (e.g., calling a shell).
4939 * After expanding environment variables, check again
4940 * if there are still wildcards present.
4941 */
4942 if (vim_strchr(eap->arg, '$') != NULL
4943 || vim_strchr(eap->arg, '~') != NULL)
4944 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004945 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004946 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 has_wildcards = mch_has_wildcard(NameBuff);
4948 p = NameBuff;
4949 }
4950 else
4951 p = NULL;
4952 }
4953 else /* n == 2 */
4954 {
4955 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004956 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957
4958 ExpandInit(&xpc);
4959 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004960 if (p_wic)
4961 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004963 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 if (p == NULL)
4965 return FAIL;
4966 }
4967 if (p != NULL)
4968 {
4969 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4970 p, cmdlinep);
4971 if (n == 2) /* p came from ExpandOne() */
4972 vim_free(p);
4973 }
4974 }
4975 }
4976 }
4977 return OK;
4978}
4979
4980/*
4981 * Replace part of the command line, keeping eap->cmd, eap->arg and
4982 * eap->nextcmd correct.
4983 * "src" points to the part that is to be replaced, of length "srclen".
4984 * "repl" is the replacement string.
4985 * Returns a pointer to the character after the replaced string.
4986 * Returns NULL for failure.
4987 */
4988 static char_u *
4989repl_cmdline(eap, src, srclen, repl, cmdlinep)
4990 exarg_T *eap;
4991 char_u *src;
4992 int srclen;
4993 char_u *repl;
4994 char_u **cmdlinep;
4995{
4996 int len;
4997 int i;
4998 char_u *new_cmdline;
4999
5000 /*
5001 * The new command line is build in new_cmdline[].
5002 * First allocate it.
5003 * Careful: a "+cmd" argument may have been NUL terminated.
5004 */
5005 len = (int)STRLEN(repl);
5006 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005007 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5009 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5010 return NULL; /* out of memory! */
5011
5012 /*
5013 * Copy the stuff before the expanded part.
5014 * Copy the expanded stuff.
5015 * Copy what came after the expanded part.
5016 * Copy the next commands, if there are any.
5017 */
5018 i = (int)(src - *cmdlinep); /* length of part before match */
5019 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005020
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 mch_memmove(new_cmdline + i, repl, (size_t)len);
5022 i += len; /* remember the end of the string */
5023 STRCPY(new_cmdline + i, src + srclen);
5024 src = new_cmdline + i; /* remember where to continue */
5025
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005026 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 {
5028 i = (int)STRLEN(new_cmdline) + 1;
5029 STRCPY(new_cmdline + i, eap->nextcmd);
5030 eap->nextcmd = new_cmdline + i;
5031 }
5032 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5033 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5034 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5035 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5036 vim_free(*cmdlinep);
5037 *cmdlinep = new_cmdline;
5038
5039 return src;
5040}
5041
5042/*
5043 * Check for '|' to separate commands and '"' to start comments.
5044 */
5045 void
5046separate_nextcmd(eap)
5047 exarg_T *eap;
5048{
5049 char_u *p;
5050
Bram Moolenaar86b68352004-12-27 21:59:20 +00005051#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005052 p = skip_grep_pat(eap);
5053#else
5054 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005055#endif
5056
5057 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
5059 if (*p == Ctrl_V)
5060 {
5061 if (eap->argt & (USECTRLV | XFILE))
5062 ++p; /* skip CTRL-V and next char */
5063 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005064 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005065 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 if (*p == NUL) /* stop at NUL after CTRL-V */
5067 break;
5068 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005069
5070#ifdef FEAT_EVAL
5071 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005072 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005073 {
5074 p += 2;
5075 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005076 }
5077#endif
5078
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 /* Check for '"': start of comment or '|': next command */
5080 /* :@" and :*" do not start a comment!
5081 * :redir @" doesn't either. */
5082 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5083 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5084 || p != eap->arg)
5085 && (eap->cmdidx != CMD_redir
5086 || p != eap->arg + 1 || p[-1] != '@'))
5087 || *p == '|' || *p == '\n')
5088 {
5089 /*
5090 * We remove the '\' before the '|', unless USECTRLV is used
5091 * AND 'b' is present in 'cpoptions'.
5092 */
5093 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5094 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5095 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005096 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 --p;
5098 }
5099 else
5100 {
5101 eap->nextcmd = check_nextcmd(p);
5102 *p = NUL;
5103 break;
5104 }
5105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005107
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5109 del_trailing_spaces(eap->arg);
5110}
5111
5112/*
5113 * get + command from ex argument
5114 */
5115 static char_u *
5116getargcmd(argp)
5117 char_u **argp;
5118{
5119 char_u *arg = *argp;
5120 char_u *command = NULL;
5121
5122 if (*arg == '+') /* +[command] */
5123 {
5124 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005125 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 command = dollar_command;
5127 else
5128 {
5129 command = arg;
5130 arg = skip_cmd_arg(command, TRUE);
5131 if (*arg != NUL)
5132 *arg++ = NUL; /* terminate command with NUL */
5133 }
5134
5135 arg = skipwhite(arg); /* skip over spaces */
5136 *argp = arg;
5137 }
5138 return command;
5139}
5140
5141/*
5142 * Find end of "+command" argument. Skip over "\ " and "\\".
5143 */
5144 static char_u *
5145skip_cmd_arg(p, rembs)
5146 char_u *p;
5147 int rembs; /* TRUE to halve the number of backslashes */
5148{
5149 while (*p && !vim_isspace(*p))
5150 {
5151 if (*p == '\\' && p[1] != NUL)
5152 {
5153 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005154 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 else
5156 ++p;
5157 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005158 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 }
5160 return p;
5161}
5162
5163/*
5164 * Get "++opt=arg" argument.
5165 * Return FAIL or OK.
5166 */
5167 static int
5168getargopt(eap)
5169 exarg_T *eap;
5170{
5171 char_u *arg = eap->arg + 2;
5172 int *pp = NULL;
5173#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005174 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 char_u *p;
5176#endif
5177
5178 /* ":edit ++[no]bin[ary] file" */
5179 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5180 {
5181 if (*arg == 'n')
5182 {
5183 arg += 2;
5184 eap->force_bin = FORCE_NOBIN;
5185 }
5186 else
5187 eap->force_bin = FORCE_BIN;
5188 if (!checkforcmd(&arg, "binary", 3))
5189 return FAIL;
5190 eap->arg = skipwhite(arg);
5191 return OK;
5192 }
5193
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005194 /* ":read ++edit file" */
5195 if (STRNCMP(arg, "edit", 4) == 0)
5196 {
5197 eap->read_edit = TRUE;
5198 eap->arg = skipwhite(arg + 4);
5199 return OK;
5200 }
5201
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 if (STRNCMP(arg, "ff", 2) == 0)
5203 {
5204 arg += 2;
5205 pp = &eap->force_ff;
5206 }
5207 else if (STRNCMP(arg, "fileformat", 10) == 0)
5208 {
5209 arg += 10;
5210 pp = &eap->force_ff;
5211 }
5212#ifdef FEAT_MBYTE
5213 else if (STRNCMP(arg, "enc", 3) == 0)
5214 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005215 if (STRNCMP(arg, "encoding", 8) == 0)
5216 arg += 8;
5217 else
5218 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 pp = &eap->force_enc;
5220 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005221 else if (STRNCMP(arg, "bad", 3) == 0)
5222 {
5223 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005224 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226#endif
5227
5228 if (pp == NULL || *arg != '=')
5229 return FAIL;
5230
5231 ++arg;
5232 *pp = (int)(arg - eap->cmd);
5233 arg = skip_cmd_arg(arg, FALSE);
5234 eap->arg = skipwhite(arg);
5235 *arg = NUL;
5236
5237#ifdef FEAT_MBYTE
5238 if (pp == &eap->force_ff)
5239 {
5240#endif
5241 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5242 return FAIL;
5243#ifdef FEAT_MBYTE
5244 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005245 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 {
5247 /* Make 'fileencoding' lower case. */
5248 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5249 *p = TOLOWER_ASC(*p);
5250 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005251 else
5252 {
5253 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5254 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005255 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005256 if (STRICMP(p, "keep") == 0)
5257 eap->bad_char = BAD_KEEP;
5258 else if (STRICMP(p, "drop") == 0)
5259 eap->bad_char = BAD_DROP;
5260 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5261 eap->bad_char = *p;
5262 else
5263 return FAIL;
5264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265#endif
5266
5267 return OK;
5268}
5269
5270/*
5271 * ":abbreviate" and friends.
5272 */
5273 static void
5274ex_abbreviate(eap)
5275 exarg_T *eap;
5276{
5277 do_exmap(eap, TRUE); /* almost the same as mapping */
5278}
5279
5280/*
5281 * ":map" and friends.
5282 */
5283 static void
5284ex_map(eap)
5285 exarg_T *eap;
5286{
5287 /*
5288 * If we are sourcing .exrc or .vimrc in current directory we
5289 * print the mappings for security reasons.
5290 */
5291 if (secure)
5292 {
5293 secure = 2;
5294 msg_outtrans(eap->cmd);
5295 msg_putchar('\n');
5296 }
5297 do_exmap(eap, FALSE);
5298}
5299
5300/*
5301 * ":unmap" and friends.
5302 */
5303 static void
5304ex_unmap(eap)
5305 exarg_T *eap;
5306{
5307 do_exmap(eap, FALSE);
5308}
5309
5310/*
5311 * ":mapclear" and friends.
5312 */
5313 static void
5314ex_mapclear(eap)
5315 exarg_T *eap;
5316{
5317 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5318}
5319
5320/*
5321 * ":abclear" and friends.
5322 */
5323 static void
5324ex_abclear(eap)
5325 exarg_T *eap;
5326{
5327 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5328}
5329
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005330#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 static void
5332ex_autocmd(eap)
5333 exarg_T *eap;
5334{
5335 /*
5336 * Disallow auto commands from .exrc and .vimrc in current
5337 * directory for security reasons.
5338 */
5339 if (secure)
5340 {
5341 secure = 2;
5342 eap->errmsg = e_curdir;
5343 }
5344 else if (eap->cmdidx == CMD_autocmd)
5345 do_autocmd(eap->arg, eap->forceit);
5346 else
5347 do_augroup(eap->arg, eap->forceit);
5348}
5349
5350/*
5351 * ":doautocmd": Apply the automatic commands to the current buffer.
5352 */
5353 static void
5354ex_doautocmd(eap)
5355 exarg_T *eap;
5356{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005357 char_u *arg = eap->arg;
5358 int call_do_modelines = check_nomodeline(&arg);
5359
5360 (void)do_doautocmd(arg, TRUE);
5361 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5362 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363}
5364#endif
5365
5366#ifdef FEAT_LISTCMDS
5367/*
5368 * :[N]bunload[!] [N] [bufname] unload buffer
5369 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5370 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5371 */
5372 static void
5373ex_bunload(eap)
5374 exarg_T *eap;
5375{
5376 eap->errmsg = do_bufdel(
5377 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5378 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5379 : DOBUF_UNLOAD, eap->arg,
5380 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5381}
5382
5383/*
5384 * :[N]buffer [N] to buffer N
5385 * :[N]sbuffer [N] to buffer N
5386 */
5387 static void
5388ex_buffer(eap)
5389 exarg_T *eap;
5390{
5391 if (*eap->arg)
5392 eap->errmsg = e_trailing;
5393 else
5394 {
5395 if (eap->addr_count == 0) /* default is current buffer */
5396 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5397 else
5398 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005399 if (eap->do_ecmd_cmd != NULL)
5400 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 }
5402}
5403
5404/*
5405 * :[N]bmodified [N] to next mod. buffer
5406 * :[N]sbmodified [N] to next mod. buffer
5407 */
5408 static void
5409ex_bmodified(eap)
5410 exarg_T *eap;
5411{
5412 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005413 if (eap->do_ecmd_cmd != NULL)
5414 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415}
5416
5417/*
5418 * :[N]bnext [N] to next buffer
5419 * :[N]sbnext [N] split and to next buffer
5420 */
5421 static void
5422ex_bnext(eap)
5423 exarg_T *eap;
5424{
5425 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005426 if (eap->do_ecmd_cmd != NULL)
5427 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428}
5429
5430/*
5431 * :[N]bNext [N] to previous buffer
5432 * :[N]bprevious [N] to previous buffer
5433 * :[N]sbNext [N] split and to previous buffer
5434 * :[N]sbprevious [N] split and to previous buffer
5435 */
5436 static void
5437ex_bprevious(eap)
5438 exarg_T *eap;
5439{
5440 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005441 if (eap->do_ecmd_cmd != NULL)
5442 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443}
5444
5445/*
5446 * :brewind to first buffer
5447 * :bfirst to first buffer
5448 * :sbrewind split and to first buffer
5449 * :sbfirst split and to first buffer
5450 */
5451 static void
5452ex_brewind(eap)
5453 exarg_T *eap;
5454{
5455 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005456 if (eap->do_ecmd_cmd != NULL)
5457 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458}
5459
5460/*
5461 * :blast to last buffer
5462 * :sblast split and to last buffer
5463 */
5464 static void
5465ex_blast(eap)
5466 exarg_T *eap;
5467{
5468 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005469 if (eap->do_ecmd_cmd != NULL)
5470 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471}
5472#endif
5473
5474 int
5475ends_excmd(c)
5476 int c;
5477{
5478 return (c == NUL || c == '|' || c == '"' || c == '\n');
5479}
5480
5481#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5482 || defined(PROTO)
5483/*
5484 * Return the next command, after the first '|' or '\n'.
5485 * Return NULL if not found.
5486 */
5487 char_u *
5488find_nextcmd(p)
5489 char_u *p;
5490{
5491 while (*p != '|' && *p != '\n')
5492 {
5493 if (*p == NUL)
5494 return NULL;
5495 ++p;
5496 }
5497 return (p + 1);
5498}
5499#endif
5500
5501/*
5502 * Check if *p is a separator between Ex commands.
5503 * Return NULL if it isn't, (p + 1) if it is.
5504 */
5505 char_u *
5506check_nextcmd(p)
5507 char_u *p;
5508{
5509 p = skipwhite(p);
5510 if (*p == '|' || *p == '\n')
5511 return (p + 1);
5512 else
5513 return NULL;
5514}
5515
5516/*
5517 * - if there are more files to edit
5518 * - and this is the last window
5519 * - and forceit not used
5520 * - and not repeated twice on a row
5521 * return FAIL and give error message if 'message' TRUE
5522 * return OK otherwise
5523 */
5524 static int
5525check_more(message, forceit)
5526 int message; /* when FALSE check only, no messages */
5527 int forceit;
5528{
5529 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5530
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005531 if (!forceit && only_one_window()
5532 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 {
5534 if (message)
5535 {
5536#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5537 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5538 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005539 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540
5541 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005542 vim_strncpy(buff,
5543 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005544 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005546 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 _("%d more files to edit. Quit anyway?"), n);
5548 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5549 return OK;
5550 return FAIL;
5551 }
5552#endif
5553 if (n == 1)
5554 EMSG(_("E173: 1 more file to edit"));
5555 else
5556 EMSGN(_("E173: %ld more files to edit"), n);
5557 quitmore = 2; /* next try to quit is allowed */
5558 }
5559 return FAIL;
5560 }
5561 return OK;
5562}
5563
5564#ifdef FEAT_CMDL_COMPL
5565/*
5566 * Function given to ExpandGeneric() to obtain the list of command names.
5567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 char_u *
5569get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005570 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 int idx;
5572{
5573 if (idx >= (int)CMD_SIZE)
5574# ifdef FEAT_USR_CMDS
5575 return get_user_command_name(idx);
5576# else
5577 return NULL;
5578# endif
5579 return cmdnames[idx].cmd_name;
5580}
5581#endif
5582
5583#if defined(FEAT_USR_CMDS) || defined(PROTO)
5584static 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));
5585static void uc_list __ARGS((char_u *name, size_t name_len));
5586static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5587static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5588static 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));
5589
5590 static int
5591uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5592 char_u *name;
5593 size_t name_len;
5594 char_u *rep;
5595 long argt;
5596 long def;
5597 int flags;
5598 int compl;
5599 char_u *compl_arg;
5600 int force;
5601{
5602 ucmd_T *cmd = NULL;
5603 char_u *p;
5604 int i;
5605 int cmp = 1;
5606 char_u *rep_buf = NULL;
5607 garray_T *gap;
5608
Bram Moolenaar9c102382006-05-03 21:26:49 +00005609 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 if (rep_buf == NULL)
5611 {
5612 /* Can't replace termcodes - try using the string as is */
5613 rep_buf = vim_strsave(rep);
5614
5615 /* Give up if out of memory */
5616 if (rep_buf == NULL)
5617 return FAIL;
5618 }
5619
5620 /* get address of growarray: global or in curbuf */
5621 if (flags & UC_BUFFER)
5622 {
5623 gap = &curbuf->b_ucmds;
5624 if (gap->ga_itemsize == 0)
5625 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5626 }
5627 else
5628 gap = &ucmds;
5629
5630 /* Search for the command in the already defined commands. */
5631 for (i = 0; i < gap->ga_len; ++i)
5632 {
5633 size_t len;
5634
5635 cmd = USER_CMD_GA(gap, i);
5636 len = STRLEN(cmd->uc_name);
5637 cmp = STRNCMP(name, cmd->uc_name, name_len);
5638 if (cmp == 0)
5639 {
5640 if (name_len < len)
5641 cmp = -1;
5642 else if (name_len > len)
5643 cmp = 1;
5644 }
5645
5646 if (cmp == 0)
5647 {
5648 if (!force)
5649 {
5650 EMSG(_("E174: Command already exists: add ! to replace it"));
5651 goto fail;
5652 }
5653
5654 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005655 cmd->uc_rep = NULL;
5656#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5657 vim_free(cmd->uc_compl_arg);
5658 cmd->uc_compl_arg = NULL;
5659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 break;
5661 }
5662
5663 /* Stop as soon as we pass the name to add */
5664 if (cmp < 0)
5665 break;
5666 }
5667
5668 /* Extend the array unless we're replacing an existing command */
5669 if (cmp != 0)
5670 {
5671 if (ga_grow(gap, 1) != OK)
5672 goto fail;
5673 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5674 goto fail;
5675
5676 cmd = USER_CMD_GA(gap, i);
5677 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5678
5679 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680
5681 cmd->uc_name = p;
5682 }
5683
5684 cmd->uc_rep = rep_buf;
5685 cmd->uc_argt = argt;
5686 cmd->uc_def = def;
5687 cmd->uc_compl = compl;
5688#ifdef FEAT_EVAL
5689 cmd->uc_scriptID = current_SID;
5690# ifdef FEAT_CMDL_COMPL
5691 cmd->uc_compl_arg = compl_arg;
5692# endif
5693#endif
5694
5695 return OK;
5696
5697fail:
5698 vim_free(rep_buf);
5699#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5700 vim_free(compl_arg);
5701#endif
5702 return FAIL;
5703}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005706#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707/*
5708 * List of names for completion for ":command" with the EXPAND_ flag.
5709 * Must be alphabetical for completion.
5710 */
5711static struct
5712{
5713 int expand;
5714 char *name;
5715} command_complete[] =
5716{
5717 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005718 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005720 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005722 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005723#if defined(FEAT_CSCOPE)
5724 {EXPAND_CSCOPE, "cscope"},
5725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5727 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005728 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729#endif
5730 {EXPAND_DIRECTORIES, "dir"},
5731 {EXPAND_ENV_VARS, "environment"},
5732 {EXPAND_EVENTS, "event"},
5733 {EXPAND_EXPRESSION, "expression"},
5734 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005735 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005736 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 {EXPAND_FUNCTIONS, "function"},
5738 {EXPAND_HELP, "help"},
5739 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005740#if defined(FEAT_CMDHIST)
5741 {EXPAND_HISTORY, "history"},
5742#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005743#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005744 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005745 {EXPAND_LOCALES, "locale"},
5746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 {EXPAND_MAPPINGS, "mapping"},
5748 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005749 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005750#if defined(FEAT_PROFILE)
5751 {EXPAND_SYNTIME, "syntime"},
5752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005754 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005755#if defined(FEAT_SIGNS)
5756 {EXPAND_SIGN, "sign"},
5757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 {EXPAND_TAGS, "tag"},
5759 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005760 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 {EXPAND_USER_VARS, "var"},
5762 {0, NULL}
5763};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005766#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 static void
5768uc_list(name, name_len)
5769 char_u *name;
5770 size_t name_len;
5771{
5772 int i, j;
5773 int found = FALSE;
5774 ucmd_T *cmd;
5775 int len;
5776 long a;
5777 garray_T *gap;
5778
5779 gap = &curbuf->b_ucmds;
5780 for (;;)
5781 {
5782 for (i = 0; i < gap->ga_len; ++i)
5783 {
5784 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005785 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786
5787 /* Skip commands which don't match the requested prefix */
5788 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5789 continue;
5790
5791 /* Put out the title first time */
5792 if (!found)
5793 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5794 found = TRUE;
5795 msg_putchar('\n');
5796 if (got_int)
5797 break;
5798
5799 /* Special cases */
5800 msg_putchar(a & BANG ? '!' : ' ');
5801 msg_putchar(a & REGSTR ? '"' : ' ');
5802 msg_putchar(gap != &ucmds ? 'b' : ' ');
5803 msg_putchar(' ');
5804
5805 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5806 len = (int)STRLEN(cmd->uc_name) + 4;
5807
5808 do {
5809 msg_putchar(' ');
5810 ++len;
5811 } while (len < 16);
5812
5813 len = 0;
5814
5815 /* Arguments */
5816 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5817 {
5818 case 0: IObuff[len++] = '0'; break;
5819 case (EXTRA): IObuff[len++] = '*'; break;
5820 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5821 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5822 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5823 }
5824
5825 do {
5826 IObuff[len++] = ' ';
5827 } while (len < 5);
5828
5829 /* Range */
5830 if (a & (RANGE|COUNT))
5831 {
5832 if (a & COUNT)
5833 {
5834 /* -count=N */
5835 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5836 len += (int)STRLEN(IObuff + len);
5837 }
5838 else if (a & DFLALL)
5839 IObuff[len++] = '%';
5840 else if (cmd->uc_def >= 0)
5841 {
5842 /* -range=N */
5843 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5844 len += (int)STRLEN(IObuff + len);
5845 }
5846 else
5847 IObuff[len++] = '.';
5848 }
5849
5850 do {
5851 IObuff[len++] = ' ';
5852 } while (len < 11);
5853
5854 /* Completion */
5855 for (j = 0; command_complete[j].expand != 0; ++j)
5856 if (command_complete[j].expand == cmd->uc_compl)
5857 {
5858 STRCPY(IObuff + len, command_complete[j].name);
5859 len += (int)STRLEN(IObuff + len);
5860 break;
5861 }
5862
5863 do {
5864 IObuff[len++] = ' ';
5865 } while (len < 21);
5866
5867 IObuff[len] = '\0';
5868 msg_outtrans(IObuff);
5869
5870 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005871#ifdef FEAT_EVAL
5872 if (p_verbose > 0)
5873 last_set_msg(cmd->uc_scriptID);
5874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 out_flush();
5876 ui_breakcheck();
5877 if (got_int)
5878 break;
5879 }
5880 if (gap == &ucmds || i < gap->ga_len)
5881 break;
5882 gap = &ucmds;
5883 }
5884
5885 if (!found)
5886 MSG(_("No user-defined commands found"));
5887}
5888
5889 static char_u *
5890uc_fun_cmd()
5891{
5892 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5893 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5894 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5895 0xb9, 0x7f, 0};
5896 int i;
5897
5898 for (i = 0; fcmd[i]; ++i)
5899 IObuff[i] = fcmd[i] - 0x40;
5900 IObuff[i] = 0;
5901 return IObuff;
5902}
5903
5904 static int
5905uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5906 char_u *attr;
5907 size_t len;
5908 long *argt;
5909 long *def;
5910 int *flags;
5911 int *compl;
5912 char_u **compl_arg;
5913{
5914 char_u *p;
5915
5916 if (len == 0)
5917 {
5918 EMSG(_("E175: No attribute specified"));
5919 return FAIL;
5920 }
5921
5922 /* First, try the simple attributes (no arguments) */
5923 if (STRNICMP(attr, "bang", len) == 0)
5924 *argt |= BANG;
5925 else if (STRNICMP(attr, "buffer", len) == 0)
5926 *flags |= UC_BUFFER;
5927 else if (STRNICMP(attr, "register", len) == 0)
5928 *argt |= REGSTR;
5929 else if (STRNICMP(attr, "bar", len) == 0)
5930 *argt |= TRLBAR;
5931 else
5932 {
5933 int i;
5934 char_u *val = NULL;
5935 size_t vallen = 0;
5936 size_t attrlen = len;
5937
5938 /* Look for the attribute name - which is the part before any '=' */
5939 for (i = 0; i < (int)len; ++i)
5940 {
5941 if (attr[i] == '=')
5942 {
5943 val = &attr[i + 1];
5944 vallen = len - i - 1;
5945 attrlen = i;
5946 break;
5947 }
5948 }
5949
5950 if (STRNICMP(attr, "nargs", attrlen) == 0)
5951 {
5952 if (vallen == 1)
5953 {
5954 if (*val == '0')
5955 /* Do nothing - this is the default */;
5956 else if (*val == '1')
5957 *argt |= (EXTRA | NOSPC | NEEDARG);
5958 else if (*val == '*')
5959 *argt |= EXTRA;
5960 else if (*val == '?')
5961 *argt |= (EXTRA | NOSPC);
5962 else if (*val == '+')
5963 *argt |= (EXTRA | NEEDARG);
5964 else
5965 goto wrong_nargs;
5966 }
5967 else
5968 {
5969wrong_nargs:
5970 EMSG(_("E176: Invalid number of arguments"));
5971 return FAIL;
5972 }
5973 }
5974 else if (STRNICMP(attr, "range", attrlen) == 0)
5975 {
5976 *argt |= RANGE;
5977 if (vallen == 1 && *val == '%')
5978 *argt |= DFLALL;
5979 else if (val != NULL)
5980 {
5981 p = val;
5982 if (*def >= 0)
5983 {
5984two_count:
5985 EMSG(_("E177: Count cannot be specified twice"));
5986 return FAIL;
5987 }
5988
5989 *def = getdigits(&p);
5990 *argt |= (ZEROR | NOTADR);
5991
5992 if (p != val + vallen || vallen == 0)
5993 {
5994invalid_count:
5995 EMSG(_("E178: Invalid default value for count"));
5996 return FAIL;
5997 }
5998 }
5999 }
6000 else if (STRNICMP(attr, "count", attrlen) == 0)
6001 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006002 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003
6004 if (val != NULL)
6005 {
6006 p = val;
6007 if (*def >= 0)
6008 goto two_count;
6009
6010 *def = getdigits(&p);
6011
6012 if (p != val + vallen)
6013 goto invalid_count;
6014 }
6015
6016 if (*def < 0)
6017 *def = 0;
6018 }
6019 else if (STRNICMP(attr, "complete", attrlen) == 0)
6020 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 if (val == NULL)
6022 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006023 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 return FAIL;
6025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006027 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6028 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 }
6031 else
6032 {
6033 char_u ch = attr[len];
6034 attr[len] = '\0';
6035 EMSG2(_("E181: Invalid attribute: %s"), attr);
6036 attr[len] = ch;
6037 return FAIL;
6038 }
6039 }
6040
6041 return OK;
6042}
6043
Bram Moolenaara850a712009-01-28 14:42:59 +00006044/*
6045 * ":command ..."
6046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 static void
6048ex_command(eap)
6049 exarg_T *eap;
6050{
6051 char_u *name;
6052 char_u *end;
6053 char_u *p;
6054 long argt = 0;
6055 long def = -1;
6056 int flags = 0;
6057 int compl = EXPAND_NOTHING;
6058 char_u *compl_arg = NULL;
6059 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006060 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061
6062 p = eap->arg;
6063
6064 /* Check for attributes */
6065 while (*p == '-')
6066 {
6067 ++p;
6068 end = skiptowhite(p);
6069 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
6070 == FAIL)
6071 return;
6072 p = skipwhite(end);
6073 }
6074
6075 /* Get the name (if any) and skip to the following argument */
6076 name = p;
6077 if (ASCII_ISALPHA(*p))
6078 while (ASCII_ISALNUM(*p))
6079 ++p;
6080 if (!ends_excmd(*p) && !vim_iswhite(*p))
6081 {
6082 EMSG(_("E182: Invalid command name"));
6083 return;
6084 }
6085 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006086 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087
6088 /* If there is nothing after the name, and no attributes were specified,
6089 * we are listing commands
6090 */
6091 p = skipwhite(end);
6092 if (!has_attr && ends_excmd(*p))
6093 {
6094 uc_list(name, end - name);
6095 }
6096 else if (!ASCII_ISUPPER(*name))
6097 {
6098 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6099 return;
6100 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006101 else if ((name_len == 1 && *name == 'X')
6102 || (name_len <= 4
6103 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6104 {
6105 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6106 return;
6107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 else
6109 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
6110 eap->forceit);
6111}
6112
6113/*
6114 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 * Clear all user commands, global and for current buffer.
6116 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006117 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006119 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120{
6121 uc_clear(&ucmds);
6122 uc_clear(&curbuf->b_ucmds);
6123}
6124
6125/*
6126 * Clear all user commands for "gap".
6127 */
6128 void
6129uc_clear(gap)
6130 garray_T *gap;
6131{
6132 int i;
6133 ucmd_T *cmd;
6134
6135 for (i = 0; i < gap->ga_len; ++i)
6136 {
6137 cmd = USER_CMD_GA(gap, i);
6138 vim_free(cmd->uc_name);
6139 vim_free(cmd->uc_rep);
6140# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6141 vim_free(cmd->uc_compl_arg);
6142# endif
6143 }
6144 ga_clear(gap);
6145}
6146
6147 static void
6148ex_delcommand(eap)
6149 exarg_T *eap;
6150{
6151 int i = 0;
6152 ucmd_T *cmd = NULL;
6153 int cmp = -1;
6154 garray_T *gap;
6155
6156 gap = &curbuf->b_ucmds;
6157 for (;;)
6158 {
6159 for (i = 0; i < gap->ga_len; ++i)
6160 {
6161 cmd = USER_CMD_GA(gap, i);
6162 cmp = STRCMP(eap->arg, cmd->uc_name);
6163 if (cmp <= 0)
6164 break;
6165 }
6166 if (gap == &ucmds || cmp == 0)
6167 break;
6168 gap = &ucmds;
6169 }
6170
6171 if (cmp != 0)
6172 {
6173 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6174 return;
6175 }
6176
6177 vim_free(cmd->uc_name);
6178 vim_free(cmd->uc_rep);
6179# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6180 vim_free(cmd->uc_compl_arg);
6181# endif
6182
6183 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184
6185 if (i < gap->ga_len)
6186 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6187}
6188
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006189/*
6190 * split and quote args for <f-args>
6191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 static char_u *
6193uc_split_args(arg, lenp)
6194 char_u *arg;
6195 size_t *lenp;
6196{
6197 char_u *buf;
6198 char_u *p;
6199 char_u *q;
6200 int len;
6201
6202 /* Precalculate length */
6203 p = arg;
6204 len = 2; /* Initial and final quotes */
6205
6206 while (*p)
6207 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006208 if (p[0] == '\\' && p[1] == '\\')
6209 {
6210 len += 2;
6211 p += 2;
6212 }
6213 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 {
6215 len += 1;
6216 p += 2;
6217 }
6218 else if (*p == '\\' || *p == '"')
6219 {
6220 len += 2;
6221 p += 1;
6222 }
6223 else if (vim_iswhite(*p))
6224 {
6225 p = skipwhite(p);
6226 if (*p == NUL)
6227 break;
6228 len += 3; /* "," */
6229 }
6230 else
6231 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006232#ifdef FEAT_MBYTE
6233 int charlen = (*mb_ptr2len)(p);
6234 len += charlen;
6235 p += charlen;
6236#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 ++len;
6238 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 }
6241 }
6242
6243 buf = alloc(len + 1);
6244 if (buf == NULL)
6245 {
6246 *lenp = 0;
6247 return buf;
6248 }
6249
6250 p = arg;
6251 q = buf;
6252 *q++ = '"';
6253 while (*p)
6254 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006255 if (p[0] == '\\' && p[1] == '\\')
6256 {
6257 *q++ = '\\';
6258 *q++ = '\\';
6259 p += 2;
6260 }
6261 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 {
6263 *q++ = p[1];
6264 p += 2;
6265 }
6266 else if (*p == '\\' || *p == '"')
6267 {
6268 *q++ = '\\';
6269 *q++ = *p++;
6270 }
6271 else if (vim_iswhite(*p))
6272 {
6273 p = skipwhite(p);
6274 if (*p == NUL)
6275 break;
6276 *q++ = '"';
6277 *q++ = ',';
6278 *q++ = '"';
6279 }
6280 else
6281 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006282 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 }
6284 }
6285 *q++ = '"';
6286 *q = 0;
6287
6288 *lenp = len;
6289 return buf;
6290}
6291
6292/*
6293 * Check for a <> code in a user command.
6294 * "code" points to the '<'. "len" the length of the <> (inclusive).
6295 * "buf" is where the result is to be added.
6296 * "split_buf" points to a buffer used for splitting, caller should free it.
6297 * "split_len" is the length of what "split_buf" contains.
6298 * Returns the length of the replacement, which has been added to "buf".
6299 * Returns -1 if there was no match, and only the "<" has been copied.
6300 */
6301 static size_t
6302uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
6303 char_u *code;
6304 size_t len;
6305 char_u *buf;
6306 ucmd_T *cmd; /* the user command we're expanding */
6307 exarg_T *eap; /* ex arguments */
6308 char_u **split_buf;
6309 size_t *split_len;
6310{
6311 size_t result = 0;
6312 char_u *p = code + 1;
6313 size_t l = len - 2;
6314 int quote = 0;
6315 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6316 ct_LT, ct_NONE } type = ct_NONE;
6317
6318 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6319 {
6320 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6321 p += 2;
6322 l -= 2;
6323 }
6324
Bram Moolenaar371d5402006-03-20 21:47:49 +00006325 ++l;
6326 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006328 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006330 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006332 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006334 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006336 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006338 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006340 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 type = ct_REGISTER;
6342
6343 switch (type)
6344 {
6345 case ct_ARGS:
6346 /* Simple case first */
6347 if (*eap->arg == NUL)
6348 {
6349 if (quote == 1)
6350 {
6351 result = 2;
6352 if (buf != NULL)
6353 STRCPY(buf, "''");
6354 }
6355 else
6356 result = 0;
6357 break;
6358 }
6359
6360 /* When specified there is a single argument don't split it.
6361 * Works for ":Cmd %" when % is "a b c". */
6362 if ((eap->argt & NOSPC) && quote == 2)
6363 quote = 1;
6364
6365 switch (quote)
6366 {
6367 case 0: /* No quoting, no splitting */
6368 result = STRLEN(eap->arg);
6369 if (buf != NULL)
6370 STRCPY(buf, eap->arg);
6371 break;
6372 case 1: /* Quote, but don't split */
6373 result = STRLEN(eap->arg) + 2;
6374 for (p = eap->arg; *p; ++p)
6375 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006376#ifdef FEAT_MBYTE
6377 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6378 /* DBCS can contain \ in a trail byte, skip the
6379 * double-byte character. */
6380 ++p;
6381 else
6382#endif
6383 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 ++result;
6385 }
6386
6387 if (buf != NULL)
6388 {
6389 *buf++ = '"';
6390 for (p = eap->arg; *p; ++p)
6391 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006392#ifdef FEAT_MBYTE
6393 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6394 /* DBCS can contain \ in a trail byte, copy the
6395 * double-byte character to avoid escaping. */
6396 *buf++ = *p++;
6397 else
6398#endif
6399 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 *buf++ = '\\';
6401 *buf++ = *p;
6402 }
6403 *buf = '"';
6404 }
6405
6406 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006407 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 /* This is hard, so only do it once, and cache the result */
6409 if (*split_buf == NULL)
6410 *split_buf = uc_split_args(eap->arg, split_len);
6411
6412 result = *split_len;
6413 if (buf != NULL && result != 0)
6414 STRCPY(buf, *split_buf);
6415
6416 break;
6417 }
6418 break;
6419
6420 case ct_BANG:
6421 result = eap->forceit ? 1 : 0;
6422 if (quote)
6423 result += 2;
6424 if (buf != NULL)
6425 {
6426 if (quote)
6427 *buf++ = '"';
6428 if (eap->forceit)
6429 *buf++ = '!';
6430 if (quote)
6431 *buf = '"';
6432 }
6433 break;
6434
6435 case ct_LINE1:
6436 case ct_LINE2:
6437 case ct_COUNT:
6438 {
6439 char num_buf[20];
6440 long num = (type == ct_LINE1) ? eap->line1 :
6441 (type == ct_LINE2) ? eap->line2 :
6442 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6443 size_t num_len;
6444
6445 sprintf(num_buf, "%ld", num);
6446 num_len = STRLEN(num_buf);
6447 result = num_len;
6448
6449 if (quote)
6450 result += 2;
6451
6452 if (buf != NULL)
6453 {
6454 if (quote)
6455 *buf++ = '"';
6456 STRCPY(buf, num_buf);
6457 buf += num_len;
6458 if (quote)
6459 *buf = '"';
6460 }
6461
6462 break;
6463 }
6464
6465 case ct_REGISTER:
6466 result = eap->regname ? 1 : 0;
6467 if (quote)
6468 result += 2;
6469 if (buf != NULL)
6470 {
6471 if (quote)
6472 *buf++ = '\'';
6473 if (eap->regname)
6474 *buf++ = eap->regname;
6475 if (quote)
6476 *buf = '\'';
6477 }
6478 break;
6479
6480 case ct_LT:
6481 result = 1;
6482 if (buf != NULL)
6483 *buf = '<';
6484 break;
6485
6486 default:
6487 /* Not recognized: just copy the '<' and return -1. */
6488 result = (size_t)-1;
6489 if (buf != NULL)
6490 *buf = '<';
6491 break;
6492 }
6493
6494 return result;
6495}
6496
6497 static void
6498do_ucmd(eap)
6499 exarg_T *eap;
6500{
6501 char_u *buf;
6502 char_u *p;
6503 char_u *q;
6504
6505 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006506 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006507 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 size_t len, totlen;
6509
6510 size_t split_len = 0;
6511 char_u *split_buf = NULL;
6512 ucmd_T *cmd;
6513#ifdef FEAT_EVAL
6514 scid_T save_current_SID = current_SID;
6515#endif
6516
6517 if (eap->cmdidx == CMD_USER)
6518 cmd = USER_CMD(eap->useridx);
6519 else
6520 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6521
6522 /*
6523 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006524 * First round: "buf" is NULL, compute length, allocate "buf".
6525 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 */
6527 buf = NULL;
6528 for (;;)
6529 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006530 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006531 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006533
6534 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006536 start = vim_strchr(p, '<');
6537 if (start != NULL)
6538 end = vim_strchr(start + 1, '>');
6539 if (buf != NULL)
6540 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006541 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6542 ;
6543 if (*ksp == K_SPECIAL
6544 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006545 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6546# ifdef FEAT_GUI
6547 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6548# endif
6549 ))
6550 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006551 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006552 * KS_SPECIAL KE_FILLER, like for mappings, but
6553 * do_cmdline() doesn't handle that, so convert it back.
6554 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6555 len = ksp - p;
6556 if (len > 0)
6557 {
6558 mch_memmove(q, p, len);
6559 q += len;
6560 }
6561 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6562 p = ksp + 3;
6563 continue;
6564 }
6565 }
6566
6567 /* break if there no <item> is found */
6568 if (start == NULL || end == NULL)
6569 break;
6570
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 /* Include the '>' */
6572 ++end;
6573
6574 /* Take everything up to the '<' */
6575 len = start - p;
6576 if (buf == NULL)
6577 totlen += len;
6578 else
6579 {
6580 mch_memmove(q, p, len);
6581 q += len;
6582 }
6583
6584 len = uc_check_code(start, end - start, q, cmd, eap,
6585 &split_buf, &split_len);
6586 if (len == (size_t)-1)
6587 {
6588 /* no match, continue after '<' */
6589 p = start + 1;
6590 len = 1;
6591 }
6592 else
6593 p = end;
6594 if (buf == NULL)
6595 totlen += len;
6596 else
6597 q += len;
6598 }
6599 if (buf != NULL) /* second time here, finished */
6600 {
6601 STRCPY(q, p);
6602 break;
6603 }
6604
6605 totlen += STRLEN(p); /* Add on the trailing characters */
6606 buf = alloc((unsigned)(totlen + 1));
6607 if (buf == NULL)
6608 {
6609 vim_free(split_buf);
6610 return;
6611 }
6612 }
6613
6614#ifdef FEAT_EVAL
6615 current_SID = cmd->uc_scriptID;
6616#endif
6617 (void)do_cmdline(buf, eap->getline, eap->cookie,
6618 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6619#ifdef FEAT_EVAL
6620 current_SID = save_current_SID;
6621#endif
6622 vim_free(buf);
6623 vim_free(split_buf);
6624}
6625
6626# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6627 static char_u *
6628get_user_command_name(idx)
6629 int idx;
6630{
6631 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6632}
6633
6634/*
6635 * Function given to ExpandGeneric() to obtain the list of user command names.
6636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 char_u *
6638get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006639 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 int idx;
6641{
6642 if (idx < curbuf->b_ucmds.ga_len)
6643 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6644 idx -= curbuf->b_ucmds.ga_len;
6645 if (idx < ucmds.ga_len)
6646 return USER_CMD(idx)->uc_name;
6647 return NULL;
6648}
6649
6650/*
6651 * Function given to ExpandGeneric() to obtain the list of user command
6652 * attributes.
6653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 char_u *
6655get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006656 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 int idx;
6658{
6659 static char *user_cmd_flags[] =
6660 {"bang", "bar", "buffer", "complete", "count",
6661 "nargs", "range", "register"};
6662
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006663 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 return NULL;
6665 return (char_u *)user_cmd_flags[idx];
6666}
6667
6668/*
6669 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 char_u *
6672get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006673 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 int idx;
6675{
6676 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6677
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006678 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 return NULL;
6680 return (char_u *)user_cmd_nargs[idx];
6681}
6682
6683/*
6684 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6685 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 char_u *
6687get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006688 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 int idx;
6690{
6691 return (char_u *)command_complete[idx].name;
6692}
6693# endif /* FEAT_CMDL_COMPL */
6694
6695#endif /* FEAT_USR_CMDS */
6696
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006697#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6698/*
6699 * Parse a completion argument "value[vallen]".
6700 * The detected completion goes in "*complp", argument type in "*argt".
6701 * When there is an argument, for function and user defined completion, it's
6702 * copied to allocated memory and stored in "*compl_arg".
6703 * Returns FAIL if something is wrong.
6704 */
6705 int
6706parse_compl_arg(value, vallen, complp, argt, compl_arg)
6707 char_u *value;
6708 int vallen;
6709 int *complp;
6710 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006711 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006712{
6713 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006714# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006715 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006716# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006717 int i;
6718 int valend = vallen;
6719
6720 /* Look for any argument part - which is the part after any ',' */
6721 for (i = 0; i < vallen; ++i)
6722 {
6723 if (value[i] == ',')
6724 {
6725 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006726# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006727 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006728# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006729 valend = i;
6730 break;
6731 }
6732 }
6733
6734 for (i = 0; command_complete[i].expand != 0; ++i)
6735 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006736 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006737 && STRNCMP(value, command_complete[i].name, valend) == 0)
6738 {
6739 *complp = command_complete[i].expand;
6740 if (command_complete[i].expand == EXPAND_BUFFERS)
6741 *argt |= BUFNAME;
6742 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6743 || command_complete[i].expand == EXPAND_FILES)
6744 *argt |= XFILE;
6745 break;
6746 }
6747 }
6748
6749 if (command_complete[i].expand == 0)
6750 {
6751 EMSG2(_("E180: Invalid complete value: %s"), value);
6752 return FAIL;
6753 }
6754
6755# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6756 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6757 && arg != NULL)
6758# else
6759 if (arg != NULL)
6760# endif
6761 {
6762 EMSG(_("E468: Completion argument only allowed for custom completion"));
6763 return FAIL;
6764 }
6765
6766# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6767 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6768 && arg == NULL)
6769 {
6770 EMSG(_("E467: Custom completion requires a function argument"));
6771 return FAIL;
6772 }
6773
6774 if (arg != NULL)
6775 *compl_arg = vim_strnsave(arg, (int)arglen);
6776# endif
6777 return OK;
6778}
6779#endif
6780
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 static void
6782ex_colorscheme(eap)
6783 exarg_T *eap;
6784{
Bram Moolenaare6850792010-05-14 15:28:44 +02006785 if (*eap->arg == NUL)
6786 {
6787#ifdef FEAT_EVAL
6788 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6789 char_u *p = NULL;
6790
6791 if (expr != NULL)
6792 {
6793 ++emsg_off;
6794 p = eval_to_string(expr, NULL, FALSE);
6795 --emsg_off;
6796 vim_free(expr);
6797 }
6798 if (p != NULL)
6799 {
6800 MSG(p);
6801 vim_free(p);
6802 }
6803 else
6804 MSG("default");
6805#else
6806 MSG(_("unknown"));
6807#endif
6808 }
6809 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006810 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811}
6812
6813 static void
6814ex_highlight(eap)
6815 exarg_T *eap;
6816{
6817 if (*eap->arg == NUL && eap->cmd[2] == '!')
6818 MSG(_("Greetings, Vim user!"));
6819 do_highlight(eap->arg, eap->forceit, FALSE);
6820}
6821
6822
6823/*
6824 * Call this function if we thought we were going to exit, but we won't
6825 * (because of an error). May need to restore the terminal mode.
6826 */
6827 void
6828not_exiting()
6829{
6830 exiting = FALSE;
6831 settmode(TMODE_RAW);
6832}
6833
6834/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006835 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 */
6837 static void
6838ex_quit(eap)
6839 exarg_T *eap;
6840{
Bram Moolenaarf240e182014-11-27 18:33:02 +01006841#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006842 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006843#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006844
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845#ifdef FEAT_CMDWIN
6846 if (cmdwin_type != 0)
6847 {
6848 cmdwin_result = Ctrl_C;
6849 return;
6850 }
6851#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006852 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006853 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006854 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006855 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006856 return;
6857 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01006858#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006859 if (eap->addr_count > 0)
6860 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01006861 int wnr = eap->line2;
6862
6863 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
6864 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006865 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006866 }
6867 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01006868#endif
6869#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006870 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006871#endif
6872
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006873#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006874 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006875 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006876 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01006877 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
6878 && wp->w_buffer->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006879 return;
6880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881
6882#ifdef FEAT_NETBEANS_INTG
6883 netbeansForcedQuit = eap->forceit;
6884#endif
6885
6886 /*
6887 * If there are more files or windows we won't exit.
6888 */
6889 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6890 exiting = TRUE;
6891 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006892 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6893 | (eap->forceit ? CCGD_FORCEIT : 0)
6894 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 || check_more(TRUE, eap->forceit) == FAIL
6896 || (only_one_window() && check_changed_any(eap->forceit)))
6897 {
6898 not_exiting();
6899 }
6900 else
6901 {
6902#ifdef FEAT_WINDOWS
6903 if (only_one_window()) /* quit last window */
6904#endif
6905 getout(0);
6906#ifdef FEAT_WINDOWS
6907# ifdef FEAT_GUI
6908 need_mouse_correct = TRUE;
6909# endif
6910 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006911 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912#endif
6913 }
6914}
6915
6916/*
6917 * ":cquit".
6918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 static void
6920ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006921 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922{
6923 getout(1); /* this does not always pass on the exit code to the Manx
6924 compiler. why? */
6925}
6926
6927/*
6928 * ":qall": try to quit all windows
6929 */
6930 static void
6931ex_quit_all(eap)
6932 exarg_T *eap;
6933{
6934# ifdef FEAT_CMDWIN
6935 if (cmdwin_type != 0)
6936 {
6937 if (eap->forceit)
6938 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6939 else
6940 cmdwin_result = K_XF2;
6941 return;
6942 }
6943# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006944
6945 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006946 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006947 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006948 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006949 return;
6950 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006951#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006952 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6953 /* Refuse to quit when locked or when the buffer in the last window is
6954 * being closed (can only happen in autocommands). */
6955 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006956 return;
6957#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006958
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 exiting = TRUE;
6960 if (eap->forceit || !check_changed_any(FALSE))
6961 getout(0);
6962 not_exiting();
6963}
6964
Bram Moolenaard9967712006-03-11 21:18:15 +00006965#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966/*
6967 * ":close": close current window, unless it is the last one
6968 */
6969 static void
6970ex_close(eap)
6971 exarg_T *eap;
6972{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006973 win_T *win;
6974 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975# ifdef FEAT_CMDWIN
6976 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006977 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 else
6979# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006980 if (!text_locked()
6981#ifdef FEAT_AUTOCMD
6982 && !curbuf_locked()
6983#endif
6984 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006985 {
6986 if (eap->addr_count == 0)
6987 ex_win_close(eap->forceit, curwin, NULL);
6988 else {
6989 for (win = firstwin; win != NULL; win = win->w_next)
6990 {
6991 winnr++;
6992 if (winnr == eap->line2)
6993 break;
6994 }
6995 if (win == NULL)
6996 win = lastwin;
6997 ex_win_close(eap->forceit, win, NULL);
6998 }
6999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000}
7001
Bram Moolenaard9967712006-03-11 21:18:15 +00007002# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007003/*
7004 * ":pclose": Close any preview window.
7005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007007ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007009{
7010 win_T *win;
7011
7012 for (win = firstwin; win != NULL; win = win->w_next)
7013 if (win->w_p_pvw)
7014 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007015 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007016 break;
7017 }
7018}
Bram Moolenaard9967712006-03-11 21:18:15 +00007019# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007020
Bram Moolenaarf740b292006-02-16 22:11:02 +00007021/*
7022 * Close window "win" and take care of handling closing the last window for a
7023 * modified buffer.
7024 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007025 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00007026ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007027 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007029 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030{
7031 int need_hide;
7032 buf_T *buf = win->w_buffer;
7033
7034 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007035 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007037# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 if ((p_confirm || cmdmod.confirm) && p_write)
7039 {
7040 dialog_changed(buf, FALSE);
7041 if (buf_valid(buf) && bufIsChanged(buf))
7042 return;
7043 need_hide = FALSE;
7044 }
7045 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007046# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 {
7048 EMSG(_(e_nowrtmsg));
7049 return;
7050 }
7051 }
7052
Bram Moolenaard9967712006-03-11 21:18:15 +00007053# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007055# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007056
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007058 if (tp == NULL)
7059 win_close(win, !need_hide && !P_HID(buf));
7060 else
7061 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062}
7063
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007065 * ":tabclose": close current tab page, unless it is the last one.
7066 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 */
7068 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007069ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 exarg_T *eap;
7071{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007072 tabpage_T *tp;
7073
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007074# ifdef FEAT_CMDWIN
7075 if (cmdwin_type != 0)
7076 cmdwin_result = K_IGNORE;
7077 else
7078# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007079 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007080 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007081 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007083 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007084 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007085 tp = find_tabpage((int)eap->line2);
7086 if (tp == NULL)
7087 {
7088 beep_flush();
7089 return;
7090 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007091 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007092 {
7093 tabpage_close_other(tp, eap->forceit);
7094 return;
7095 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007096 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007097 if (!text_locked()
7098#ifdef FEAT_AUTOCMD
7099 && !curbuf_locked()
7100#endif
7101 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007102 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007104}
7105
7106/*
7107 * ":tabonly": close all tab pages except the current one
7108 */
7109 static void
7110ex_tabonly(eap)
7111 exarg_T *eap;
7112{
7113 tabpage_T *tp;
7114 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007115
7116# ifdef FEAT_CMDWIN
7117 if (cmdwin_type != 0)
7118 cmdwin_result = K_IGNORE;
7119 else
7120# endif
7121 if (first_tabpage->tp_next == NULL)
7122 MSG(_("Already only one tab page"));
7123 else
7124 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007125 if (eap->addr_count > 0)
7126 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007127 /* Repeat this up to a 1000 times, because autocommands may mess
7128 * up the lists. */
7129 for (done = 0; done < 1000; ++done)
7130 {
7131 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7132 if (tp->tp_topframe != topframe)
7133 {
7134 tabpage_close_other(tp, eap->forceit);
7135 /* if we failed to close it quit */
7136 if (valid_tabpage(tp))
7137 done = 1000;
7138 /* start over, "tp" is now invalid */
7139 break;
7140 }
7141 if (first_tabpage->tp_next == NULL)
7142 break;
7143 }
7144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146
7147/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007148 * Close the current tab page.
7149 */
7150 void
7151tabpage_close(forceit)
7152 int forceit;
7153{
7154 /* First close all the windows but the current one. If that worked then
7155 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007156 if (lastwin != firstwin)
7157 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007158 if (lastwin == firstwin)
7159 ex_win_close(forceit, curwin, NULL);
7160# ifdef FEAT_GUI
7161 need_mouse_correct = TRUE;
7162# endif
7163}
7164
7165/*
7166 * Close tab page "tp", which is not the current tab page.
7167 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007168 * Also takes care of the tab pages line disappearing when closing the
7169 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007170 */
7171 void
7172tabpage_close_other(tp, forceit)
7173 tabpage_T *tp;
7174 int forceit;
7175{
7176 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007177 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007178 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007179
7180 /* Limit to 1000 windows, autocommands may add a window while we close
7181 * one. OK, so I'm paranoid... */
7182 while (++done < 1000)
7183 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007184 wp = tp->tp_firstwin;
7185 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007186
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007187 /* Autocommands may delete the tab page under our fingers and we may
7188 * fail to close a window with a modified buffer. */
7189 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007190 break;
7191 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007192
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007193 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007194 if (h != tabline_height())
7195 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007196}
7197
7198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 * ":only".
7200 */
7201 static void
7202ex_only(eap)
7203 exarg_T *eap;
7204{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007205 win_T *wp;
7206 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207# ifdef FEAT_GUI
7208 need_mouse_correct = TRUE;
7209# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007210 if (eap->addr_count > 0)
7211 {
7212 wnr = eap->line2;
7213 for (wp = firstwin; --wnr > 0; )
7214 {
7215 if (wp->w_next == NULL)
7216 break;
7217 else
7218 wp = wp->w_next;
7219 }
7220 win_goto(wp);
7221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 close_others(TRUE, eap->forceit);
7223}
7224
7225/*
7226 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007227 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007229 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230ex_all(eap)
7231 exarg_T *eap;
7232{
7233 if (eap->addr_count == 0)
7234 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007235 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236}
7237#endif /* FEAT_WINDOWS */
7238
7239 static void
7240ex_hide(eap)
7241 exarg_T *eap;
7242{
7243 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
7244 eap->errmsg = e_invarg;
7245 else
7246 {
7247 /* ":hide" or ":hide | cmd": hide current window */
7248 eap->nextcmd = check_nextcmd(eap->arg);
7249#ifdef FEAT_WINDOWS
7250 if (!eap->skip)
7251 {
7252# ifdef FEAT_GUI
7253 need_mouse_correct = TRUE;
7254# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007255 if (eap->addr_count == 0)
7256 win_close(curwin, FALSE); /* don't free buffer */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007257 else
7258 {
7259 int winnr = 0;
7260 win_T *win;
7261
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007262 for (win = firstwin; win != NULL; win = win->w_next)
7263 {
7264 winnr++;
7265 if (winnr == eap->line2)
7266 break;
7267 }
7268 if (win == NULL)
7269 win = lastwin;
7270 win_close(win, FALSE);
7271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 }
7273#endif
7274 }
7275}
7276
7277/*
7278 * ":stop" and ":suspend": Suspend Vim.
7279 */
7280 static void
7281ex_stop(eap)
7282 exarg_T *eap;
7283{
7284 /*
7285 * Disallow suspending for "rvim".
7286 */
7287 if (!check_restricted()
7288#ifdef WIN3264
7289 /*
7290 * Check if external commands are allowed now.
7291 */
7292 && can_end_termcap_mode(TRUE)
7293#endif
7294 )
7295 {
7296 if (!eap->forceit)
7297 autowrite_all();
7298 windgoto((int)Rows - 1, 0);
7299 out_char('\n');
7300 out_flush();
7301 stoptermcap();
7302 out_flush(); /* needed for SUN to restore xterm buffer */
7303#ifdef FEAT_TITLE
7304 mch_restore_title(3); /* restore window titles */
7305#endif
7306 ui_suspend(); /* call machine specific function */
7307#ifdef FEAT_TITLE
7308 maketitle();
7309 resettitle(); /* force updating the title */
7310#endif
7311 starttermcap();
7312 scroll_start(); /* scroll screen before redrawing */
7313 redraw_later_clear();
7314 shell_resized(); /* may have resized window */
7315 }
7316}
7317
7318/*
7319 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7320 */
7321 static void
7322ex_exit(eap)
7323 exarg_T *eap;
7324{
7325#ifdef FEAT_CMDWIN
7326 if (cmdwin_type != 0)
7327 {
7328 cmdwin_result = Ctrl_C;
7329 return;
7330 }
7331#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007332 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007333 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007334 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007335 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007336 return;
7337 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007338#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007339 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7340 /* Refuse to quit when locked or when the buffer in the last window is
7341 * being closed (can only happen in autocommands). */
7342 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007343 return;
7344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345
7346 /*
7347 * if more files or windows we won't exit
7348 */
7349 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7350 exiting = TRUE;
7351 if ( ((eap->cmdidx == CMD_wq
7352 || curbufIsChanged())
7353 && do_write(eap) == FAIL)
7354 || check_more(TRUE, eap->forceit) == FAIL
7355 || (only_one_window() && check_changed_any(eap->forceit)))
7356 {
7357 not_exiting();
7358 }
7359 else
7360 {
7361#ifdef FEAT_WINDOWS
7362 if (only_one_window()) /* quit last window, exit Vim */
7363#endif
7364 getout(0);
7365#ifdef FEAT_WINDOWS
7366# ifdef FEAT_GUI
7367 need_mouse_correct = TRUE;
7368# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007369 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 win_close(curwin, !P_HID(curwin->w_buffer));
7371#endif
7372 }
7373}
7374
7375/*
7376 * ":print", ":list", ":number".
7377 */
7378 static void
7379ex_print(eap)
7380 exarg_T *eap;
7381{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007382 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7383 EMSG(_(e_emptybuf));
7384 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007386 for ( ;!got_int; ui_breakcheck())
7387 {
7388 print_line(eap->line1,
7389 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7390 || (eap->flags & EXFLAG_NR)),
7391 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7392 if (++eap->line1 > eap->line2)
7393 break;
7394 out_flush(); /* show one line at a time */
7395 }
7396 setpcmark();
7397 /* put cursor at last line */
7398 curwin->w_cursor.lnum = eap->line2;
7399 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 }
7401
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403}
7404
7405#ifdef FEAT_BYTEOFF
7406 static void
7407ex_goto(eap)
7408 exarg_T *eap;
7409{
7410 goto_byte(eap->line2);
7411}
7412#endif
7413
7414/*
7415 * ":shell".
7416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 static void
7418ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007419 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420{
7421 do_shell(NULL, 0);
7422}
7423
7424#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7425 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007426 || defined(FEAT_GUI_MSWIN) \
7427 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 || defined(PROTO)
7429
7430/*
7431 * Handle a file drop. The code is here because a drop is *nearly* like an
7432 * :args command, but not quite (we have a list of exact filenames, so we
7433 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7434 * code is very similar to :args and hence needs access to a lot of the static
7435 * functions in this file.
7436 *
7437 * The list should be allocated using alloc(), as should each item in the
7438 * list. This function takes over responsibility for freeing the list.
7439 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007440 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7442 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7443 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7444 * file functionality is (currently) not in EMX this is not presently a
7445 * problem.
7446 */
7447 void
7448handle_drop(filec, filev, split)
7449 int filec; /* the number of files dropped */
7450 char_u **filev; /* the list of files dropped */
7451 int split; /* force splitting the window */
7452{
7453 exarg_T ea;
7454 int save_msg_scroll = msg_scroll;
7455
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007456 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007457 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007459#ifdef FEAT_AUTOCMD
7460 if (curbuf_locked())
7461 return;
7462#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007463 /* When the screen is being updated we should not change buffers and
7464 * windows structures, it may cause freed memory to be used. */
7465 if (updating_screen)
7466 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467
7468 /* Check whether the current buffer is changed. If so, we will need
7469 * to split the current window or data could be lost.
7470 * We don't need to check if the 'hidden' option is set, as in this
7471 * case the buffer won't be lost.
7472 */
7473 if (!P_HID(curbuf) && !split)
7474 {
7475 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007476 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 --emsg_off;
7478 }
7479 if (split)
7480 {
7481# ifdef FEAT_WINDOWS
7482 if (win_split(0, 0) == FAIL)
7483 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007484 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485
7486 /* When splitting the window, create a new alist. Otherwise the
7487 * existing one is overwritten. */
7488 alist_unlink(curwin->w_alist);
7489 alist_new();
7490# else
7491 return; /* can't split, always fail */
7492# endif
7493 }
7494
7495 /*
7496 * Set up the new argument list.
7497 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007498 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499
7500 /*
7501 * Move to the first file.
7502 */
7503 /* Fake up a minimal "next" command for do_argfile() */
7504 vim_memset(&ea, 0, sizeof(ea));
7505 ea.cmd = (char_u *)"next";
7506 do_argfile(&ea, 0);
7507
7508 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7509 * mode that is not needed here. */
7510 need_start_insertmode = FALSE;
7511
7512 /* Restore msg_scroll, otherwise a following command may cause scrolling
7513 * unexpectedly. The screen will be redrawn by the caller, thus
7514 * msg_scroll being set by displaying a message is irrelevant. */
7515 msg_scroll = save_msg_scroll;
7516}
7517#endif
7518
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519/*
7520 * Clear an argument list: free all file names and reset it to zero entries.
7521 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007522 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523alist_clear(al)
7524 alist_T *al;
7525{
7526 while (--al->al_ga.ga_len >= 0)
7527 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7528 ga_clear(&al->al_ga);
7529}
7530
7531/*
7532 * Init an argument list.
7533 */
7534 void
7535alist_init(al)
7536 alist_T *al;
7537{
7538 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7539}
7540
7541#if defined(FEAT_WINDOWS) || defined(PROTO)
7542
7543/*
7544 * Remove a reference from an argument list.
7545 * Ignored when the argument list is the global one.
7546 * If the argument list is no longer used by any window, free it.
7547 */
7548 void
7549alist_unlink(al)
7550 alist_T *al;
7551{
7552 if (al != &global_alist && --al->al_refcount <= 0)
7553 {
7554 alist_clear(al);
7555 vim_free(al);
7556 }
7557}
7558
7559# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7560/*
7561 * Create a new argument list and use it for the current window.
7562 */
7563 void
7564alist_new()
7565{
7566 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7567 if (curwin->w_alist == NULL)
7568 {
7569 curwin->w_alist = &global_alist;
7570 ++global_alist.al_refcount;
7571 }
7572 else
7573 {
7574 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007575 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 alist_init(curwin->w_alist);
7577 }
7578}
7579# endif
7580#endif
7581
7582#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7583/*
7584 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007585 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7586 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 */
7588 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007589alist_expand(fnum_list, fnum_len)
7590 int *fnum_list;
7591 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592{
7593 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007594 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 char_u **new_arg_files;
7596 int new_arg_file_count;
7597 char_u *save_p_su = p_su;
7598 int i;
7599
7600 /* Don't use 'suffixes' here. This should work like the shell did the
7601 * expansion. Also, the vimrc file isn't read yet, thus the user
7602 * can't set the options. */
7603 p_su = empty_option;
7604 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7605 if (old_arg_files != NULL)
7606 {
7607 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007608 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7609 old_arg_count = GARGCOUNT;
7610 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007612 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 && new_arg_file_count > 0)
7614 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007615 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7616 TRUE, fnum_list, fnum_len);
7617 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 }
7620 p_su = save_p_su;
7621}
7622#endif
7623
7624/*
7625 * Set the argument list for the current window.
7626 * Takes over the allocated files[] and the allocated fnames in it.
7627 */
7628 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007629alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 alist_T *al;
7631 int count;
7632 char_u **files;
7633 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007634 int *fnum_list;
7635 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636{
7637 int i;
7638
7639 alist_clear(al);
7640 if (ga_grow(&al->al_ga, count) == OK)
7641 {
7642 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007643 {
7644 if (got_int)
7645 {
7646 /* When adding many buffers this can take a long time. Allow
7647 * interrupting here. */
7648 while (i < count)
7649 vim_free(files[i++]);
7650 break;
7651 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007652
7653 /* May set buffer name of a buffer previously used for the
7654 * argument list, so that it's re-used by alist_add. */
7655 if (fnum_list != NULL && i < fnum_len)
7656 buf_set_name(fnum_list[i], files[i]);
7657
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007659 ui_breakcheck();
7660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 vim_free(files);
7662 }
7663 else
7664 FreeWild(count, files);
7665#ifdef FEAT_WINDOWS
7666 if (al == &global_alist)
7667#endif
7668 arg_had_last = FALSE;
7669}
7670
7671/*
7672 * Add file "fname" to argument list "al".
7673 * "fname" must have been allocated and "al" must have been checked for room.
7674 */
7675 void
7676alist_add(al, fname, set_fnum)
7677 alist_T *al;
7678 char_u *fname;
7679 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7680{
7681 if (fname == NULL) /* don't add NULL file names */
7682 return;
7683#ifdef BACKSLASH_IN_FILENAME
7684 slash_adjust(fname);
7685#endif
7686 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7687 if (set_fnum > 0)
7688 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7689 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7690 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691}
7692
7693#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7694/*
7695 * Adjust slashes in file names. Called after 'shellslash' was set.
7696 */
7697 void
7698alist_slash_adjust()
7699{
7700 int i;
7701# ifdef FEAT_WINDOWS
7702 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007703 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704# endif
7705
7706 for (i = 0; i < GARGCOUNT; ++i)
7707 if (GARGLIST[i].ae_fname != NULL)
7708 slash_adjust(GARGLIST[i].ae_fname);
7709# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007710 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 if (wp->w_alist != &global_alist)
7712 for (i = 0; i < WARGCOUNT(wp); ++i)
7713 if (WARGLIST(wp)[i].ae_fname != NULL)
7714 slash_adjust(WARGLIST(wp)[i].ae_fname);
7715# endif
7716}
7717#endif
7718
7719/*
7720 * ":preserve".
7721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 static void
7723ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007724 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007726 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 ml_preserve(curbuf, TRUE);
7728}
7729
7730/*
7731 * ":recover".
7732 */
7733 static void
7734ex_recover(eap)
7735 exarg_T *eap;
7736{
7737 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7738 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007739 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7740 | CCGD_MULTWIN
7741 | (eap->forceit ? CCGD_FORCEIT : 0)
7742 | CCGD_EXCMD)
7743
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 && (*eap->arg == NUL
7745 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7746 ml_recover();
7747 recoverymode = FALSE;
7748}
7749
7750/*
7751 * Command modifier used in a wrong way.
7752 */
7753 static void
7754ex_wrongmodifier(eap)
7755 exarg_T *eap;
7756{
7757 eap->errmsg = e_invcmd;
7758}
7759
7760#ifdef FEAT_WINDOWS
7761/*
7762 * :sview [+command] file split window with new file, read-only
7763 * :split [[+command] file] split window with current or new file
7764 * :vsplit [[+command] file] split window vertically with current or new file
7765 * :new [[+command] file] split window with no or new file
7766 * :vnew [[+command] file] split vertically window with no or new file
7767 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007768 *
7769 * :tabedit open new Tab page with empty window
7770 * :tabedit [+command] file open new Tab page and edit "file"
7771 * :tabnew [[+command] file] just like :tabedit
7772 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 */
7774 void
7775ex_splitview(eap)
7776 exarg_T *eap;
7777{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007778 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007779# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007781# endif
7782# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007786# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7788 {
7789 ex_ni(eap);
7790 return;
7791 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007792# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007794# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007796# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007798# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007800 * quickfix windows. But it's OK when doing ":tab split". */
7801 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 {
7803 if (eap->cmdidx == CMD_split)
7804 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007805# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 if (eap->cmdidx == CMD_vsplit)
7807 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007810# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007812# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007813 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 {
7815 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7816 FNAME_MESS, TRUE, curbuf->b_ffname);
7817 if (fname == NULL)
7818 goto theend;
7819 eap->arg = fname;
7820 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007821# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007823# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007825# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007827# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007829# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 && eap->cmdidx != CMD_new)
7831 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007832# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007833 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007834# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007835 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007836# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007837 au_has_group((char_u *)"FileExplorer"))
7838 {
7839 /* No browsing supported but we do have the file explorer:
7840 * Edit the directory. */
7841 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7842 eap->arg = (char_u *)".";
7843 }
7844 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007845# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007846 {
7847 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007849 if (fname == NULL)
7850 goto theend;
7851 eap->arg = fname;
7852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 }
7854 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007857 /*
7858 * Either open new tab page or split the window.
7859 */
7860 if (eap->cmdidx == CMD_tabedit
7861 || eap->cmdidx == CMD_tabfind
7862 || eap->cmdidx == CMD_tabnew)
7863 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007864 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7865 : eap->addr_count == 0 ? 0
7866 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007867 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007868 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007869
7870 /* set the alternate buffer for the window we came from */
7871 if (curwin != old_curwin
7872 && win_valid(old_curwin)
7873 && old_curwin->w_buffer != curbuf
7874 && !cmdmod.keepalt)
7875 old_curwin->w_alt_fnum = curbuf->b_fnum;
7876 }
7877 }
7878 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7880 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007881# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 /* Reset 'scrollbind' when editing another file, but keep it when
7883 * doing ":split" without arguments. */
7884 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007885# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007887# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007889 {
7890 RESET_BINDING(curwin);
7891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 else
7893 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007894# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 do_exedit(eap, old_curwin);
7896 }
7897
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007898# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007900# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007902# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903theend:
7904 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007907
7908/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007909 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007910 */
7911 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007912tabpage_new()
7913{
7914 exarg_T ea;
7915
7916 vim_memset(&ea, 0, sizeof(ea));
7917 ea.cmdidx = CMD_tabnew;
7918 ea.cmd = (char_u *)"tabn";
7919 ea.arg = (char_u *)"";
7920 ex_splitview(&ea);
7921}
7922
7923/*
7924 * :tabnext command
7925 */
7926 static void
7927ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007928 exarg_T *eap;
7929{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007930 switch (eap->cmdidx)
7931 {
7932 case CMD_tabfirst:
7933 case CMD_tabrewind:
7934 goto_tabpage(1);
7935 break;
7936 case CMD_tablast:
7937 goto_tabpage(9999);
7938 break;
7939 case CMD_tabprevious:
7940 case CMD_tabNext:
7941 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7942 break;
7943 default: /* CMD_tabnext */
7944 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7945 break;
7946 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007947}
7948
7949/*
7950 * :tabmove command
7951 */
7952 static void
7953ex_tabmove(eap)
7954 exarg_T *eap;
7955{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007956 int tab_number = 9999;
7957
7958 if (eap->arg && *eap->arg != NUL)
7959 {
7960 char_u *p = eap->arg;
7961 int relative = 0; /* argument +N/-N means: move N places to the
7962 * right/left relative to the current position. */
7963
7964 if (*eap->arg == '-')
7965 {
7966 relative = -1;
7967 p = eap->arg + 1;
7968 }
7969 else if (*eap->arg == '+')
7970 {
7971 relative = 1;
7972 p = eap->arg + 1;
7973 }
7974 else
7975 p = eap->arg;
7976
7977 if (p == skipdigits(p))
7978 {
7979 /* No numbers as argument. */
7980 eap->errmsg = e_invarg;
7981 return;
7982 }
7983
7984 tab_number = getdigits(&p);
7985 if (relative != 0)
7986 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7987 }
7988 else if (eap->addr_count != 0)
7989 tab_number = eap->line2;
7990
7991 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007992}
7993
7994/*
7995 * :tabs command: List tabs and their contents.
7996 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007997 static void
7998ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007999 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008000{
8001 tabpage_T *tp;
8002 win_T *wp;
8003 int tabcount = 1;
8004
8005 msg_start();
8006 msg_scroll = TRUE;
8007 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8008 {
8009 msg_putchar('\n');
8010 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8011 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8012 out_flush(); /* output one line at a time */
8013 ui_breakcheck();
8014
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008015 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008016 wp = firstwin;
8017 else
8018 wp = tp->tp_firstwin;
8019 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8020 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008021 msg_putchar('\n');
8022 msg_putchar(wp == curwin ? '>' : ' ');
8023 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008024 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8025 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008026 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008027 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008028 else
8029 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8030 IObuff, IOSIZE, TRUE);
8031 msg_outtrans(IObuff);
8032 out_flush(); /* output one line at a time */
8033 ui_breakcheck();
8034 }
8035 }
8036}
8037
8038#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039
8040/*
8041 * ":mode": Set screen mode.
8042 * If no argument given, just get the screen size and redraw.
8043 */
8044 static void
8045ex_mode(eap)
8046 exarg_T *eap;
8047{
8048 if (*eap->arg == NUL)
8049 shell_resized();
8050 else
8051 mch_screenmode(eap->arg);
8052}
8053
8054#ifdef FEAT_WINDOWS
8055/*
8056 * ":resize".
8057 * set, increment or decrement current window height
8058 */
8059 static void
8060ex_resize(eap)
8061 exarg_T *eap;
8062{
8063 int n;
8064 win_T *wp = curwin;
8065
8066 if (eap->addr_count > 0)
8067 {
8068 n = eap->line2;
8069 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8070 ;
8071 }
8072
8073#ifdef FEAT_GUI
8074 need_mouse_correct = TRUE;
8075#endif
8076 n = atol((char *)eap->arg);
8077#ifdef FEAT_VERTSPLIT
8078 if (cmdmod.split & WSP_VERT)
8079 {
8080 if (*eap->arg == '-' || *eap->arg == '+')
8081 n += W_WIDTH(curwin);
8082 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8083 n = 9999;
8084 win_setwidth_win((int)n, wp);
8085 }
8086 else
8087#endif
8088 {
8089 if (*eap->arg == '-' || *eap->arg == '+')
8090 n += curwin->w_height;
8091 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8092 n = 9999;
8093 win_setheight_win((int)n, wp);
8094 }
8095}
8096#endif
8097
8098/*
8099 * ":find [+command] <file>" command.
8100 */
8101 static void
8102ex_find(eap)
8103 exarg_T *eap;
8104{
8105#ifdef FEAT_SEARCHPATH
8106 char_u *fname;
8107 int count;
8108
8109 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8110 TRUE, curbuf->b_ffname);
8111 if (eap->addr_count > 0)
8112 {
8113 /* Repeat finding the file "count" times. This matters when it
8114 * appears several times in the path. */
8115 count = eap->line2;
8116 while (fname != NULL && --count > 0)
8117 {
8118 vim_free(fname);
8119 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8120 FALSE, curbuf->b_ffname);
8121 }
8122 }
8123
8124 if (fname != NULL)
8125 {
8126 eap->arg = fname;
8127#endif
8128 do_exedit(eap, NULL);
8129#ifdef FEAT_SEARCHPATH
8130 vim_free(fname);
8131 }
8132#endif
8133}
8134
8135/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008136 * ":open" simulation: for now just work like ":visual".
8137 */
8138 static void
8139ex_open(eap)
8140 exarg_T *eap;
8141{
8142 regmatch_T regmatch;
8143 char_u *p;
8144
8145 curwin->w_cursor.lnum = eap->line2;
8146 beginline(BL_SOL | BL_FIX);
8147 if (*eap->arg == '/')
8148 {
8149 /* ":open /pattern/": put cursor in column found with pattern */
8150 ++eap->arg;
8151 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8152 *p = NUL;
8153 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8154 if (regmatch.regprog != NULL)
8155 {
8156 regmatch.rm_ic = p_ic;
8157 p = ml_get_curline();
8158 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008159 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008160 else
8161 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008162 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008163 }
8164 /* Move to the NUL, ignore any other arguments. */
8165 eap->arg += STRLEN(eap->arg);
8166 }
8167 check_cursor();
8168
8169 eap->cmdidx = CMD_visual;
8170 do_exedit(eap, NULL);
8171}
8172
8173/*
8174 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 */
8176 static void
8177ex_edit(eap)
8178 exarg_T *eap;
8179{
8180 do_exedit(eap, NULL);
8181}
8182
8183/*
8184 * ":edit <file>" command and alikes.
8185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 void
8187do_exedit(eap, old_curwin)
8188 exarg_T *eap;
8189 win_T *old_curwin; /* curwin before doing a split or NULL */
8190{
8191 int n;
8192#ifdef FEAT_WINDOWS
8193 int need_hide;
8194#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008195 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196
8197 /*
8198 * ":vi" command ends Ex mode.
8199 */
8200 if (exmode_active && (eap->cmdidx == CMD_visual
8201 || eap->cmdidx == CMD_view))
8202 {
8203 exmode_active = FALSE;
8204 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008205 {
8206 /* Special case: ":global/pat/visual\NLvi-commands" */
8207 if (global_busy)
8208 {
8209 int rd = RedrawingDisabled;
8210 int nwr = no_wait_return;
8211 int ms = msg_scroll;
8212#ifdef FEAT_GUI
8213 int he = hold_gui_events;
8214#endif
8215
8216 if (eap->nextcmd != NULL)
8217 {
8218 stuffReadbuff(eap->nextcmd);
8219 eap->nextcmd = NULL;
8220 }
8221
8222 if (exmode_was != EXMODE_VIM)
8223 settmode(TMODE_RAW);
8224 RedrawingDisabled = 0;
8225 no_wait_return = 0;
8226 need_wait_return = FALSE;
8227 msg_scroll = 0;
8228#ifdef FEAT_GUI
8229 hold_gui_events = 0;
8230#endif
8231 must_redraw = CLEAR;
8232
8233 main_loop(FALSE, TRUE);
8234
8235 RedrawingDisabled = rd;
8236 no_wait_return = nwr;
8237 msg_scroll = ms;
8238#ifdef FEAT_GUI
8239 hold_gui_events = he;
8240#endif
8241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 }
8245
8246 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008247 || eap->cmdidx == CMD_tabnew
8248 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249#ifdef FEAT_VERTSPLIT
8250 || eap->cmdidx == CMD_vnew
8251#endif
8252 ) && *eap->arg == NUL)
8253 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008254 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 setpcmark();
8256 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008257 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8258 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 }
8260 else if ((eap->cmdidx != CMD_split
8261#ifdef FEAT_VERTSPLIT
8262 && eap->cmdidx != CMD_vsplit
8263#endif
8264 )
8265 || *eap->arg != NUL
8266#ifdef FEAT_BROWSE
8267 || cmdmod.browse
8268#endif
8269 )
8270 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008271#ifdef FEAT_AUTOCMD
8272 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8273 * can bring us here, others are stopped earlier. */
8274 if (*eap->arg != NUL && curbuf_locked())
8275 return;
8276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 n = readonlymode;
8278 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8279 readonlymode = TRUE;
8280 else if (eap->cmdidx == CMD_enew)
8281 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8282 empty buffer */
8283 setpcmark();
8284 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8285 NULL, eap,
8286 /* ":edit" goes to first line if Vi compatible */
8287 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8288 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8289 ? ECMD_ONE : eap->do_ecmd_lnum,
8290 (P_HID(curbuf) ? ECMD_HIDE : 0)
8291 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008292 /* after a split we can use an existing buffer */
8293 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294#ifdef FEAT_LISTCMDS
8295 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8296#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008297 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {
8299 /* Editing the file failed. If the window was split, close it. */
8300#ifdef FEAT_WINDOWS
8301 if (old_curwin != NULL)
8302 {
8303 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8304 if (!need_hide || P_HID(curbuf))
8305 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008306# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8307 cleanup_T cs;
8308
8309 /* Reset the error/interrupt/exception state here so that
8310 * aborting() returns FALSE when closing a window. */
8311 enter_cleanup(&cs);
8312# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313# ifdef FEAT_GUI
8314 need_mouse_correct = TRUE;
8315# endif
8316 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008317
8318# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8319 /* Restore the error/interrupt/exception state if not
8320 * discarded by a new aborting error, interrupt, or
8321 * uncaught exception. */
8322 leave_cleanup(&cs);
8323# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 }
8325 }
8326#endif
8327 }
8328 else if (readonlymode && curbuf->b_nwindows == 1)
8329 {
8330 /* When editing an already visited buffer, 'readonly' won't be set
8331 * but the previous value is kept. With ":view" and ":sview" we
8332 * want the file to be readonly, except when another window is
8333 * editing the same buffer. */
8334 curbuf->b_p_ro = TRUE;
8335 }
8336 readonlymode = n;
8337 }
8338 else
8339 {
8340 if (eap->do_ecmd_cmd != NULL)
8341 do_cmdline_cmd(eap->do_ecmd_cmd);
8342#ifdef FEAT_TITLE
8343 n = curwin->w_arg_idx_invalid;
8344#endif
8345 check_arg_idx(curwin);
8346#ifdef FEAT_TITLE
8347 if (n != curwin->w_arg_idx_invalid)
8348 maketitle();
8349#endif
8350 }
8351
8352#ifdef FEAT_WINDOWS
8353 /*
8354 * if ":split file" worked, set alternate file name in old window to new
8355 * file
8356 */
8357 if (old_curwin != NULL
8358 && *eap->arg != NUL
8359 && curwin != old_curwin
8360 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008361 && old_curwin->w_buffer != curbuf
8362 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 old_curwin->w_alt_fnum = curbuf->b_fnum;
8364#endif
8365
8366 ex_no_reprint = TRUE;
8367}
8368
8369#ifndef FEAT_GUI
8370/*
8371 * ":gui" and ":gvim" when there is no GUI.
8372 */
8373 static void
8374ex_nogui(eap)
8375 exarg_T *eap;
8376{
8377 eap->errmsg = e_nogvim;
8378}
8379#endif
8380
8381#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8382 static void
8383ex_tearoff(eap)
8384 exarg_T *eap;
8385{
8386 gui_make_tearoff(eap->arg);
8387}
8388#endif
8389
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008390#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 static void
8392ex_popup(eap)
8393 exarg_T *eap;
8394{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008395 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396}
8397#endif
8398
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 static void
8400ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008401 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402{
8403 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8404 MSG(_("No swap file"));
8405 else
8406 msg(curbuf->b_ml.ml_mfp->mf_fname);
8407}
8408
8409/*
8410 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8411 * offset.
8412 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 static void
8415ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008416 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417{
8418#ifdef FEAT_SCROLLBIND
8419 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008420 win_T *save_curwin = curwin;
8421 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 long topline;
8423 long y;
8424 linenr_T old_linenr = curwin->w_cursor.lnum;
8425
8426 setpcmark();
8427
8428 /*
8429 * determine max topline
8430 */
8431 if (curwin->w_p_scb)
8432 {
8433 topline = curwin->w_topline;
8434 for (wp = firstwin; wp; wp = wp->w_next)
8435 {
8436 if (wp->w_p_scb && wp->w_buffer)
8437 {
8438 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8439 if (topline > y)
8440 topline = y;
8441 }
8442 }
8443 if (topline < 1)
8444 topline = 1;
8445 }
8446 else
8447 {
8448 topline = 1;
8449 }
8450
8451
8452 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008453 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8456 {
8457 if (curwin->w_p_scb)
8458 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008459 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 y = topline - curwin->w_topline;
8461 if (y > 0)
8462 scrollup(y, TRUE);
8463 else
8464 scrolldown(-y, TRUE);
8465 curwin->w_scbind_pos = topline;
8466 redraw_later(VALID);
8467 cursor_correct();
8468#ifdef FEAT_WINDOWS
8469 curwin->w_redr_status = TRUE;
8470#endif
8471 }
8472 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008473 curwin = save_curwin;
8474 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 if (curwin->w_p_scb)
8476 {
8477 did_syncbind = TRUE;
8478 checkpcmark();
8479 if (old_linenr != curwin->w_cursor.lnum)
8480 {
8481 char_u ctrl_o[2];
8482
8483 ctrl_o[0] = Ctrl_O;
8484 ctrl_o[1] = 0;
8485 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8486 }
8487 }
8488#endif
8489}
8490
8491
8492 static void
8493ex_read(eap)
8494 exarg_T *eap;
8495{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008496 int i;
8497 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8498 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499
8500 if (eap->usefilter) /* :r!cmd */
8501 do_bang(1, eap, FALSE, FALSE, TRUE);
8502 else
8503 {
8504 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8505 return;
8506
8507#ifdef FEAT_BROWSE
8508 if (cmdmod.browse)
8509 {
8510 char_u *browseFile;
8511
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008512 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 NULL, NULL, NULL, curbuf);
8514 if (browseFile != NULL)
8515 {
8516 i = readfile(browseFile, NULL,
8517 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8518 vim_free(browseFile);
8519 }
8520 else
8521 i = OK;
8522 }
8523 else
8524#endif
8525 if (*eap->arg == NUL)
8526 {
8527 if (check_fname() == FAIL) /* check for no file name */
8528 return;
8529 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8530 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8531 }
8532 else
8533 {
8534 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8535 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8536 i = readfile(eap->arg, NULL,
8537 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8538
8539 }
8540 if (i == FAIL)
8541 {
8542#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8543 if (!aborting())
8544#endif
8545 EMSG2(_(e_notopen), eap->arg);
8546 }
8547 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008548 {
8549 if (empty && exmode_active)
8550 {
8551 /* Delete the empty line that remains. Historically ex does
8552 * this but vi doesn't. */
8553 if (eap->line2 == 0)
8554 lnum = curbuf->b_ml.ml_line_count;
8555 else
8556 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008557 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008558 {
8559 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008560 if (curwin->w_cursor.lnum > 1
8561 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008562 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008563 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008564 }
8565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 }
8569}
8570
Bram Moolenaarea408852005-06-25 22:49:46 +00008571static char_u *prev_dir = NULL;
8572
8573#if defined(EXITFREE) || defined(PROTO)
8574 void
8575free_cd_dir()
8576{
8577 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008578 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008579
8580 vim_free(globaldir);
8581 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008582}
8583#endif
8584
Bram Moolenaarf4258302013-06-02 18:20:17 +02008585/*
8586 * Deal with the side effects of changing the current directory.
8587 * When "local" is TRUE then this was after an ":lcd" command.
8588 */
8589 void
8590post_chdir(local)
8591 int local;
8592{
8593 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008594 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008595 if (local)
8596 {
8597 /* If still in global directory, need to remember current
8598 * directory as global directory. */
8599 if (globaldir == NULL && prev_dir != NULL)
8600 globaldir = vim_strsave(prev_dir);
8601 /* Remember this local directory for the window. */
8602 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8603 curwin->w_localdir = vim_strsave(NameBuff);
8604 }
8605 else
8606 {
8607 /* We are now in the global directory, no need to remember its
8608 * name. */
8609 vim_free(globaldir);
8610 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008611 }
8612
8613 shorten_fnames(TRUE);
8614}
8615
Bram Moolenaarea408852005-06-25 22:49:46 +00008616
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617/*
8618 * ":cd", ":lcd", ":chdir" and ":lchdir".
8619 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008620 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621ex_cd(eap)
8622 exarg_T *eap;
8623{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 char_u *new_dir;
8625 char_u *tofree;
8626
8627 new_dir = eap->arg;
8628#if !defined(UNIX) && !defined(VMS)
8629 /* for non-UNIX ":cd" means: print current directory */
8630 if (*new_dir == NUL)
8631 ex_pwd(NULL);
8632 else
8633#endif
8634 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008635#ifdef FEAT_AUTOCMD
8636 if (allbuf_locked())
8637 return;
8638#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008639 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8640 && !eap->forceit)
8641 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008642 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008643 return;
8644 }
8645
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 /* ":cd -": Change to previous directory */
8647 if (STRCMP(new_dir, "-") == 0)
8648 {
8649 if (prev_dir == NULL)
8650 {
8651 EMSG(_("E186: No previous directory"));
8652 return;
8653 }
8654 new_dir = prev_dir;
8655 }
8656
8657 /* Save current directory for next ":cd -" */
8658 tofree = prev_dir;
8659 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8660 prev_dir = vim_strsave(NameBuff);
8661 else
8662 prev_dir = NULL;
8663
8664#if defined(UNIX) || defined(VMS)
8665 /* for UNIX ":cd" means: go to home directory */
8666 if (*new_dir == NUL)
8667 {
8668 /* use NameBuff for home directory name */
8669# ifdef VMS
8670 char_u *p;
8671
8672 p = mch_getenv((char_u *)"SYS$LOGIN");
8673 if (p == NULL || *p == NUL) /* empty is the same as not set */
8674 NameBuff[0] = NUL;
8675 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008676 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677# else
8678 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8679# endif
8680 new_dir = NameBuff;
8681 }
8682#endif
8683 if (new_dir == NULL || vim_chdir(new_dir))
8684 EMSG(_(e_failed));
8685 else
8686 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008687 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688
8689 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008690 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 ex_pwd(eap);
8692 }
8693 vim_free(tofree);
8694 }
8695}
8696
8697/*
8698 * ":pwd".
8699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 static void
8701ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008702 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703{
8704 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8705 {
8706#ifdef BACKSLASH_IN_FILENAME
8707 slash_adjust(NameBuff);
8708#endif
8709 msg(NameBuff);
8710 }
8711 else
8712 EMSG(_("E187: Unknown"));
8713}
8714
8715/*
8716 * ":=".
8717 */
8718 static void
8719ex_equal(eap)
8720 exarg_T *eap;
8721{
8722 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008723 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724}
8725
8726 static void
8727ex_sleep(eap)
8728 exarg_T *eap;
8729{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008730 int n;
8731 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732
8733 if (cursor_valid())
8734 {
8735 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8736 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008737 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008739
8740 len = eap->line2;
8741 switch (*eap->arg)
8742 {
8743 case 'm': break;
8744 case NUL: len *= 1000L; break;
8745 default: EMSG2(_(e_invarg2), eap->arg); return;
8746 }
8747 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748}
8749
8750/*
8751 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8752 */
8753 void
8754do_sleep(msec)
8755 long msec;
8756{
8757 long done;
8758
8759 cursor_on();
8760 out_flush();
8761 for (done = 0; !got_int && done < msec; done += 1000L)
8762 {
8763 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8764 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008765#ifdef FEAT_NETBEANS_INTG
8766 /* Process the netbeans messages that may have been received in the
8767 * call to ui_breakcheck() when the GUI is in use. This may occur when
8768 * running a test case. */
8769 netbeans_parse_messages();
8770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 }
8772}
8773
8774 static void
8775do_exmap(eap, isabbrev)
8776 exarg_T *eap;
8777 int isabbrev;
8778{
8779 int mode;
8780 char_u *cmdp;
8781
8782 cmdp = eap->cmd;
8783 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8784
8785 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8786 eap->arg, mode, isabbrev))
8787 {
8788 case 1: EMSG(_(e_invarg));
8789 break;
8790 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8791 break;
8792 }
8793}
8794
8795/*
8796 * ":winsize" command (obsolete).
8797 */
8798 static void
8799ex_winsize(eap)
8800 exarg_T *eap;
8801{
8802 int w, h;
8803 char_u *arg = eap->arg;
8804 char_u *p;
8805
8806 w = getdigits(&arg);
8807 arg = skipwhite(arg);
8808 p = arg;
8809 h = getdigits(&arg);
8810 if (*p != NUL && *arg == NUL)
8811 set_shellsize(w, h, TRUE);
8812 else
8813 EMSG(_("E465: :winsize requires two number arguments"));
8814}
8815
8816#ifdef FEAT_WINDOWS
8817 static void
8818ex_wincmd(eap)
8819 exarg_T *eap;
8820{
8821 int xchar = NUL;
8822 char_u *p;
8823
8824 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8825 {
8826 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8827 if (eap->arg[1] == NUL)
8828 {
8829 EMSG(_(e_invarg));
8830 return;
8831 }
8832 xchar = eap->arg[1];
8833 p = eap->arg + 2;
8834 }
8835 else
8836 p = eap->arg + 1;
8837
8838 eap->nextcmd = check_nextcmd(p);
8839 p = skipwhite(p);
8840 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8841 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008842 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 {
8844 /* Pass flags on for ":vertical wincmd ]". */
8845 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008846 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8848 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008849 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 }
8851}
8852#endif
8853
Bram Moolenaar843ee412004-06-30 16:16:41 +00008854#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855/*
8856 * ":winpos".
8857 */
8858 static void
8859ex_winpos(eap)
8860 exarg_T *eap;
8861{
8862 int x, y;
8863 char_u *arg = eap->arg;
8864 char_u *p;
8865
8866 if (*arg == NUL)
8867 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008868# if defined(FEAT_GUI) || defined(MSWIN)
8869# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008871# else
8872 if (mch_get_winpos(&x, &y) != FAIL)
8873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 {
8875 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8876 msg(IObuff);
8877 }
8878 else
8879# endif
8880 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8881 }
8882 else
8883 {
8884 x = getdigits(&arg);
8885 arg = skipwhite(arg);
8886 p = arg;
8887 y = getdigits(&arg);
8888 if (*p == NUL || *arg != NUL)
8889 {
8890 EMSG(_("E466: :winpos requires two number arguments"));
8891 return;
8892 }
8893# ifdef FEAT_GUI
8894 if (gui.in_use)
8895 gui_mch_set_winpos(x, y);
8896 else if (gui.starting)
8897 {
8898 /* Remember the coordinates for when the window is opened. */
8899 gui_win_x = x;
8900 gui_win_y = y;
8901 }
8902# ifdef HAVE_TGETENT
8903 else
8904# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008905# else
8906# ifdef MSWIN
8907 mch_set_winpos(x, y);
8908# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909# endif
8910# ifdef HAVE_TGETENT
8911 if (*T_CWP)
8912 term_set_winpos(x, y);
8913# endif
8914 }
8915}
8916#endif
8917
8918/*
8919 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8920 */
8921 static void
8922ex_operators(eap)
8923 exarg_T *eap;
8924{
8925 oparg_T oa;
8926
8927 clear_oparg(&oa);
8928 oa.regname = eap->regname;
8929 oa.start.lnum = eap->line1;
8930 oa.end.lnum = eap->line2;
8931 oa.line_count = eap->line2 - eap->line1 + 1;
8932 oa.motion_type = MLINE;
8933#ifdef FEAT_VIRTUALEDIT
8934 virtual_op = FALSE;
8935#endif
8936 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8937 {
8938 setpcmark();
8939 curwin->w_cursor.lnum = eap->line1;
8940 beginline(BL_SOL | BL_FIX);
8941 }
8942
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008943 if (VIsual_active)
8944 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008945
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 switch (eap->cmdidx)
8947 {
8948 case CMD_delete:
8949 oa.op_type = OP_DELETE;
8950 op_delete(&oa);
8951 break;
8952
8953 case CMD_yank:
8954 oa.op_type = OP_YANK;
8955 (void)op_yank(&oa, FALSE, TRUE);
8956 break;
8957
8958 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02008959 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008961 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8962#else
8963 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008965 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 oa.op_type = OP_RSHIFT;
8967 else
8968 oa.op_type = OP_LSHIFT;
8969 op_shift(&oa, FALSE, eap->amount);
8970 break;
8971 }
8972#ifdef FEAT_VIRTUALEDIT
8973 virtual_op = MAYBE;
8974#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008975 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976}
8977
8978/*
8979 * ":put".
8980 */
8981 static void
8982ex_put(eap)
8983 exarg_T *eap;
8984{
8985 /* ":0put" works like ":1put!". */
8986 if (eap->line2 == 0)
8987 {
8988 eap->line2 = 1;
8989 eap->forceit = TRUE;
8990 }
8991 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008992 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8993 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994}
8995
8996/*
8997 * Handle ":copy" and ":move".
8998 */
8999 static void
9000ex_copymove(eap)
9001 exarg_T *eap;
9002{
9003 long n;
9004
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01009005 n = get_address(&eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 if (eap->arg == NULL) /* error detected */
9007 {
9008 eap->nextcmd = NULL;
9009 return;
9010 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009011 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012
9013 /*
9014 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9015 */
9016 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9017 {
9018 EMSG(_(e_invaddr));
9019 return;
9020 }
9021
9022 if (eap->cmdidx == CMD_move)
9023 {
9024 if (do_move(eap->line1, eap->line2, n) == FAIL)
9025 return;
9026 }
9027 else
9028 ex_copy(eap->line1, eap->line2, n);
9029 u_clearline();
9030 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009031 ex_may_print(eap);
9032}
9033
9034/*
9035 * Print the current line if flags were given to the Ex command.
9036 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009037 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00009038ex_may_print(eap)
9039 exarg_T *eap;
9040{
9041 if (eap->flags != 0)
9042 {
9043 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9044 (eap->flags & EXFLAG_LIST));
9045 ex_no_reprint = TRUE;
9046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047}
9048
9049/*
9050 * ":smagic" and ":snomagic".
9051 */
9052 static void
9053ex_submagic(eap)
9054 exarg_T *eap;
9055{
9056 int magic_save = p_magic;
9057
9058 p_magic = (eap->cmdidx == CMD_smagic);
9059 do_sub(eap);
9060 p_magic = magic_save;
9061}
9062
9063/*
9064 * ":join".
9065 */
9066 static void
9067ex_join(eap)
9068 exarg_T *eap;
9069{
9070 curwin->w_cursor.lnum = eap->line1;
9071 if (eap->line1 == eap->line2)
9072 {
9073 if (eap->addr_count >= 2) /* :2,2join does nothing */
9074 return;
9075 if (eap->line2 == curbuf->b_ml.ml_line_count)
9076 {
9077 beep_flush();
9078 return;
9079 }
9080 ++eap->line2;
9081 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009082 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009084 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085}
9086
9087/*
9088 * ":[addr]@r" or ":[addr]*r": execute register
9089 */
9090 static void
9091ex_at(eap)
9092 exarg_T *eap;
9093{
9094 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009095 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096
9097 curwin->w_cursor.lnum = eap->line2;
9098
9099#ifdef USE_ON_FLY_SCROLL
9100 dont_scroll = TRUE; /* disallow scrolling here */
9101#endif
9102
9103 /* get the register name. No name means to use the previous one */
9104 c = *eap->arg;
9105 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9106 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009107 /* Put the register in the typeahead buffer with the "silent" flag. */
9108 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9109 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009110 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 else
9114 {
9115 int save_efr = exec_from_reg;
9116
9117 exec_from_reg = TRUE;
9118
9119 /*
9120 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009121 * Continue until the stuff buffer is empty and all added characters
9122 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009124 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9126
9127 exec_from_reg = save_efr;
9128 }
9129}
9130
9131/*
9132 * ":!".
9133 */
9134 static void
9135ex_bang(eap)
9136 exarg_T *eap;
9137{
9138 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9139}
9140
9141/*
9142 * ":undo".
9143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 static void
9145ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009146 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009148 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009149 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009150 else
9151 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152}
9153
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009154#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009155 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009156ex_wundo(eap)
9157 exarg_T *eap;
9158{
9159 char_u hash[UNDO_HASH_SIZE];
9160
9161 u_compute_hash(hash);
9162 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9163}
9164
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009165 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009166ex_rundo(eap)
9167 exarg_T *eap;
9168{
9169 char_u hash[UNDO_HASH_SIZE];
9170
9171 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009172 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009173}
9174#endif
9175
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176/*
9177 * ":redo".
9178 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 static void
9180ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009181 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182{
9183 u_redo(1);
9184}
9185
9186/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009187 * ":earlier" and ":later".
9188 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009189 static void
9190ex_later(eap)
9191 exarg_T *eap;
9192{
9193 long count = 0;
9194 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009195 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009196 char_u *p = eap->arg;
9197
9198 if (*p == NUL)
9199 count = 1;
9200 else if (isdigit(*p))
9201 {
9202 count = getdigits(&p);
9203 switch (*p)
9204 {
9205 case 's': ++p; sec = TRUE; break;
9206 case 'm': ++p; sec = TRUE; count *= 60; break;
9207 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009208 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9209 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009210 }
9211 }
9212
9213 if (*p != NUL)
9214 EMSG2(_(e_invarg2), eap->arg);
9215 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009216 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9217 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009218}
9219
9220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 * ":redir": start/stop redirection.
9222 */
9223 static void
9224ex_redir(eap)
9225 exarg_T *eap;
9226{
9227 char *mode;
9228 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009229 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230
9231 if (STRICMP(eap->arg, "END") == 0)
9232 close_redir();
9233 else
9234 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009235 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009237 ++arg;
9238 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009240 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 mode = "a";
9242 }
9243 else
9244 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009245 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246
9247 close_redir();
9248
9249 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009250 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 if (fname == NULL)
9252 return;
9253#ifdef FEAT_BROWSE
9254 if (cmdmod.browse)
9255 {
9256 char_u *browseFile;
9257
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009258 browseFile = do_browse(BROWSE_SAVE,
9259 (char_u *)_("Save Redirection"),
9260 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 if (browseFile == NULL)
9262 return; /* operation cancelled */
9263 vim_free(fname);
9264 fname = browseFile;
9265 eap->forceit = TRUE; /* since dialog already asked */
9266 }
9267#endif
9268
9269 redir_fd = open_exfile(fname, eap->forceit, mode);
9270 vim_free(fname);
9271 }
9272#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009273 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 {
9275 /* redirect to a register a-z (resp. A-Z for appending) */
9276 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009277 ++arg;
9278 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009280 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009281 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009283 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009285 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009286 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009287 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009288 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009290 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009291 if (*arg == '>')
9292 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009293 /* Make register empty when not using @A-@Z and the
9294 * command is valid. */
9295 if (*arg == NUL && !isupper(redir_reg))
9296 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009298 }
9299 if (*arg != NUL)
9300 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009301 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009302 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009303 }
9304 }
9305 else if (*arg == '=' && arg[1] == '>')
9306 {
9307 int append;
9308
9309 /* redirect to a variable */
9310 close_redir();
9311 arg += 2;
9312
9313 if (*arg == '>')
9314 {
9315 ++arg;
9316 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 }
9318 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009319 append = FALSE;
9320
9321 if (var_redir_start(skipwhite(arg), append) == OK)
9322 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 }
9324#endif
9325
9326 /* TODO: redirect to a buffer */
9327
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009329 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009331
9332 /* Make sure redirection is not off. Can happen for cmdline completion
9333 * that indirectly invokes a command to catch its output. */
9334 if (redir_fd != NULL
9335#ifdef FEAT_EVAL
9336 || redir_reg || redir_vname
9337#endif
9338 )
9339 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340}
9341
9342/*
9343 * ":redraw": force redraw
9344 */
9345 static void
9346ex_redraw(eap)
9347 exarg_T *eap;
9348{
9349 int r = RedrawingDisabled;
9350 int p = p_lz;
9351
9352 RedrawingDisabled = 0;
9353 p_lz = FALSE;
9354 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009355 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356#ifdef FEAT_TITLE
9357 if (need_maketitle)
9358 maketitle();
9359#endif
9360 RedrawingDisabled = r;
9361 p_lz = p;
9362
9363 /* Reset msg_didout, so that a message that's there is overwritten. */
9364 msg_didout = FALSE;
9365 msg_col = 0;
9366
Bram Moolenaar56667a52013-07-17 11:54:28 +02009367 /* No need to wait after an intentional redraw. */
9368 need_wait_return = FALSE;
9369
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 out_flush();
9371}
9372
9373/*
9374 * ":redrawstatus": force redraw of status line(s)
9375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 static void
9377ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009378 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379{
9380#if defined(FEAT_WINDOWS)
9381 int r = RedrawingDisabled;
9382 int p = p_lz;
9383
9384 RedrawingDisabled = 0;
9385 p_lz = FALSE;
9386 if (eap->forceit)
9387 status_redraw_all();
9388 else
9389 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009390 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 RedrawingDisabled = r;
9392 p_lz = p;
9393 out_flush();
9394#endif
9395}
9396
9397 static void
9398close_redir()
9399{
9400 if (redir_fd != NULL)
9401 {
9402 fclose(redir_fd);
9403 redir_fd = NULL;
9404 }
9405#ifdef FEAT_EVAL
9406 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009407 if (redir_vname)
9408 {
9409 var_redir_stop();
9410 redir_vname = 0;
9411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412#endif
9413}
9414
9415#if defined(FEAT_SESSION) && defined(USE_CRNL)
9416# define MKSESSION_NL
9417static int mksession_nl = FALSE; /* use NL only in put_eol() */
9418#endif
9419
9420/*
9421 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9422 */
9423 static void
9424ex_mkrc(eap)
9425 exarg_T *eap;
9426{
9427 FILE *fd;
9428 int failed = FALSE;
9429 char_u *fname;
9430#ifdef FEAT_BROWSE
9431 char_u *browseFile = NULL;
9432#endif
9433#ifdef FEAT_SESSION
9434 int view_session = FALSE;
9435 int using_vdir = FALSE; /* using 'viewdir'? */
9436 char_u *viewFile = NULL;
9437 unsigned *flagp;
9438#endif
9439
9440 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9441 {
9442#ifdef FEAT_SESSION
9443 view_session = TRUE;
9444#else
9445 ex_ni(eap);
9446 return;
9447#endif
9448 }
9449
9450#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009451 /* Use the short file name until ":lcd" is used. We also don't use the
9452 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009453 did_lcd = FALSE;
9454
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9456 if (eap->cmdidx == CMD_mkview
9457 && (*eap->arg == NUL
9458 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9459 {
9460 eap->forceit = TRUE;
9461 fname = get_view_file(*eap->arg);
9462 if (fname == NULL)
9463 return;
9464 viewFile = fname;
9465 using_vdir = TRUE;
9466 }
9467 else
9468#endif
9469 if (*eap->arg != NUL)
9470 fname = eap->arg;
9471 else if (eap->cmdidx == CMD_mkvimrc)
9472 fname = (char_u *)VIMRC_FILE;
9473#ifdef FEAT_SESSION
9474 else if (eap->cmdidx == CMD_mksession)
9475 fname = (char_u *)SESSION_FILE;
9476#endif
9477 else
9478 fname = (char_u *)EXRC_FILE;
9479
9480#ifdef FEAT_BROWSE
9481 if (cmdmod.browse)
9482 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009483 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484# ifdef FEAT_SESSION
9485 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9486 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9487# endif
9488 (char_u *)_("Save Setup"),
9489 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9490 if (browseFile == NULL)
9491 goto theend;
9492 fname = browseFile;
9493 eap->forceit = TRUE; /* since dialog already asked */
9494 }
9495#endif
9496
9497#if defined(FEAT_SESSION) && defined(vim_mkdir)
9498 /* When using 'viewdir' may have to create the directory. */
9499 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009500 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501#endif
9502
9503 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9504 if (fd != NULL)
9505 {
9506#ifdef FEAT_SESSION
9507 if (eap->cmdidx == CMD_mkview)
9508 flagp = &vop_flags;
9509 else
9510 flagp = &ssop_flags;
9511#endif
9512
9513#ifdef MKSESSION_NL
9514 /* "unix" in 'sessionoptions': use NL line separator */
9515 if (view_session && (*flagp & SSOP_UNIX))
9516 mksession_nl = TRUE;
9517#endif
9518
9519 /* Write the version command for :mkvimrc */
9520 if (eap->cmdidx == CMD_mkvimrc)
9521 (void)put_line(fd, "version 6.0");
9522
9523#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009524 if (eap->cmdidx == CMD_mksession)
9525 {
9526 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9527 failed = TRUE;
9528 }
9529
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 if (eap->cmdidx != CMD_mkview)
9531#endif
9532 {
9533 /* Write setting 'compatible' first, because it has side effects.
9534 * For that same reason only do it when needed. */
9535 if (p_cp)
9536 (void)put_line(fd, "if !&cp | set cp | endif");
9537 else
9538 (void)put_line(fd, "if &cp | set nocp | endif");
9539 }
9540
9541#ifdef FEAT_SESSION
9542 if (!view_session
9543 || (eap->cmdidx == CMD_mksession
9544 && (*flagp & SSOP_OPTIONS)))
9545#endif
9546 failed |= (makemap(fd, NULL) == FAIL
9547 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9548
9549#ifdef FEAT_SESSION
9550 if (!failed && view_session)
9551 {
9552 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9553 failed = TRUE;
9554 if (eap->cmdidx == CMD_mksession)
9555 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009556 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557
Bram Moolenaard9462e32011-04-11 21:35:11 +02009558 dirnow = alloc(MAXPATHL);
9559 if (dirnow == NULL)
9560 failed = TRUE;
9561 else
9562 {
9563 /*
9564 * Change to session file's dir.
9565 */
9566 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009568 *dirnow = NUL;
9569 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9570 {
9571 if (vim_chdirfile(fname) == OK)
9572 shorten_fnames(TRUE);
9573 }
9574 else if (*dirnow != NUL
9575 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9576 {
9577 if (mch_chdir((char *)globaldir) == 0)
9578 shorten_fnames(TRUE);
9579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580
Bram Moolenaard9462e32011-04-11 21:35:11 +02009581 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582
Bram Moolenaard9462e32011-04-11 21:35:11 +02009583 /* restore original dir */
9584 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009586 {
9587 if (mch_chdir((char *)dirnow) != 0)
9588 EMSG(_(e_prev_dir));
9589 shorten_fnames(TRUE);
9590 }
9591 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 }
9593 }
9594 else
9595 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009596 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9597 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 }
9599 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9600 == FAIL)
9601 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009602 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9603 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009604 if (eap->cmdidx == CMD_mksession)
9605 {
9606 if (put_line(fd, "unlet SessionLoad") == FAIL)
9607 failed = TRUE;
9608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 }
9610#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009611 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9612 failed = TRUE;
9613
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 failed |= fclose(fd);
9615
9616 if (failed)
9617 EMSG(_(e_write));
9618#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9619 else if (eap->cmdidx == CMD_mksession)
9620 {
9621 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009622 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623
Bram Moolenaard9462e32011-04-11 21:35:11 +02009624 tbuf = alloc(MAXPATHL);
9625 if (tbuf != NULL)
9626 {
9627 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9628 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9629 vim_free(tbuf);
9630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 }
9632#endif
9633#ifdef MKSESSION_NL
9634 mksession_nl = FALSE;
9635#endif
9636 }
9637
9638#ifdef FEAT_BROWSE
9639theend:
9640 vim_free(browseFile);
9641#endif
9642#ifdef FEAT_SESSION
9643 vim_free(viewFile);
9644#endif
9645}
9646
Bram Moolenaardf177f62005-02-22 08:39:57 +00009647#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9648 || defined(PROTO)
9649 int
9650vim_mkdir_emsg(name, prot)
9651 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009652 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009653{
9654 if (vim_mkdir(name, prot) != 0)
9655 {
9656 EMSG2(_("E739: Cannot create directory: %s"), name);
9657 return FAIL;
9658 }
9659 return OK;
9660}
9661#endif
9662
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663/*
9664 * Open a file for writing for an Ex command, with some checks.
9665 * Return file descriptor, or NULL on failure.
9666 */
9667 FILE *
9668open_exfile(fname, forceit, mode)
9669 char_u *fname;
9670 int forceit;
9671 char *mode; /* "w" for create new file or "a" for append */
9672{
9673 FILE *fd;
9674
9675#ifdef UNIX
9676 /* with Unix it is possible to open a directory */
9677 if (mch_isdir(fname))
9678 {
9679 EMSG2(_(e_isadir2), fname);
9680 return NULL;
9681 }
9682#endif
9683 if (!forceit && *mode != 'a' && vim_fexists(fname))
9684 {
9685 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9686 return NULL;
9687 }
9688
9689 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9690 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9691
9692 return fd;
9693}
9694
9695/*
9696 * ":mark" and ":k".
9697 */
9698 static void
9699ex_mark(eap)
9700 exarg_T *eap;
9701{
9702 pos_T pos;
9703
9704 if (*eap->arg == NUL) /* No argument? */
9705 EMSG(_(e_argreq));
9706 else if (eap->arg[1] != NUL) /* more than one character? */
9707 EMSG(_(e_trailing));
9708 else
9709 {
9710 pos = curwin->w_cursor; /* save curwin->w_cursor */
9711 curwin->w_cursor.lnum = eap->line2;
9712 beginline(BL_WHITE | BL_FIX);
9713 if (setmark(*eap->arg) == FAIL) /* set mark */
9714 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9715 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9716 }
9717}
9718
9719/*
9720 * Update w_topline, w_leftcol and the cursor position.
9721 */
9722 void
9723update_topline_cursor()
9724{
9725 check_cursor(); /* put cursor on valid line */
9726 update_topline();
9727 if (!curwin->w_p_wrap)
9728 validate_cursor();
9729 update_curswant();
9730}
9731
9732#ifdef FEAT_EX_EXTRA
9733/*
9734 * ":normal[!] {commands}": Execute normal mode commands.
9735 */
9736 static void
9737ex_normal(eap)
9738 exarg_T *eap;
9739{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 int save_msg_scroll = msg_scroll;
9741 int save_restart_edit = restart_edit;
9742 int save_msg_didout = msg_didout;
9743 int save_State = State;
9744 tasave_T tabuf;
9745 int save_insertmode = p_im;
9746 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009747 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748#ifdef FEAT_MBYTE
9749 char_u *arg = NULL;
9750 int l;
9751 char_u *p;
9752#endif
9753
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009754 if (ex_normal_lock > 0)
9755 {
9756 EMSG(_(e_secure));
9757 return;
9758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 if (ex_normal_busy >= p_mmd)
9760 {
9761 EMSG(_("E192: Recursive use of :normal too deep"));
9762 return;
9763 }
9764 ++ex_normal_busy;
9765
9766 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9767 restart_edit = 0; /* don't go to Insert mode */
9768 p_im = FALSE; /* don't use 'insertmode' */
9769
9770#ifdef FEAT_MBYTE
9771 /*
9772 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9773 * this for the K_SPECIAL leading byte, otherwise special keys will not
9774 * work.
9775 */
9776 if (has_mbyte)
9777 {
9778 int len = 0;
9779
9780 /* Count the number of characters to be escaped. */
9781 for (p = eap->arg; *p != NUL; ++p)
9782 {
9783# ifdef FEAT_GUI
9784 if (*p == CSI) /* leadbyte CSI */
9785 len += 2;
9786# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009787 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9789# ifdef FEAT_GUI
9790 || *p == CSI
9791# endif
9792 )
9793 len += 2;
9794 }
9795 if (len > 0)
9796 {
9797 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9798 if (arg != NULL)
9799 {
9800 len = 0;
9801 for (p = eap->arg; *p != NUL; ++p)
9802 {
9803 arg[len++] = *p;
9804# ifdef FEAT_GUI
9805 if (*p == CSI)
9806 {
9807 arg[len++] = KS_EXTRA;
9808 arg[len++] = (int)KE_CSI;
9809 }
9810# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009811 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 {
9813 arg[len++] = *++p;
9814 if (*p == K_SPECIAL)
9815 {
9816 arg[len++] = KS_SPECIAL;
9817 arg[len++] = KE_FILLER;
9818 }
9819# ifdef FEAT_GUI
9820 else if (*p == CSI)
9821 {
9822 arg[len++] = KS_EXTRA;
9823 arg[len++] = (int)KE_CSI;
9824 }
9825# endif
9826 }
9827 arg[len] = NUL;
9828 }
9829 }
9830 }
9831 }
9832#endif
9833
9834 /*
9835 * Save the current typeahead. This is required to allow using ":normal"
9836 * from an event handler and makes sure we don't hang when the argument
9837 * ends with half a command.
9838 */
9839 save_typeahead(&tabuf);
9840 if (tabuf.typebuf_valid)
9841 {
9842 /*
9843 * Repeat the :normal command for each line in the range. When no
9844 * range given, execute it just once, without positioning the cursor
9845 * first.
9846 */
9847 do
9848 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 if (eap->addr_count != 0)
9850 {
9851 curwin->w_cursor.lnum = eap->line1++;
9852 curwin->w_cursor.col = 0;
9853 }
9854
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009855 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856#ifdef FEAT_MBYTE
9857 arg != NULL ? arg :
9858#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009859 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 }
9861 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9862 }
9863
9864 /* Might not return to the main loop when in an event handler. */
9865 update_topline_cursor();
9866
9867 /* Restore the previous typeahead. */
9868 restore_typeahead(&tabuf);
9869
9870 --ex_normal_busy;
9871 msg_scroll = save_msg_scroll;
9872 restart_edit = save_restart_edit;
9873 p_im = save_insertmode;
9874 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009875 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9877
9878 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +01009879 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +01009881#ifdef FEAT_MOUSE
9882 setmouse();
9883#endif
9884#ifdef CURSOR_SHAPE
9885 ui_cursor_shape(); /* may show different cursor shape */
9886#endif
9887
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888#ifdef FEAT_MBYTE
9889 vim_free(arg);
9890#endif
9891}
9892
9893/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009894 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 */
9896 static void
9897ex_startinsert(eap)
9898 exarg_T *eap;
9899{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009900 if (eap->forceit)
9901 {
9902 coladvance((colnr_T)MAXCOL);
9903 curwin->w_curswant = MAXCOL;
9904 curwin->w_set_curswant = FALSE;
9905 }
9906
Bram Moolenaara40c5002005-01-09 21:16:21 +00009907 /* Ignore the command when already in Insert mode. Inserting an
9908 * expression register that invokes a function can do this. */
9909 if (State & INSERT)
9910 return;
9911
Bram Moolenaar64969662005-12-14 21:59:55 +00009912 if (eap->cmdidx == CMD_startinsert)
9913 restart_edit = 'a';
9914 else if (eap->cmdidx == CMD_startreplace)
9915 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009917 restart_edit = 'V';
9918
9919 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009921 if (eap->cmdidx == CMD_startinsert)
9922 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 curwin->w_curswant = 0; /* avoid MAXCOL */
9924 }
9925}
9926
9927/*
9928 * ":stopinsert"
9929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 static void
9931ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009932 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933{
9934 restart_edit = 0;
9935 stop_insert_mode = TRUE;
9936}
9937#endif
9938
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009939#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9940/*
9941 * Execute normal mode command "cmd".
9942 * "remap" can be REMAP_NONE or REMAP_YES.
9943 */
9944 void
9945exec_normal_cmd(cmd, remap, silent)
9946 char_u *cmd;
9947 int remap;
9948 int silent;
9949{
9950 oparg_T oa;
9951
9952 /*
9953 * Stuff the argument into the typeahead buffer.
9954 * Execute normal_cmd() until there is no typeahead left.
9955 */
9956 clear_oparg(&oa);
9957 finish_op = FALSE;
9958 ins_typebuf(cmd, remap, 0, TRUE, silent);
9959 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9960 && !got_int)
9961 {
9962 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009963 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009964 }
9965}
9966#endif
9967
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968#ifdef FEAT_FIND_ID
9969 static void
9970ex_checkpath(eap)
9971 exarg_T *eap;
9972{
9973 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9974 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9975 (linenr_T)1, (linenr_T)MAXLNUM);
9976}
9977
9978#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9979/*
9980 * ":psearch"
9981 */
9982 static void
9983ex_psearch(eap)
9984 exarg_T *eap;
9985{
9986 g_do_tagpreview = p_pvh;
9987 ex_findpat(eap);
9988 g_do_tagpreview = 0;
9989}
9990#endif
9991
9992 static void
9993ex_findpat(eap)
9994 exarg_T *eap;
9995{
9996 int whole = TRUE;
9997 long n;
9998 char_u *p;
9999 int action;
10000
10001 switch (cmdnames[eap->cmdidx].cmd_name[2])
10002 {
10003 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10004 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10005 action = ACTION_GOTO;
10006 else
10007 action = ACTION_SHOW;
10008 break;
10009 case 'i': /* ":ilist" and ":dlist" */
10010 action = ACTION_SHOW_ALL;
10011 break;
10012 case 'u': /* ":ijump" and ":djump" */
10013 action = ACTION_GOTO;
10014 break;
10015 default: /* ":isplit" and ":dsplit" */
10016 action = ACTION_SPLIT;
10017 break;
10018 }
10019
10020 n = 1;
10021 if (vim_isdigit(*eap->arg)) /* get count */
10022 {
10023 n = getdigits(&eap->arg);
10024 eap->arg = skipwhite(eap->arg);
10025 }
10026 if (*eap->arg == '/') /* Match regexp, not just whole words */
10027 {
10028 whole = FALSE;
10029 ++eap->arg;
10030 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10031 if (*p)
10032 {
10033 *p++ = NUL;
10034 p = skipwhite(p);
10035
10036 /* Check for trailing illegal characters */
10037 if (!ends_excmd(*p))
10038 eap->errmsg = e_trailing;
10039 else
10040 eap->nextcmd = check_nextcmd(p);
10041 }
10042 }
10043 if (!eap->skip)
10044 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10045 whole, !eap->forceit,
10046 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10047 n, action, eap->line1, eap->line2);
10048}
10049#endif
10050
10051#ifdef FEAT_WINDOWS
10052
10053# ifdef FEAT_QUICKFIX
10054/*
10055 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10056 */
10057 static void
10058ex_ptag(eap)
10059 exarg_T *eap;
10060{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010061 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10063}
10064
10065/*
10066 * ":pedit"
10067 */
10068 static void
10069ex_pedit(eap)
10070 exarg_T *eap;
10071{
10072 win_T *curwin_save = curwin;
10073
10074 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010075 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 keep_help_flag = curwin_save->w_buffer->b_help;
10077 do_exedit(eap, NULL);
10078 keep_help_flag = FALSE;
10079 if (curwin != curwin_save && win_valid(curwin_save))
10080 {
10081 /* Return cursor to where we were */
10082 validate_cursor();
10083 redraw_later(VALID);
10084 win_enter(curwin_save, TRUE);
10085 }
10086 g_do_tagpreview = 0;
10087}
10088# endif
10089
10090/*
10091 * ":stag", ":stselect" and ":stjump".
10092 */
10093 static void
10094ex_stag(eap)
10095 exarg_T *eap;
10096{
10097 postponed_split = -1;
10098 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010099 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10101 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010102 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103}
10104#endif
10105
10106/*
10107 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10108 */
10109 static void
10110ex_tag(eap)
10111 exarg_T *eap;
10112{
10113 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10114}
10115
10116 static void
10117ex_tag_cmd(eap, name)
10118 exarg_T *eap;
10119 char_u *name;
10120{
10121 int cmd;
10122
10123 switch (name[1])
10124 {
10125 case 'j': cmd = DT_JUMP; /* ":tjump" */
10126 break;
10127 case 's': cmd = DT_SELECT; /* ":tselect" */
10128 break;
10129 case 'p': cmd = DT_PREV; /* ":tprevious" */
10130 break;
10131 case 'N': cmd = DT_PREV; /* ":tNext" */
10132 break;
10133 case 'n': cmd = DT_NEXT; /* ":tnext" */
10134 break;
10135 case 'o': cmd = DT_POP; /* ":pop" */
10136 break;
10137 case 'f': /* ":tfirst" */
10138 case 'r': cmd = DT_FIRST; /* ":trewind" */
10139 break;
10140 case 'l': cmd = DT_LAST; /* ":tlast" */
10141 break;
10142 default: /* ":tag" */
10143#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010144 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 {
10146 do_cstag(eap);
10147 return;
10148 }
10149#endif
10150 cmd = DT_TAG;
10151 break;
10152 }
10153
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010154 if (name[0] == 'l')
10155 {
10156#ifndef FEAT_QUICKFIX
10157 ex_ni(eap);
10158 return;
10159#else
10160 cmd = DT_LTAG;
10161#endif
10162 }
10163
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10165 eap->forceit, TRUE);
10166}
10167
10168/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010169 * Check "str" for starting with a special cmdline variable.
10170 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10171 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10172 */
10173 int
10174find_cmdline_var(src, usedlen)
10175 char_u *src;
10176 int *usedlen;
10177{
10178 int len;
10179 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010180 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010181 "%",
10182#define SPEC_PERC 0
10183 "#",
10184#define SPEC_HASH 1
10185 "<cword>", /* cursor word */
10186#define SPEC_CWORD 2
10187 "<cWORD>", /* cursor WORD */
10188#define SPEC_CCWORD 3
10189 "<cfile>", /* cursor path name */
10190#define SPEC_CFILE 4
10191 "<sfile>", /* ":so" file name */
10192#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010193 "<slnum>", /* ":so" file line number */
10194#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010195#ifdef FEAT_AUTOCMD
10196 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010197# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010198 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010199# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010200 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010201# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010202#endif
10203#ifdef FEAT_CLIENTSERVER
10204 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010205# ifdef FEAT_AUTOCMD
10206# define SPEC_CLIENT 10
10207# else
10208# define SPEC_CLIENT 7
10209# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010210#endif
10211 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010212
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010213 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010214 {
10215 len = (int)STRLEN(spec_str[i]);
10216 if (STRNCMP(src, spec_str[i], len) == 0)
10217 {
10218 *usedlen = len;
10219 return i;
10220 }
10221 }
10222 return -1;
10223}
10224
10225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 * Evaluate cmdline variables.
10227 *
10228 * change '%' to curbuf->b_ffname
10229 * '#' to curwin->w_altfile
10230 * '<cword>' to word under the cursor
10231 * '<cWORD>' to WORD under the cursor
10232 * '<cfile>' to path name under the cursor
10233 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010234 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 * '<afile>' to file name for autocommand
10236 * '<abuf>' to buffer number for autocommand
10237 * '<amatch>' to matching name for autocommand
10238 *
10239 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10240 * "" for error without a message) and NULL is returned.
10241 * Returns an allocated string if a valid match was found.
10242 * Returns NULL if no match was found. "usedlen" then still contains the
10243 * number of characters to skip.
10244 */
10245 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +000010246eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010248 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 int *usedlen; /* characters after src that are used */
10250 linenr_T *lnump; /* line number for :e command, or NULL */
10251 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010252 int *escaped; /* return value has escaped white space (can
10253 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254{
10255 int i;
10256 char_u *s;
10257 char_u *result;
10258 char_u *resultbuf = NULL;
10259 int resultlen;
10260 buf_T *buf;
10261 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10262 int spec_idx;
10263#ifdef FEAT_MODIFY_FNAME
10264 int skip_mod = FALSE;
10265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267
10268 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010269 if (escaped != NULL)
10270 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271
10272 /*
10273 * Check if there is something to do.
10274 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010275 spec_idx = find_cmdline_var(src, usedlen);
10276 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 {
10278 *usedlen = 1;
10279 return NULL;
10280 }
10281
10282 /*
10283 * Skip when preceded with a backslash "\%" and "\#".
10284 * Note: In "\\%" the % is also not recognized!
10285 */
10286 if (src > srcstart && src[-1] == '\\')
10287 {
10288 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010289 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 return NULL;
10291 }
10292
10293 /*
10294 * word or WORD under cursor
10295 */
10296 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10297 {
10298 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10299 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10300 if (resultlen == 0)
10301 {
10302 *errormsg = (char_u *)"";
10303 return NULL;
10304 }
10305 }
10306
10307 /*
10308 * '#': Alternate file name
10309 * '%': Current file name
10310 * File name under the cursor
10311 * File name for autocommand
10312 * and following modifiers
10313 */
10314 else
10315 {
10316 switch (spec_idx)
10317 {
10318 case SPEC_PERC: /* '%': current file */
10319 if (curbuf->b_fname == NULL)
10320 {
10321 result = (char_u *)"";
10322 valid = 0; /* Must have ":p:h" to be valid */
10323 }
10324 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 break;
10327
10328 case SPEC_HASH: /* '#' or "#99": alternate file */
10329 if (src[1] == '#') /* "##": the argument list */
10330 {
10331 result = arg_all();
10332 resultbuf = result;
10333 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010334 if (escaped != NULL)
10335 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336#ifdef FEAT_MODIFY_FNAME
10337 skip_mod = TRUE;
10338#endif
10339 break;
10340 }
10341 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010342 if (*s == '<') /* "#<99" uses v:oldfiles */
10343 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 i = (int)getdigits(&s);
10345 *usedlen = (int)(s - src); /* length of what we expand */
10346
Bram Moolenaard812df62008-11-09 12:46:09 +000010347 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010349 if (*usedlen < 2)
10350 {
10351 /* Should we give an error message for #<text? */
10352 *usedlen = 1;
10353 return NULL;
10354 }
10355#ifdef FEAT_EVAL
10356 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10357 (long)i);
10358 if (result == NULL)
10359 {
10360 *errormsg = (char_u *)"";
10361 return NULL;
10362 }
10363#else
10364 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 }
10368 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010369 {
10370 buf = buflist_findnr(i);
10371 if (buf == NULL)
10372 {
10373 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10374 return NULL;
10375 }
10376 if (lnump != NULL)
10377 *lnump = ECMD_LAST;
10378 if (buf->b_fname == NULL)
10379 {
10380 result = (char_u *)"";
10381 valid = 0; /* Must have ":p:h" to be valid */
10382 }
10383 else
10384 result = buf->b_fname;
10385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 break;
10387
10388#ifdef FEAT_SEARCHPATH
10389 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010390 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 if (result == NULL)
10392 {
10393 *errormsg = (char_u *)"";
10394 return NULL;
10395 }
10396 resultbuf = result; /* remember allocated string */
10397 break;
10398#endif
10399
10400#ifdef FEAT_AUTOCMD
10401 case SPEC_AFILE: /* file name for autocommand */
10402 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010403 if (result != NULL && !autocmd_fname_full)
10404 {
10405 /* Still need to turn the fname into a full path. It is
10406 * postponed to avoid a delay when <afile> is not used. */
10407 autocmd_fname_full = TRUE;
10408 result = FullName_save(autocmd_fname, FALSE);
10409 vim_free(autocmd_fname);
10410 autocmd_fname = result;
10411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412 if (result == NULL)
10413 {
10414 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10415 return NULL;
10416 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010417 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418 break;
10419
10420 case SPEC_ABUF: /* buffer number for autocommand */
10421 if (autocmd_bufnr <= 0)
10422 {
10423 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10424 return NULL;
10425 }
10426 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10427 result = strbuf;
10428 break;
10429
10430 case SPEC_AMATCH: /* match name for autocommand */
10431 result = autocmd_match;
10432 if (result == NULL)
10433 {
10434 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10435 return NULL;
10436 }
10437 break;
10438
10439#endif
10440 case SPEC_SFILE: /* file name for ":so" command */
10441 result = sourcing_name;
10442 if (result == NULL)
10443 {
10444 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10445 return NULL;
10446 }
10447 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010448 case SPEC_SLNUM: /* line in file for ":so" command */
10449 if (sourcing_name == NULL || sourcing_lnum == 0)
10450 {
10451 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10452 return NULL;
10453 }
10454 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10455 result = strbuf;
10456 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457#if defined(FEAT_CLIENTSERVER)
10458 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010459 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10460 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 result = strbuf;
10462 break;
10463#endif
10464 }
10465
10466 resultlen = (int)STRLEN(result); /* length of new string */
10467 if (src[*usedlen] == '<') /* remove the file name extension */
10468 {
10469 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 resultlen = (int)(s - result);
10472 }
10473#ifdef FEAT_MODIFY_FNAME
10474 else if (!skip_mod)
10475 {
10476 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10477 &resultlen);
10478 if (result == NULL)
10479 {
10480 *errormsg = (char_u *)"";
10481 return NULL;
10482 }
10483 }
10484#endif
10485 }
10486
10487 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10488 {
10489 if (valid != VALID_HEAD + VALID_PATH)
10490 /* xgettext:no-c-format */
10491 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10492 else
10493 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10494 result = NULL;
10495 }
10496 else
10497 result = vim_strnsave(result, resultlen);
10498 vim_free(resultbuf);
10499 return result;
10500}
10501
10502/*
10503 * Concatenate all files in the argument list, separated by spaces, and return
10504 * it in one allocated string.
10505 * Spaces and backslashes in the file names are escaped with a backslash.
10506 * Returns NULL when out of memory.
10507 */
10508 static char_u *
10509arg_all()
10510{
10511 int len;
10512 int idx;
10513 char_u *retval = NULL;
10514 char_u *p;
10515
10516 /*
10517 * Do this loop two times:
10518 * first time: compute the total length
10519 * second time: concatenate the names
10520 */
10521 for (;;)
10522 {
10523 len = 0;
10524 for (idx = 0; idx < ARGCOUNT; ++idx)
10525 {
10526 p = alist_name(&ARGLIST[idx]);
10527 if (p != NULL)
10528 {
10529 if (len > 0)
10530 {
10531 /* insert a space in between names */
10532 if (retval != NULL)
10533 retval[len] = ' ';
10534 ++len;
10535 }
10536 for ( ; *p != NUL; ++p)
10537 {
10538 if (*p == ' ' || *p == '\\')
10539 {
10540 /* insert a backslash */
10541 if (retval != NULL)
10542 retval[len] = '\\';
10543 ++len;
10544 }
10545 if (retval != NULL)
10546 retval[len] = *p;
10547 ++len;
10548 }
10549 }
10550 }
10551
10552 /* second time: break here */
10553 if (retval != NULL)
10554 {
10555 retval[len] = NUL;
10556 break;
10557 }
10558
10559 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010560 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 if (retval == NULL)
10562 break;
10563 }
10564
10565 return retval;
10566}
10567
10568#if defined(FEAT_AUTOCMD) || defined(PROTO)
10569/*
10570 * Expand the <sfile> string in "arg".
10571 *
10572 * Returns an allocated string, or NULL for any error.
10573 */
10574 char_u *
10575expand_sfile(arg)
10576 char_u *arg;
10577{
10578 char_u *errormsg;
10579 int len;
10580 char_u *result;
10581 char_u *newres;
10582 char_u *repl;
10583 int srclen;
10584 char_u *p;
10585
10586 result = vim_strsave(arg);
10587 if (result == NULL)
10588 return NULL;
10589
10590 for (p = result; *p; )
10591 {
10592 if (STRNCMP(p, "<sfile>", 7) != 0)
10593 ++p;
10594 else
10595 {
10596 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010597 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598 if (errormsg != NULL)
10599 {
10600 if (*errormsg)
10601 emsg(errormsg);
10602 vim_free(result);
10603 return NULL;
10604 }
10605 if (repl == NULL) /* no match (cannot happen) */
10606 {
10607 p += srclen;
10608 continue;
10609 }
10610 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10611 newres = alloc(len);
10612 if (newres == NULL)
10613 {
10614 vim_free(repl);
10615 vim_free(result);
10616 return NULL;
10617 }
10618 mch_memmove(newres, result, (size_t)(p - result));
10619 STRCPY(newres + (p - result), repl);
10620 len = (int)STRLEN(newres);
10621 STRCAT(newres, p + srclen);
10622 vim_free(repl);
10623 vim_free(result);
10624 result = newres;
10625 p = newres + len; /* continue after the match */
10626 }
10627 }
10628
10629 return result;
10630}
10631#endif
10632
10633#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010634static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10635 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10637static frame_T *ses_skipframe __ARGS((frame_T *fr));
10638static int ses_do_frame __ARGS((frame_T *fr));
10639static int ses_do_win __ARGS((win_T *wp));
10640static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10641static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10642static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10643
10644/*
10645 * Write openfile commands for the current buffers to an .exrc file.
10646 * Return FAIL on error, OK otherwise.
10647 */
10648 static int
10649makeopens(fd, dirnow)
10650 FILE *fd;
10651 char_u *dirnow; /* Current directory name */
10652{
10653 buf_T *buf;
10654 int only_save_windows = TRUE;
10655 int nr;
10656 int cnr = 1;
10657 int restore_size = TRUE;
10658 win_T *wp;
10659 char_u *sname;
10660 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010661 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010662 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010663 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010664 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010665 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010666 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667
10668 if (ssop_flags & SSOP_BUFFERS)
10669 only_save_windows = FALSE; /* Save ALL buffers */
10670
10671 /*
10672 * Begin by setting the this_session variable, and then other
10673 * sessionable variables.
10674 */
10675#ifdef FEAT_EVAL
10676 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10677 return FAIL;
10678 if (ssop_flags & SSOP_GLOBALS)
10679 if (store_session_globals(fd) == FAIL)
10680 return FAIL;
10681#endif
10682
10683 /*
10684 * Close all windows but one.
10685 */
10686 if (put_line(fd, "silent only") == FAIL)
10687 return FAIL;
10688
10689 /*
10690 * Now a :cd command to the session directory or the current directory
10691 */
10692 if (ssop_flags & SSOP_SESDIR)
10693 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010694 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10695 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 return FAIL;
10697 }
10698 else if (ssop_flags & SSOP_CURDIR)
10699 {
10700 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10701 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010702 || fputs("cd ", fd) < 0
10703 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10704 || put_eol(fd) == FAIL)
10705 {
10706 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 vim_free(sname);
10710 }
10711
10712 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010713 * If there is an empty, unnamed buffer we will wipe it out later.
10714 * Remember the buffer number.
10715 */
10716 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10717 return FAIL;
10718 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10719 return FAIL;
10720 if (put_line(fd, "endif") == FAIL)
10721 return FAIL;
10722
10723 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 * Now save the current files, current buffer first.
10725 */
10726 if (put_line(fd, "set shortmess=aoO") == FAIL)
10727 return FAIL;
10728
10729 /* Now put the other buffers into the buffer list */
10730 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10731 {
10732 if (!(only_save_windows && buf->b_nwindows == 0)
10733 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10734 && buf->b_fname != NULL
10735 && buf->b_p_bl)
10736 {
10737 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10738 : buf->b_wininfo->wi_fpos.lnum) < 0
10739 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10740 return FAIL;
10741 }
10742 }
10743
10744 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010745 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10747 return FAIL;
10748
10749 if (ssop_flags & SSOP_RESIZE)
10750 {
10751 /* Note: after the restore we still check it worked!*/
10752 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10753 || put_eol(fd) == FAIL)
10754 return FAIL;
10755 }
10756
10757#ifdef FEAT_GUI
10758 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10759 {
10760 int x, y;
10761
10762 if (gui_mch_get_winpos(&x, &y) == OK)
10763 {
10764 /* Note: after the restore we still check it worked!*/
10765 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10766 return FAIL;
10767 }
10768 }
10769#endif
10770
10771 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010772 * When there are two or more tabpages and 'showtabline' is 1 the tabline
10773 * will be displayed when creating the next tab. That resizes the windows
10774 * in the first tab, which may cause problems. Set 'showtabline' to 2
10775 * temporarily to avoid that.
10776 */
10777 if (p_stal == 1 && first_tabpage->tp_next != NULL)
10778 {
10779 if (put_line(fd, "set stal=2") == FAIL)
10780 return FAIL;
10781 restore_stal = TRUE;
10782 }
10783
10784 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010785 * May repeat putting Windows for each tab, when "tabpages" is in
10786 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010787 * Don't use goto_tabpage(), it may change directory and trigger
10788 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010790 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010791 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010792 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010794 int need_tabnew = FALSE;
10795
Bram Moolenaar18144c82006-04-12 21:52:12 +000010796 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010798 tabpage_T *tp = find_tabpage(tabnr);
10799
10800 if (tp == NULL)
10801 break; /* done all tab pages */
10802 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010803 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010804 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010805 tab_topframe = topframe;
10806 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010807 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010808 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010809 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010810 tab_topframe = tp->tp_topframe;
10811 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010812 if (tabnr > 1)
10813 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815
Bram Moolenaar18144c82006-04-12 21:52:12 +000010816 /*
10817 * Before creating the window layout, try loading one file. If this
10818 * is aborted we don't end up with a number of useless windows.
10819 * This may have side effects! (e.g., compressed or network file).
10820 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010821 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010822 {
10823 if (ses_do_win(wp)
10824 && wp->w_buffer->b_ffname != NULL
10825 && !wp->w_buffer->b_help
10826#ifdef FEAT_QUICKFIX
10827 && !bt_nofile(wp->w_buffer)
10828#endif
10829 )
10830 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010831 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010832 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10833 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010834 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010835 if (!wp->w_arg_idx_invalid)
10836 edited_win = wp;
10837 break;
10838 }
10839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010841 /* If no file got edited create an empty tab page. */
10842 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10843 return FAIL;
10844
Bram Moolenaar18144c82006-04-12 21:52:12 +000010845 /*
10846 * Save current window layout.
10847 */
10848 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010850 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010852 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10853 return FAIL;
10854 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10855 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856
Bram Moolenaar18144c82006-04-12 21:52:12 +000010857 /*
10858 * Check if window sizes can be restored (no windows omitted).
10859 * Remember the window number of the current window after restoring.
10860 */
10861 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010862 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010863 {
10864 if (ses_do_win(wp))
10865 ++nr;
10866 else
10867 restore_size = FALSE;
10868 if (curwin == wp)
10869 cnr = nr;
10870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871
Bram Moolenaar18144c82006-04-12 21:52:12 +000010872 /* Go to the first window. */
10873 if (put_line(fd, "wincmd t") == FAIL)
10874 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010875
Bram Moolenaar18144c82006-04-12 21:52:12 +000010876 /*
10877 * If more than one window, see if sizes can be restored.
10878 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10879 * resized when moving between windows.
10880 * Do this before restoring the view, so that the topline and the
10881 * cursor can be set. This is done again below.
10882 */
10883 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10884 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010885 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010886 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887
Bram Moolenaar18144c82006-04-12 21:52:12 +000010888 /*
10889 * Restore the view of the window (options, file, cursor, etc.).
10890 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010891 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010892 {
10893 if (!ses_do_win(wp))
10894 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010895 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10896 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010897 return FAIL;
10898 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10899 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010900 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010901 }
10902
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010903 /* The argument index in the first tab page is zero, need to set it in
10904 * each window. For further tab pages it's the window where we do
10905 * "tabedit". */
10906 cur_arg_idx = next_arg_idx;
10907
Bram Moolenaar18144c82006-04-12 21:52:12 +000010908 /*
10909 * Restore cursor to the current window if it's not the first one.
10910 */
10911 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10912 || put_eol(fd) == FAIL))
10913 return FAIL;
10914
10915 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010916 * Restore window sizes again after jumping around in windows, because
10917 * the current window has a minimum size while others may not.
10918 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010919 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010920 return FAIL;
10921
Bram Moolenaar18144c82006-04-12 21:52:12 +000010922 /* Don't continue in another tab page when doing only the current one
10923 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010924 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010925 break;
10926 }
10927
10928 if (ssop_flags & SSOP_TABPAGES)
10929 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010930 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10931 || put_eol(fd) == FAIL)
10932 return FAIL;
10933 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010934 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
10935 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010936
Bram Moolenaar9c102382006-05-03 21:26:49 +000010937 /*
10938 * Wipe out an empty unnamed buffer we started in.
10939 */
10940 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10941 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010942 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010943 return FAIL;
10944 if (put_line(fd, "endif") == FAIL)
10945 return FAIL;
10946 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10947 return FAIL;
10948
10949 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10950 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10951 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10952 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953
10954 /*
10955 * Lastly, execute the x.vim file if it exists.
10956 */
10957 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10958 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010959 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 || put_line(fd, "endif") == FAIL)
10961 return FAIL;
10962
10963 return OK;
10964}
10965
10966 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010967ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 FILE *fd;
10969 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010970 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971{
10972 int n = 0;
10973 win_T *wp;
10974
10975 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10976 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010977 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 {
10979 if (!ses_do_win(wp))
10980 continue;
10981 ++n;
10982
10983 /* restore height when not full height */
10984 if (wp->w_height + wp->w_status_height < topframe->fr_height
10985 && (fprintf(fd,
10986 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10987 n, (long)wp->w_height, Rows / 2, Rows) < 0
10988 || put_eol(fd) == FAIL))
10989 return FAIL;
10990
10991 /* restore width when not full width */
10992 if (wp->w_width < Columns && (fprintf(fd,
10993 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10994 n, (long)wp->w_width, Columns / 2, Columns) < 0
10995 || put_eol(fd) == FAIL))
10996 return FAIL;
10997 }
10998 }
10999 else
11000 {
11001 /* Just equalise window sizes */
11002 if (put_line(fd, "wincmd =") == FAIL)
11003 return FAIL;
11004 }
11005 return OK;
11006}
11007
11008/*
11009 * Write commands to "fd" to recursively create windows for frame "fr",
11010 * horizontally and vertically split.
11011 * After the commands the last window in the frame is the current window.
11012 * Returns FAIL when writing the commands to "fd" fails.
11013 */
11014 static int
11015ses_win_rec(fd, fr)
11016 FILE *fd;
11017 frame_T *fr;
11018{
11019 frame_T *frc;
11020 int count = 0;
11021
11022 if (fr->fr_layout != FR_LEAF)
11023 {
11024 /* Find first frame that's not skipped and then create a window for
11025 * each following one (first frame is already there). */
11026 frc = ses_skipframe(fr->fr_child);
11027 if (frc != NULL)
11028 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11029 {
11030 /* Make window as big as possible so that we have lots of room
11031 * to split. */
11032 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11033 || put_line(fd, fr->fr_layout == FR_COL
11034 ? "split" : "vsplit") == FAIL)
11035 return FAIL;
11036 ++count;
11037 }
11038
11039 /* Go back to the first window. */
11040 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11041 ? "%dwincmd k" : "%dwincmd h", count) < 0
11042 || put_eol(fd) == FAIL))
11043 return FAIL;
11044
11045 /* Recursively create frames/windows in each window of this column or
11046 * row. */
11047 frc = ses_skipframe(fr->fr_child);
11048 while (frc != NULL)
11049 {
11050 ses_win_rec(fd, frc);
11051 frc = ses_skipframe(frc->fr_next);
11052 /* Go to next window. */
11053 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11054 return FAIL;
11055 }
11056 }
11057 return OK;
11058}
11059
11060/*
11061 * Skip frames that don't contain windows we want to save in the Session.
11062 * Returns NULL when there none.
11063 */
11064 static frame_T *
11065ses_skipframe(fr)
11066 frame_T *fr;
11067{
11068 frame_T *frc;
11069
11070 for (frc = fr; frc != NULL; frc = frc->fr_next)
11071 if (ses_do_frame(frc))
11072 break;
11073 return frc;
11074}
11075
11076/*
11077 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11078 * the Session.
11079 */
11080 static int
11081ses_do_frame(fr)
11082 frame_T *fr;
11083{
11084 frame_T *frc;
11085
11086 if (fr->fr_layout == FR_LEAF)
11087 return ses_do_win(fr->fr_win);
11088 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11089 if (ses_do_frame(frc))
11090 return TRUE;
11091 return FALSE;
11092}
11093
11094/*
11095 * Return non-zero if window "wp" is to be stored in the Session.
11096 */
11097 static int
11098ses_do_win(wp)
11099 win_T *wp;
11100{
11101 if (wp->w_buffer->b_fname == NULL
11102#ifdef FEAT_QUICKFIX
11103 /* When 'buftype' is "nofile" can't restore the window contents. */
11104 || bt_nofile(wp->w_buffer)
11105#endif
11106 )
11107 return (ssop_flags & SSOP_BLANK);
11108 if (wp->w_buffer->b_help)
11109 return (ssop_flags & SSOP_HELP);
11110 return TRUE;
11111}
11112
11113/*
11114 * Write commands to "fd" to restore the view of a window.
11115 * Caller must make sure 'scrolloff' is zero.
11116 */
11117 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011118put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 FILE *fd;
11120 win_T *wp;
11121 int add_edit; /* add ":edit" command to view */
11122 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011123 int current_arg_idx; /* current argument index of the window, use
11124 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125{
11126 win_T *save_curwin;
11127 int f;
11128 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011129 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130
11131 /* Always restore cursor position for ":mksession". For ":mkview" only
11132 * when 'viewoptions' contains "cursor". */
11133 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11134
11135 /*
11136 * Local argument list.
11137 */
11138 if (wp->w_alist == &global_alist)
11139 {
11140 if (put_line(fd, "argglobal") == FAIL)
11141 return FAIL;
11142 }
11143 else
11144 {
11145 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11146 flagp == &vop_flags
11147 || !(*flagp & SSOP_CURDIR)
11148 || wp->w_localdir != NULL, flagp) == FAIL)
11149 return FAIL;
11150 }
11151
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011152 /* Only when part of a session: restore the argument index. Some
11153 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011154 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011155 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011157 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 || put_eol(fd) == FAIL)
11159 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011160 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 }
11162
11163 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011164 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 {
11166 /*
11167 * Load the file.
11168 */
11169 if (wp->w_buffer->b_ffname != NULL
11170#ifdef FEAT_QUICKFIX
11171 && !bt_nofile(wp->w_buffer)
11172#endif
11173 )
11174 {
11175 /*
11176 * Editing a file in this buffer: use ":edit file".
11177 * This may have side effects! (e.g., compressed or network file).
11178 */
11179 if (fputs("edit ", fd) < 0
11180 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11181 return FAIL;
11182 }
11183 else
11184 {
11185 /* No file in this buffer, just make it empty. */
11186 if (put_line(fd, "enew") == FAIL)
11187 return FAIL;
11188#ifdef FEAT_QUICKFIX
11189 if (wp->w_buffer->b_ffname != NULL)
11190 {
11191 /* The buffer does have a name, but it's not a file name. */
11192 if (fputs("file ", fd) < 0
11193 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11194 return FAIL;
11195 }
11196#endif
11197 do_cursor = FALSE;
11198 }
11199 }
11200
11201 /*
11202 * Local mappings and abbreviations.
11203 */
11204 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11205 && makemap(fd, wp->w_buffer) == FAIL)
11206 return FAIL;
11207
11208 /*
11209 * Local options. Need to go to the window temporarily.
11210 * Store only local values when using ":mkview" and when ":mksession" is
11211 * used and 'sessionoptions' doesn't include "options".
11212 * Some folding options are always stored when "folds" is included,
11213 * otherwise the folds would not be restored correctly.
11214 */
11215 save_curwin = curwin;
11216 curwin = wp;
11217 curbuf = curwin->w_buffer;
11218 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11219 f = makeset(fd, OPT_LOCAL,
11220 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11221#ifdef FEAT_FOLDING
11222 else if (*flagp & SSOP_FOLDS)
11223 f = makefoldset(fd);
11224#endif
11225 else
11226 f = OK;
11227 curwin = save_curwin;
11228 curbuf = curwin->w_buffer;
11229 if (f == FAIL)
11230 return FAIL;
11231
11232#ifdef FEAT_FOLDING
11233 /*
11234 * Save Folds when 'buftype' is empty and for help files.
11235 */
11236 if ((*flagp & SSOP_FOLDS)
11237 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011238# ifdef FEAT_QUICKFIX
11239 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11240# endif
11241 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242 {
11243 if (put_folds(fd, wp) == FAIL)
11244 return FAIL;
11245 }
11246#endif
11247
11248 /*
11249 * Set the cursor after creating folds, since that moves the cursor.
11250 */
11251 if (do_cursor)
11252 {
11253
11254 /* Restore the cursor line in the file and relatively in the
11255 * window. Don't use "G", it changes the jumplist. */
11256 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11257 (long)wp->w_cursor.lnum,
11258 (long)(wp->w_cursor.lnum - wp->w_topline),
11259 (long)wp->w_height / 2, (long)wp->w_height) < 0
11260 || put_eol(fd) == FAIL
11261 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11262 || put_line(fd, "exe s:l") == FAIL
11263 || put_line(fd, "normal! zt") == FAIL
11264 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11265 || put_eol(fd) == FAIL)
11266 return FAIL;
11267 /* Restore the cursor column and left offset when not wrapping. */
11268 if (wp->w_cursor.col == 0)
11269 {
11270 if (put_line(fd, "normal! 0") == FAIL)
11271 return FAIL;
11272 }
11273 else
11274 {
11275 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11276 {
11277 if (fprintf(fd,
11278 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011279 (long)wp->w_virtcol + 1,
11280 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 (long)wp->w_width / 2, (long)wp->w_width) < 0
11282 || put_eol(fd) == FAIL
11283 || put_line(fd, "if s:c > 0") == FAIL
11284 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011285 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11286 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 || put_eol(fd) == FAIL
11288 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011289 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 || put_eol(fd) == FAIL
11291 || put_line(fd, "endif") == FAIL)
11292 return FAIL;
11293 }
11294 else
11295 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011296 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 || put_eol(fd) == FAIL)
11298 return FAIL;
11299 }
11300 }
11301 }
11302
11303 /*
11304 * Local directory.
11305 */
11306 if (wp->w_localdir != NULL)
11307 {
11308 if (fputs("lcd ", fd) < 0
11309 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11310 || put_eol(fd) == FAIL)
11311 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011312 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313 }
11314
11315 return OK;
11316}
11317
11318/*
11319 * Write an argument list to the session file.
11320 * Returns FAIL if writing fails.
11321 */
11322 static int
11323ses_arglist(fd, cmd, gap, fullname, flagp)
11324 FILE *fd;
11325 char *cmd;
11326 garray_T *gap;
11327 int fullname; /* TRUE: use full path name */
11328 unsigned *flagp;
11329{
11330 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011331 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 char_u *s;
11333
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011334 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11335 return FAIL;
11336 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337 return FAIL;
11338 for (i = 0; i < gap->ga_len; ++i)
11339 {
11340 /* NULL file names are skipped (only happens when out of memory). */
11341 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11342 if (s != NULL)
11343 {
11344 if (fullname)
11345 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011346 buf = alloc(MAXPATHL);
11347 if (buf != NULL)
11348 {
11349 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11350 s = buf;
11351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011353 if (fputs("argadd ", fd) < 0
11354 || ses_put_fname(fd, s, flagp) == FAIL
11355 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011356 {
11357 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011359 }
11360 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 }
11362 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011363 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364}
11365
11366/*
11367 * Write a buffer name to the session file.
11368 * Also ends the line.
11369 * Returns FAIL if writing fails.
11370 */
11371 static int
11372ses_fname(fd, buf, flagp)
11373 FILE *fd;
11374 buf_T *buf;
11375 unsigned *flagp;
11376{
11377 char_u *name;
11378
11379 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011380 * the session file will be sourced.
11381 * Don't do this for ":mkview", we don't know the current directory.
11382 * Don't do this after ":lcd", we don't keep track of what the current
11383 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384 if (buf->b_sfname != NULL
11385 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011386 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011387#ifdef FEAT_AUTOCHDIR
11388 && !p_acd
11389#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011390 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391 name = buf->b_sfname;
11392 else
11393 name = buf->b_ffname;
11394 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11395 return FAIL;
11396 return OK;
11397}
11398
11399/*
11400 * Write a file name to the session file.
11401 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11402 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011403 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404 */
11405 static int
11406ses_put_fname(fd, name, flagp)
11407 FILE *fd;
11408 char_u *name;
11409 unsigned *flagp;
11410{
11411 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011412 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414
11415 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011416 if (sname == NULL)
11417 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011419 if (*flagp & SSOP_SLASH)
11420 {
11421 /* change all backslashes to forward slashes */
11422 for (p = sname; *p != NUL; mb_ptr_adv(p))
11423 if (*p == '\\')
11424 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011426
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011427 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011428 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011430 if (p == NULL)
11431 return FAIL;
11432
11433 /* write the result */
11434 if (fputs((char *)p, fd) < 0)
11435 retval = FAIL;
11436
11437 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438 return retval;
11439}
11440
11441/*
11442 * ":loadview [nr]"
11443 */
11444 static void
11445ex_loadview(eap)
11446 exarg_T *eap;
11447{
11448 char_u *fname;
11449
11450 fname = get_view_file(*eap->arg);
11451 if (fname != NULL)
11452 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011453 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 vim_free(fname);
11455 }
11456}
11457
11458/*
11459 * Get the name of the view file for the current buffer.
11460 */
11461 static char_u *
11462get_view_file(c)
11463 int c;
11464{
11465 int len = 0;
11466 char_u *p, *s;
11467 char_u *retval;
11468 char_u *sname;
11469
11470 if (curbuf->b_ffname == NULL)
11471 {
11472 EMSG(_(e_noname));
11473 return NULL;
11474 }
11475 sname = home_replace_save(NULL, curbuf->b_ffname);
11476 if (sname == NULL)
11477 return NULL;
11478
11479 /*
11480 * We want a file name without separators, because we're not going to make
11481 * a directory.
11482 * "normal" path separator -> "=+"
11483 * "=" -> "=="
11484 * ":" path separator -> "=-"
11485 */
11486 for (p = sname; *p; ++p)
11487 if (*p == '=' || vim_ispathsep(*p))
11488 ++len;
11489 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11490 if (retval != NULL)
11491 {
11492 STRCPY(retval, p_vdir);
11493 add_pathsep(retval);
11494 s = retval + STRLEN(retval);
11495 for (p = sname; *p; ++p)
11496 {
11497 if (*p == '=')
11498 {
11499 *s++ = '=';
11500 *s++ = '=';
11501 }
11502 else if (vim_ispathsep(*p))
11503 {
11504 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011505#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506 if (*p == ':')
11507 *s++ = '-';
11508 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011510 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511 }
11512 else
11513 *s++ = *p;
11514 }
11515 *s++ = '=';
11516 *s++ = c;
11517 STRCPY(s, ".vim");
11518 }
11519
11520 vim_free(sname);
11521 return retval;
11522}
11523
11524#endif /* FEAT_SESSION */
11525
11526/*
11527 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11528 * Return FAIL for a write error.
11529 */
11530 int
11531put_eol(fd)
11532 FILE *fd;
11533{
11534 if (
11535#ifdef USE_CRNL
11536 (
11537# ifdef MKSESSION_NL
11538 !mksession_nl &&
11539# endif
11540 (putc('\r', fd) < 0)) ||
11541#endif
11542 (putc('\n', fd) < 0))
11543 return FAIL;
11544 return OK;
11545}
11546
11547/*
11548 * Write a line to "fd".
11549 * Return FAIL for a write error.
11550 */
11551 int
11552put_line(fd, s)
11553 FILE *fd;
11554 char *s;
11555{
11556 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11557 return FAIL;
11558 return OK;
11559}
11560
11561#ifdef FEAT_VIMINFO
11562/*
11563 * ":rviminfo" and ":wviminfo".
11564 */
11565 static void
11566ex_viminfo(eap)
11567 exarg_T *eap;
11568{
11569 char_u *save_viminfo;
11570
11571 save_viminfo = p_viminfo;
11572 if (*p_viminfo == NUL)
11573 p_viminfo = (char_u *)"'100";
11574 if (eap->cmdidx == CMD_rviminfo)
11575 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011576 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11577 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578 EMSG(_("E195: Cannot open viminfo file for reading"));
11579 }
11580 else
11581 write_viminfo(eap->arg, eap->forceit);
11582 p_viminfo = save_viminfo;
11583}
11584#endif
11585
11586#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011587/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011588 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011589 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591 void
11592dialog_msg(buff, format, fname)
11593 char_u *buff;
11594 char *format;
11595 char_u *fname;
11596{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597 if (fname == NULL)
11598 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011599 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600}
11601#endif
11602
11603/*
11604 * ":behave {mswin,xterm}"
11605 */
11606 static void
11607ex_behave(eap)
11608 exarg_T *eap;
11609{
11610 if (STRCMP(eap->arg, "mswin") == 0)
11611 {
11612 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11613 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11614 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11615 set_option_value((char_u *)"keymodel", 0L,
11616 (char_u *)"startsel,stopsel", 0);
11617 }
11618 else if (STRCMP(eap->arg, "xterm") == 0)
11619 {
11620 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11621 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11622 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11623 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11624 }
11625 else
11626 EMSG2(_(e_invarg2), eap->arg);
11627}
11628
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011629#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11630/*
11631 * Function given to ExpandGeneric() to obtain the possible arguments of the
11632 * ":behave {mswin,xterm}" command.
11633 */
11634 char_u *
11635get_behave_arg(xp, idx)
11636 expand_T *xp UNUSED;
11637 int idx;
11638{
11639 if (idx == 0)
11640 return (char_u *)"mswin";
11641 if (idx == 1)
11642 return (char_u *)"xterm";
11643 return NULL;
11644}
11645#endif
11646
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647#ifdef FEAT_AUTOCMD
11648static int filetype_detect = FALSE;
11649static int filetype_plugin = FALSE;
11650static int filetype_indent = FALSE;
11651
11652/*
11653 * ":filetype [plugin] [indent] {on,off,detect}"
11654 * on: Load the filetype.vim file to install autocommands for file types.
11655 * off: Load the ftoff.vim file to remove all autocommands for file types.
11656 * plugin on: load filetype.vim and ftplugin.vim
11657 * plugin off: load ftplugof.vim
11658 * indent on: load filetype.vim and indent.vim
11659 * indent off: load indoff.vim
11660 */
11661 static void
11662ex_filetype(eap)
11663 exarg_T *eap;
11664{
11665 char_u *arg = eap->arg;
11666 int plugin = FALSE;
11667 int indent = FALSE;
11668
11669 if (*eap->arg == NUL)
11670 {
11671 /* Print current status. */
11672 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11673 filetype_detect ? "ON" : "OFF",
11674 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11675 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11676 return;
11677 }
11678
11679 /* Accept "plugin" and "indent" in any order. */
11680 for (;;)
11681 {
11682 if (STRNCMP(arg, "plugin", 6) == 0)
11683 {
11684 plugin = TRUE;
11685 arg = skipwhite(arg + 6);
11686 continue;
11687 }
11688 if (STRNCMP(arg, "indent", 6) == 0)
11689 {
11690 indent = TRUE;
11691 arg = skipwhite(arg + 6);
11692 continue;
11693 }
11694 break;
11695 }
11696 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11697 {
11698 if (*arg == 'o' || !filetype_detect)
11699 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011700 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701 filetype_detect = TRUE;
11702 if (plugin)
11703 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011704 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 filetype_plugin = TRUE;
11706 }
11707 if (indent)
11708 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011709 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 filetype_indent = TRUE;
11711 }
11712 }
11713 if (*arg == 'd')
11714 {
11715 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011716 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717 }
11718 }
11719 else if (STRCMP(arg, "off") == 0)
11720 {
11721 if (plugin || indent)
11722 {
11723 if (plugin)
11724 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011725 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726 filetype_plugin = FALSE;
11727 }
11728 if (indent)
11729 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011730 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 filetype_indent = FALSE;
11732 }
11733 }
11734 else
11735 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011736 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 filetype_detect = FALSE;
11738 }
11739 }
11740 else
11741 EMSG2(_(e_invarg2), arg);
11742}
11743
11744/*
11745 * ":setfiletype {name}"
11746 */
11747 static void
11748ex_setfiletype(eap)
11749 exarg_T *eap;
11750{
11751 if (!did_filetype)
11752 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11753}
11754#endif
11755
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756 static void
11757ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011758 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759{
11760#ifdef FEAT_DIGRAPHS
11761 if (*eap->arg != NUL)
11762 putdigraph(eap->arg);
11763 else
11764 listdigraphs();
11765#else
11766 EMSG(_("E196: No digraphs in this version"));
11767#endif
11768}
11769
11770 static void
11771ex_set(eap)
11772 exarg_T *eap;
11773{
11774 int flags = 0;
11775
11776 if (eap->cmdidx == CMD_setlocal)
11777 flags = OPT_LOCAL;
11778 else if (eap->cmdidx == CMD_setglobal)
11779 flags = OPT_GLOBAL;
11780#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11781 if (cmdmod.browse && flags == 0)
11782 ex_options(eap);
11783 else
11784#endif
11785 (void)do_set(eap->arg, flags);
11786}
11787
11788#ifdef FEAT_SEARCH_EXTRA
11789/*
11790 * ":nohlsearch"
11791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792 static void
11793ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011794 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011796 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011797 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798}
11799
11800/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011801 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802 * Sets nextcmd to the start of the next command, if any. Also called when
11803 * skipping commands to find the next command.
11804 */
11805 static void
11806ex_match(eap)
11807 exarg_T *eap;
11808{
11809 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011810 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811 char_u *end;
11812 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011813 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011814
11815 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011816 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011817 else
11818 {
11819 EMSG(e_invcmd);
11820 return;
11821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822
11823 /* First clear any old pattern. */
11824 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011825 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826
11827 if (ends_excmd(*eap->arg))
11828 end = eap->arg;
11829 else if ((STRNICMP(eap->arg, "none", 4) == 0
11830 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11831 end = eap->arg + 4;
11832 else
11833 {
11834 p = skiptowhite(eap->arg);
11835 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011836 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 p = skipwhite(p);
11838 if (*p == NUL)
11839 {
11840 /* There must be two arguments. */
11841 EMSG2(_(e_invarg2), eap->arg);
11842 return;
11843 }
11844 end = skip_regexp(p + 1, *p, TRUE, NULL);
11845 if (!eap->skip)
11846 {
11847 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11848 {
11849 eap->errmsg = e_trailing;
11850 return;
11851 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011852 if (*end != *p)
11853 {
11854 EMSG2(_(e_invarg2), p);
11855 return;
11856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857
11858 c = *end;
11859 *end = NUL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011860 match_add(curwin, g, p + 1, 10, id, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011861 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011862 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863 }
11864 }
11865 eap->nextcmd = find_nextcmd(end);
11866}
11867#endif
11868
11869#ifdef FEAT_CRYPT
11870/*
11871 * ":X": Get crypt key
11872 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 static void
11874ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011875 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010011877 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020011878 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879}
11880#endif
11881
11882#ifdef FEAT_FOLDING
11883 static void
11884ex_fold(eap)
11885 exarg_T *eap;
11886{
11887 if (foldManualAllowed(TRUE))
11888 foldCreate(eap->line1, eap->line2);
11889}
11890
11891 static void
11892ex_foldopen(eap)
11893 exarg_T *eap;
11894{
11895 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11896 eap->forceit, FALSE);
11897}
11898
11899 static void
11900ex_folddo(eap)
11901 exarg_T *eap;
11902{
11903 linenr_T lnum;
11904
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011905#ifdef FEAT_CLIPBOARD
11906 start_global_changes();
11907#endif
11908
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909 /* First set the marks for all lines closed/open. */
11910 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11911 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11912 ml_setmarked(lnum);
11913
11914 /* Execute the command on the marked lines. */
11915 global_exe(eap->arg);
11916 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011917#ifdef FEAT_CLIPBOARD
11918 end_global_changes();
11919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920}
11921#endif