blob: 607dbd9eec165f9623de14c2dab1ed686abd8bd1 [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 Moolenaarf8a447c2014-11-30 22:51:06 +01002132 if (ea.cmdidx != CMD_SIZE
2133#ifdef FEAT_USR_CMDS
2134 && ea.cmdidx != CMD_USER
2135#endif
2136 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002137 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2138 else
2139 ea.addr_type = ADDR_LINES;
2140 ea.cmd = cmd;
2141
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 /* repeat for all ',' or ';' separated addresses */
2143 for (;;)
2144 {
2145 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002146 switch (ea.addr_type)
2147 {
2148 case ADDR_LINES:
2149 /* default is current line number */
2150 ea.line2 = curwin->w_cursor.lnum;
2151 break;
2152 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002153 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002154 ea.line2 = lnum;
2155 break;
2156 case ADDR_ARGUMENTS:
2157 ea.line2 = curwin->w_arg_idx + 1;
2158 break;
2159 case ADDR_LOADED_BUFFERS:
2160 case ADDR_UNLOADED_BUFFERS:
2161 ea.line2 = curbuf->b_fnum;
2162 break;
2163 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002164 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002165 ea.line2 = lnum;
2166 break;
2167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002169 lnum = get_address(&ea.cmd, ea.addr_type, ea.skip, ea.addr_count == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 if (ea.cmd == NULL) /* error detected */
2171 goto doend;
2172 if (lnum == MAXLNUM)
2173 {
2174 if (*ea.cmd == '%') /* '%' - all lines */
2175 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002176 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002178 switch (ea.addr_type)
2179 {
2180 case ADDR_LINES:
2181 ea.line1 = 1;
2182 ea.line2 = curbuf->b_ml.ml_line_count;
2183 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002184 case ADDR_LOADED_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002185 buf = firstbuf;
2186 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2187 buf = buf->b_next;
2188 ea.line1 = buf->b_fnum;
2189 buf = lastbuf;
2190 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2191 buf = buf->b_prev;
2192 ea.line2 = buf->b_fnum;
2193 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002194 case ADDR_UNLOADED_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002195 ea.line1 = firstbuf->b_fnum;
2196 ea.line2 = lastbuf->b_fnum;
2197 break;
2198 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002199 case ADDR_TABS:
2200 errormsg = (char_u *)_(e_invrange);
2201 goto doend;
2202 break;
2203 case ADDR_ARGUMENTS:
2204 ea.line1 = 1;
2205 ea.line2 = ARGCOUNT;
2206 break;
2207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 ++ea.addr_count;
2209 }
2210 /* '*' - visual area */
2211 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2212 {
2213 pos_T *fp;
2214
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002215 if (ea.addr_type != ADDR_LINES)
2216 {
2217 errormsg = (char_u *)_(e_invrange);
2218 goto doend;
2219 }
2220
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 ++ea.cmd;
2222 if (!ea.skip)
2223 {
2224 fp = getmark('<', FALSE);
2225 if (check_mark(fp) == FAIL)
2226 goto doend;
2227 ea.line1 = fp->lnum;
2228 fp = getmark('>', FALSE);
2229 if (check_mark(fp) == FAIL)
2230 goto doend;
2231 ea.line2 = fp->lnum;
2232 ++ea.addr_count;
2233 }
2234 }
2235 }
2236 else
2237 ea.line2 = lnum;
2238 ea.addr_count++;
2239
2240 if (*ea.cmd == ';')
2241 {
2242 if (!ea.skip)
2243 curwin->w_cursor.lnum = ea.line2;
2244 }
2245 else if (*ea.cmd != ',')
2246 break;
2247 ++ea.cmd;
2248 }
2249
2250 /* One address given: set start and end lines */
2251 if (ea.addr_count == 1)
2252 {
2253 ea.line1 = ea.line2;
2254 /* ... but only implicit: really no address given */
2255 if (lnum == MAXLNUM)
2256 ea.addr_count = 0;
2257 }
2258
2259 /* Don't leave the cursor on an illegal line (caused by ';') */
2260 check_cursor_lnum();
2261
2262/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002263 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 */
2265
2266 /*
2267 * Skip ':' and any white space
2268 */
2269 ea.cmd = skipwhite(ea.cmd);
2270 while (*ea.cmd == ':')
2271 ea.cmd = skipwhite(ea.cmd + 1);
2272
2273 /*
2274 * If we got a line, but no command, then go to the line.
2275 * If we find a '|' or '\n' we set ea.nextcmd.
2276 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002277 if (*ea.cmd == NUL || *ea.cmd == '"'
2278 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 {
2280 /*
2281 * strange vi behaviour:
2282 * ":3" jumps to line 3
2283 * ":3|..." prints line 3
2284 * ":|" prints current line
2285 */
2286 if (ea.skip) /* skip this if inside :if */
2287 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002288 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
2290 ea.cmdidx = CMD_print;
2291 ea.argt = RANGE+COUNT+TRLBAR;
2292 if ((errormsg = invalid_range(&ea)) == NULL)
2293 {
2294 correct_range(&ea);
2295 ex_print(&ea);
2296 }
2297 }
2298 else if (ea.addr_count != 0)
2299 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002300 if (ea.line2 > curbuf->b_ml.ml_line_count)
2301 {
2302 /* With '-' in 'cpoptions' a line number past the file is an
2303 * error, otherwise put it at the end of the file. */
2304 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2305 ea.line2 = -1;
2306 else
2307 ea.line2 = curbuf->b_ml.ml_line_count;
2308 }
2309
2310 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002311 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 else
2313 {
2314 if (ea.line2 == 0)
2315 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 else
2317 curwin->w_cursor.lnum = ea.line2;
2318 beginline(BL_SOL | BL_FIX);
2319 }
2320 }
2321 goto doend;
2322 }
2323
Bram Moolenaard5005162014-08-22 23:05:54 +02002324#ifdef FEAT_AUTOCMD
2325 /* If this looks like an undefined user command and there are CmdUndefined
2326 * autocommands defined, trigger the matching autocommands. */
2327 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2328 && ASCII_ISUPPER(*ea.cmd)
2329 && has_cmdundefined())
2330 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002331 int ret;
2332
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002333 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002334 while (ASCII_ISALNUM(*p))
2335 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002336 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002337 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2338 vim_free(p);
2339 if (ret && !aborting())
2340 p = find_command(&ea, NULL);
2341 }
2342#endif
2343
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344#ifdef FEAT_USR_CMDS
2345 if (p == NULL)
2346 {
2347 if (!ea.skip)
2348 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2349 goto doend;
2350 }
2351 /* Check for wrong commands. */
2352 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2353 {
2354 errormsg = uc_fun_cmd();
2355 goto doend;
2356 }
2357#endif
2358 if (ea.cmdidx == CMD_SIZE)
2359 {
2360 if (!ea.skip)
2361 {
2362 STRCPY(IObuff, _("E492: Not an editor command"));
2363 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002364 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002366 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368 goto doend;
2369 }
2370
Bram Moolenaar958636c2014-10-21 20:01:58 +02002371 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2372 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002373#ifdef HAVE_EX_SCRIPT_NI
2374 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2375#endif
2376 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
2378#ifndef FEAT_EVAL
2379 /*
2380 * When the expression evaluation is disabled, recognize the ":if" and
2381 * ":endif" commands and ignore everything in between it.
2382 */
2383 if (ea.cmdidx == CMD_if)
2384 ++if_level;
2385 if (if_level)
2386 {
2387 if (ea.cmdidx == CMD_endif)
2388 --if_level;
2389 goto doend;
2390 }
2391
2392#endif
2393
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002394 /* forced commands */
2395 if (*p == '!' && ea.cmdidx != CMD_substitute
2396 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
2398 ++p;
2399 ea.forceit = TRUE;
2400 }
2401 else
2402 ea.forceit = FALSE;
2403
2404/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002405 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002407 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002408 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409
2410 if (!ea.skip)
2411 {
2412#ifdef HAVE_SANDBOX
2413 if (sandbox != 0 && !(ea.argt & SBOXOK))
2414 {
2415 /* Command not allowed in sandbox. */
2416 errormsg = (char_u *)_(e_sandbox);
2417 goto doend;
2418 }
2419#endif
2420 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2421 {
2422 /* Command not allowed in non-'modifiable' buffer */
2423 errormsg = (char_u *)_(e_modifiable);
2424 goto doend;
2425 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002426
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002427 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002428 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002430 /* Command not allowed when editing the command line. */
2431#ifdef FEAT_CMDWIN
2432 if (cmdwin_type != 0)
2433 errormsg = (char_u *)_(e_cmdwin);
2434 else
2435#endif
2436 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 goto doend;
2438 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002439#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002440 /* Disallow editing another buffer when "curbuf_lock" is set.
2441 * Do allow ":edit" (check for argument later).
2442 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002443 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002444 && ea.cmdidx != CMD_edit
2445 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002446 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002447 && curbuf_locked())
2448 goto doend;
2449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450
2451 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2452 {
2453 /* no range allowed */
2454 errormsg = (char_u *)_(e_norange);
2455 goto doend;
2456 }
2457 }
2458
2459 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2460 {
2461 errormsg = (char_u *)_(e_nobang);
2462 goto doend;
2463 }
2464
2465 /*
2466 * Don't complain about the range if it is not used
2467 * (could happen if line_count is accidentally set to 0).
2468 */
2469 if (!ea.skip && !ni)
2470 {
2471 /*
2472 * If the range is backwards, ask for confirmation and, if given, swap
2473 * ea.line1 & ea.line2 so it's forwards again.
2474 * When global command is busy, don't ask, will fail below.
2475 */
2476 if (!global_busy && ea.line1 > ea.line2)
2477 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002478 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002480 if (sourcing || exmode_active)
2481 {
2482 errormsg = (char_u *)_("E493: Backwards range given");
2483 goto doend;
2484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 if (ask_yesno((char_u *)
2486 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002487 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 }
2489 lnum = ea.line1;
2490 ea.line1 = ea.line2;
2491 ea.line2 = lnum;
2492 }
2493 if ((errormsg = invalid_range(&ea)) != NULL)
2494 goto doend;
2495 }
2496
2497 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2498 ea.line2 = 1;
2499
2500 correct_range(&ea);
2501
2502#ifdef FEAT_FOLDING
2503 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2504 {
2505 /* Put the first line at the start of a closed fold, put the last line
2506 * at the end of a closed fold. */
2507 (void)hasFolding(ea.line1, &ea.line1, NULL);
2508 (void)hasFolding(ea.line2, NULL, &ea.line2);
2509 }
2510#endif
2511
2512#ifdef FEAT_QUICKFIX
2513 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002514 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 * option here, so things like % get expanded.
2516 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002517 p = replace_makeprg(&ea, p, cmdlinep);
2518 if (p == NULL)
2519 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520#endif
2521
2522 /*
2523 * Skip to start of argument.
2524 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2525 */
2526 if (ea.cmdidx == CMD_bang)
2527 ea.arg = p;
2528 else
2529 ea.arg = skipwhite(p);
2530
2531 /*
2532 * Check for "++opt=val" argument.
2533 * Must be first, allow ":w ++enc=utf8 !cmd"
2534 */
2535 if (ea.argt & ARGOPT)
2536 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2537 if (getargopt(&ea) == FAIL && !ni)
2538 {
2539 errormsg = (char_u *)_(e_invarg);
2540 goto doend;
2541 }
2542
2543 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2544 {
2545 if (*ea.arg == '>') /* append */
2546 {
2547 if (*++ea.arg != '>') /* typed wrong */
2548 {
2549 errormsg = (char_u *)_("E494: Use w or w>>");
2550 goto doend;
2551 }
2552 ea.arg = skipwhite(ea.arg + 1);
2553 ea.append = TRUE;
2554 }
2555 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2556 {
2557 ++ea.arg;
2558 ea.usefilter = TRUE;
2559 }
2560 }
2561
2562 if (ea.cmdidx == CMD_read)
2563 {
2564 if (ea.forceit)
2565 {
2566 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2567 ea.forceit = FALSE;
2568 }
2569 else if (*ea.arg == '!') /* :r !filter */
2570 {
2571 ++ea.arg;
2572 ea.usefilter = TRUE;
2573 }
2574 }
2575
2576 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2577 {
2578 ea.amount = 1;
2579 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2580 {
2581 ++ea.arg;
2582 ++ea.amount;
2583 }
2584 ea.arg = skipwhite(ea.arg);
2585 }
2586
2587 /*
2588 * Check for "+command" argument, before checking for next command.
2589 * Don't do this for ":read !cmd" and ":write !cmd".
2590 */
2591 if ((ea.argt & EDITCMD) && !ea.usefilter)
2592 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2593
2594 /*
2595 * Check for '|' to separate commands and '"' to start comments.
2596 * Don't do this for ":read !cmd" and ":write !cmd".
2597 */
2598 if ((ea.argt & TRLBAR) && !ea.usefilter)
2599 separate_nextcmd(&ea);
2600
2601 /*
2602 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002603 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2604 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002606 else if (ea.cmdidx == CMD_bang
2607 || ea.cmdidx == CMD_global
2608 || ea.cmdidx == CMD_vglobal
2609 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 {
2611 for (p = ea.arg; *p; ++p)
2612 {
2613 /* Remove one backslash before a newline, so that it's possible to
2614 * pass a newline to the shell and also a newline that is preceded
2615 * with a backslash. This makes it impossible to end a shell
2616 * command in a backslash, but that doesn't appear useful.
2617 * Halving the number of backslashes is incompatible with previous
2618 * versions. */
2619 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002620 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 else if (*p == '\n')
2622 {
2623 ea.nextcmd = p + 1;
2624 *p = NUL;
2625 break;
2626 }
2627 }
2628 }
2629
2630 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2631 {
2632 ea.line1 = 1;
2633 ea.line2 = curbuf->b_ml.ml_line_count;
2634 }
2635
2636 /* accept numbered register only when no count allowed (:put) */
2637 if ( (ea.argt & REGSTR)
2638 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002639 /* Do not allow register = for user commands */
2640 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2642 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002643#ifndef FEAT_CLIPBOARD
2644 /* check these explicitly for a more specific error message */
2645 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002647 errormsg = (char_u *)_(e_invalidreg);
2648 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
2650#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002651 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2652 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002653 {
2654 ea.regname = *ea.arg++;
2655#ifdef FEAT_EVAL
2656 /* for '=' register: accept the rest of the line as an expression */
2657 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2658 {
2659 set_expr_line(vim_strsave(ea.arg));
2660 ea.arg += STRLEN(ea.arg);
2661 }
2662#endif
2663 ea.arg = skipwhite(ea.arg);
2664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 }
2666
2667 /*
2668 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2669 * count, it's a buffer name.
2670 */
2671 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2672 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2673 || vim_iswhite(*p)))
2674 {
2675 n = getdigits(&ea.arg);
2676 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002677 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 {
2679 errormsg = (char_u *)_(e_zerocount);
2680 goto doend;
2681 }
2682 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2683 {
2684 ea.line2 = n;
2685 if (ea.addr_count == 0)
2686 ea.addr_count = 1;
2687 }
2688 else
2689 {
2690 ea.line1 = ea.line2;
2691 ea.line2 += n - 1;
2692 ++ea.addr_count;
2693 /*
2694 * Be vi compatible: no error message for out of range.
2695 */
2696 if (ea.line2 > curbuf->b_ml.ml_line_count)
2697 ea.line2 = curbuf->b_ml.ml_line_count;
2698 }
2699 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002700
2701 /*
2702 * Check for flags: 'l', 'p' and '#'.
2703 */
2704 if (ea.argt & EXFLAGS)
2705 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002707 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002708 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 {
2710 errormsg = (char_u *)_(e_trailing);
2711 goto doend;
2712 }
2713
2714 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2715 {
2716 errormsg = (char_u *)_(e_argreq);
2717 goto doend;
2718 }
2719
2720#ifdef FEAT_EVAL
2721 /*
2722 * Skip the command when it's not going to be executed.
2723 * The commands like :if, :endif, etc. always need to be executed.
2724 * Also make an exception for commands that handle a trailing command
2725 * themselves.
2726 */
2727 if (ea.skip)
2728 {
2729 switch (ea.cmdidx)
2730 {
2731 /* commands that need evaluation */
2732 case CMD_while:
2733 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002734 case CMD_for:
2735 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 case CMD_if:
2737 case CMD_elseif:
2738 case CMD_else:
2739 case CMD_endif:
2740 case CMD_try:
2741 case CMD_catch:
2742 case CMD_finally:
2743 case CMD_endtry:
2744 case CMD_function:
2745 break;
2746
2747 /* Commands that handle '|' themselves. Check: A command should
2748 * either have the TRLBAR flag, appear in this list or appear in
2749 * the list at ":help :bar". */
2750 case CMD_aboveleft:
2751 case CMD_and:
2752 case CMD_belowright:
2753 case CMD_botright:
2754 case CMD_browse:
2755 case CMD_call:
2756 case CMD_confirm:
2757 case CMD_delfunction:
2758 case CMD_djump:
2759 case CMD_dlist:
2760 case CMD_dsearch:
2761 case CMD_dsplit:
2762 case CMD_echo:
2763 case CMD_echoerr:
2764 case CMD_echomsg:
2765 case CMD_echon:
2766 case CMD_execute:
2767 case CMD_help:
2768 case CMD_hide:
2769 case CMD_ijump:
2770 case CMD_ilist:
2771 case CMD_isearch:
2772 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002773 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 case CMD_keepjumps:
2775 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002776 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 case CMD_leftabove:
2778 case CMD_let:
2779 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002780 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002782 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002783 case CMD_noautocmd:
2784 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 case CMD_perl:
2786 case CMD_psearch:
2787 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002788 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002789 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 case CMD_return:
2791 case CMD_rightbelow:
2792 case CMD_ruby:
2793 case CMD_silent:
2794 case CMD_smagic:
2795 case CMD_snomagic:
2796 case CMD_substitute:
2797 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002798 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 case CMD_tcl:
2800 case CMD_throw:
2801 case CMD_tilde:
2802 case CMD_topleft:
2803 case CMD_unlet:
2804 case CMD_verbose:
2805 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002806 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 break;
2808
2809 default: goto doend;
2810 }
2811 }
2812#endif
2813
2814 if (ea.argt & XFILE)
2815 {
2816 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2817 goto doend;
2818 }
2819
2820#ifdef FEAT_LISTCMDS
2821 /*
2822 * Accept buffer name. Cannot be used at the same time with a buffer
2823 * number. Don't do this for a user command.
2824 */
2825 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002826 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 {
2828 /*
2829 * :bdelete, :bwipeout and :bunload take several arguments, separated
2830 * by spaces: find next space (skipping over escaped characters).
2831 * The others take one argument: ignore trailing spaces.
2832 */
2833 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2834 || ea.cmdidx == CMD_bunload)
2835 p = skiptowhite_esc(ea.arg);
2836 else
2837 {
2838 p = ea.arg + STRLEN(ea.arg);
2839 while (p > ea.arg && vim_iswhite(p[-1]))
2840 --p;
2841 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002842 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2843 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 if (ea.line2 < 0) /* failed */
2845 goto doend;
2846 ea.addr_count = 1;
2847 ea.arg = skipwhite(p);
2848 }
2849#endif
2850
2851/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002852 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 *
2854 * The "ea" structure holds the arguments that can be used.
2855 */
2856 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002857 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 ea.cookie = cookie;
2859#ifdef FEAT_EVAL
2860 ea.cstack = cstack;
2861#endif
2862
2863#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002864 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 {
2866 /*
2867 * Execute a user-defined command.
2868 */
2869 do_ucmd(&ea);
2870 }
2871 else
2872#endif
2873 {
2874 /*
2875 * Call the function to execute the command.
2876 */
2877 ea.errmsg = NULL;
2878 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2879 if (ea.errmsg != NULL)
2880 errormsg = (char_u *)_(ea.errmsg);
2881 }
2882
2883#ifdef FEAT_EVAL
2884 /*
2885 * If the command just executed called do_cmdline(), any throw or ":return"
2886 * or ":finish" encountered there must also check the cstack of the still
2887 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2888 * exception, or reanimate a returned function or finished script file and
2889 * return or finish it again.
2890 */
2891 if (need_rethrow)
2892 do_throw(cstack);
2893 else if (check_cstack)
2894 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002895 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002897 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 && current_func_returned())
2899 do_return(&ea, TRUE, FALSE, NULL);
2900 }
2901 need_rethrow = check_cstack = FALSE;
2902#endif
2903
2904doend:
2905 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2906 curwin->w_cursor.lnum = 1;
2907
2908 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2909 {
2910 if (sourcing)
2911 {
2912 if (errormsg != IObuff)
2913 {
2914 STRCPY(IObuff, errormsg);
2915 errormsg = IObuff;
2916 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002917 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 }
2919 emsg(errormsg);
2920 }
2921#ifdef FEAT_EVAL
2922 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002923 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2924 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925#endif
2926
2927 if (verbose_save >= 0)
2928 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002929#ifdef FEAT_AUTOCMD
2930 if (cmdmod.save_ei != NULL)
2931 {
2932 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002933 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2934 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002935 free_string_option(cmdmod.save_ei);
2936 }
2937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938
2939 cmdmod = save_cmdmod;
2940
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002941 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 {
2943 /* messages could be enabled for a serious error, need to check if the
2944 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002945 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002946 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 emsg_silent -= did_esilent;
2948 if (emsg_silent < 0)
2949 emsg_silent = 0;
2950 /* Restore msg_scroll, it's set by file I/O commands, even when no
2951 * message is actually displayed. */
2952 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002953
2954 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2955 * somewhere in the line. Put it back in the first column. */
2956 if (redirecting())
2957 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
2959
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002960#ifdef HAVE_SANDBOX
2961 if (did_sandbox)
2962 --sandbox;
2963#endif
2964
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2966 ea.nextcmd = NULL;
2967
2968#ifdef FEAT_EVAL
2969 --ex_nesting_level;
2970#endif
2971
2972 return ea.nextcmd;
2973}
2974#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002975 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976#endif
2977
2978/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002979 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 * If there is a match advance "pp" to the argument and return TRUE.
2981 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002982 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002984 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 char *cmd; /* name of command */
2986 int len; /* required length */
2987{
2988 int i;
2989
2990 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002991 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 break;
2993 if (i >= len && !isalpha((*pp)[i]))
2994 {
2995 *pp = skipwhite(*pp + i);
2996 return TRUE;
2997 }
2998 return FALSE;
2999}
3000
3001/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003002 * Append "cmd" to the error message in IObuff.
3003 * Takes care of limiting the length and handling 0xa0, which would be
3004 * invisible otherwise.
3005 */
3006 static void
3007append_command(cmd)
3008 char_u *cmd;
3009{
3010 char_u *s = cmd;
3011 char_u *d;
3012
3013 STRCAT(IObuff, ": ");
3014 d = IObuff + STRLEN(IObuff);
3015 while (*s != NUL && d - IObuff < IOSIZE - 7)
3016 {
3017 if (
3018#ifdef FEAT_MBYTE
3019 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3020#endif
3021 *s == 0xa0)
3022 {
3023 s +=
3024#ifdef FEAT_MBYTE
3025 enc_utf8 ? 2 :
3026#endif
3027 1;
3028 STRCPY(d, "<a0>");
3029 d += 4;
3030 }
3031 else
3032 MB_COPY_CHAR(s, d);
3033 }
3034 *d = NUL;
3035}
3036
3037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003039 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003041 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 * Returns NULL for an ambiguous user command.
3043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 static char_u *
3045find_command(eap, full)
3046 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003047 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048{
3049 int len;
3050 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003051 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052
3053 /*
3054 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003055 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 * - the 'k' command can directly be followed by any character.
3057 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3058 * but :sre[wind] is another command, as are :scrip[tnames],
3059 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003060 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 */
3062 p = eap->cmd;
3063 if (*p == 'k')
3064 {
3065 eap->cmdidx = CMD_k;
3066 ++p;
3067 }
3068 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003069 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
3070 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 || p[1] == 'g'
3072 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3073 || p[1] == 'I'
3074 || (p[1] == 'r' && p[2] != 'e')))
3075 {
3076 eap->cmdidx = CMD_substitute;
3077 ++p;
3078 }
3079 else
3080 {
3081 while (ASCII_ISALPHA(*p))
3082 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003083 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003084 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003085 while (ASCII_ISALNUM(*p))
3086 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003087
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 /* check for non-alpha command */
3089 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3090 ++p;
3091 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003092 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3093 {
3094 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3095 * :delete with the 'l' flag. Same for 'p'. */
3096 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003097 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003098 break;
3099 if (i == len - 1)
3100 {
3101 --len;
3102 if (p[-1] == 'l')
3103 eap->flags |= EXFLAG_LIST;
3104 else
3105 eap->flags |= EXFLAG_PRINT;
3106 }
3107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108
3109 if (ASCII_ISLOWER(*eap->cmd))
3110 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3111 else
3112 eap->cmdidx = cmdidxs[26];
3113
3114 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3115 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3116 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3117 (size_t)len) == 0)
3118 {
3119#ifdef FEAT_EVAL
3120 if (full != NULL
3121 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3122 *full = TRUE;
3123#endif
3124 break;
3125 }
3126
3127#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003128 /* Look for a user defined command as a last resort. Let ":Print" be
3129 * overruled by a user defined command. */
3130 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3131 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003133 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 while (ASCII_ISALNUM(*p))
3135 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003136 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 }
3138#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003139 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 eap->cmdidx = CMD_SIZE;
3141 }
3142
3143 return p;
3144}
3145
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003146#ifdef FEAT_USR_CMDS
3147/*
3148 * Search for a user command that matches "eap->cmd".
3149 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3150 * Return a pointer to just after the command.
3151 * Return NULL if there is no matching command.
3152 */
3153 static char_u *
3154find_ucmd(eap, p, full, xp, compl)
3155 exarg_T *eap;
3156 char_u *p; /* end of the command (possibly including count) */
3157 int *full; /* set to TRUE for a full match */
3158 expand_T *xp; /* used for completion, NULL otherwise */
3159 int *compl; /* completion flags or NULL */
3160{
3161 int len = (int)(p - eap->cmd);
3162 int j, k, matchlen = 0;
3163 ucmd_T *uc;
3164 int found = FALSE;
3165 int possible = FALSE;
3166 char_u *cp, *np; /* Point into typed cmd and test name */
3167 garray_T *gap;
3168 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3169 only full match global is accepted. */
3170
3171 /*
3172 * Look for buffer-local user commands first, then global ones.
3173 */
3174 gap = &curbuf->b_ucmds;
3175 for (;;)
3176 {
3177 for (j = 0; j < gap->ga_len; ++j)
3178 {
3179 uc = USER_CMD_GA(gap, j);
3180 cp = eap->cmd;
3181 np = uc->uc_name;
3182 k = 0;
3183 while (k < len && *np != NUL && *cp++ == *np++)
3184 k++;
3185 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3186 {
3187 /* If finding a second match, the command is ambiguous. But
3188 * not if a buffer-local command wasn't a full match and a
3189 * global command is a full match. */
3190 if (k == len && found && *np != NUL)
3191 {
3192 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003193 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003194 amb_local = TRUE;
3195 }
3196
3197 if (!found || (k == len && *np == NUL))
3198 {
3199 /* If we matched up to a digit, then there could
3200 * be another command including the digit that we
3201 * should use instead.
3202 */
3203 if (k == len)
3204 found = TRUE;
3205 else
3206 possible = TRUE;
3207
3208 if (gap == &ucmds)
3209 eap->cmdidx = CMD_USER;
3210 else
3211 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003212 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003213 eap->useridx = j;
3214
3215# ifdef FEAT_CMDL_COMPL
3216 if (compl != NULL)
3217 *compl = uc->uc_compl;
3218# ifdef FEAT_EVAL
3219 if (xp != NULL)
3220 {
3221 xp->xp_arg = uc->uc_compl_arg;
3222 xp->xp_scriptID = uc->uc_scriptID;
3223 }
3224# endif
3225# endif
3226 /* Do not search for further abbreviations
3227 * if this is an exact match. */
3228 matchlen = k;
3229 if (k == len && *np == NUL)
3230 {
3231 if (full != NULL)
3232 *full = TRUE;
3233 amb_local = FALSE;
3234 break;
3235 }
3236 }
3237 }
3238 }
3239
3240 /* Stop if we found a full match or searched all. */
3241 if (j < gap->ga_len || gap == &ucmds)
3242 break;
3243 gap = &ucmds;
3244 }
3245
3246 /* Only found ambiguous matches. */
3247 if (amb_local)
3248 {
3249 if (xp != NULL)
3250 xp->xp_context = EXPAND_UNSUCCESSFUL;
3251 return NULL;
3252 }
3253
3254 /* The match we found may be followed immediately by a number. Move "p"
3255 * back to point to it. */
3256 if (found || possible)
3257 return p + (matchlen - len);
3258 return p;
3259}
3260#endif
3261
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003263static struct cmdmod
3264{
3265 char *name;
3266 int minlen;
3267 int has_count; /* :123verbose :3tab */
3268} cmdmods[] = {
3269 {"aboveleft", 3, FALSE},
3270 {"belowright", 3, FALSE},
3271 {"botright", 2, FALSE},
3272 {"browse", 3, FALSE},
3273 {"confirm", 4, FALSE},
3274 {"hide", 3, FALSE},
3275 {"keepalt", 5, FALSE},
3276 {"keepjumps", 5, FALSE},
3277 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003278 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003279 {"leftabove", 5, FALSE},
3280 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003281 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003282 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003283 {"rightbelow", 6, FALSE},
3284 {"sandbox", 3, FALSE},
3285 {"silent", 3, FALSE},
3286 {"tab", 3, TRUE},
3287 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003288 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003289 {"verbose", 4, TRUE},
3290 {"vertical", 4, FALSE},
3291};
3292
3293/*
3294 * Return length of a command modifier (including optional count).
3295 * Return zero when it's not a modifier.
3296 */
3297 int
3298modifier_len(cmd)
3299 char_u *cmd;
3300{
3301 int i, j;
3302 char_u *p = cmd;
3303
3304 if (VIM_ISDIGIT(*cmd))
3305 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003306 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003307 {
3308 for (j = 0; p[j] != NUL; ++j)
3309 if (p[j] != cmdmods[i].name[j])
3310 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003311 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003312 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003313 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003314 }
3315 return 0;
3316}
3317
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318/*
3319 * Return > 0 if an Ex command "name" exists.
3320 * Return 2 if there is an exact match.
3321 * Return 3 if there is an ambiguous match.
3322 */
3323 int
3324cmd_exists(name)
3325 char_u *name;
3326{
3327 exarg_T ea;
3328 int full = FALSE;
3329 int i;
3330 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003331 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332
3333 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003334 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 {
3336 for (j = 0; name[j] != NUL; ++j)
3337 if (name[j] != cmdmods[i].name[j])
3338 break;
3339 if (name[j] == NUL && j >= cmdmods[i].minlen)
3340 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3341 }
3342
Bram Moolenaara9587612006-05-04 21:47:50 +00003343 /* Check built-in commands and user defined commands.
3344 * For ":2match" and ":3match" we need to skip the number. */
3345 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003347 p = find_command(&ea, &full);
3348 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003350 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3351 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003352 if (*skipwhite(p) != NUL)
3353 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3355}
3356#endif
3357
3358/*
3359 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3360 * we don't need/want deleted. Maybe this could be done better if we didn't
3361 * repeat all this stuff. The only problem is that they may not stay
3362 * perfectly compatible with each other, but then the command line syntax
3363 * probably won't change that much -- webb.
3364 */
3365 char_u *
3366set_one_cmd_context(xp, buff)
3367 expand_T *xp;
3368 char_u *buff; /* buffer for command string */
3369{
3370 char_u *p;
3371 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003372 int len = 0;
3373 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3375 int compl = EXPAND_NOTHING;
3376#endif
3377#ifdef FEAT_CMDL_COMPL
3378 int delim;
3379#endif
3380 int forceit = FALSE;
3381 int usefilter = FALSE; /* filter instead of file name */
3382
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003383 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 xp->xp_pattern = buff;
3385 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003386 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387
3388/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003389 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 */
3391 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3392 ;
3393 xp->xp_pattern = cmd;
3394
3395 if (*cmd == NUL)
3396 return NULL;
3397 if (*cmd == '"') /* ignore comment lines */
3398 {
3399 xp->xp_context = EXPAND_NOTHING;
3400 return NULL;
3401 }
3402
3403/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003404 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 */
3406 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 xp->xp_pattern = cmd;
3408 if (*cmd == NUL)
3409 return NULL;
3410 if (*cmd == '"')
3411 {
3412 xp->xp_context = EXPAND_NOTHING;
3413 return NULL;
3414 }
3415
3416 if (*cmd == '|' || *cmd == '\n')
3417 return cmd + 1; /* There's another command */
3418
3419 /*
3420 * Isolate the command and search for it in the command table.
3421 * Exceptions:
3422 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003423 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3425 */
3426 if (*cmd == 'k' && cmd[1] != 'e')
3427 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003428 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 p = cmd + 1;
3430 }
3431 else
3432 {
3433 p = cmd;
3434 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3435 ++p;
3436 /* check for non-alpha command */
3437 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3438 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003439 /* for python 3.x: ":py3*" commands completion */
3440 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003441 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003442 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003443 while (ASCII_ISALPHA(*p) || *p == '*')
3444 ++p;
3445 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003446 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003448 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 {
3450 xp->xp_context = EXPAND_UNSUCCESSFUL;
3451 return NULL;
3452 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003453 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003454 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3455 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3456 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 break;
3458
3459#ifdef FEAT_USR_CMDS
3460 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3462 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463#endif
3464 }
3465
3466 /*
3467 * If the cursor is touching the command, and it ends in an alpha-numeric
3468 * character, complete the command name.
3469 */
3470 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3471 return NULL;
3472
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003473 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 {
3475 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3476 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003477 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 p = cmd + 1;
3479 }
3480#ifdef FEAT_USR_CMDS
3481 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3482 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003483 ea.cmd = cmd;
3484 p = find_ucmd(&ea, p, NULL, xp,
3485# if defined(FEAT_CMDL_COMPL)
3486 &compl
3487# else
3488 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003490 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003491 if (p == NULL)
3492 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
3494#endif
3495 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003496 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 {
3498 /* Not still touching the command and it was an illegal one */
3499 xp->xp_context = EXPAND_UNSUCCESSFUL;
3500 return NULL;
3501 }
3502
3503 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3504
3505 if (*p == '!') /* forced commands */
3506 {
3507 forceit = TRUE;
3508 ++p;
3509 }
3510
3511/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003512 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003514 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003515 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516
3517 arg = skipwhite(p);
3518
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003519 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 {
3521 if (*arg == '>') /* append */
3522 {
3523 if (*++arg == '>')
3524 ++arg;
3525 arg = skipwhite(arg);
3526 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003527 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
3529 ++arg;
3530 usefilter = TRUE;
3531 }
3532 }
3533
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003534 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 {
3536 usefilter = forceit; /* :r! filter if forced */
3537 if (*arg == '!') /* :r !filter */
3538 {
3539 ++arg;
3540 usefilter = TRUE;
3541 }
3542 }
3543
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003544 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 {
3546 while (*arg == *cmd) /* allow any number of '>' or '<' */
3547 ++arg;
3548 arg = skipwhite(arg);
3549 }
3550
3551 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003552 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 {
3554 /* Check if we're in the +command */
3555 p = arg + 1;
3556 arg = skip_cmd_arg(arg, FALSE);
3557
3558 /* Still touching the command after '+'? */
3559 if (*arg == NUL)
3560 return p;
3561
3562 /* Skip space(s) after +command to get to the real argument */
3563 arg = skipwhite(arg);
3564 }
3565
3566 /*
3567 * Check for '|' to separate commands and '"' to start comments.
3568 * Don't do this for ":read !cmd" and ":write !cmd".
3569 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003570 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
3572 p = arg;
3573 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003574 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 p += 2;
3576 while (*p)
3577 {
3578 if (*p == Ctrl_V)
3579 {
3580 if (p[1] != NUL)
3581 ++p;
3582 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003583 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 || *p == '|' || *p == '\n')
3585 {
3586 if (*(p - 1) != '\\')
3587 {
3588 if (*p == '|' || *p == '\n')
3589 return p + 1;
3590 return NULL; /* It's a comment */
3591 }
3592 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003593 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
3595 }
3596
3597 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003598 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 vim_strchr((char_u *)"|\"", *arg) == NULL)
3600 return NULL;
3601
3602 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003603 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003605 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003606 while (*p && p < buff + len)
3607 {
3608 if (*p == ' ' || *p == TAB)
3609 {
3610 /* argument starts after a space */
3611 xp->xp_pattern = ++p;
3612 }
3613 else
3614 {
3615 if (*p == '\\' && *(p + 1) != NUL)
3616 ++p; /* skip over escaped character */
3617 mb_ptr_adv(p);
3618 }
3619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003621 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003623 int c;
3624 int in_quote = FALSE;
3625 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626
3627 /*
3628 * Allow spaces within back-quotes to count as part of the argument
3629 * being expanded.
3630 */
3631 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003632 p = xp->xp_pattern;
3633 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003635#ifdef FEAT_MBYTE
3636 if (has_mbyte)
3637 c = mb_ptr2char(p);
3638 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003640 c = *p;
3641 if (c == '\\' && p[1] != NUL)
3642 ++p;
3643 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 {
3645 if (!in_quote)
3646 {
3647 xp->xp_pattern = p;
3648 bow = p + 1;
3649 }
3650 in_quote = !in_quote;
3651 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003652 /* An argument can contain just about everything, except
3653 * characters that end the command and white space. */
3654 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003655#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003656 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003657#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003658 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003659 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003660 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003661 while (*p != NUL)
3662 {
3663#ifdef FEAT_MBYTE
3664 if (has_mbyte)
3665 c = mb_ptr2char(p);
3666 else
3667#endif
3668 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003669 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003670 break;
3671#ifdef FEAT_MBYTE
3672 if (has_mbyte)
3673 len = (*mb_ptr2len)(p);
3674 else
3675#endif
3676 len = 1;
3677 mb_ptr_adv(p);
3678 }
3679 if (in_quote)
3680 bow = p;
3681 else
3682 xp->xp_pattern = p;
3683 p -= len;
3684 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003685 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 }
3687
3688 /*
3689 * If we are still inside the quotes, and we passed a space, just
3690 * expand from there.
3691 */
3692 if (bow != NULL && in_quote)
3693 xp->xp_pattern = bow;
3694 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003695
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003696 /* For a shell command more chars need to be escaped. */
3697 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003698 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003699#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003700 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003701#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003702 /* When still after the command name expand executables. */
3703 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003704 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
3707 /* Check for environment variable */
3708 if (*xp->xp_pattern == '$'
3709#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3710 || *xp->xp_pattern == '%'
3711#endif
3712 )
3713 {
3714 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3715 if (!vim_isIDc(*p))
3716 break;
3717 if (*p == NUL)
3718 {
3719 xp->xp_context = EXPAND_ENV_VARS;
3720 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003721#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3722 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003723 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003724 compl = EXPAND_ENV_VARS;
3725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
3727 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003728#if defined(FEAT_CMDL_COMPL)
3729 /* Check for user names */
3730 if (*xp->xp_pattern == '~')
3731 {
3732 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3733 ;
3734 /* Complete ~user only if it partially matches a user name.
3735 * A full match ~user<Tab> will be replaced by user's home
3736 * directory i.e. something like ~user<Tab> -> /home/user/ */
3737 if (*p == NUL && p > xp->xp_pattern + 1
3738 && match_user(xp->xp_pattern + 1) == 1)
3739 {
3740 xp->xp_context = EXPAND_USER;
3741 ++xp->xp_pattern;
3742 }
3743 }
3744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 }
3746
3747/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003748 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003750 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003752 case CMD_find:
3753 case CMD_sfind:
3754 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003755 if (xp->xp_context == EXPAND_FILES)
3756 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003757 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 case CMD_cd:
3759 case CMD_chdir:
3760 case CMD_lcd:
3761 case CMD_lchdir:
3762 if (xp->xp_context == EXPAND_FILES)
3763 xp->xp_context = EXPAND_DIRECTORIES;
3764 break;
3765 case CMD_help:
3766 xp->xp_context = EXPAND_HELP;
3767 xp->xp_pattern = arg;
3768 break;
3769
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003770 /* Command modifiers: return the argument.
3771 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003773 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 case CMD_belowright:
3775 case CMD_botright:
3776 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003777 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003779 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 case CMD_folddoclosed:
3781 case CMD_folddoopen:
3782 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003783 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 case CMD_keepjumps:
3785 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003786 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 case CMD_leftabove:
3788 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003789 case CMD_noautocmd:
3790 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003792 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003794 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003795 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 case CMD_topleft:
3797 case CMD_verbose:
3798 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003799 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 return arg;
3801
Bram Moolenaar4f688582007-07-24 12:34:30 +00003802#ifdef FEAT_CMDL_COMPL
3803# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 case CMD_match:
3805 if (*arg == NUL || !ends_excmd(*arg))
3806 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003807 /* also complete "None" */
3808 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 arg = skipwhite(skiptowhite(arg));
3810 if (*arg != NUL)
3811 {
3812 xp->xp_context = EXPAND_NOTHING;
3813 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3814 }
3815 }
3816 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003817# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819/*
3820 * All completion for the +cmdline_compl feature goes here.
3821 */
3822
3823# ifdef FEAT_USR_CMDS
3824 case CMD_command:
3825 /* Check for attributes */
3826 while (*arg == '-')
3827 {
3828 arg++; /* Skip "-" */
3829 p = skiptowhite(arg);
3830 if (*p == NUL)
3831 {
3832 /* Cursor is still in the attribute */
3833 p = vim_strchr(arg, '=');
3834 if (p == NULL)
3835 {
3836 /* No "=", so complete attribute names */
3837 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3838 xp->xp_pattern = arg;
3839 return NULL;
3840 }
3841
3842 /* For the -complete and -nargs attributes, we complete
3843 * their arguments as well.
3844 */
3845 if (STRNICMP(arg, "complete", p - arg) == 0)
3846 {
3847 xp->xp_context = EXPAND_USER_COMPLETE;
3848 xp->xp_pattern = p + 1;
3849 return NULL;
3850 }
3851 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3852 {
3853 xp->xp_context = EXPAND_USER_NARGS;
3854 xp->xp_pattern = p + 1;
3855 return NULL;
3856 }
3857 return NULL;
3858 }
3859 arg = skipwhite(p);
3860 }
3861
3862 /* After the attributes comes the new command name */
3863 p = skiptowhite(arg);
3864 if (*p == NUL)
3865 {
3866 xp->xp_context = EXPAND_USER_COMMANDS;
3867 xp->xp_pattern = arg;
3868 break;
3869 }
3870
3871 /* And finally comes a normal command */
3872 return skipwhite(p);
3873
3874 case CMD_delcommand:
3875 xp->xp_context = EXPAND_USER_COMMANDS;
3876 xp->xp_pattern = arg;
3877 break;
3878# endif
3879
3880 case CMD_global:
3881 case CMD_vglobal:
3882 delim = *arg; /* get the delimiter */
3883 if (delim)
3884 ++arg; /* skip delimiter if there is one */
3885
3886 while (arg[0] != NUL && arg[0] != delim)
3887 {
3888 if (arg[0] == '\\' && arg[1] != NUL)
3889 ++arg;
3890 ++arg;
3891 }
3892 if (arg[0] != NUL)
3893 return arg + 1;
3894 break;
3895 case CMD_and:
3896 case CMD_substitute:
3897 delim = *arg;
3898 if (delim)
3899 {
3900 /* skip "from" part */
3901 ++arg;
3902 arg = skip_regexp(arg, delim, p_magic, NULL);
3903 }
3904 /* skip "to" part */
3905 while (arg[0] != NUL && arg[0] != delim)
3906 {
3907 if (arg[0] == '\\' && arg[1] != NUL)
3908 ++arg;
3909 ++arg;
3910 }
3911 if (arg[0] != NUL) /* skip delimiter */
3912 ++arg;
3913 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3914 ++arg;
3915 if (arg[0] != NUL)
3916 return arg;
3917 break;
3918 case CMD_isearch:
3919 case CMD_dsearch:
3920 case CMD_ilist:
3921 case CMD_dlist:
3922 case CMD_ijump:
3923 case CMD_psearch:
3924 case CMD_djump:
3925 case CMD_isplit:
3926 case CMD_dsplit:
3927 arg = skipwhite(skipdigits(arg)); /* skip count */
3928 if (*arg == '/') /* Match regexp, not just whole words */
3929 {
3930 for (++arg; *arg && *arg != '/'; arg++)
3931 if (*arg == '\\' && arg[1] != NUL)
3932 arg++;
3933 if (*arg)
3934 {
3935 arg = skipwhite(arg + 1);
3936
3937 /* Check for trailing illegal characters */
3938 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3939 xp->xp_context = EXPAND_NOTHING;
3940 else
3941 return arg;
3942 }
3943 }
3944 break;
3945#ifdef FEAT_AUTOCMD
3946 case CMD_autocmd:
3947 return set_context_in_autocmd(xp, arg, FALSE);
3948
3949 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003950 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 return set_context_in_autocmd(xp, arg, TRUE);
3952#endif
3953 case CMD_set:
3954 set_context_in_set_cmd(xp, arg, 0);
3955 break;
3956 case CMD_setglobal:
3957 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3958 break;
3959 case CMD_setlocal:
3960 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3961 break;
3962 case CMD_tag:
3963 case CMD_stag:
3964 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003965 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 case CMD_tselect:
3967 case CMD_stselect:
3968 case CMD_ptselect:
3969 case CMD_tjump:
3970 case CMD_stjump:
3971 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003972 if (*p_wop != NUL)
3973 xp->xp_context = EXPAND_TAGS_LISTFILES;
3974 else
3975 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 xp->xp_pattern = arg;
3977 break;
3978 case CMD_augroup:
3979 xp->xp_context = EXPAND_AUGROUP;
3980 xp->xp_pattern = arg;
3981 break;
3982#ifdef FEAT_SYN_HL
3983 case CMD_syntax:
3984 set_context_in_syntax_cmd(xp, arg);
3985 break;
3986#endif
3987#ifdef FEAT_EVAL
3988 case CMD_let:
3989 case CMD_if:
3990 case CMD_elseif:
3991 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003992 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 case CMD_echo:
3994 case CMD_echon:
3995 case CMD_execute:
3996 case CMD_echomsg:
3997 case CMD_echoerr:
3998 case CMD_call:
3999 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004000 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 break;
4002
4003 case CMD_unlet:
4004 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4005 arg = xp->xp_pattern + 1;
4006 xp->xp_context = EXPAND_USER_VARS;
4007 xp->xp_pattern = arg;
4008 break;
4009
4010 case CMD_function:
4011 case CMD_delfunction:
4012 xp->xp_context = EXPAND_USER_FUNC;
4013 xp->xp_pattern = arg;
4014 break;
4015
4016 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004017 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 break;
4019#endif
4020 case CMD_highlight:
4021 set_context_in_highlight_cmd(xp, arg);
4022 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004023#ifdef FEAT_CSCOPE
4024 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004025 case CMD_lcscope:
4026 case CMD_scscope:
4027 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004028 break;
4029#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004030#ifdef FEAT_SIGNS
4031 case CMD_sign:
4032 set_context_in_sign_cmd(xp, arg);
4033 break;
4034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035#ifdef FEAT_LISTCMDS
4036 case CMD_bdelete:
4037 case CMD_bwipeout:
4038 case CMD_bunload:
4039 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4040 arg = xp->xp_pattern + 1;
4041 /*FALLTHROUGH*/
4042 case CMD_buffer:
4043 case CMD_sbuffer:
4044 case CMD_checktime:
4045 xp->xp_context = EXPAND_BUFFERS;
4046 xp->xp_pattern = arg;
4047 break;
4048#endif
4049#ifdef FEAT_USR_CMDS
4050 case CMD_USER:
4051 case CMD_USER_BUF:
4052 if (compl != EXPAND_NOTHING)
4053 {
4054 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004055 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 {
4057# ifdef FEAT_MENU
4058 if (compl == EXPAND_MENUS)
4059 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4060# endif
4061 if (compl == EXPAND_COMMANDS)
4062 return arg;
4063 if (compl == EXPAND_MAPPINGS)
4064 return set_context_in_map_cmd(xp, (char_u *)"map",
4065 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004066 /* Find start of last argument. */
4067 p = arg;
4068 while (*p)
4069 {
4070 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004071 /* argument starts after a space */
4072 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004073 else if (*p == '\\' && *(p + 1) != NUL)
4074 ++p; /* skip over escaped character */
4075 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 xp->xp_pattern = arg;
4078 }
4079 xp->xp_context = compl;
4080 }
4081 break;
4082#endif
4083 case CMD_map: case CMD_noremap:
4084 case CMD_nmap: case CMD_nnoremap:
4085 case CMD_vmap: case CMD_vnoremap:
4086 case CMD_omap: case CMD_onoremap:
4087 case CMD_imap: case CMD_inoremap:
4088 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004089 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004090 case CMD_smap: case CMD_snoremap:
4091 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004093 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 case CMD_unmap:
4095 case CMD_nunmap:
4096 case CMD_vunmap:
4097 case CMD_ounmap:
4098 case CMD_iunmap:
4099 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004100 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004101 case CMD_sunmap:
4102 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004104 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 case CMD_abbreviate: case CMD_noreabbrev:
4106 case CMD_cabbrev: case CMD_cnoreabbrev:
4107 case CMD_iabbrev: case CMD_inoreabbrev:
4108 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004109 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 case CMD_unabbreviate:
4111 case CMD_cunabbrev:
4112 case CMD_iunabbrev:
4113 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004114 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115#ifdef FEAT_MENU
4116 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4117 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4118 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4119 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4120 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4121 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4122 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4123 case CMD_tmenu: case CMD_tunmenu:
4124 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4125 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4126#endif
4127
4128 case CMD_colorscheme:
4129 xp->xp_context = EXPAND_COLORS;
4130 xp->xp_pattern = arg;
4131 break;
4132
4133 case CMD_compiler:
4134 xp->xp_context = EXPAND_COMPILER;
4135 xp->xp_pattern = arg;
4136 break;
4137
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004138 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004139 xp->xp_context = EXPAND_OWNSYNTAX;
4140 xp->xp_pattern = arg;
4141 break;
4142
4143 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004144 xp->xp_context = EXPAND_FILETYPE;
4145 xp->xp_pattern = arg;
4146 break;
4147
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4149 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4150 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004151 p = skiptowhite(arg);
4152 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 {
4154 xp->xp_context = EXPAND_LANGUAGE;
4155 xp->xp_pattern = arg;
4156 }
4157 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004158 {
4159 if ( STRNCMP(arg, "messages", p - arg) == 0
4160 || STRNCMP(arg, "ctype", p - arg) == 0
4161 || STRNCMP(arg, "time", p - arg) == 0)
4162 {
4163 xp->xp_context = EXPAND_LOCALES;
4164 xp->xp_pattern = skipwhite(p);
4165 }
4166 else
4167 xp->xp_context = EXPAND_NOTHING;
4168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 break;
4170#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004171#if defined(FEAT_PROFILE)
4172 case CMD_profile:
4173 set_context_in_profile_cmd(xp, arg);
4174 break;
4175#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004176 case CMD_behave:
4177 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004178 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004179 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004181#if defined(FEAT_CMDHIST)
4182 case CMD_history:
4183 xp->xp_context = EXPAND_HISTORY;
4184 xp->xp_pattern = arg;
4185 break;
4186#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004187#if defined(FEAT_PROFILE)
4188 case CMD_syntime:
4189 xp->xp_context = EXPAND_SYNTIME;
4190 xp->xp_pattern = arg;
4191 break;
4192#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004193
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194#endif /* FEAT_CMDL_COMPL */
4195
4196 default:
4197 break;
4198 }
4199 return NULL;
4200}
4201
4202/*
4203 * skip a range specifier of the form: addr [,addr] [;addr] ..
4204 *
4205 * Backslashed delimiters after / or ? will be skipped, and commands will
4206 * not be expanded between /'s and ?'s or after "'".
4207 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004208 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 * Returns the "cmd" pointer advanced to beyond the range.
4210 */
4211 char_u *
4212skip_range(cmd, ctx)
4213 char_u *cmd;
4214 int *ctx; /* pointer to xp_context or NULL */
4215{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004216 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217
Bram Moolenaardf177f62005-02-22 08:39:57 +00004218 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 {
4220 if (*cmd == '\'')
4221 {
4222 if (*++cmd == NUL && ctx != NULL)
4223 *ctx = EXPAND_NOTHING;
4224 }
4225 else if (*cmd == '/' || *cmd == '?')
4226 {
4227 delim = *cmd++;
4228 while (*cmd != NUL && *cmd != delim)
4229 if (*cmd++ == '\\' && *cmd != NUL)
4230 ++cmd;
4231 if (*cmd == NUL && ctx != NULL)
4232 *ctx = EXPAND_NOTHING;
4233 }
4234 if (*cmd != NUL)
4235 ++cmd;
4236 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004237
4238 /* Skip ":" and white space. */
4239 while (*cmd == ':')
4240 cmd = skipwhite(cmd + 1);
4241
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 return cmd;
4243}
4244
4245/*
4246 * get a single EX address
4247 *
4248 * Set ptr to the next character after the part that was interpreted.
4249 * Set ptr to NULL when an error is encountered.
4250 *
4251 * Return MAXLNUM when no Ex address was found.
4252 */
4253 static linenr_T
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004254get_address(ptr, addr_type, skip, to_other_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 char_u **ptr;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004256 int addr_type; /* flag: one of ADDR_LINES, ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 int skip; /* only skip the address, don't use it */
4258 int to_other_file; /* flag: may jump to other file */
4259{
4260 int c;
4261 int i;
4262 long n;
4263 char_u *cmd;
4264 pos_T pos;
4265 pos_T *fp;
4266 linenr_T lnum;
4267
4268 cmd = skipwhite(*ptr);
4269 lnum = MAXLNUM;
4270 do
4271 {
4272 switch (*cmd)
4273 {
4274 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004275 ++cmd;
4276 switch (addr_type)
4277 {
4278 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 lnum = curwin->w_cursor.lnum;
4280 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004281 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004282 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004283 break;
4284 case ADDR_ARGUMENTS:
4285 lnum = curwin->w_arg_idx + 1;
4286 break;
4287 case ADDR_LOADED_BUFFERS:
4288 case ADDR_UNLOADED_BUFFERS:
4289 lnum = curbuf->b_fnum;
4290 break;
4291 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004292 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004293 break;
4294 }
4295 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
4297 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004298 ++cmd;
4299 switch (addr_type)
4300 {
4301 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 lnum = curbuf->b_ml.ml_line_count;
4303 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004304 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004305 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004306 break;
4307 case ADDR_ARGUMENTS:
4308 lnum = ARGCOUNT;
4309 break;
4310 case ADDR_LOADED_BUFFERS:
4311 case ADDR_UNLOADED_BUFFERS:
4312 lnum = lastbuf->b_fnum;
4313 break;
4314 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004315 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004316 break;
4317 }
4318 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
4320 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004321 if (*++cmd == NUL)
4322 {
4323 cmd = NULL;
4324 goto error;
4325 }
4326 if (addr_type != ADDR_LINES)
4327 {
4328 EMSG(_(e_invaddr));
4329 goto error;
4330 }
4331 if (skip)
4332 ++cmd;
4333 else
4334 {
4335 /* Only accept a mark in another file when it is
4336 * used by itself: ":'M". */
4337 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4338 ++cmd;
4339 if (fp == (pos_T *)-1)
4340 /* Jumped to another file. */
4341 lnum = curwin->w_cursor.lnum;
4342 else
4343 {
4344 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 {
4346 cmd = NULL;
4347 goto error;
4348 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004349 lnum = fp->lnum;
4350 }
4351 }
4352 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353
4354 case '/':
4355 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004356 c = *cmd++;
4357 if (addr_type != ADDR_LINES)
4358 {
4359 EMSG(_(e_invaddr));
4360 goto error;
4361 }
4362 if (skip) /* skip "/pat/" */
4363 {
4364 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4365 if (*cmd == c)
4366 ++cmd;
4367 }
4368 else
4369 {
4370 pos = curwin->w_cursor; /* save curwin->w_cursor */
4371 /*
4372 * When '/' or '?' follows another address, start
4373 * from there.
4374 */
4375 if (lnum != MAXLNUM)
4376 curwin->w_cursor.lnum = lnum;
4377 /*
4378 * Start a forward search at the end of the line.
4379 * Start a backward search at the start of the line.
4380 * This makes sure we never match in the current
4381 * line, and can match anywhere in the
4382 * next/previous line.
4383 */
4384 if (c == '/')
4385 curwin->w_cursor.col = MAXCOL;
4386 else
4387 curwin->w_cursor.col = 0;
4388 searchcmdlen = 0;
4389 if (!do_search(NULL, c, cmd, 1L,
4390 SEARCH_HIS | SEARCH_MSG, NULL))
4391 {
4392 curwin->w_cursor = pos;
4393 cmd = NULL;
4394 goto error;
4395 }
4396 lnum = curwin->w_cursor.lnum;
4397 curwin->w_cursor = pos;
4398 /* adjust command string pointer */
4399 cmd += searchcmdlen;
4400 }
4401 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
4403 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004404 ++cmd;
4405 if (addr_type != ADDR_LINES)
4406 {
4407 EMSG(_(e_invaddr));
4408 goto error;
4409 }
4410 if (*cmd == '&')
4411 i = RE_SUBST;
4412 else if (*cmd == '?' || *cmd == '/')
4413 i = RE_SEARCH;
4414 else
4415 {
4416 EMSG(_(e_backslash));
4417 cmd = NULL;
4418 goto error;
4419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004421 if (!skip)
4422 {
4423 /*
4424 * When search follows another address, start from
4425 * there.
4426 */
4427 if (lnum != MAXLNUM)
4428 pos.lnum = lnum;
4429 else
4430 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004432 /*
4433 * Start the search just like for the above
4434 * do_search().
4435 */
4436 if (*cmd != '?')
4437 pos.col = MAXCOL;
4438 else
4439 pos.col = 0;
4440 if (searchit(curwin, curbuf, &pos,
4441 *cmd == '?' ? BACKWARD : FORWARD,
4442 (char_u *)"", 1L, SEARCH_MSG,
4443 i, (linenr_T)0, NULL) != FAIL)
4444 lnum = pos.lnum;
4445 else
4446 {
4447 cmd = NULL;
4448 goto error;
4449 }
4450 }
4451 ++cmd;
4452 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
4454 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004455 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4456 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 }
4458
4459 for (;;)
4460 {
4461 cmd = skipwhite(cmd);
4462 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4463 break;
4464
4465 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004466 {
4467 switch (addr_type)
4468 {
4469 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004470 /* "+1" is same as ".+1" */
4471 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004472 break;
4473 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004474 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004475 break;
4476 case ADDR_ARGUMENTS:
4477 lnum = curwin->w_arg_idx + 1;
4478 break;
4479 case ADDR_LOADED_BUFFERS:
4480 case ADDR_UNLOADED_BUFFERS:
4481 lnum = curbuf->b_fnum;
4482 break;
4483 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004484 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004485 break;
4486 }
4487 }
4488
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 if (VIM_ISDIGIT(*cmd))
4490 i = '+'; /* "number" is same as "+number" */
4491 else
4492 i = *cmd++;
4493 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4494 n = 1;
4495 else
4496 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004497 if (addr_type == ADDR_LOADED_BUFFERS
4498 || addr_type == ADDR_UNLOADED_BUFFERS)
Bram Moolenaar4d84d932014-11-30 14:50:16 +01004499 lnum = compute_buffer_local_count(addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004500 else if (i == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 lnum -= n;
4502 else
4503 lnum += n;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004504
4505 switch (addr_type)
4506 {
4507 case ADDR_LINES:
4508 break;
4509 case ADDR_ARGUMENTS:
4510 if (lnum < 0)
4511 lnum = 0;
4512 else if (lnum >= ARGCOUNT)
4513 lnum = ARGCOUNT;
4514 break;
4515 case ADDR_TABS:
4516 if (lnum < 0)
4517 {
4518 lnum = 0;
4519 break;
4520 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01004521 if (lnum >= LAST_TAB_NR)
4522 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004523 break;
4524 case ADDR_WINDOWS:
4525 if (lnum < 0)
4526 {
4527 lnum = 0;
4528 break;
4529 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01004530 if (lnum >= LAST_WIN_NR)
4531 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004532 break;
4533 case ADDR_LOADED_BUFFERS:
4534 case ADDR_UNLOADED_BUFFERS:
4535 if (lnum < firstbuf->b_fnum)
4536 {
4537 lnum = firstbuf->b_fnum;
4538 break;
4539 }
4540 if (lnum > lastbuf->b_fnum)
4541 lnum = lastbuf->b_fnum;
4542 break;
4543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 }
4545 } while (*cmd == '/' || *cmd == '?');
4546
4547error:
4548 *ptr = cmd;
4549 return lnum;
4550}
4551
4552/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004553 * Get flags from an Ex command argument.
4554 */
4555 static void
4556get_flags(eap)
4557 exarg_T *eap;
4558{
4559 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4560 {
4561 if (*eap->arg == 'l')
4562 eap->flags |= EXFLAG_LIST;
4563 else if (*eap->arg == 'p')
4564 eap->flags |= EXFLAG_PRINT;
4565 else
4566 eap->flags |= EXFLAG_NR;
4567 eap->arg = skipwhite(eap->arg + 1);
4568 }
4569}
4570
4571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 * Function called for command which is Not Implemented. NI!
4573 */
4574 void
4575ex_ni(eap)
4576 exarg_T *eap;
4577{
4578 if (!eap->skip)
4579 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4580}
4581
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004582#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583/*
4584 * Function called for script command which is Not Implemented. NI!
4585 * Skips over ":perl <<EOF" constructs.
4586 */
4587 static void
4588ex_script_ni(eap)
4589 exarg_T *eap;
4590{
4591 if (!eap->skip)
4592 ex_ni(eap);
4593 else
4594 vim_free(script_get(eap, eap->arg));
4595}
4596#endif
4597
4598/*
4599 * Check range in Ex command for validity.
4600 * Return NULL when valid, error message when invalid.
4601 */
4602 static char_u *
4603invalid_range(eap)
4604 exarg_T *eap;
4605{
4606 if ( eap->line1 < 0
4607 || eap->line2 < 0
4608 || eap->line1 > eap->line2
4609 || ((eap->argt & RANGE)
4610 && !(eap->argt & NOTADR)
4611 && eap->line2 > curbuf->b_ml.ml_line_count
4612#ifdef FEAT_DIFF
4613 + (eap->cmdidx == CMD_diffget)
4614#endif
4615 ))
4616 return (char_u *)_(e_invrange);
4617 return NULL;
4618}
4619
4620/*
4621 * Correct the range for zero line number, if required.
4622 */
4623 static void
4624correct_range(eap)
4625 exarg_T *eap;
4626{
4627 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4628 {
4629 if (eap->line1 == 0)
4630 eap->line1 = 1;
4631 if (eap->line2 == 0)
4632 eap->line2 = 1;
4633 }
4634}
4635
Bram Moolenaar748bf032005-02-02 23:04:36 +00004636#ifdef FEAT_QUICKFIX
4637static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4638
4639/*
4640 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4641 * pattern. Otherwise return eap->arg.
4642 */
4643 static char_u *
4644skip_grep_pat(eap)
4645 exarg_T *eap;
4646{
4647 char_u *p = eap->arg;
4648
Bram Moolenaara37420f2006-02-04 22:37:47 +00004649 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4650 || eap->cmdidx == CMD_vimgrepadd
4651 || eap->cmdidx == CMD_lvimgrepadd
4652 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004653 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004654 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004655 if (p == NULL)
4656 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004657 }
4658 return p;
4659}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004660
4661/*
4662 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4663 * in the command line, so that things like % get expanded.
4664 */
4665 static char_u *
4666replace_makeprg(eap, p, cmdlinep)
4667 exarg_T *eap;
4668 char_u *p;
4669 char_u **cmdlinep;
4670{
4671 char_u *new_cmdline;
4672 char_u *program;
4673 char_u *pos;
4674 char_u *ptr;
4675 int len;
4676 int i;
4677
4678 /*
4679 * Don't do it when ":vimgrep" is used for ":grep".
4680 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004681 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4682 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4683 || eap->cmdidx == CMD_grepadd
4684 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004685 && !grep_internal(eap->cmdidx))
4686 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004687 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4688 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004689 {
4690 if (*curbuf->b_p_gp == NUL)
4691 program = p_gp;
4692 else
4693 program = curbuf->b_p_gp;
4694 }
4695 else
4696 {
4697 if (*curbuf->b_p_mp == NUL)
4698 program = p_mp;
4699 else
4700 program = curbuf->b_p_mp;
4701 }
4702
4703 p = skipwhite(p);
4704
4705 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4706 {
4707 /* replace $* by given arguments */
4708 i = 1;
4709 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4710 ++i;
4711 len = (int)STRLEN(p);
4712 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4713 if (new_cmdline == NULL)
4714 return NULL; /* out of memory */
4715 ptr = new_cmdline;
4716 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4717 {
4718 i = (int)(pos - program);
4719 STRNCPY(ptr, program, i);
4720 STRCPY(ptr += i, p);
4721 ptr += len;
4722 program = pos + 2;
4723 }
4724 STRCPY(ptr, program);
4725 }
4726 else
4727 {
4728 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4729 if (new_cmdline == NULL)
4730 return NULL; /* out of memory */
4731 STRCPY(new_cmdline, program);
4732 STRCAT(new_cmdline, " ");
4733 STRCAT(new_cmdline, p);
4734 }
4735 msg_make(p);
4736
4737 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4738 vim_free(*cmdlinep);
4739 *cmdlinep = new_cmdline;
4740 p = new_cmdline;
4741 }
4742 return p;
4743}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004744#endif
4745
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746/*
4747 * Expand file name in Ex command argument.
4748 * Return FAIL for failure, OK otherwise.
4749 */
4750 int
4751expand_filename(eap, cmdlinep, errormsgp)
4752 exarg_T *eap;
4753 char_u **cmdlinep;
4754 char_u **errormsgp;
4755{
4756 int has_wildcards; /* need to expand wildcards */
4757 char_u *repl;
4758 int srclen;
4759 char_u *p;
4760 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004761 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
Bram Moolenaar748bf032005-02-02 23:04:36 +00004763#ifdef FEAT_QUICKFIX
4764 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4765 p = skip_grep_pat(eap);
4766#else
4767 p = eap->arg;
4768#endif
4769
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 /*
4771 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4772 * the file name contains a wildcard it should not cause expanding.
4773 * (it will be expanded anyway if there is a wildcard before replacing).
4774 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004775 has_wildcards = mch_has_wildcard(p);
4776 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004778#ifdef FEAT_EVAL
4779 /* Skip over `=expr`, wildcards in it are not expanded. */
4780 if (p[0] == '`' && p[1] == '=')
4781 {
4782 p += 2;
4783 (void)skip_expr(&p);
4784 if (*p == '`')
4785 ++p;
4786 continue;
4787 }
4788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 /*
4790 * Quick check if this cannot be the start of a special string.
4791 * Also removes backslash before '%', '#' and '<'.
4792 */
4793 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4794 {
4795 ++p;
4796 continue;
4797 }
4798
4799 /*
4800 * Try to find a match at this position.
4801 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004802 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4803 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 if (*errormsgp != NULL) /* error detected */
4805 return FAIL;
4806 if (repl == NULL) /* no match found */
4807 {
4808 p += srclen;
4809 continue;
4810 }
4811
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004812 /* Wildcards won't be expanded below, the replacement is taken
4813 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4814 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4815 {
4816 char_u *l = repl;
4817
4818 repl = expand_env_save(repl);
4819 vim_free(l);
4820 }
4821
Bram Moolenaar63b92542007-03-27 14:57:09 +00004822 /* Need to escape white space et al. with a backslash.
4823 * Don't do this for:
4824 * - replacement that already has been escaped: "##"
4825 * - shell commands (may have to use quotes instead).
4826 * - non-unix systems when there is a single argument (spaces don't
4827 * separate arguments then).
4828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004830 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 && eap->cmdidx != CMD_bang
4832 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004833 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004835 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004837 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838#ifndef UNIX
4839 && !(eap->argt & NOSPC)
4840#endif
4841 )
4842 {
4843 char_u *l;
4844#ifdef BACKSLASH_IN_FILENAME
4845 /* Don't escape a backslash here, because rem_backslash() doesn't
4846 * remove it later. */
4847 static char_u *nobslash = (char_u *)" \t\"|";
4848# define ESCAPE_CHARS nobslash
4849#else
4850# define ESCAPE_CHARS escape_chars
4851#endif
4852
4853 for (l = repl; *l; ++l)
4854 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4855 {
4856 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4857 if (l != NULL)
4858 {
4859 vim_free(repl);
4860 repl = l;
4861 }
4862 break;
4863 }
4864 }
4865
4866 /* For a shell command a '!' must be escaped. */
4867 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004868 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 {
4870 char_u *l;
4871
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004872 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 if (l != NULL)
4874 {
4875 vim_free(repl);
4876 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878 }
4879
4880 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4881 vim_free(repl);
4882 if (p == NULL)
4883 return FAIL;
4884 }
4885
4886 /*
4887 * One file argument: Expand wildcards.
4888 * Don't do this with ":r !command" or ":w !command".
4889 */
4890 if ((eap->argt & NOSPC) && !eap->usefilter)
4891 {
4892 /*
4893 * May do this twice:
4894 * 1. Replace environment variables.
4895 * 2. Replace any other wildcards, remove backslashes.
4896 */
4897 for (n = 1; n <= 2; ++n)
4898 {
4899 if (n == 2)
4900 {
4901#ifdef UNIX
4902 /*
4903 * Only for Unix we check for more than one file name.
4904 * For other systems spaces are considered to be part
4905 * of the file name.
4906 * Only check here if there is no wildcard, otherwise
4907 * ExpandOne() will check for errors. This allows
4908 * ":e `ls ve*.c`" on Unix.
4909 */
4910 if (!has_wildcards)
4911 for (p = eap->arg; *p; ++p)
4912 {
4913 /* skip escaped characters */
4914 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4915 ++p;
4916 else if (vim_iswhite(*p))
4917 {
4918 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4919 return FAIL;
4920 }
4921 }
4922#endif
4923
4924 /*
4925 * Halve the number of backslashes (this is Vi compatible).
4926 * For Unix and OS/2, when wildcards are expanded, this is
4927 * done by ExpandOne() below.
4928 */
4929#if defined(UNIX) || defined(OS2)
4930 if (!has_wildcards)
4931#endif
4932 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934
4935 if (has_wildcards)
4936 {
4937 if (n == 1)
4938 {
4939 /*
4940 * First loop: May expand environment variables. This
4941 * can be done much faster with expand_env() than with
4942 * something else (e.g., calling a shell).
4943 * After expanding environment variables, check again
4944 * if there are still wildcards present.
4945 */
4946 if (vim_strchr(eap->arg, '$') != NULL
4947 || vim_strchr(eap->arg, '~') != NULL)
4948 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004949 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004950 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 has_wildcards = mch_has_wildcard(NameBuff);
4952 p = NameBuff;
4953 }
4954 else
4955 p = NULL;
4956 }
4957 else /* n == 2 */
4958 {
4959 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004960 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961
4962 ExpandInit(&xpc);
4963 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004964 if (p_wic)
4965 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004967 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 if (p == NULL)
4969 return FAIL;
4970 }
4971 if (p != NULL)
4972 {
4973 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4974 p, cmdlinep);
4975 if (n == 2) /* p came from ExpandOne() */
4976 vim_free(p);
4977 }
4978 }
4979 }
4980 }
4981 return OK;
4982}
4983
4984/*
4985 * Replace part of the command line, keeping eap->cmd, eap->arg and
4986 * eap->nextcmd correct.
4987 * "src" points to the part that is to be replaced, of length "srclen".
4988 * "repl" is the replacement string.
4989 * Returns a pointer to the character after the replaced string.
4990 * Returns NULL for failure.
4991 */
4992 static char_u *
4993repl_cmdline(eap, src, srclen, repl, cmdlinep)
4994 exarg_T *eap;
4995 char_u *src;
4996 int srclen;
4997 char_u *repl;
4998 char_u **cmdlinep;
4999{
5000 int len;
5001 int i;
5002 char_u *new_cmdline;
5003
5004 /*
5005 * The new command line is build in new_cmdline[].
5006 * First allocate it.
5007 * Careful: a "+cmd" argument may have been NUL terminated.
5008 */
5009 len = (int)STRLEN(repl);
5010 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005011 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5013 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5014 return NULL; /* out of memory! */
5015
5016 /*
5017 * Copy the stuff before the expanded part.
5018 * Copy the expanded stuff.
5019 * Copy what came after the expanded part.
5020 * Copy the next commands, if there are any.
5021 */
5022 i = (int)(src - *cmdlinep); /* length of part before match */
5023 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005024
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 mch_memmove(new_cmdline + i, repl, (size_t)len);
5026 i += len; /* remember the end of the string */
5027 STRCPY(new_cmdline + i, src + srclen);
5028 src = new_cmdline + i; /* remember where to continue */
5029
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005030 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 {
5032 i = (int)STRLEN(new_cmdline) + 1;
5033 STRCPY(new_cmdline + i, eap->nextcmd);
5034 eap->nextcmd = new_cmdline + i;
5035 }
5036 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5037 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5038 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5039 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5040 vim_free(*cmdlinep);
5041 *cmdlinep = new_cmdline;
5042
5043 return src;
5044}
5045
5046/*
5047 * Check for '|' to separate commands and '"' to start comments.
5048 */
5049 void
5050separate_nextcmd(eap)
5051 exarg_T *eap;
5052{
5053 char_u *p;
5054
Bram Moolenaar86b68352004-12-27 21:59:20 +00005055#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005056 p = skip_grep_pat(eap);
5057#else
5058 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005059#endif
5060
5061 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 {
5063 if (*p == Ctrl_V)
5064 {
5065 if (eap->argt & (USECTRLV | XFILE))
5066 ++p; /* skip CTRL-V and next char */
5067 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005068 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005069 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 if (*p == NUL) /* stop at NUL after CTRL-V */
5071 break;
5072 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005073
5074#ifdef FEAT_EVAL
5075 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005076 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005077 {
5078 p += 2;
5079 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005080 }
5081#endif
5082
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 /* Check for '"': start of comment or '|': next command */
5084 /* :@" and :*" do not start a comment!
5085 * :redir @" doesn't either. */
5086 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5087 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5088 || p != eap->arg)
5089 && (eap->cmdidx != CMD_redir
5090 || p != eap->arg + 1 || p[-1] != '@'))
5091 || *p == '|' || *p == '\n')
5092 {
5093 /*
5094 * We remove the '\' before the '|', unless USECTRLV is used
5095 * AND 'b' is present in 'cpoptions'.
5096 */
5097 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5098 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5099 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005100 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 --p;
5102 }
5103 else
5104 {
5105 eap->nextcmd = check_nextcmd(p);
5106 *p = NUL;
5107 break;
5108 }
5109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005111
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5113 del_trailing_spaces(eap->arg);
5114}
5115
5116/*
5117 * get + command from ex argument
5118 */
5119 static char_u *
5120getargcmd(argp)
5121 char_u **argp;
5122{
5123 char_u *arg = *argp;
5124 char_u *command = NULL;
5125
5126 if (*arg == '+') /* +[command] */
5127 {
5128 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005129 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 command = dollar_command;
5131 else
5132 {
5133 command = arg;
5134 arg = skip_cmd_arg(command, TRUE);
5135 if (*arg != NUL)
5136 *arg++ = NUL; /* terminate command with NUL */
5137 }
5138
5139 arg = skipwhite(arg); /* skip over spaces */
5140 *argp = arg;
5141 }
5142 return command;
5143}
5144
5145/*
5146 * Find end of "+command" argument. Skip over "\ " and "\\".
5147 */
5148 static char_u *
5149skip_cmd_arg(p, rembs)
5150 char_u *p;
5151 int rembs; /* TRUE to halve the number of backslashes */
5152{
5153 while (*p && !vim_isspace(*p))
5154 {
5155 if (*p == '\\' && p[1] != NUL)
5156 {
5157 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005158 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 else
5160 ++p;
5161 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005162 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
5164 return p;
5165}
5166
5167/*
5168 * Get "++opt=arg" argument.
5169 * Return FAIL or OK.
5170 */
5171 static int
5172getargopt(eap)
5173 exarg_T *eap;
5174{
5175 char_u *arg = eap->arg + 2;
5176 int *pp = NULL;
5177#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005178 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 char_u *p;
5180#endif
5181
5182 /* ":edit ++[no]bin[ary] file" */
5183 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5184 {
5185 if (*arg == 'n')
5186 {
5187 arg += 2;
5188 eap->force_bin = FORCE_NOBIN;
5189 }
5190 else
5191 eap->force_bin = FORCE_BIN;
5192 if (!checkforcmd(&arg, "binary", 3))
5193 return FAIL;
5194 eap->arg = skipwhite(arg);
5195 return OK;
5196 }
5197
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005198 /* ":read ++edit file" */
5199 if (STRNCMP(arg, "edit", 4) == 0)
5200 {
5201 eap->read_edit = TRUE;
5202 eap->arg = skipwhite(arg + 4);
5203 return OK;
5204 }
5205
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 if (STRNCMP(arg, "ff", 2) == 0)
5207 {
5208 arg += 2;
5209 pp = &eap->force_ff;
5210 }
5211 else if (STRNCMP(arg, "fileformat", 10) == 0)
5212 {
5213 arg += 10;
5214 pp = &eap->force_ff;
5215 }
5216#ifdef FEAT_MBYTE
5217 else if (STRNCMP(arg, "enc", 3) == 0)
5218 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005219 if (STRNCMP(arg, "encoding", 8) == 0)
5220 arg += 8;
5221 else
5222 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 pp = &eap->force_enc;
5224 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005225 else if (STRNCMP(arg, "bad", 3) == 0)
5226 {
5227 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005228 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230#endif
5231
5232 if (pp == NULL || *arg != '=')
5233 return FAIL;
5234
5235 ++arg;
5236 *pp = (int)(arg - eap->cmd);
5237 arg = skip_cmd_arg(arg, FALSE);
5238 eap->arg = skipwhite(arg);
5239 *arg = NUL;
5240
5241#ifdef FEAT_MBYTE
5242 if (pp == &eap->force_ff)
5243 {
5244#endif
5245 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5246 return FAIL;
5247#ifdef FEAT_MBYTE
5248 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005249 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 {
5251 /* Make 'fileencoding' lower case. */
5252 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5253 *p = TOLOWER_ASC(*p);
5254 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005255 else
5256 {
5257 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5258 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005259 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005260 if (STRICMP(p, "keep") == 0)
5261 eap->bad_char = BAD_KEEP;
5262 else if (STRICMP(p, "drop") == 0)
5263 eap->bad_char = BAD_DROP;
5264 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5265 eap->bad_char = *p;
5266 else
5267 return FAIL;
5268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269#endif
5270
5271 return OK;
5272}
5273
5274/*
5275 * ":abbreviate" and friends.
5276 */
5277 static void
5278ex_abbreviate(eap)
5279 exarg_T *eap;
5280{
5281 do_exmap(eap, TRUE); /* almost the same as mapping */
5282}
5283
5284/*
5285 * ":map" and friends.
5286 */
5287 static void
5288ex_map(eap)
5289 exarg_T *eap;
5290{
5291 /*
5292 * If we are sourcing .exrc or .vimrc in current directory we
5293 * print the mappings for security reasons.
5294 */
5295 if (secure)
5296 {
5297 secure = 2;
5298 msg_outtrans(eap->cmd);
5299 msg_putchar('\n');
5300 }
5301 do_exmap(eap, FALSE);
5302}
5303
5304/*
5305 * ":unmap" and friends.
5306 */
5307 static void
5308ex_unmap(eap)
5309 exarg_T *eap;
5310{
5311 do_exmap(eap, FALSE);
5312}
5313
5314/*
5315 * ":mapclear" and friends.
5316 */
5317 static void
5318ex_mapclear(eap)
5319 exarg_T *eap;
5320{
5321 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5322}
5323
5324/*
5325 * ":abclear" and friends.
5326 */
5327 static void
5328ex_abclear(eap)
5329 exarg_T *eap;
5330{
5331 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5332}
5333
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005334#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 static void
5336ex_autocmd(eap)
5337 exarg_T *eap;
5338{
5339 /*
5340 * Disallow auto commands from .exrc and .vimrc in current
5341 * directory for security reasons.
5342 */
5343 if (secure)
5344 {
5345 secure = 2;
5346 eap->errmsg = e_curdir;
5347 }
5348 else if (eap->cmdidx == CMD_autocmd)
5349 do_autocmd(eap->arg, eap->forceit);
5350 else
5351 do_augroup(eap->arg, eap->forceit);
5352}
5353
5354/*
5355 * ":doautocmd": Apply the automatic commands to the current buffer.
5356 */
5357 static void
5358ex_doautocmd(eap)
5359 exarg_T *eap;
5360{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005361 char_u *arg = eap->arg;
5362 int call_do_modelines = check_nomodeline(&arg);
5363
5364 (void)do_doautocmd(arg, TRUE);
5365 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5366 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367}
5368#endif
5369
5370#ifdef FEAT_LISTCMDS
5371/*
5372 * :[N]bunload[!] [N] [bufname] unload buffer
5373 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5374 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5375 */
5376 static void
5377ex_bunload(eap)
5378 exarg_T *eap;
5379{
5380 eap->errmsg = do_bufdel(
5381 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5382 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5383 : DOBUF_UNLOAD, eap->arg,
5384 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5385}
5386
5387/*
5388 * :[N]buffer [N] to buffer N
5389 * :[N]sbuffer [N] to buffer N
5390 */
5391 static void
5392ex_buffer(eap)
5393 exarg_T *eap;
5394{
5395 if (*eap->arg)
5396 eap->errmsg = e_trailing;
5397 else
5398 {
5399 if (eap->addr_count == 0) /* default is current buffer */
5400 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5401 else
5402 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005403 if (eap->do_ecmd_cmd != NULL)
5404 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 }
5406}
5407
5408/*
5409 * :[N]bmodified [N] to next mod. buffer
5410 * :[N]sbmodified [N] to next mod. buffer
5411 */
5412 static void
5413ex_bmodified(eap)
5414 exarg_T *eap;
5415{
5416 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005417 if (eap->do_ecmd_cmd != NULL)
5418 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419}
5420
5421/*
5422 * :[N]bnext [N] to next buffer
5423 * :[N]sbnext [N] split and to next buffer
5424 */
5425 static void
5426ex_bnext(eap)
5427 exarg_T *eap;
5428{
5429 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005430 if (eap->do_ecmd_cmd != NULL)
5431 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432}
5433
5434/*
5435 * :[N]bNext [N] to previous buffer
5436 * :[N]bprevious [N] to previous buffer
5437 * :[N]sbNext [N] split and to previous buffer
5438 * :[N]sbprevious [N] split and to previous buffer
5439 */
5440 static void
5441ex_bprevious(eap)
5442 exarg_T *eap;
5443{
5444 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005445 if (eap->do_ecmd_cmd != NULL)
5446 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447}
5448
5449/*
5450 * :brewind to first buffer
5451 * :bfirst to first buffer
5452 * :sbrewind split and to first buffer
5453 * :sbfirst split and to first buffer
5454 */
5455 static void
5456ex_brewind(eap)
5457 exarg_T *eap;
5458{
5459 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005460 if (eap->do_ecmd_cmd != NULL)
5461 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462}
5463
5464/*
5465 * :blast to last buffer
5466 * :sblast split and to last buffer
5467 */
5468 static void
5469ex_blast(eap)
5470 exarg_T *eap;
5471{
5472 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005473 if (eap->do_ecmd_cmd != NULL)
5474 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475}
5476#endif
5477
5478 int
5479ends_excmd(c)
5480 int c;
5481{
5482 return (c == NUL || c == '|' || c == '"' || c == '\n');
5483}
5484
5485#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5486 || defined(PROTO)
5487/*
5488 * Return the next command, after the first '|' or '\n'.
5489 * Return NULL if not found.
5490 */
5491 char_u *
5492find_nextcmd(p)
5493 char_u *p;
5494{
5495 while (*p != '|' && *p != '\n')
5496 {
5497 if (*p == NUL)
5498 return NULL;
5499 ++p;
5500 }
5501 return (p + 1);
5502}
5503#endif
5504
5505/*
5506 * Check if *p is a separator between Ex commands.
5507 * Return NULL if it isn't, (p + 1) if it is.
5508 */
5509 char_u *
5510check_nextcmd(p)
5511 char_u *p;
5512{
5513 p = skipwhite(p);
5514 if (*p == '|' || *p == '\n')
5515 return (p + 1);
5516 else
5517 return NULL;
5518}
5519
5520/*
5521 * - if there are more files to edit
5522 * - and this is the last window
5523 * - and forceit not used
5524 * - and not repeated twice on a row
5525 * return FAIL and give error message if 'message' TRUE
5526 * return OK otherwise
5527 */
5528 static int
5529check_more(message, forceit)
5530 int message; /* when FALSE check only, no messages */
5531 int forceit;
5532{
5533 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5534
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005535 if (!forceit && only_one_window()
5536 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 {
5538 if (message)
5539 {
5540#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5541 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5542 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005543 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544
5545 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005546 vim_strncpy(buff,
5547 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005548 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005550 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 _("%d more files to edit. Quit anyway?"), n);
5552 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5553 return OK;
5554 return FAIL;
5555 }
5556#endif
5557 if (n == 1)
5558 EMSG(_("E173: 1 more file to edit"));
5559 else
5560 EMSGN(_("E173: %ld more files to edit"), n);
5561 quitmore = 2; /* next try to quit is allowed */
5562 }
5563 return FAIL;
5564 }
5565 return OK;
5566}
5567
5568#ifdef FEAT_CMDL_COMPL
5569/*
5570 * Function given to ExpandGeneric() to obtain the list of command names.
5571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 char_u *
5573get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005574 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 int idx;
5576{
5577 if (idx >= (int)CMD_SIZE)
5578# ifdef FEAT_USR_CMDS
5579 return get_user_command_name(idx);
5580# else
5581 return NULL;
5582# endif
5583 return cmdnames[idx].cmd_name;
5584}
5585#endif
5586
5587#if defined(FEAT_USR_CMDS) || defined(PROTO)
5588static 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));
5589static void uc_list __ARGS((char_u *name, size_t name_len));
5590static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5591static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5592static 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));
5593
5594 static int
5595uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5596 char_u *name;
5597 size_t name_len;
5598 char_u *rep;
5599 long argt;
5600 long def;
5601 int flags;
5602 int compl;
5603 char_u *compl_arg;
5604 int force;
5605{
5606 ucmd_T *cmd = NULL;
5607 char_u *p;
5608 int i;
5609 int cmp = 1;
5610 char_u *rep_buf = NULL;
5611 garray_T *gap;
5612
Bram Moolenaar9c102382006-05-03 21:26:49 +00005613 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 if (rep_buf == NULL)
5615 {
5616 /* Can't replace termcodes - try using the string as is */
5617 rep_buf = vim_strsave(rep);
5618
5619 /* Give up if out of memory */
5620 if (rep_buf == NULL)
5621 return FAIL;
5622 }
5623
5624 /* get address of growarray: global or in curbuf */
5625 if (flags & UC_BUFFER)
5626 {
5627 gap = &curbuf->b_ucmds;
5628 if (gap->ga_itemsize == 0)
5629 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5630 }
5631 else
5632 gap = &ucmds;
5633
5634 /* Search for the command in the already defined commands. */
5635 for (i = 0; i < gap->ga_len; ++i)
5636 {
5637 size_t len;
5638
5639 cmd = USER_CMD_GA(gap, i);
5640 len = STRLEN(cmd->uc_name);
5641 cmp = STRNCMP(name, cmd->uc_name, name_len);
5642 if (cmp == 0)
5643 {
5644 if (name_len < len)
5645 cmp = -1;
5646 else if (name_len > len)
5647 cmp = 1;
5648 }
5649
5650 if (cmp == 0)
5651 {
5652 if (!force)
5653 {
5654 EMSG(_("E174: Command already exists: add ! to replace it"));
5655 goto fail;
5656 }
5657
5658 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005659 cmd->uc_rep = NULL;
5660#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5661 vim_free(cmd->uc_compl_arg);
5662 cmd->uc_compl_arg = NULL;
5663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 break;
5665 }
5666
5667 /* Stop as soon as we pass the name to add */
5668 if (cmp < 0)
5669 break;
5670 }
5671
5672 /* Extend the array unless we're replacing an existing command */
5673 if (cmp != 0)
5674 {
5675 if (ga_grow(gap, 1) != OK)
5676 goto fail;
5677 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5678 goto fail;
5679
5680 cmd = USER_CMD_GA(gap, i);
5681 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5682
5683 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684
5685 cmd->uc_name = p;
5686 }
5687
5688 cmd->uc_rep = rep_buf;
5689 cmd->uc_argt = argt;
5690 cmd->uc_def = def;
5691 cmd->uc_compl = compl;
5692#ifdef FEAT_EVAL
5693 cmd->uc_scriptID = current_SID;
5694# ifdef FEAT_CMDL_COMPL
5695 cmd->uc_compl_arg = compl_arg;
5696# endif
5697#endif
5698
5699 return OK;
5700
5701fail:
5702 vim_free(rep_buf);
5703#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5704 vim_free(compl_arg);
5705#endif
5706 return FAIL;
5707}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005710#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711/*
5712 * List of names for completion for ":command" with the EXPAND_ flag.
5713 * Must be alphabetical for completion.
5714 */
5715static struct
5716{
5717 int expand;
5718 char *name;
5719} command_complete[] =
5720{
5721 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005722 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005724 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005726 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005727#if defined(FEAT_CSCOPE)
5728 {EXPAND_CSCOPE, "cscope"},
5729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5731 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005732 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733#endif
5734 {EXPAND_DIRECTORIES, "dir"},
5735 {EXPAND_ENV_VARS, "environment"},
5736 {EXPAND_EVENTS, "event"},
5737 {EXPAND_EXPRESSION, "expression"},
5738 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005739 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005740 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 {EXPAND_FUNCTIONS, "function"},
5742 {EXPAND_HELP, "help"},
5743 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005744#if defined(FEAT_CMDHIST)
5745 {EXPAND_HISTORY, "history"},
5746#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005747#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005748 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005749 {EXPAND_LOCALES, "locale"},
5750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 {EXPAND_MAPPINGS, "mapping"},
5752 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005753 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005754#if defined(FEAT_PROFILE)
5755 {EXPAND_SYNTIME, "syntime"},
5756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005758 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005759#if defined(FEAT_SIGNS)
5760 {EXPAND_SIGN, "sign"},
5761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 {EXPAND_TAGS, "tag"},
5763 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005764 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 {EXPAND_USER_VARS, "var"},
5766 {0, NULL}
5767};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005770#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 static void
5772uc_list(name, name_len)
5773 char_u *name;
5774 size_t name_len;
5775{
5776 int i, j;
5777 int found = FALSE;
5778 ucmd_T *cmd;
5779 int len;
5780 long a;
5781 garray_T *gap;
5782
5783 gap = &curbuf->b_ucmds;
5784 for (;;)
5785 {
5786 for (i = 0; i < gap->ga_len; ++i)
5787 {
5788 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005789 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790
5791 /* Skip commands which don't match the requested prefix */
5792 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5793 continue;
5794
5795 /* Put out the title first time */
5796 if (!found)
5797 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5798 found = TRUE;
5799 msg_putchar('\n');
5800 if (got_int)
5801 break;
5802
5803 /* Special cases */
5804 msg_putchar(a & BANG ? '!' : ' ');
5805 msg_putchar(a & REGSTR ? '"' : ' ');
5806 msg_putchar(gap != &ucmds ? 'b' : ' ');
5807 msg_putchar(' ');
5808
5809 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5810 len = (int)STRLEN(cmd->uc_name) + 4;
5811
5812 do {
5813 msg_putchar(' ');
5814 ++len;
5815 } while (len < 16);
5816
5817 len = 0;
5818
5819 /* Arguments */
5820 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5821 {
5822 case 0: IObuff[len++] = '0'; break;
5823 case (EXTRA): IObuff[len++] = '*'; break;
5824 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5825 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5826 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5827 }
5828
5829 do {
5830 IObuff[len++] = ' ';
5831 } while (len < 5);
5832
5833 /* Range */
5834 if (a & (RANGE|COUNT))
5835 {
5836 if (a & COUNT)
5837 {
5838 /* -count=N */
5839 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5840 len += (int)STRLEN(IObuff + len);
5841 }
5842 else if (a & DFLALL)
5843 IObuff[len++] = '%';
5844 else if (cmd->uc_def >= 0)
5845 {
5846 /* -range=N */
5847 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5848 len += (int)STRLEN(IObuff + len);
5849 }
5850 else
5851 IObuff[len++] = '.';
5852 }
5853
5854 do {
5855 IObuff[len++] = ' ';
5856 } while (len < 11);
5857
5858 /* Completion */
5859 for (j = 0; command_complete[j].expand != 0; ++j)
5860 if (command_complete[j].expand == cmd->uc_compl)
5861 {
5862 STRCPY(IObuff + len, command_complete[j].name);
5863 len += (int)STRLEN(IObuff + len);
5864 break;
5865 }
5866
5867 do {
5868 IObuff[len++] = ' ';
5869 } while (len < 21);
5870
5871 IObuff[len] = '\0';
5872 msg_outtrans(IObuff);
5873
5874 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005875#ifdef FEAT_EVAL
5876 if (p_verbose > 0)
5877 last_set_msg(cmd->uc_scriptID);
5878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 out_flush();
5880 ui_breakcheck();
5881 if (got_int)
5882 break;
5883 }
5884 if (gap == &ucmds || i < gap->ga_len)
5885 break;
5886 gap = &ucmds;
5887 }
5888
5889 if (!found)
5890 MSG(_("No user-defined commands found"));
5891}
5892
5893 static char_u *
5894uc_fun_cmd()
5895{
5896 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5897 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5898 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5899 0xb9, 0x7f, 0};
5900 int i;
5901
5902 for (i = 0; fcmd[i]; ++i)
5903 IObuff[i] = fcmd[i] - 0x40;
5904 IObuff[i] = 0;
5905 return IObuff;
5906}
5907
5908 static int
5909uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5910 char_u *attr;
5911 size_t len;
5912 long *argt;
5913 long *def;
5914 int *flags;
5915 int *compl;
5916 char_u **compl_arg;
5917{
5918 char_u *p;
5919
5920 if (len == 0)
5921 {
5922 EMSG(_("E175: No attribute specified"));
5923 return FAIL;
5924 }
5925
5926 /* First, try the simple attributes (no arguments) */
5927 if (STRNICMP(attr, "bang", len) == 0)
5928 *argt |= BANG;
5929 else if (STRNICMP(attr, "buffer", len) == 0)
5930 *flags |= UC_BUFFER;
5931 else if (STRNICMP(attr, "register", len) == 0)
5932 *argt |= REGSTR;
5933 else if (STRNICMP(attr, "bar", len) == 0)
5934 *argt |= TRLBAR;
5935 else
5936 {
5937 int i;
5938 char_u *val = NULL;
5939 size_t vallen = 0;
5940 size_t attrlen = len;
5941
5942 /* Look for the attribute name - which is the part before any '=' */
5943 for (i = 0; i < (int)len; ++i)
5944 {
5945 if (attr[i] == '=')
5946 {
5947 val = &attr[i + 1];
5948 vallen = len - i - 1;
5949 attrlen = i;
5950 break;
5951 }
5952 }
5953
5954 if (STRNICMP(attr, "nargs", attrlen) == 0)
5955 {
5956 if (vallen == 1)
5957 {
5958 if (*val == '0')
5959 /* Do nothing - this is the default */;
5960 else if (*val == '1')
5961 *argt |= (EXTRA | NOSPC | NEEDARG);
5962 else if (*val == '*')
5963 *argt |= EXTRA;
5964 else if (*val == '?')
5965 *argt |= (EXTRA | NOSPC);
5966 else if (*val == '+')
5967 *argt |= (EXTRA | NEEDARG);
5968 else
5969 goto wrong_nargs;
5970 }
5971 else
5972 {
5973wrong_nargs:
5974 EMSG(_("E176: Invalid number of arguments"));
5975 return FAIL;
5976 }
5977 }
5978 else if (STRNICMP(attr, "range", attrlen) == 0)
5979 {
5980 *argt |= RANGE;
5981 if (vallen == 1 && *val == '%')
5982 *argt |= DFLALL;
5983 else if (val != NULL)
5984 {
5985 p = val;
5986 if (*def >= 0)
5987 {
5988two_count:
5989 EMSG(_("E177: Count cannot be specified twice"));
5990 return FAIL;
5991 }
5992
5993 *def = getdigits(&p);
5994 *argt |= (ZEROR | NOTADR);
5995
5996 if (p != val + vallen || vallen == 0)
5997 {
5998invalid_count:
5999 EMSG(_("E178: Invalid default value for count"));
6000 return FAIL;
6001 }
6002 }
6003 }
6004 else if (STRNICMP(attr, "count", attrlen) == 0)
6005 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006006 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007
6008 if (val != NULL)
6009 {
6010 p = val;
6011 if (*def >= 0)
6012 goto two_count;
6013
6014 *def = getdigits(&p);
6015
6016 if (p != val + vallen)
6017 goto invalid_count;
6018 }
6019
6020 if (*def < 0)
6021 *def = 0;
6022 }
6023 else if (STRNICMP(attr, "complete", attrlen) == 0)
6024 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 if (val == NULL)
6026 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006027 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 return FAIL;
6029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006031 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6032 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 }
6035 else
6036 {
6037 char_u ch = attr[len];
6038 attr[len] = '\0';
6039 EMSG2(_("E181: Invalid attribute: %s"), attr);
6040 attr[len] = ch;
6041 return FAIL;
6042 }
6043 }
6044
6045 return OK;
6046}
6047
Bram Moolenaara850a712009-01-28 14:42:59 +00006048/*
6049 * ":command ..."
6050 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 static void
6052ex_command(eap)
6053 exarg_T *eap;
6054{
6055 char_u *name;
6056 char_u *end;
6057 char_u *p;
6058 long argt = 0;
6059 long def = -1;
6060 int flags = 0;
6061 int compl = EXPAND_NOTHING;
6062 char_u *compl_arg = NULL;
6063 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006064 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065
6066 p = eap->arg;
6067
6068 /* Check for attributes */
6069 while (*p == '-')
6070 {
6071 ++p;
6072 end = skiptowhite(p);
6073 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
6074 == FAIL)
6075 return;
6076 p = skipwhite(end);
6077 }
6078
6079 /* Get the name (if any) and skip to the following argument */
6080 name = p;
6081 if (ASCII_ISALPHA(*p))
6082 while (ASCII_ISALNUM(*p))
6083 ++p;
6084 if (!ends_excmd(*p) && !vim_iswhite(*p))
6085 {
6086 EMSG(_("E182: Invalid command name"));
6087 return;
6088 }
6089 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006090 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
6092 /* If there is nothing after the name, and no attributes were specified,
6093 * we are listing commands
6094 */
6095 p = skipwhite(end);
6096 if (!has_attr && ends_excmd(*p))
6097 {
6098 uc_list(name, end - name);
6099 }
6100 else if (!ASCII_ISUPPER(*name))
6101 {
6102 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6103 return;
6104 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006105 else if ((name_len == 1 && *name == 'X')
6106 || (name_len <= 4
6107 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6108 {
6109 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6110 return;
6111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 else
6113 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
6114 eap->forceit);
6115}
6116
6117/*
6118 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 * Clear all user commands, global and for current buffer.
6120 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006121 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006123 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
6125 uc_clear(&ucmds);
6126 uc_clear(&curbuf->b_ucmds);
6127}
6128
6129/*
6130 * Clear all user commands for "gap".
6131 */
6132 void
6133uc_clear(gap)
6134 garray_T *gap;
6135{
6136 int i;
6137 ucmd_T *cmd;
6138
6139 for (i = 0; i < gap->ga_len; ++i)
6140 {
6141 cmd = USER_CMD_GA(gap, i);
6142 vim_free(cmd->uc_name);
6143 vim_free(cmd->uc_rep);
6144# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6145 vim_free(cmd->uc_compl_arg);
6146# endif
6147 }
6148 ga_clear(gap);
6149}
6150
6151 static void
6152ex_delcommand(eap)
6153 exarg_T *eap;
6154{
6155 int i = 0;
6156 ucmd_T *cmd = NULL;
6157 int cmp = -1;
6158 garray_T *gap;
6159
6160 gap = &curbuf->b_ucmds;
6161 for (;;)
6162 {
6163 for (i = 0; i < gap->ga_len; ++i)
6164 {
6165 cmd = USER_CMD_GA(gap, i);
6166 cmp = STRCMP(eap->arg, cmd->uc_name);
6167 if (cmp <= 0)
6168 break;
6169 }
6170 if (gap == &ucmds || cmp == 0)
6171 break;
6172 gap = &ucmds;
6173 }
6174
6175 if (cmp != 0)
6176 {
6177 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6178 return;
6179 }
6180
6181 vim_free(cmd->uc_name);
6182 vim_free(cmd->uc_rep);
6183# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6184 vim_free(cmd->uc_compl_arg);
6185# endif
6186
6187 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188
6189 if (i < gap->ga_len)
6190 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6191}
6192
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006193/*
6194 * split and quote args for <f-args>
6195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 static char_u *
6197uc_split_args(arg, lenp)
6198 char_u *arg;
6199 size_t *lenp;
6200{
6201 char_u *buf;
6202 char_u *p;
6203 char_u *q;
6204 int len;
6205
6206 /* Precalculate length */
6207 p = arg;
6208 len = 2; /* Initial and final quotes */
6209
6210 while (*p)
6211 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006212 if (p[0] == '\\' && p[1] == '\\')
6213 {
6214 len += 2;
6215 p += 2;
6216 }
6217 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 {
6219 len += 1;
6220 p += 2;
6221 }
6222 else if (*p == '\\' || *p == '"')
6223 {
6224 len += 2;
6225 p += 1;
6226 }
6227 else if (vim_iswhite(*p))
6228 {
6229 p = skipwhite(p);
6230 if (*p == NUL)
6231 break;
6232 len += 3; /* "," */
6233 }
6234 else
6235 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006236#ifdef FEAT_MBYTE
6237 int charlen = (*mb_ptr2len)(p);
6238 len += charlen;
6239 p += charlen;
6240#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 ++len;
6242 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 }
6245 }
6246
6247 buf = alloc(len + 1);
6248 if (buf == NULL)
6249 {
6250 *lenp = 0;
6251 return buf;
6252 }
6253
6254 p = arg;
6255 q = buf;
6256 *q++ = '"';
6257 while (*p)
6258 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006259 if (p[0] == '\\' && p[1] == '\\')
6260 {
6261 *q++ = '\\';
6262 *q++ = '\\';
6263 p += 2;
6264 }
6265 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 {
6267 *q++ = p[1];
6268 p += 2;
6269 }
6270 else if (*p == '\\' || *p == '"')
6271 {
6272 *q++ = '\\';
6273 *q++ = *p++;
6274 }
6275 else if (vim_iswhite(*p))
6276 {
6277 p = skipwhite(p);
6278 if (*p == NUL)
6279 break;
6280 *q++ = '"';
6281 *q++ = ',';
6282 *q++ = '"';
6283 }
6284 else
6285 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006286 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 }
6288 }
6289 *q++ = '"';
6290 *q = 0;
6291
6292 *lenp = len;
6293 return buf;
6294}
6295
6296/*
6297 * Check for a <> code in a user command.
6298 * "code" points to the '<'. "len" the length of the <> (inclusive).
6299 * "buf" is where the result is to be added.
6300 * "split_buf" points to a buffer used for splitting, caller should free it.
6301 * "split_len" is the length of what "split_buf" contains.
6302 * Returns the length of the replacement, which has been added to "buf".
6303 * Returns -1 if there was no match, and only the "<" has been copied.
6304 */
6305 static size_t
6306uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
6307 char_u *code;
6308 size_t len;
6309 char_u *buf;
6310 ucmd_T *cmd; /* the user command we're expanding */
6311 exarg_T *eap; /* ex arguments */
6312 char_u **split_buf;
6313 size_t *split_len;
6314{
6315 size_t result = 0;
6316 char_u *p = code + 1;
6317 size_t l = len - 2;
6318 int quote = 0;
6319 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6320 ct_LT, ct_NONE } type = ct_NONE;
6321
6322 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6323 {
6324 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6325 p += 2;
6326 l -= 2;
6327 }
6328
Bram Moolenaar371d5402006-03-20 21:47:49 +00006329 ++l;
6330 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006332 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006334 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006336 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006338 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006340 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006342 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006344 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 type = ct_REGISTER;
6346
6347 switch (type)
6348 {
6349 case ct_ARGS:
6350 /* Simple case first */
6351 if (*eap->arg == NUL)
6352 {
6353 if (quote == 1)
6354 {
6355 result = 2;
6356 if (buf != NULL)
6357 STRCPY(buf, "''");
6358 }
6359 else
6360 result = 0;
6361 break;
6362 }
6363
6364 /* When specified there is a single argument don't split it.
6365 * Works for ":Cmd %" when % is "a b c". */
6366 if ((eap->argt & NOSPC) && quote == 2)
6367 quote = 1;
6368
6369 switch (quote)
6370 {
6371 case 0: /* No quoting, no splitting */
6372 result = STRLEN(eap->arg);
6373 if (buf != NULL)
6374 STRCPY(buf, eap->arg);
6375 break;
6376 case 1: /* Quote, but don't split */
6377 result = STRLEN(eap->arg) + 2;
6378 for (p = eap->arg; *p; ++p)
6379 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006380#ifdef FEAT_MBYTE
6381 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6382 /* DBCS can contain \ in a trail byte, skip the
6383 * double-byte character. */
6384 ++p;
6385 else
6386#endif
6387 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 ++result;
6389 }
6390
6391 if (buf != NULL)
6392 {
6393 *buf++ = '"';
6394 for (p = eap->arg; *p; ++p)
6395 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006396#ifdef FEAT_MBYTE
6397 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6398 /* DBCS can contain \ in a trail byte, copy the
6399 * double-byte character to avoid escaping. */
6400 *buf++ = *p++;
6401 else
6402#endif
6403 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 *buf++ = '\\';
6405 *buf++ = *p;
6406 }
6407 *buf = '"';
6408 }
6409
6410 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006411 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 /* This is hard, so only do it once, and cache the result */
6413 if (*split_buf == NULL)
6414 *split_buf = uc_split_args(eap->arg, split_len);
6415
6416 result = *split_len;
6417 if (buf != NULL && result != 0)
6418 STRCPY(buf, *split_buf);
6419
6420 break;
6421 }
6422 break;
6423
6424 case ct_BANG:
6425 result = eap->forceit ? 1 : 0;
6426 if (quote)
6427 result += 2;
6428 if (buf != NULL)
6429 {
6430 if (quote)
6431 *buf++ = '"';
6432 if (eap->forceit)
6433 *buf++ = '!';
6434 if (quote)
6435 *buf = '"';
6436 }
6437 break;
6438
6439 case ct_LINE1:
6440 case ct_LINE2:
6441 case ct_COUNT:
6442 {
6443 char num_buf[20];
6444 long num = (type == ct_LINE1) ? eap->line1 :
6445 (type == ct_LINE2) ? eap->line2 :
6446 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6447 size_t num_len;
6448
6449 sprintf(num_buf, "%ld", num);
6450 num_len = STRLEN(num_buf);
6451 result = num_len;
6452
6453 if (quote)
6454 result += 2;
6455
6456 if (buf != NULL)
6457 {
6458 if (quote)
6459 *buf++ = '"';
6460 STRCPY(buf, num_buf);
6461 buf += num_len;
6462 if (quote)
6463 *buf = '"';
6464 }
6465
6466 break;
6467 }
6468
6469 case ct_REGISTER:
6470 result = eap->regname ? 1 : 0;
6471 if (quote)
6472 result += 2;
6473 if (buf != NULL)
6474 {
6475 if (quote)
6476 *buf++ = '\'';
6477 if (eap->regname)
6478 *buf++ = eap->regname;
6479 if (quote)
6480 *buf = '\'';
6481 }
6482 break;
6483
6484 case ct_LT:
6485 result = 1;
6486 if (buf != NULL)
6487 *buf = '<';
6488 break;
6489
6490 default:
6491 /* Not recognized: just copy the '<' and return -1. */
6492 result = (size_t)-1;
6493 if (buf != NULL)
6494 *buf = '<';
6495 break;
6496 }
6497
6498 return result;
6499}
6500
6501 static void
6502do_ucmd(eap)
6503 exarg_T *eap;
6504{
6505 char_u *buf;
6506 char_u *p;
6507 char_u *q;
6508
6509 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006510 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006511 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 size_t len, totlen;
6513
6514 size_t split_len = 0;
6515 char_u *split_buf = NULL;
6516 ucmd_T *cmd;
6517#ifdef FEAT_EVAL
6518 scid_T save_current_SID = current_SID;
6519#endif
6520
6521 if (eap->cmdidx == CMD_USER)
6522 cmd = USER_CMD(eap->useridx);
6523 else
6524 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6525
6526 /*
6527 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006528 * First round: "buf" is NULL, compute length, allocate "buf".
6529 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 */
6531 buf = NULL;
6532 for (;;)
6533 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006534 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006535 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006537
6538 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006540 start = vim_strchr(p, '<');
6541 if (start != NULL)
6542 end = vim_strchr(start + 1, '>');
6543 if (buf != NULL)
6544 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006545 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6546 ;
6547 if (*ksp == K_SPECIAL
6548 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006549 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6550# ifdef FEAT_GUI
6551 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6552# endif
6553 ))
6554 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006555 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006556 * KS_SPECIAL KE_FILLER, like for mappings, but
6557 * do_cmdline() doesn't handle that, so convert it back.
6558 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6559 len = ksp - p;
6560 if (len > 0)
6561 {
6562 mch_memmove(q, p, len);
6563 q += len;
6564 }
6565 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6566 p = ksp + 3;
6567 continue;
6568 }
6569 }
6570
6571 /* break if there no <item> is found */
6572 if (start == NULL || end == NULL)
6573 break;
6574
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 /* Include the '>' */
6576 ++end;
6577
6578 /* Take everything up to the '<' */
6579 len = start - p;
6580 if (buf == NULL)
6581 totlen += len;
6582 else
6583 {
6584 mch_memmove(q, p, len);
6585 q += len;
6586 }
6587
6588 len = uc_check_code(start, end - start, q, cmd, eap,
6589 &split_buf, &split_len);
6590 if (len == (size_t)-1)
6591 {
6592 /* no match, continue after '<' */
6593 p = start + 1;
6594 len = 1;
6595 }
6596 else
6597 p = end;
6598 if (buf == NULL)
6599 totlen += len;
6600 else
6601 q += len;
6602 }
6603 if (buf != NULL) /* second time here, finished */
6604 {
6605 STRCPY(q, p);
6606 break;
6607 }
6608
6609 totlen += STRLEN(p); /* Add on the trailing characters */
6610 buf = alloc((unsigned)(totlen + 1));
6611 if (buf == NULL)
6612 {
6613 vim_free(split_buf);
6614 return;
6615 }
6616 }
6617
6618#ifdef FEAT_EVAL
6619 current_SID = cmd->uc_scriptID;
6620#endif
6621 (void)do_cmdline(buf, eap->getline, eap->cookie,
6622 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6623#ifdef FEAT_EVAL
6624 current_SID = save_current_SID;
6625#endif
6626 vim_free(buf);
6627 vim_free(split_buf);
6628}
6629
6630# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6631 static char_u *
6632get_user_command_name(idx)
6633 int idx;
6634{
6635 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6636}
6637
6638/*
6639 * Function given to ExpandGeneric() to obtain the list of user command names.
6640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 char_u *
6642get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006643 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 int idx;
6645{
6646 if (idx < curbuf->b_ucmds.ga_len)
6647 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6648 idx -= curbuf->b_ucmds.ga_len;
6649 if (idx < ucmds.ga_len)
6650 return USER_CMD(idx)->uc_name;
6651 return NULL;
6652}
6653
6654/*
6655 * Function given to ExpandGeneric() to obtain the list of user command
6656 * attributes.
6657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 char_u *
6659get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006660 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 int idx;
6662{
6663 static char *user_cmd_flags[] =
6664 {"bang", "bar", "buffer", "complete", "count",
6665 "nargs", "range", "register"};
6666
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006667 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 return NULL;
6669 return (char_u *)user_cmd_flags[idx];
6670}
6671
6672/*
6673 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 char_u *
6676get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006677 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 int idx;
6679{
6680 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6681
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006682 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 return NULL;
6684 return (char_u *)user_cmd_nargs[idx];
6685}
6686
6687/*
6688 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6689 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 char_u *
6691get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006692 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 int idx;
6694{
6695 return (char_u *)command_complete[idx].name;
6696}
6697# endif /* FEAT_CMDL_COMPL */
6698
6699#endif /* FEAT_USR_CMDS */
6700
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006701#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6702/*
6703 * Parse a completion argument "value[vallen]".
6704 * The detected completion goes in "*complp", argument type in "*argt".
6705 * When there is an argument, for function and user defined completion, it's
6706 * copied to allocated memory and stored in "*compl_arg".
6707 * Returns FAIL if something is wrong.
6708 */
6709 int
6710parse_compl_arg(value, vallen, complp, argt, compl_arg)
6711 char_u *value;
6712 int vallen;
6713 int *complp;
6714 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006715 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006716{
6717 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006718# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006719 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006720# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006721 int i;
6722 int valend = vallen;
6723
6724 /* Look for any argument part - which is the part after any ',' */
6725 for (i = 0; i < vallen; ++i)
6726 {
6727 if (value[i] == ',')
6728 {
6729 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006730# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006731 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006732# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006733 valend = i;
6734 break;
6735 }
6736 }
6737
6738 for (i = 0; command_complete[i].expand != 0; ++i)
6739 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006740 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006741 && STRNCMP(value, command_complete[i].name, valend) == 0)
6742 {
6743 *complp = command_complete[i].expand;
6744 if (command_complete[i].expand == EXPAND_BUFFERS)
6745 *argt |= BUFNAME;
6746 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6747 || command_complete[i].expand == EXPAND_FILES)
6748 *argt |= XFILE;
6749 break;
6750 }
6751 }
6752
6753 if (command_complete[i].expand == 0)
6754 {
6755 EMSG2(_("E180: Invalid complete value: %s"), value);
6756 return FAIL;
6757 }
6758
6759# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6760 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6761 && arg != NULL)
6762# else
6763 if (arg != NULL)
6764# endif
6765 {
6766 EMSG(_("E468: Completion argument only allowed for custom completion"));
6767 return FAIL;
6768 }
6769
6770# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6771 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6772 && arg == NULL)
6773 {
6774 EMSG(_("E467: Custom completion requires a function argument"));
6775 return FAIL;
6776 }
6777
6778 if (arg != NULL)
6779 *compl_arg = vim_strnsave(arg, (int)arglen);
6780# endif
6781 return OK;
6782}
6783#endif
6784
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 static void
6786ex_colorscheme(eap)
6787 exarg_T *eap;
6788{
Bram Moolenaare6850792010-05-14 15:28:44 +02006789 if (*eap->arg == NUL)
6790 {
6791#ifdef FEAT_EVAL
6792 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6793 char_u *p = NULL;
6794
6795 if (expr != NULL)
6796 {
6797 ++emsg_off;
6798 p = eval_to_string(expr, NULL, FALSE);
6799 --emsg_off;
6800 vim_free(expr);
6801 }
6802 if (p != NULL)
6803 {
6804 MSG(p);
6805 vim_free(p);
6806 }
6807 else
6808 MSG("default");
6809#else
6810 MSG(_("unknown"));
6811#endif
6812 }
6813 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006814 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815}
6816
6817 static void
6818ex_highlight(eap)
6819 exarg_T *eap;
6820{
6821 if (*eap->arg == NUL && eap->cmd[2] == '!')
6822 MSG(_("Greetings, Vim user!"));
6823 do_highlight(eap->arg, eap->forceit, FALSE);
6824}
6825
6826
6827/*
6828 * Call this function if we thought we were going to exit, but we won't
6829 * (because of an error). May need to restore the terminal mode.
6830 */
6831 void
6832not_exiting()
6833{
6834 exiting = FALSE;
6835 settmode(TMODE_RAW);
6836}
6837
6838/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006839 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 */
6841 static void
6842ex_quit(eap)
6843 exarg_T *eap;
6844{
Bram Moolenaarf240e182014-11-27 18:33:02 +01006845#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006846 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006847#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006848
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849#ifdef FEAT_CMDWIN
6850 if (cmdwin_type != 0)
6851 {
6852 cmdwin_result = Ctrl_C;
6853 return;
6854 }
6855#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006856 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006857 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006858 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006859 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006860 return;
6861 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01006862#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006863 if (eap->addr_count > 0)
6864 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01006865 int wnr = eap->line2;
6866
6867 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
6868 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006869 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006870 }
6871 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01006872#endif
6873#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006874 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006875#endif
6876
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006877#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006878 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006879 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006880 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01006881 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
6882 && wp->w_buffer->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006883 return;
6884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885
6886#ifdef FEAT_NETBEANS_INTG
6887 netbeansForcedQuit = eap->forceit;
6888#endif
6889
6890 /*
6891 * If there are more files or windows we won't exit.
6892 */
6893 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6894 exiting = TRUE;
6895 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006896 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6897 | (eap->forceit ? CCGD_FORCEIT : 0)
6898 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 || check_more(TRUE, eap->forceit) == FAIL
6900 || (only_one_window() && check_changed_any(eap->forceit)))
6901 {
6902 not_exiting();
6903 }
6904 else
6905 {
6906#ifdef FEAT_WINDOWS
6907 if (only_one_window()) /* quit last window */
6908#endif
6909 getout(0);
6910#ifdef FEAT_WINDOWS
6911# ifdef FEAT_GUI
6912 need_mouse_correct = TRUE;
6913# endif
6914 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006915 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916#endif
6917 }
6918}
6919
6920/*
6921 * ":cquit".
6922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 static void
6924ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006925 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926{
6927 getout(1); /* this does not always pass on the exit code to the Manx
6928 compiler. why? */
6929}
6930
6931/*
6932 * ":qall": try to quit all windows
6933 */
6934 static void
6935ex_quit_all(eap)
6936 exarg_T *eap;
6937{
6938# ifdef FEAT_CMDWIN
6939 if (cmdwin_type != 0)
6940 {
6941 if (eap->forceit)
6942 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6943 else
6944 cmdwin_result = K_XF2;
6945 return;
6946 }
6947# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006948
6949 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006950 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006951 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006952 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006953 return;
6954 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006955#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006956 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6957 /* Refuse to quit when locked or when the buffer in the last window is
6958 * being closed (can only happen in autocommands). */
6959 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006960 return;
6961#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006962
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 exiting = TRUE;
6964 if (eap->forceit || !check_changed_any(FALSE))
6965 getout(0);
6966 not_exiting();
6967}
6968
Bram Moolenaard9967712006-03-11 21:18:15 +00006969#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970/*
6971 * ":close": close current window, unless it is the last one
6972 */
6973 static void
6974ex_close(eap)
6975 exarg_T *eap;
6976{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006977 win_T *win;
6978 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979# ifdef FEAT_CMDWIN
6980 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006981 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 else
6983# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006984 if (!text_locked()
6985#ifdef FEAT_AUTOCMD
6986 && !curbuf_locked()
6987#endif
6988 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006989 {
6990 if (eap->addr_count == 0)
6991 ex_win_close(eap->forceit, curwin, NULL);
6992 else {
6993 for (win = firstwin; win != NULL; win = win->w_next)
6994 {
6995 winnr++;
6996 if (winnr == eap->line2)
6997 break;
6998 }
6999 if (win == NULL)
7000 win = lastwin;
7001 ex_win_close(eap->forceit, win, NULL);
7002 }
7003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004}
7005
Bram Moolenaard9967712006-03-11 21:18:15 +00007006# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007007/*
7008 * ":pclose": Close any preview window.
7009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007011ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007013{
7014 win_T *win;
7015
7016 for (win = firstwin; win != NULL; win = win->w_next)
7017 if (win->w_p_pvw)
7018 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007019 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007020 break;
7021 }
7022}
Bram Moolenaard9967712006-03-11 21:18:15 +00007023# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007024
Bram Moolenaarf740b292006-02-16 22:11:02 +00007025/*
7026 * Close window "win" and take care of handling closing the last window for a
7027 * modified buffer.
7028 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007029 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00007030ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007031 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007033 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034{
7035 int need_hide;
7036 buf_T *buf = win->w_buffer;
7037
7038 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007039 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007041# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 if ((p_confirm || cmdmod.confirm) && p_write)
7043 {
7044 dialog_changed(buf, FALSE);
7045 if (buf_valid(buf) && bufIsChanged(buf))
7046 return;
7047 need_hide = FALSE;
7048 }
7049 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007050# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 {
7052 EMSG(_(e_nowrtmsg));
7053 return;
7054 }
7055 }
7056
Bram Moolenaard9967712006-03-11 21:18:15 +00007057# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007059# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007060
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007062 if (tp == NULL)
7063 win_close(win, !need_hide && !P_HID(buf));
7064 else
7065 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066}
7067
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007069 * ":tabclose": close current tab page, unless it is the last one.
7070 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 */
7072 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007073ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 exarg_T *eap;
7075{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007076 tabpage_T *tp;
7077
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007078# ifdef FEAT_CMDWIN
7079 if (cmdwin_type != 0)
7080 cmdwin_result = K_IGNORE;
7081 else
7082# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007083 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007084 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007085 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007087 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007088 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007089 tp = find_tabpage((int)eap->line2);
7090 if (tp == NULL)
7091 {
7092 beep_flush();
7093 return;
7094 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007095 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007096 {
7097 tabpage_close_other(tp, eap->forceit);
7098 return;
7099 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007100 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007101 if (!text_locked()
7102#ifdef FEAT_AUTOCMD
7103 && !curbuf_locked()
7104#endif
7105 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007106 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007108}
7109
7110/*
7111 * ":tabonly": close all tab pages except the current one
7112 */
7113 static void
7114ex_tabonly(eap)
7115 exarg_T *eap;
7116{
7117 tabpage_T *tp;
7118 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007119
7120# ifdef FEAT_CMDWIN
7121 if (cmdwin_type != 0)
7122 cmdwin_result = K_IGNORE;
7123 else
7124# endif
7125 if (first_tabpage->tp_next == NULL)
7126 MSG(_("Already only one tab page"));
7127 else
7128 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007129 if (eap->addr_count > 0)
7130 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007131 /* Repeat this up to a 1000 times, because autocommands may mess
7132 * up the lists. */
7133 for (done = 0; done < 1000; ++done)
7134 {
7135 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7136 if (tp->tp_topframe != topframe)
7137 {
7138 tabpage_close_other(tp, eap->forceit);
7139 /* if we failed to close it quit */
7140 if (valid_tabpage(tp))
7141 done = 1000;
7142 /* start over, "tp" is now invalid */
7143 break;
7144 }
7145 if (first_tabpage->tp_next == NULL)
7146 break;
7147 }
7148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150
7151/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007152 * Close the current tab page.
7153 */
7154 void
7155tabpage_close(forceit)
7156 int forceit;
7157{
7158 /* First close all the windows but the current one. If that worked then
7159 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007160 if (lastwin != firstwin)
7161 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007162 if (lastwin == firstwin)
7163 ex_win_close(forceit, curwin, NULL);
7164# ifdef FEAT_GUI
7165 need_mouse_correct = TRUE;
7166# endif
7167}
7168
7169/*
7170 * Close tab page "tp", which is not the current tab page.
7171 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007172 * Also takes care of the tab pages line disappearing when closing the
7173 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007174 */
7175 void
7176tabpage_close_other(tp, forceit)
7177 tabpage_T *tp;
7178 int forceit;
7179{
7180 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007181 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007182 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007183
7184 /* Limit to 1000 windows, autocommands may add a window while we close
7185 * one. OK, so I'm paranoid... */
7186 while (++done < 1000)
7187 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007188 wp = tp->tp_firstwin;
7189 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007190
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007191 /* Autocommands may delete the tab page under our fingers and we may
7192 * fail to close a window with a modified buffer. */
7193 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007194 break;
7195 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007196
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007197 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007198 if (h != tabline_height())
7199 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007200}
7201
7202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 * ":only".
7204 */
7205 static void
7206ex_only(eap)
7207 exarg_T *eap;
7208{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007209 win_T *wp;
7210 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211# ifdef FEAT_GUI
7212 need_mouse_correct = TRUE;
7213# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007214 if (eap->addr_count > 0)
7215 {
7216 wnr = eap->line2;
7217 for (wp = firstwin; --wnr > 0; )
7218 {
7219 if (wp->w_next == NULL)
7220 break;
7221 else
7222 wp = wp->w_next;
7223 }
7224 win_goto(wp);
7225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 close_others(TRUE, eap->forceit);
7227}
7228
7229/*
7230 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007231 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007233 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234ex_all(eap)
7235 exarg_T *eap;
7236{
7237 if (eap->addr_count == 0)
7238 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007239 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240}
7241#endif /* FEAT_WINDOWS */
7242
7243 static void
7244ex_hide(eap)
7245 exarg_T *eap;
7246{
7247 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
7248 eap->errmsg = e_invarg;
7249 else
7250 {
7251 /* ":hide" or ":hide | cmd": hide current window */
7252 eap->nextcmd = check_nextcmd(eap->arg);
7253#ifdef FEAT_WINDOWS
7254 if (!eap->skip)
7255 {
7256# ifdef FEAT_GUI
7257 need_mouse_correct = TRUE;
7258# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007259 if (eap->addr_count == 0)
7260 win_close(curwin, FALSE); /* don't free buffer */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007261 else
7262 {
7263 int winnr = 0;
7264 win_T *win;
7265
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007266 for (win = firstwin; win != NULL; win = win->w_next)
7267 {
7268 winnr++;
7269 if (winnr == eap->line2)
7270 break;
7271 }
7272 if (win == NULL)
7273 win = lastwin;
7274 win_close(win, FALSE);
7275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 }
7277#endif
7278 }
7279}
7280
7281/*
7282 * ":stop" and ":suspend": Suspend Vim.
7283 */
7284 static void
7285ex_stop(eap)
7286 exarg_T *eap;
7287{
7288 /*
7289 * Disallow suspending for "rvim".
7290 */
7291 if (!check_restricted()
7292#ifdef WIN3264
7293 /*
7294 * Check if external commands are allowed now.
7295 */
7296 && can_end_termcap_mode(TRUE)
7297#endif
7298 )
7299 {
7300 if (!eap->forceit)
7301 autowrite_all();
7302 windgoto((int)Rows - 1, 0);
7303 out_char('\n');
7304 out_flush();
7305 stoptermcap();
7306 out_flush(); /* needed for SUN to restore xterm buffer */
7307#ifdef FEAT_TITLE
7308 mch_restore_title(3); /* restore window titles */
7309#endif
7310 ui_suspend(); /* call machine specific function */
7311#ifdef FEAT_TITLE
7312 maketitle();
7313 resettitle(); /* force updating the title */
7314#endif
7315 starttermcap();
7316 scroll_start(); /* scroll screen before redrawing */
7317 redraw_later_clear();
7318 shell_resized(); /* may have resized window */
7319 }
7320}
7321
7322/*
7323 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7324 */
7325 static void
7326ex_exit(eap)
7327 exarg_T *eap;
7328{
7329#ifdef FEAT_CMDWIN
7330 if (cmdwin_type != 0)
7331 {
7332 cmdwin_result = Ctrl_C;
7333 return;
7334 }
7335#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007336 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007337 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007338 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007339 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007340 return;
7341 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007342#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007343 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7344 /* Refuse to quit when locked or when the buffer in the last window is
7345 * being closed (can only happen in autocommands). */
7346 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007347 return;
7348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349
7350 /*
7351 * if more files or windows we won't exit
7352 */
7353 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7354 exiting = TRUE;
7355 if ( ((eap->cmdidx == CMD_wq
7356 || curbufIsChanged())
7357 && do_write(eap) == FAIL)
7358 || check_more(TRUE, eap->forceit) == FAIL
7359 || (only_one_window() && check_changed_any(eap->forceit)))
7360 {
7361 not_exiting();
7362 }
7363 else
7364 {
7365#ifdef FEAT_WINDOWS
7366 if (only_one_window()) /* quit last window, exit Vim */
7367#endif
7368 getout(0);
7369#ifdef FEAT_WINDOWS
7370# ifdef FEAT_GUI
7371 need_mouse_correct = TRUE;
7372# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007373 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 win_close(curwin, !P_HID(curwin->w_buffer));
7375#endif
7376 }
7377}
7378
7379/*
7380 * ":print", ":list", ":number".
7381 */
7382 static void
7383ex_print(eap)
7384 exarg_T *eap;
7385{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007386 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7387 EMSG(_(e_emptybuf));
7388 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007390 for ( ;!got_int; ui_breakcheck())
7391 {
7392 print_line(eap->line1,
7393 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7394 || (eap->flags & EXFLAG_NR)),
7395 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7396 if (++eap->line1 > eap->line2)
7397 break;
7398 out_flush(); /* show one line at a time */
7399 }
7400 setpcmark();
7401 /* put cursor at last line */
7402 curwin->w_cursor.lnum = eap->line2;
7403 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 }
7405
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407}
7408
7409#ifdef FEAT_BYTEOFF
7410 static void
7411ex_goto(eap)
7412 exarg_T *eap;
7413{
7414 goto_byte(eap->line2);
7415}
7416#endif
7417
7418/*
7419 * ":shell".
7420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 static void
7422ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007423 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424{
7425 do_shell(NULL, 0);
7426}
7427
7428#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7429 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007430 || defined(FEAT_GUI_MSWIN) \
7431 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 || defined(PROTO)
7433
7434/*
7435 * Handle a file drop. The code is here because a drop is *nearly* like an
7436 * :args command, but not quite (we have a list of exact filenames, so we
7437 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7438 * code is very similar to :args and hence needs access to a lot of the static
7439 * functions in this file.
7440 *
7441 * The list should be allocated using alloc(), as should each item in the
7442 * list. This function takes over responsibility for freeing the list.
7443 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007444 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7446 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7447 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7448 * file functionality is (currently) not in EMX this is not presently a
7449 * problem.
7450 */
7451 void
7452handle_drop(filec, filev, split)
7453 int filec; /* the number of files dropped */
7454 char_u **filev; /* the list of files dropped */
7455 int split; /* force splitting the window */
7456{
7457 exarg_T ea;
7458 int save_msg_scroll = msg_scroll;
7459
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007460 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007461 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007463#ifdef FEAT_AUTOCMD
7464 if (curbuf_locked())
7465 return;
7466#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007467 /* When the screen is being updated we should not change buffers and
7468 * windows structures, it may cause freed memory to be used. */
7469 if (updating_screen)
7470 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471
7472 /* Check whether the current buffer is changed. If so, we will need
7473 * to split the current window or data could be lost.
7474 * We don't need to check if the 'hidden' option is set, as in this
7475 * case the buffer won't be lost.
7476 */
7477 if (!P_HID(curbuf) && !split)
7478 {
7479 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007480 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 --emsg_off;
7482 }
7483 if (split)
7484 {
7485# ifdef FEAT_WINDOWS
7486 if (win_split(0, 0) == FAIL)
7487 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007488 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489
7490 /* When splitting the window, create a new alist. Otherwise the
7491 * existing one is overwritten. */
7492 alist_unlink(curwin->w_alist);
7493 alist_new();
7494# else
7495 return; /* can't split, always fail */
7496# endif
7497 }
7498
7499 /*
7500 * Set up the new argument list.
7501 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007502 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503
7504 /*
7505 * Move to the first file.
7506 */
7507 /* Fake up a minimal "next" command for do_argfile() */
7508 vim_memset(&ea, 0, sizeof(ea));
7509 ea.cmd = (char_u *)"next";
7510 do_argfile(&ea, 0);
7511
7512 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7513 * mode that is not needed here. */
7514 need_start_insertmode = FALSE;
7515
7516 /* Restore msg_scroll, otherwise a following command may cause scrolling
7517 * unexpectedly. The screen will be redrawn by the caller, thus
7518 * msg_scroll being set by displaying a message is irrelevant. */
7519 msg_scroll = save_msg_scroll;
7520}
7521#endif
7522
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523/*
7524 * Clear an argument list: free all file names and reset it to zero entries.
7525 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007526 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527alist_clear(al)
7528 alist_T *al;
7529{
7530 while (--al->al_ga.ga_len >= 0)
7531 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7532 ga_clear(&al->al_ga);
7533}
7534
7535/*
7536 * Init an argument list.
7537 */
7538 void
7539alist_init(al)
7540 alist_T *al;
7541{
7542 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7543}
7544
7545#if defined(FEAT_WINDOWS) || defined(PROTO)
7546
7547/*
7548 * Remove a reference from an argument list.
7549 * Ignored when the argument list is the global one.
7550 * If the argument list is no longer used by any window, free it.
7551 */
7552 void
7553alist_unlink(al)
7554 alist_T *al;
7555{
7556 if (al != &global_alist && --al->al_refcount <= 0)
7557 {
7558 alist_clear(al);
7559 vim_free(al);
7560 }
7561}
7562
7563# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7564/*
7565 * Create a new argument list and use it for the current window.
7566 */
7567 void
7568alist_new()
7569{
7570 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7571 if (curwin->w_alist == NULL)
7572 {
7573 curwin->w_alist = &global_alist;
7574 ++global_alist.al_refcount;
7575 }
7576 else
7577 {
7578 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007579 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 alist_init(curwin->w_alist);
7581 }
7582}
7583# endif
7584#endif
7585
7586#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7587/*
7588 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007589 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7590 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 */
7592 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007593alist_expand(fnum_list, fnum_len)
7594 int *fnum_list;
7595 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
7597 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007598 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 char_u **new_arg_files;
7600 int new_arg_file_count;
7601 char_u *save_p_su = p_su;
7602 int i;
7603
7604 /* Don't use 'suffixes' here. This should work like the shell did the
7605 * expansion. Also, the vimrc file isn't read yet, thus the user
7606 * can't set the options. */
7607 p_su = empty_option;
7608 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7609 if (old_arg_files != NULL)
7610 {
7611 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007612 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7613 old_arg_count = GARGCOUNT;
7614 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007616 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 && new_arg_file_count > 0)
7618 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007619 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7620 TRUE, fnum_list, fnum_len);
7621 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 }
7624 p_su = save_p_su;
7625}
7626#endif
7627
7628/*
7629 * Set the argument list for the current window.
7630 * Takes over the allocated files[] and the allocated fnames in it.
7631 */
7632 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007633alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 alist_T *al;
7635 int count;
7636 char_u **files;
7637 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007638 int *fnum_list;
7639 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640{
7641 int i;
7642
7643 alist_clear(al);
7644 if (ga_grow(&al->al_ga, count) == OK)
7645 {
7646 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007647 {
7648 if (got_int)
7649 {
7650 /* When adding many buffers this can take a long time. Allow
7651 * interrupting here. */
7652 while (i < count)
7653 vim_free(files[i++]);
7654 break;
7655 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007656
7657 /* May set buffer name of a buffer previously used for the
7658 * argument list, so that it's re-used by alist_add. */
7659 if (fnum_list != NULL && i < fnum_len)
7660 buf_set_name(fnum_list[i], files[i]);
7661
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007663 ui_breakcheck();
7664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 vim_free(files);
7666 }
7667 else
7668 FreeWild(count, files);
7669#ifdef FEAT_WINDOWS
7670 if (al == &global_alist)
7671#endif
7672 arg_had_last = FALSE;
7673}
7674
7675/*
7676 * Add file "fname" to argument list "al".
7677 * "fname" must have been allocated and "al" must have been checked for room.
7678 */
7679 void
7680alist_add(al, fname, set_fnum)
7681 alist_T *al;
7682 char_u *fname;
7683 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7684{
7685 if (fname == NULL) /* don't add NULL file names */
7686 return;
7687#ifdef BACKSLASH_IN_FILENAME
7688 slash_adjust(fname);
7689#endif
7690 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7691 if (set_fnum > 0)
7692 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7693 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7694 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695}
7696
7697#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7698/*
7699 * Adjust slashes in file names. Called after 'shellslash' was set.
7700 */
7701 void
7702alist_slash_adjust()
7703{
7704 int i;
7705# ifdef FEAT_WINDOWS
7706 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007707 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708# endif
7709
7710 for (i = 0; i < GARGCOUNT; ++i)
7711 if (GARGLIST[i].ae_fname != NULL)
7712 slash_adjust(GARGLIST[i].ae_fname);
7713# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007714 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 if (wp->w_alist != &global_alist)
7716 for (i = 0; i < WARGCOUNT(wp); ++i)
7717 if (WARGLIST(wp)[i].ae_fname != NULL)
7718 slash_adjust(WARGLIST(wp)[i].ae_fname);
7719# endif
7720}
7721#endif
7722
7723/*
7724 * ":preserve".
7725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 static void
7727ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007728 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007730 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 ml_preserve(curbuf, TRUE);
7732}
7733
7734/*
7735 * ":recover".
7736 */
7737 static void
7738ex_recover(eap)
7739 exarg_T *eap;
7740{
7741 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7742 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007743 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7744 | CCGD_MULTWIN
7745 | (eap->forceit ? CCGD_FORCEIT : 0)
7746 | CCGD_EXCMD)
7747
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 && (*eap->arg == NUL
7749 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7750 ml_recover();
7751 recoverymode = FALSE;
7752}
7753
7754/*
7755 * Command modifier used in a wrong way.
7756 */
7757 static void
7758ex_wrongmodifier(eap)
7759 exarg_T *eap;
7760{
7761 eap->errmsg = e_invcmd;
7762}
7763
7764#ifdef FEAT_WINDOWS
7765/*
7766 * :sview [+command] file split window with new file, read-only
7767 * :split [[+command] file] split window with current or new file
7768 * :vsplit [[+command] file] split window vertically with current or new file
7769 * :new [[+command] file] split window with no or new file
7770 * :vnew [[+command] file] split vertically window with no or new file
7771 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007772 *
7773 * :tabedit open new Tab page with empty window
7774 * :tabedit [+command] file open new Tab page and edit "file"
7775 * :tabnew [[+command] file] just like :tabedit
7776 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 */
7778 void
7779ex_splitview(eap)
7780 exarg_T *eap;
7781{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007782 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007783# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007785# endif
7786# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007788# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007790# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7792 {
7793 ex_ni(eap);
7794 return;
7795 }
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_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007802# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007804 * quickfix windows. But it's OK when doing ":tab split". */
7805 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 {
7807 if (eap->cmdidx == CMD_split)
7808 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007809# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 if (eap->cmdidx == CMD_vsplit)
7811 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007812# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007814# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007816# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007817 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 {
7819 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7820 FNAME_MESS, TRUE, curbuf->b_ffname);
7821 if (fname == NULL)
7822 goto theend;
7823 eap->arg = fname;
7824 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007825# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007827# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007829# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007831# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007833# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 && eap->cmdidx != CMD_new)
7835 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007836# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007837 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007838# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007839 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007840# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007841 au_has_group((char_u *)"FileExplorer"))
7842 {
7843 /* No browsing supported but we do have the file explorer:
7844 * Edit the directory. */
7845 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7846 eap->arg = (char_u *)".";
7847 }
7848 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007849# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007850 {
7851 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007853 if (fname == NULL)
7854 goto theend;
7855 eap->arg = fname;
7856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 }
7858 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007859# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007861 /*
7862 * Either open new tab page or split the window.
7863 */
7864 if (eap->cmdidx == CMD_tabedit
7865 || eap->cmdidx == CMD_tabfind
7866 || eap->cmdidx == CMD_tabnew)
7867 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007868 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7869 : eap->addr_count == 0 ? 0
7870 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007871 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007872 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007873
7874 /* set the alternate buffer for the window we came from */
7875 if (curwin != old_curwin
7876 && win_valid(old_curwin)
7877 && old_curwin->w_buffer != curbuf
7878 && !cmdmod.keepalt)
7879 old_curwin->w_alt_fnum = curbuf->b_fnum;
7880 }
7881 }
7882 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7884 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007885# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 /* Reset 'scrollbind' when editing another file, but keep it when
7887 * doing ":split" without arguments. */
7888 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007889# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007891# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007893 {
7894 RESET_BINDING(curwin);
7895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 else
7897 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007898# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 do_exedit(eap, old_curwin);
7900 }
7901
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007902# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007904# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007906# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907theend:
7908 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007909# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007911
7912/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007913 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007914 */
7915 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007916tabpage_new()
7917{
7918 exarg_T ea;
7919
7920 vim_memset(&ea, 0, sizeof(ea));
7921 ea.cmdidx = CMD_tabnew;
7922 ea.cmd = (char_u *)"tabn";
7923 ea.arg = (char_u *)"";
7924 ex_splitview(&ea);
7925}
7926
7927/*
7928 * :tabnext command
7929 */
7930 static void
7931ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007932 exarg_T *eap;
7933{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007934 switch (eap->cmdidx)
7935 {
7936 case CMD_tabfirst:
7937 case CMD_tabrewind:
7938 goto_tabpage(1);
7939 break;
7940 case CMD_tablast:
7941 goto_tabpage(9999);
7942 break;
7943 case CMD_tabprevious:
7944 case CMD_tabNext:
7945 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7946 break;
7947 default: /* CMD_tabnext */
7948 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7949 break;
7950 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007951}
7952
7953/*
7954 * :tabmove command
7955 */
7956 static void
7957ex_tabmove(eap)
7958 exarg_T *eap;
7959{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007960 int tab_number = 9999;
7961
7962 if (eap->arg && *eap->arg != NUL)
7963 {
7964 char_u *p = eap->arg;
7965 int relative = 0; /* argument +N/-N means: move N places to the
7966 * right/left relative to the current position. */
7967
7968 if (*eap->arg == '-')
7969 {
7970 relative = -1;
7971 p = eap->arg + 1;
7972 }
7973 else if (*eap->arg == '+')
7974 {
7975 relative = 1;
7976 p = eap->arg + 1;
7977 }
7978 else
7979 p = eap->arg;
7980
7981 if (p == skipdigits(p))
7982 {
7983 /* No numbers as argument. */
7984 eap->errmsg = e_invarg;
7985 return;
7986 }
7987
7988 tab_number = getdigits(&p);
7989 if (relative != 0)
7990 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7991 }
7992 else if (eap->addr_count != 0)
7993 tab_number = eap->line2;
7994
7995 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007996}
7997
7998/*
7999 * :tabs command: List tabs and their contents.
8000 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008001 static void
8002ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008003 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008004{
8005 tabpage_T *tp;
8006 win_T *wp;
8007 int tabcount = 1;
8008
8009 msg_start();
8010 msg_scroll = TRUE;
8011 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8012 {
8013 msg_putchar('\n');
8014 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8015 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8016 out_flush(); /* output one line at a time */
8017 ui_breakcheck();
8018
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008019 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008020 wp = firstwin;
8021 else
8022 wp = tp->tp_firstwin;
8023 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8024 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008025 msg_putchar('\n');
8026 msg_putchar(wp == curwin ? '>' : ' ');
8027 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008028 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8029 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008030 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008031 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008032 else
8033 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8034 IObuff, IOSIZE, TRUE);
8035 msg_outtrans(IObuff);
8036 out_flush(); /* output one line at a time */
8037 ui_breakcheck();
8038 }
8039 }
8040}
8041
8042#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043
8044/*
8045 * ":mode": Set screen mode.
8046 * If no argument given, just get the screen size and redraw.
8047 */
8048 static void
8049ex_mode(eap)
8050 exarg_T *eap;
8051{
8052 if (*eap->arg == NUL)
8053 shell_resized();
8054 else
8055 mch_screenmode(eap->arg);
8056}
8057
8058#ifdef FEAT_WINDOWS
8059/*
8060 * ":resize".
8061 * set, increment or decrement current window height
8062 */
8063 static void
8064ex_resize(eap)
8065 exarg_T *eap;
8066{
8067 int n;
8068 win_T *wp = curwin;
8069
8070 if (eap->addr_count > 0)
8071 {
8072 n = eap->line2;
8073 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8074 ;
8075 }
8076
8077#ifdef FEAT_GUI
8078 need_mouse_correct = TRUE;
8079#endif
8080 n = atol((char *)eap->arg);
8081#ifdef FEAT_VERTSPLIT
8082 if (cmdmod.split & WSP_VERT)
8083 {
8084 if (*eap->arg == '-' || *eap->arg == '+')
8085 n += W_WIDTH(curwin);
8086 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8087 n = 9999;
8088 win_setwidth_win((int)n, wp);
8089 }
8090 else
8091#endif
8092 {
8093 if (*eap->arg == '-' || *eap->arg == '+')
8094 n += curwin->w_height;
8095 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8096 n = 9999;
8097 win_setheight_win((int)n, wp);
8098 }
8099}
8100#endif
8101
8102/*
8103 * ":find [+command] <file>" command.
8104 */
8105 static void
8106ex_find(eap)
8107 exarg_T *eap;
8108{
8109#ifdef FEAT_SEARCHPATH
8110 char_u *fname;
8111 int count;
8112
8113 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8114 TRUE, curbuf->b_ffname);
8115 if (eap->addr_count > 0)
8116 {
8117 /* Repeat finding the file "count" times. This matters when it
8118 * appears several times in the path. */
8119 count = eap->line2;
8120 while (fname != NULL && --count > 0)
8121 {
8122 vim_free(fname);
8123 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8124 FALSE, curbuf->b_ffname);
8125 }
8126 }
8127
8128 if (fname != NULL)
8129 {
8130 eap->arg = fname;
8131#endif
8132 do_exedit(eap, NULL);
8133#ifdef FEAT_SEARCHPATH
8134 vim_free(fname);
8135 }
8136#endif
8137}
8138
8139/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008140 * ":open" simulation: for now just work like ":visual".
8141 */
8142 static void
8143ex_open(eap)
8144 exarg_T *eap;
8145{
8146 regmatch_T regmatch;
8147 char_u *p;
8148
8149 curwin->w_cursor.lnum = eap->line2;
8150 beginline(BL_SOL | BL_FIX);
8151 if (*eap->arg == '/')
8152 {
8153 /* ":open /pattern/": put cursor in column found with pattern */
8154 ++eap->arg;
8155 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8156 *p = NUL;
8157 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8158 if (regmatch.regprog != NULL)
8159 {
8160 regmatch.rm_ic = p_ic;
8161 p = ml_get_curline();
8162 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008163 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008164 else
8165 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008166 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008167 }
8168 /* Move to the NUL, ignore any other arguments. */
8169 eap->arg += STRLEN(eap->arg);
8170 }
8171 check_cursor();
8172
8173 eap->cmdidx = CMD_visual;
8174 do_exedit(eap, NULL);
8175}
8176
8177/*
8178 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 */
8180 static void
8181ex_edit(eap)
8182 exarg_T *eap;
8183{
8184 do_exedit(eap, NULL);
8185}
8186
8187/*
8188 * ":edit <file>" command and alikes.
8189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 void
8191do_exedit(eap, old_curwin)
8192 exarg_T *eap;
8193 win_T *old_curwin; /* curwin before doing a split or NULL */
8194{
8195 int n;
8196#ifdef FEAT_WINDOWS
8197 int need_hide;
8198#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008199 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200
8201 /*
8202 * ":vi" command ends Ex mode.
8203 */
8204 if (exmode_active && (eap->cmdidx == CMD_visual
8205 || eap->cmdidx == CMD_view))
8206 {
8207 exmode_active = FALSE;
8208 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008209 {
8210 /* Special case: ":global/pat/visual\NLvi-commands" */
8211 if (global_busy)
8212 {
8213 int rd = RedrawingDisabled;
8214 int nwr = no_wait_return;
8215 int ms = msg_scroll;
8216#ifdef FEAT_GUI
8217 int he = hold_gui_events;
8218#endif
8219
8220 if (eap->nextcmd != NULL)
8221 {
8222 stuffReadbuff(eap->nextcmd);
8223 eap->nextcmd = NULL;
8224 }
8225
8226 if (exmode_was != EXMODE_VIM)
8227 settmode(TMODE_RAW);
8228 RedrawingDisabled = 0;
8229 no_wait_return = 0;
8230 need_wait_return = FALSE;
8231 msg_scroll = 0;
8232#ifdef FEAT_GUI
8233 hold_gui_events = 0;
8234#endif
8235 must_redraw = CLEAR;
8236
8237 main_loop(FALSE, TRUE);
8238
8239 RedrawingDisabled = rd;
8240 no_wait_return = nwr;
8241 msg_scroll = ms;
8242#ifdef FEAT_GUI
8243 hold_gui_events = he;
8244#endif
8245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 }
8249
8250 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008251 || eap->cmdidx == CMD_tabnew
8252 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253#ifdef FEAT_VERTSPLIT
8254 || eap->cmdidx == CMD_vnew
8255#endif
8256 ) && *eap->arg == NUL)
8257 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008258 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 setpcmark();
8260 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008261 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8262 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 }
8264 else if ((eap->cmdidx != CMD_split
8265#ifdef FEAT_VERTSPLIT
8266 && eap->cmdidx != CMD_vsplit
8267#endif
8268 )
8269 || *eap->arg != NUL
8270#ifdef FEAT_BROWSE
8271 || cmdmod.browse
8272#endif
8273 )
8274 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008275#ifdef FEAT_AUTOCMD
8276 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8277 * can bring us here, others are stopped earlier. */
8278 if (*eap->arg != NUL && curbuf_locked())
8279 return;
8280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 n = readonlymode;
8282 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8283 readonlymode = TRUE;
8284 else if (eap->cmdidx == CMD_enew)
8285 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8286 empty buffer */
8287 setpcmark();
8288 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8289 NULL, eap,
8290 /* ":edit" goes to first line if Vi compatible */
8291 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8292 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8293 ? ECMD_ONE : eap->do_ecmd_lnum,
8294 (P_HID(curbuf) ? ECMD_HIDE : 0)
8295 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008296 /* after a split we can use an existing buffer */
8297 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298#ifdef FEAT_LISTCMDS
8299 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8300#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008301 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {
8303 /* Editing the file failed. If the window was split, close it. */
8304#ifdef FEAT_WINDOWS
8305 if (old_curwin != NULL)
8306 {
8307 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8308 if (!need_hide || P_HID(curbuf))
8309 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008310# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8311 cleanup_T cs;
8312
8313 /* Reset the error/interrupt/exception state here so that
8314 * aborting() returns FALSE when closing a window. */
8315 enter_cleanup(&cs);
8316# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317# ifdef FEAT_GUI
8318 need_mouse_correct = TRUE;
8319# endif
8320 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008321
8322# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8323 /* Restore the error/interrupt/exception state if not
8324 * discarded by a new aborting error, interrupt, or
8325 * uncaught exception. */
8326 leave_cleanup(&cs);
8327# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 }
8329 }
8330#endif
8331 }
8332 else if (readonlymode && curbuf->b_nwindows == 1)
8333 {
8334 /* When editing an already visited buffer, 'readonly' won't be set
8335 * but the previous value is kept. With ":view" and ":sview" we
8336 * want the file to be readonly, except when another window is
8337 * editing the same buffer. */
8338 curbuf->b_p_ro = TRUE;
8339 }
8340 readonlymode = n;
8341 }
8342 else
8343 {
8344 if (eap->do_ecmd_cmd != NULL)
8345 do_cmdline_cmd(eap->do_ecmd_cmd);
8346#ifdef FEAT_TITLE
8347 n = curwin->w_arg_idx_invalid;
8348#endif
8349 check_arg_idx(curwin);
8350#ifdef FEAT_TITLE
8351 if (n != curwin->w_arg_idx_invalid)
8352 maketitle();
8353#endif
8354 }
8355
8356#ifdef FEAT_WINDOWS
8357 /*
8358 * if ":split file" worked, set alternate file name in old window to new
8359 * file
8360 */
8361 if (old_curwin != NULL
8362 && *eap->arg != NUL
8363 && curwin != old_curwin
8364 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008365 && old_curwin->w_buffer != curbuf
8366 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 old_curwin->w_alt_fnum = curbuf->b_fnum;
8368#endif
8369
8370 ex_no_reprint = TRUE;
8371}
8372
8373#ifndef FEAT_GUI
8374/*
8375 * ":gui" and ":gvim" when there is no GUI.
8376 */
8377 static void
8378ex_nogui(eap)
8379 exarg_T *eap;
8380{
8381 eap->errmsg = e_nogvim;
8382}
8383#endif
8384
8385#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8386 static void
8387ex_tearoff(eap)
8388 exarg_T *eap;
8389{
8390 gui_make_tearoff(eap->arg);
8391}
8392#endif
8393
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008394#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 static void
8396ex_popup(eap)
8397 exarg_T *eap;
8398{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008399 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400}
8401#endif
8402
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 static void
8404ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008405 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406{
8407 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8408 MSG(_("No swap file"));
8409 else
8410 msg(curbuf->b_ml.ml_mfp->mf_fname);
8411}
8412
8413/*
8414 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8415 * offset.
8416 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 static void
8419ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008420 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421{
8422#ifdef FEAT_SCROLLBIND
8423 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008424 win_T *save_curwin = curwin;
8425 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 long topline;
8427 long y;
8428 linenr_T old_linenr = curwin->w_cursor.lnum;
8429
8430 setpcmark();
8431
8432 /*
8433 * determine max topline
8434 */
8435 if (curwin->w_p_scb)
8436 {
8437 topline = curwin->w_topline;
8438 for (wp = firstwin; wp; wp = wp->w_next)
8439 {
8440 if (wp->w_p_scb && wp->w_buffer)
8441 {
8442 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8443 if (topline > y)
8444 topline = y;
8445 }
8446 }
8447 if (topline < 1)
8448 topline = 1;
8449 }
8450 else
8451 {
8452 topline = 1;
8453 }
8454
8455
8456 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008457 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8460 {
8461 if (curwin->w_p_scb)
8462 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008463 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 y = topline - curwin->w_topline;
8465 if (y > 0)
8466 scrollup(y, TRUE);
8467 else
8468 scrolldown(-y, TRUE);
8469 curwin->w_scbind_pos = topline;
8470 redraw_later(VALID);
8471 cursor_correct();
8472#ifdef FEAT_WINDOWS
8473 curwin->w_redr_status = TRUE;
8474#endif
8475 }
8476 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008477 curwin = save_curwin;
8478 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 if (curwin->w_p_scb)
8480 {
8481 did_syncbind = TRUE;
8482 checkpcmark();
8483 if (old_linenr != curwin->w_cursor.lnum)
8484 {
8485 char_u ctrl_o[2];
8486
8487 ctrl_o[0] = Ctrl_O;
8488 ctrl_o[1] = 0;
8489 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8490 }
8491 }
8492#endif
8493}
8494
8495
8496 static void
8497ex_read(eap)
8498 exarg_T *eap;
8499{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008500 int i;
8501 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8502 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503
8504 if (eap->usefilter) /* :r!cmd */
8505 do_bang(1, eap, FALSE, FALSE, TRUE);
8506 else
8507 {
8508 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8509 return;
8510
8511#ifdef FEAT_BROWSE
8512 if (cmdmod.browse)
8513 {
8514 char_u *browseFile;
8515
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008516 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 NULL, NULL, NULL, curbuf);
8518 if (browseFile != NULL)
8519 {
8520 i = readfile(browseFile, NULL,
8521 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8522 vim_free(browseFile);
8523 }
8524 else
8525 i = OK;
8526 }
8527 else
8528#endif
8529 if (*eap->arg == NUL)
8530 {
8531 if (check_fname() == FAIL) /* check for no file name */
8532 return;
8533 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8534 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8535 }
8536 else
8537 {
8538 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8539 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8540 i = readfile(eap->arg, NULL,
8541 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8542
8543 }
8544 if (i == FAIL)
8545 {
8546#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8547 if (!aborting())
8548#endif
8549 EMSG2(_(e_notopen), eap->arg);
8550 }
8551 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008552 {
8553 if (empty && exmode_active)
8554 {
8555 /* Delete the empty line that remains. Historically ex does
8556 * this but vi doesn't. */
8557 if (eap->line2 == 0)
8558 lnum = curbuf->b_ml.ml_line_count;
8559 else
8560 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008561 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008562 {
8563 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008564 if (curwin->w_cursor.lnum > 1
8565 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008566 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008567 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008568 }
8569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 }
8573}
8574
Bram Moolenaarea408852005-06-25 22:49:46 +00008575static char_u *prev_dir = NULL;
8576
8577#if defined(EXITFREE) || defined(PROTO)
8578 void
8579free_cd_dir()
8580{
8581 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008582 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008583
8584 vim_free(globaldir);
8585 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008586}
8587#endif
8588
Bram Moolenaarf4258302013-06-02 18:20:17 +02008589/*
8590 * Deal with the side effects of changing the current directory.
8591 * When "local" is TRUE then this was after an ":lcd" command.
8592 */
8593 void
8594post_chdir(local)
8595 int local;
8596{
8597 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008598 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008599 if (local)
8600 {
8601 /* If still in global directory, need to remember current
8602 * directory as global directory. */
8603 if (globaldir == NULL && prev_dir != NULL)
8604 globaldir = vim_strsave(prev_dir);
8605 /* Remember this local directory for the window. */
8606 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8607 curwin->w_localdir = vim_strsave(NameBuff);
8608 }
8609 else
8610 {
8611 /* We are now in the global directory, no need to remember its
8612 * name. */
8613 vim_free(globaldir);
8614 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008615 }
8616
8617 shorten_fnames(TRUE);
8618}
8619
Bram Moolenaarea408852005-06-25 22:49:46 +00008620
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621/*
8622 * ":cd", ":lcd", ":chdir" and ":lchdir".
8623 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008624 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625ex_cd(eap)
8626 exarg_T *eap;
8627{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 char_u *new_dir;
8629 char_u *tofree;
8630
8631 new_dir = eap->arg;
8632#if !defined(UNIX) && !defined(VMS)
8633 /* for non-UNIX ":cd" means: print current directory */
8634 if (*new_dir == NUL)
8635 ex_pwd(NULL);
8636 else
8637#endif
8638 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008639#ifdef FEAT_AUTOCMD
8640 if (allbuf_locked())
8641 return;
8642#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008643 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8644 && !eap->forceit)
8645 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008646 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008647 return;
8648 }
8649
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 /* ":cd -": Change to previous directory */
8651 if (STRCMP(new_dir, "-") == 0)
8652 {
8653 if (prev_dir == NULL)
8654 {
8655 EMSG(_("E186: No previous directory"));
8656 return;
8657 }
8658 new_dir = prev_dir;
8659 }
8660
8661 /* Save current directory for next ":cd -" */
8662 tofree = prev_dir;
8663 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8664 prev_dir = vim_strsave(NameBuff);
8665 else
8666 prev_dir = NULL;
8667
8668#if defined(UNIX) || defined(VMS)
8669 /* for UNIX ":cd" means: go to home directory */
8670 if (*new_dir == NUL)
8671 {
8672 /* use NameBuff for home directory name */
8673# ifdef VMS
8674 char_u *p;
8675
8676 p = mch_getenv((char_u *)"SYS$LOGIN");
8677 if (p == NULL || *p == NUL) /* empty is the same as not set */
8678 NameBuff[0] = NUL;
8679 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008680 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681# else
8682 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8683# endif
8684 new_dir = NameBuff;
8685 }
8686#endif
8687 if (new_dir == NULL || vim_chdir(new_dir))
8688 EMSG(_(e_failed));
8689 else
8690 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008691 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692
8693 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008694 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 ex_pwd(eap);
8696 }
8697 vim_free(tofree);
8698 }
8699}
8700
8701/*
8702 * ":pwd".
8703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 static void
8705ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008706 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707{
8708 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8709 {
8710#ifdef BACKSLASH_IN_FILENAME
8711 slash_adjust(NameBuff);
8712#endif
8713 msg(NameBuff);
8714 }
8715 else
8716 EMSG(_("E187: Unknown"));
8717}
8718
8719/*
8720 * ":=".
8721 */
8722 static void
8723ex_equal(eap)
8724 exarg_T *eap;
8725{
8726 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008727 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728}
8729
8730 static void
8731ex_sleep(eap)
8732 exarg_T *eap;
8733{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008734 int n;
8735 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736
8737 if (cursor_valid())
8738 {
8739 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8740 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008741 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008743
8744 len = eap->line2;
8745 switch (*eap->arg)
8746 {
8747 case 'm': break;
8748 case NUL: len *= 1000L; break;
8749 default: EMSG2(_(e_invarg2), eap->arg); return;
8750 }
8751 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752}
8753
8754/*
8755 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8756 */
8757 void
8758do_sleep(msec)
8759 long msec;
8760{
8761 long done;
8762
8763 cursor_on();
8764 out_flush();
8765 for (done = 0; !got_int && done < msec; done += 1000L)
8766 {
8767 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8768 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008769#ifdef FEAT_NETBEANS_INTG
8770 /* Process the netbeans messages that may have been received in the
8771 * call to ui_breakcheck() when the GUI is in use. This may occur when
8772 * running a test case. */
8773 netbeans_parse_messages();
8774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 }
8776}
8777
8778 static void
8779do_exmap(eap, isabbrev)
8780 exarg_T *eap;
8781 int isabbrev;
8782{
8783 int mode;
8784 char_u *cmdp;
8785
8786 cmdp = eap->cmd;
8787 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8788
8789 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8790 eap->arg, mode, isabbrev))
8791 {
8792 case 1: EMSG(_(e_invarg));
8793 break;
8794 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8795 break;
8796 }
8797}
8798
8799/*
8800 * ":winsize" command (obsolete).
8801 */
8802 static void
8803ex_winsize(eap)
8804 exarg_T *eap;
8805{
8806 int w, h;
8807 char_u *arg = eap->arg;
8808 char_u *p;
8809
8810 w = getdigits(&arg);
8811 arg = skipwhite(arg);
8812 p = arg;
8813 h = getdigits(&arg);
8814 if (*p != NUL && *arg == NUL)
8815 set_shellsize(w, h, TRUE);
8816 else
8817 EMSG(_("E465: :winsize requires two number arguments"));
8818}
8819
8820#ifdef FEAT_WINDOWS
8821 static void
8822ex_wincmd(eap)
8823 exarg_T *eap;
8824{
8825 int xchar = NUL;
8826 char_u *p;
8827
8828 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8829 {
8830 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8831 if (eap->arg[1] == NUL)
8832 {
8833 EMSG(_(e_invarg));
8834 return;
8835 }
8836 xchar = eap->arg[1];
8837 p = eap->arg + 2;
8838 }
8839 else
8840 p = eap->arg + 1;
8841
8842 eap->nextcmd = check_nextcmd(p);
8843 p = skipwhite(p);
8844 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8845 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008846 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 {
8848 /* Pass flags on for ":vertical wincmd ]". */
8849 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008850 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8852 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008853 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 }
8855}
8856#endif
8857
Bram Moolenaar843ee412004-06-30 16:16:41 +00008858#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859/*
8860 * ":winpos".
8861 */
8862 static void
8863ex_winpos(eap)
8864 exarg_T *eap;
8865{
8866 int x, y;
8867 char_u *arg = eap->arg;
8868 char_u *p;
8869
8870 if (*arg == NUL)
8871 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008872# if defined(FEAT_GUI) || defined(MSWIN)
8873# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008875# else
8876 if (mch_get_winpos(&x, &y) != FAIL)
8877# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 {
8879 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8880 msg(IObuff);
8881 }
8882 else
8883# endif
8884 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8885 }
8886 else
8887 {
8888 x = getdigits(&arg);
8889 arg = skipwhite(arg);
8890 p = arg;
8891 y = getdigits(&arg);
8892 if (*p == NUL || *arg != NUL)
8893 {
8894 EMSG(_("E466: :winpos requires two number arguments"));
8895 return;
8896 }
8897# ifdef FEAT_GUI
8898 if (gui.in_use)
8899 gui_mch_set_winpos(x, y);
8900 else if (gui.starting)
8901 {
8902 /* Remember the coordinates for when the window is opened. */
8903 gui_win_x = x;
8904 gui_win_y = y;
8905 }
8906# ifdef HAVE_TGETENT
8907 else
8908# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008909# else
8910# ifdef MSWIN
8911 mch_set_winpos(x, y);
8912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913# endif
8914# ifdef HAVE_TGETENT
8915 if (*T_CWP)
8916 term_set_winpos(x, y);
8917# endif
8918 }
8919}
8920#endif
8921
8922/*
8923 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8924 */
8925 static void
8926ex_operators(eap)
8927 exarg_T *eap;
8928{
8929 oparg_T oa;
8930
8931 clear_oparg(&oa);
8932 oa.regname = eap->regname;
8933 oa.start.lnum = eap->line1;
8934 oa.end.lnum = eap->line2;
8935 oa.line_count = eap->line2 - eap->line1 + 1;
8936 oa.motion_type = MLINE;
8937#ifdef FEAT_VIRTUALEDIT
8938 virtual_op = FALSE;
8939#endif
8940 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8941 {
8942 setpcmark();
8943 curwin->w_cursor.lnum = eap->line1;
8944 beginline(BL_SOL | BL_FIX);
8945 }
8946
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008947 if (VIsual_active)
8948 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008949
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 switch (eap->cmdidx)
8951 {
8952 case CMD_delete:
8953 oa.op_type = OP_DELETE;
8954 op_delete(&oa);
8955 break;
8956
8957 case CMD_yank:
8958 oa.op_type = OP_YANK;
8959 (void)op_yank(&oa, FALSE, TRUE);
8960 break;
8961
8962 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02008963 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008965 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8966#else
8967 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008969 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 oa.op_type = OP_RSHIFT;
8971 else
8972 oa.op_type = OP_LSHIFT;
8973 op_shift(&oa, FALSE, eap->amount);
8974 break;
8975 }
8976#ifdef FEAT_VIRTUALEDIT
8977 virtual_op = MAYBE;
8978#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008979 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980}
8981
8982/*
8983 * ":put".
8984 */
8985 static void
8986ex_put(eap)
8987 exarg_T *eap;
8988{
8989 /* ":0put" works like ":1put!". */
8990 if (eap->line2 == 0)
8991 {
8992 eap->line2 = 1;
8993 eap->forceit = TRUE;
8994 }
8995 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008996 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8997 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998}
8999
9000/*
9001 * Handle ":copy" and ":move".
9002 */
9003 static void
9004ex_copymove(eap)
9005 exarg_T *eap;
9006{
9007 long n;
9008
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01009009 n = get_address(&eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 if (eap->arg == NULL) /* error detected */
9011 {
9012 eap->nextcmd = NULL;
9013 return;
9014 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009015 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016
9017 /*
9018 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9019 */
9020 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9021 {
9022 EMSG(_(e_invaddr));
9023 return;
9024 }
9025
9026 if (eap->cmdidx == CMD_move)
9027 {
9028 if (do_move(eap->line1, eap->line2, n) == FAIL)
9029 return;
9030 }
9031 else
9032 ex_copy(eap->line1, eap->line2, n);
9033 u_clearline();
9034 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009035 ex_may_print(eap);
9036}
9037
9038/*
9039 * Print the current line if flags were given to the Ex command.
9040 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009041 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00009042ex_may_print(eap)
9043 exarg_T *eap;
9044{
9045 if (eap->flags != 0)
9046 {
9047 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9048 (eap->flags & EXFLAG_LIST));
9049 ex_no_reprint = TRUE;
9050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051}
9052
9053/*
9054 * ":smagic" and ":snomagic".
9055 */
9056 static void
9057ex_submagic(eap)
9058 exarg_T *eap;
9059{
9060 int magic_save = p_magic;
9061
9062 p_magic = (eap->cmdidx == CMD_smagic);
9063 do_sub(eap);
9064 p_magic = magic_save;
9065}
9066
9067/*
9068 * ":join".
9069 */
9070 static void
9071ex_join(eap)
9072 exarg_T *eap;
9073{
9074 curwin->w_cursor.lnum = eap->line1;
9075 if (eap->line1 == eap->line2)
9076 {
9077 if (eap->addr_count >= 2) /* :2,2join does nothing */
9078 return;
9079 if (eap->line2 == curbuf->b_ml.ml_line_count)
9080 {
9081 beep_flush();
9082 return;
9083 }
9084 ++eap->line2;
9085 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009086 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009088 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089}
9090
9091/*
9092 * ":[addr]@r" or ":[addr]*r": execute register
9093 */
9094 static void
9095ex_at(eap)
9096 exarg_T *eap;
9097{
9098 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009099 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100
9101 curwin->w_cursor.lnum = eap->line2;
9102
9103#ifdef USE_ON_FLY_SCROLL
9104 dont_scroll = TRUE; /* disallow scrolling here */
9105#endif
9106
9107 /* get the register name. No name means to use the previous one */
9108 c = *eap->arg;
9109 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9110 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009111 /* Put the register in the typeahead buffer with the "silent" flag. */
9112 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9113 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009114 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 else
9118 {
9119 int save_efr = exec_from_reg;
9120
9121 exec_from_reg = TRUE;
9122
9123 /*
9124 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009125 * Continue until the stuff buffer is empty and all added characters
9126 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009128 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9130
9131 exec_from_reg = save_efr;
9132 }
9133}
9134
9135/*
9136 * ":!".
9137 */
9138 static void
9139ex_bang(eap)
9140 exarg_T *eap;
9141{
9142 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9143}
9144
9145/*
9146 * ":undo".
9147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 static void
9149ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009150 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009152 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009153 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009154 else
9155 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156}
9157
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009158#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009159 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009160ex_wundo(eap)
9161 exarg_T *eap;
9162{
9163 char_u hash[UNDO_HASH_SIZE];
9164
9165 u_compute_hash(hash);
9166 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9167}
9168
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009169 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009170ex_rundo(eap)
9171 exarg_T *eap;
9172{
9173 char_u hash[UNDO_HASH_SIZE];
9174
9175 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009176 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009177}
9178#endif
9179
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180/*
9181 * ":redo".
9182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 static void
9184ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009185 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186{
9187 u_redo(1);
9188}
9189
9190/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009191 * ":earlier" and ":later".
9192 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009193 static void
9194ex_later(eap)
9195 exarg_T *eap;
9196{
9197 long count = 0;
9198 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009199 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009200 char_u *p = eap->arg;
9201
9202 if (*p == NUL)
9203 count = 1;
9204 else if (isdigit(*p))
9205 {
9206 count = getdigits(&p);
9207 switch (*p)
9208 {
9209 case 's': ++p; sec = TRUE; break;
9210 case 'm': ++p; sec = TRUE; count *= 60; break;
9211 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009212 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9213 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009214 }
9215 }
9216
9217 if (*p != NUL)
9218 EMSG2(_(e_invarg2), eap->arg);
9219 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009220 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9221 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009222}
9223
9224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 * ":redir": start/stop redirection.
9226 */
9227 static void
9228ex_redir(eap)
9229 exarg_T *eap;
9230{
9231 char *mode;
9232 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009233 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234
9235 if (STRICMP(eap->arg, "END") == 0)
9236 close_redir();
9237 else
9238 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009239 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009241 ++arg;
9242 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009244 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 mode = "a";
9246 }
9247 else
9248 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009249 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250
9251 close_redir();
9252
9253 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009254 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 if (fname == NULL)
9256 return;
9257#ifdef FEAT_BROWSE
9258 if (cmdmod.browse)
9259 {
9260 char_u *browseFile;
9261
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009262 browseFile = do_browse(BROWSE_SAVE,
9263 (char_u *)_("Save Redirection"),
9264 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 if (browseFile == NULL)
9266 return; /* operation cancelled */
9267 vim_free(fname);
9268 fname = browseFile;
9269 eap->forceit = TRUE; /* since dialog already asked */
9270 }
9271#endif
9272
9273 redir_fd = open_exfile(fname, eap->forceit, mode);
9274 vim_free(fname);
9275 }
9276#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009277 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 {
9279 /* redirect to a register a-z (resp. A-Z for appending) */
9280 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009281 ++arg;
9282 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009284 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009285 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009287 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009289 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009290 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009291 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009292 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009294 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009295 if (*arg == '>')
9296 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009297 /* Make register empty when not using @A-@Z and the
9298 * command is valid. */
9299 if (*arg == NUL && !isupper(redir_reg))
9300 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009302 }
9303 if (*arg != NUL)
9304 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009305 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009306 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009307 }
9308 }
9309 else if (*arg == '=' && arg[1] == '>')
9310 {
9311 int append;
9312
9313 /* redirect to a variable */
9314 close_redir();
9315 arg += 2;
9316
9317 if (*arg == '>')
9318 {
9319 ++arg;
9320 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 }
9322 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009323 append = FALSE;
9324
9325 if (var_redir_start(skipwhite(arg), append) == OK)
9326 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 }
9328#endif
9329
9330 /* TODO: redirect to a buffer */
9331
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009333 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009335
9336 /* Make sure redirection is not off. Can happen for cmdline completion
9337 * that indirectly invokes a command to catch its output. */
9338 if (redir_fd != NULL
9339#ifdef FEAT_EVAL
9340 || redir_reg || redir_vname
9341#endif
9342 )
9343 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344}
9345
9346/*
9347 * ":redraw": force redraw
9348 */
9349 static void
9350ex_redraw(eap)
9351 exarg_T *eap;
9352{
9353 int r = RedrawingDisabled;
9354 int p = p_lz;
9355
9356 RedrawingDisabled = 0;
9357 p_lz = FALSE;
9358 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009359 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360#ifdef FEAT_TITLE
9361 if (need_maketitle)
9362 maketitle();
9363#endif
9364 RedrawingDisabled = r;
9365 p_lz = p;
9366
9367 /* Reset msg_didout, so that a message that's there is overwritten. */
9368 msg_didout = FALSE;
9369 msg_col = 0;
9370
Bram Moolenaar56667a52013-07-17 11:54:28 +02009371 /* No need to wait after an intentional redraw. */
9372 need_wait_return = FALSE;
9373
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 out_flush();
9375}
9376
9377/*
9378 * ":redrawstatus": force redraw of status line(s)
9379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 static void
9381ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009382 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383{
9384#if defined(FEAT_WINDOWS)
9385 int r = RedrawingDisabled;
9386 int p = p_lz;
9387
9388 RedrawingDisabled = 0;
9389 p_lz = FALSE;
9390 if (eap->forceit)
9391 status_redraw_all();
9392 else
9393 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009394 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 RedrawingDisabled = r;
9396 p_lz = p;
9397 out_flush();
9398#endif
9399}
9400
9401 static void
9402close_redir()
9403{
9404 if (redir_fd != NULL)
9405 {
9406 fclose(redir_fd);
9407 redir_fd = NULL;
9408 }
9409#ifdef FEAT_EVAL
9410 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009411 if (redir_vname)
9412 {
9413 var_redir_stop();
9414 redir_vname = 0;
9415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416#endif
9417}
9418
9419#if defined(FEAT_SESSION) && defined(USE_CRNL)
9420# define MKSESSION_NL
9421static int mksession_nl = FALSE; /* use NL only in put_eol() */
9422#endif
9423
9424/*
9425 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9426 */
9427 static void
9428ex_mkrc(eap)
9429 exarg_T *eap;
9430{
9431 FILE *fd;
9432 int failed = FALSE;
9433 char_u *fname;
9434#ifdef FEAT_BROWSE
9435 char_u *browseFile = NULL;
9436#endif
9437#ifdef FEAT_SESSION
9438 int view_session = FALSE;
9439 int using_vdir = FALSE; /* using 'viewdir'? */
9440 char_u *viewFile = NULL;
9441 unsigned *flagp;
9442#endif
9443
9444 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9445 {
9446#ifdef FEAT_SESSION
9447 view_session = TRUE;
9448#else
9449 ex_ni(eap);
9450 return;
9451#endif
9452 }
9453
9454#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009455 /* Use the short file name until ":lcd" is used. We also don't use the
9456 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009457 did_lcd = FALSE;
9458
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9460 if (eap->cmdidx == CMD_mkview
9461 && (*eap->arg == NUL
9462 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9463 {
9464 eap->forceit = TRUE;
9465 fname = get_view_file(*eap->arg);
9466 if (fname == NULL)
9467 return;
9468 viewFile = fname;
9469 using_vdir = TRUE;
9470 }
9471 else
9472#endif
9473 if (*eap->arg != NUL)
9474 fname = eap->arg;
9475 else if (eap->cmdidx == CMD_mkvimrc)
9476 fname = (char_u *)VIMRC_FILE;
9477#ifdef FEAT_SESSION
9478 else if (eap->cmdidx == CMD_mksession)
9479 fname = (char_u *)SESSION_FILE;
9480#endif
9481 else
9482 fname = (char_u *)EXRC_FILE;
9483
9484#ifdef FEAT_BROWSE
9485 if (cmdmod.browse)
9486 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009487 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488# ifdef FEAT_SESSION
9489 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9490 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9491# endif
9492 (char_u *)_("Save Setup"),
9493 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9494 if (browseFile == NULL)
9495 goto theend;
9496 fname = browseFile;
9497 eap->forceit = TRUE; /* since dialog already asked */
9498 }
9499#endif
9500
9501#if defined(FEAT_SESSION) && defined(vim_mkdir)
9502 /* When using 'viewdir' may have to create the directory. */
9503 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009504 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505#endif
9506
9507 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9508 if (fd != NULL)
9509 {
9510#ifdef FEAT_SESSION
9511 if (eap->cmdidx == CMD_mkview)
9512 flagp = &vop_flags;
9513 else
9514 flagp = &ssop_flags;
9515#endif
9516
9517#ifdef MKSESSION_NL
9518 /* "unix" in 'sessionoptions': use NL line separator */
9519 if (view_session && (*flagp & SSOP_UNIX))
9520 mksession_nl = TRUE;
9521#endif
9522
9523 /* Write the version command for :mkvimrc */
9524 if (eap->cmdidx == CMD_mkvimrc)
9525 (void)put_line(fd, "version 6.0");
9526
9527#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009528 if (eap->cmdidx == CMD_mksession)
9529 {
9530 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9531 failed = TRUE;
9532 }
9533
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 if (eap->cmdidx != CMD_mkview)
9535#endif
9536 {
9537 /* Write setting 'compatible' first, because it has side effects.
9538 * For that same reason only do it when needed. */
9539 if (p_cp)
9540 (void)put_line(fd, "if !&cp | set cp | endif");
9541 else
9542 (void)put_line(fd, "if &cp | set nocp | endif");
9543 }
9544
9545#ifdef FEAT_SESSION
9546 if (!view_session
9547 || (eap->cmdidx == CMD_mksession
9548 && (*flagp & SSOP_OPTIONS)))
9549#endif
9550 failed |= (makemap(fd, NULL) == FAIL
9551 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9552
9553#ifdef FEAT_SESSION
9554 if (!failed && view_session)
9555 {
9556 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9557 failed = TRUE;
9558 if (eap->cmdidx == CMD_mksession)
9559 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009560 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561
Bram Moolenaard9462e32011-04-11 21:35:11 +02009562 dirnow = alloc(MAXPATHL);
9563 if (dirnow == NULL)
9564 failed = TRUE;
9565 else
9566 {
9567 /*
9568 * Change to session file's dir.
9569 */
9570 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009572 *dirnow = NUL;
9573 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9574 {
9575 if (vim_chdirfile(fname) == OK)
9576 shorten_fnames(TRUE);
9577 }
9578 else if (*dirnow != NUL
9579 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9580 {
9581 if (mch_chdir((char *)globaldir) == 0)
9582 shorten_fnames(TRUE);
9583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584
Bram Moolenaard9462e32011-04-11 21:35:11 +02009585 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586
Bram Moolenaard9462e32011-04-11 21:35:11 +02009587 /* restore original dir */
9588 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009590 {
9591 if (mch_chdir((char *)dirnow) != 0)
9592 EMSG(_(e_prev_dir));
9593 shorten_fnames(TRUE);
9594 }
9595 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 }
9597 }
9598 else
9599 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009600 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9601 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 }
9603 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9604 == FAIL)
9605 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009606 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9607 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009608 if (eap->cmdidx == CMD_mksession)
9609 {
9610 if (put_line(fd, "unlet SessionLoad") == FAIL)
9611 failed = TRUE;
9612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 }
9614#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009615 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9616 failed = TRUE;
9617
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 failed |= fclose(fd);
9619
9620 if (failed)
9621 EMSG(_(e_write));
9622#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9623 else if (eap->cmdidx == CMD_mksession)
9624 {
9625 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009626 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627
Bram Moolenaard9462e32011-04-11 21:35:11 +02009628 tbuf = alloc(MAXPATHL);
9629 if (tbuf != NULL)
9630 {
9631 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9632 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9633 vim_free(tbuf);
9634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 }
9636#endif
9637#ifdef MKSESSION_NL
9638 mksession_nl = FALSE;
9639#endif
9640 }
9641
9642#ifdef FEAT_BROWSE
9643theend:
9644 vim_free(browseFile);
9645#endif
9646#ifdef FEAT_SESSION
9647 vim_free(viewFile);
9648#endif
9649}
9650
Bram Moolenaardf177f62005-02-22 08:39:57 +00009651#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9652 || defined(PROTO)
9653 int
9654vim_mkdir_emsg(name, prot)
9655 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009656 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009657{
9658 if (vim_mkdir(name, prot) != 0)
9659 {
9660 EMSG2(_("E739: Cannot create directory: %s"), name);
9661 return FAIL;
9662 }
9663 return OK;
9664}
9665#endif
9666
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667/*
9668 * Open a file for writing for an Ex command, with some checks.
9669 * Return file descriptor, or NULL on failure.
9670 */
9671 FILE *
9672open_exfile(fname, forceit, mode)
9673 char_u *fname;
9674 int forceit;
9675 char *mode; /* "w" for create new file or "a" for append */
9676{
9677 FILE *fd;
9678
9679#ifdef UNIX
9680 /* with Unix it is possible to open a directory */
9681 if (mch_isdir(fname))
9682 {
9683 EMSG2(_(e_isadir2), fname);
9684 return NULL;
9685 }
9686#endif
9687 if (!forceit && *mode != 'a' && vim_fexists(fname))
9688 {
9689 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9690 return NULL;
9691 }
9692
9693 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9694 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9695
9696 return fd;
9697}
9698
9699/*
9700 * ":mark" and ":k".
9701 */
9702 static void
9703ex_mark(eap)
9704 exarg_T *eap;
9705{
9706 pos_T pos;
9707
9708 if (*eap->arg == NUL) /* No argument? */
9709 EMSG(_(e_argreq));
9710 else if (eap->arg[1] != NUL) /* more than one character? */
9711 EMSG(_(e_trailing));
9712 else
9713 {
9714 pos = curwin->w_cursor; /* save curwin->w_cursor */
9715 curwin->w_cursor.lnum = eap->line2;
9716 beginline(BL_WHITE | BL_FIX);
9717 if (setmark(*eap->arg) == FAIL) /* set mark */
9718 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9719 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9720 }
9721}
9722
9723/*
9724 * Update w_topline, w_leftcol and the cursor position.
9725 */
9726 void
9727update_topline_cursor()
9728{
9729 check_cursor(); /* put cursor on valid line */
9730 update_topline();
9731 if (!curwin->w_p_wrap)
9732 validate_cursor();
9733 update_curswant();
9734}
9735
9736#ifdef FEAT_EX_EXTRA
9737/*
9738 * ":normal[!] {commands}": Execute normal mode commands.
9739 */
9740 static void
9741ex_normal(eap)
9742 exarg_T *eap;
9743{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 int save_msg_scroll = msg_scroll;
9745 int save_restart_edit = restart_edit;
9746 int save_msg_didout = msg_didout;
9747 int save_State = State;
9748 tasave_T tabuf;
9749 int save_insertmode = p_im;
9750 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009751 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752#ifdef FEAT_MBYTE
9753 char_u *arg = NULL;
9754 int l;
9755 char_u *p;
9756#endif
9757
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009758 if (ex_normal_lock > 0)
9759 {
9760 EMSG(_(e_secure));
9761 return;
9762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 if (ex_normal_busy >= p_mmd)
9764 {
9765 EMSG(_("E192: Recursive use of :normal too deep"));
9766 return;
9767 }
9768 ++ex_normal_busy;
9769
9770 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9771 restart_edit = 0; /* don't go to Insert mode */
9772 p_im = FALSE; /* don't use 'insertmode' */
9773
9774#ifdef FEAT_MBYTE
9775 /*
9776 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9777 * this for the K_SPECIAL leading byte, otherwise special keys will not
9778 * work.
9779 */
9780 if (has_mbyte)
9781 {
9782 int len = 0;
9783
9784 /* Count the number of characters to be escaped. */
9785 for (p = eap->arg; *p != NUL; ++p)
9786 {
9787# ifdef FEAT_GUI
9788 if (*p == CSI) /* leadbyte CSI */
9789 len += 2;
9790# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009791 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9793# ifdef FEAT_GUI
9794 || *p == CSI
9795# endif
9796 )
9797 len += 2;
9798 }
9799 if (len > 0)
9800 {
9801 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9802 if (arg != NULL)
9803 {
9804 len = 0;
9805 for (p = eap->arg; *p != NUL; ++p)
9806 {
9807 arg[len++] = *p;
9808# ifdef FEAT_GUI
9809 if (*p == CSI)
9810 {
9811 arg[len++] = KS_EXTRA;
9812 arg[len++] = (int)KE_CSI;
9813 }
9814# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009815 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 {
9817 arg[len++] = *++p;
9818 if (*p == K_SPECIAL)
9819 {
9820 arg[len++] = KS_SPECIAL;
9821 arg[len++] = KE_FILLER;
9822 }
9823# ifdef FEAT_GUI
9824 else if (*p == CSI)
9825 {
9826 arg[len++] = KS_EXTRA;
9827 arg[len++] = (int)KE_CSI;
9828 }
9829# endif
9830 }
9831 arg[len] = NUL;
9832 }
9833 }
9834 }
9835 }
9836#endif
9837
9838 /*
9839 * Save the current typeahead. This is required to allow using ":normal"
9840 * from an event handler and makes sure we don't hang when the argument
9841 * ends with half a command.
9842 */
9843 save_typeahead(&tabuf);
9844 if (tabuf.typebuf_valid)
9845 {
9846 /*
9847 * Repeat the :normal command for each line in the range. When no
9848 * range given, execute it just once, without positioning the cursor
9849 * first.
9850 */
9851 do
9852 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 if (eap->addr_count != 0)
9854 {
9855 curwin->w_cursor.lnum = eap->line1++;
9856 curwin->w_cursor.col = 0;
9857 }
9858
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009859 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860#ifdef FEAT_MBYTE
9861 arg != NULL ? arg :
9862#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009863 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 }
9865 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9866 }
9867
9868 /* Might not return to the main loop when in an event handler. */
9869 update_topline_cursor();
9870
9871 /* Restore the previous typeahead. */
9872 restore_typeahead(&tabuf);
9873
9874 --ex_normal_busy;
9875 msg_scroll = save_msg_scroll;
9876 restart_edit = save_restart_edit;
9877 p_im = save_insertmode;
9878 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009879 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9881
9882 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +01009883 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +01009885#ifdef FEAT_MOUSE
9886 setmouse();
9887#endif
9888#ifdef CURSOR_SHAPE
9889 ui_cursor_shape(); /* may show different cursor shape */
9890#endif
9891
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892#ifdef FEAT_MBYTE
9893 vim_free(arg);
9894#endif
9895}
9896
9897/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009898 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 */
9900 static void
9901ex_startinsert(eap)
9902 exarg_T *eap;
9903{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009904 if (eap->forceit)
9905 {
9906 coladvance((colnr_T)MAXCOL);
9907 curwin->w_curswant = MAXCOL;
9908 curwin->w_set_curswant = FALSE;
9909 }
9910
Bram Moolenaara40c5002005-01-09 21:16:21 +00009911 /* Ignore the command when already in Insert mode. Inserting an
9912 * expression register that invokes a function can do this. */
9913 if (State & INSERT)
9914 return;
9915
Bram Moolenaar64969662005-12-14 21:59:55 +00009916 if (eap->cmdidx == CMD_startinsert)
9917 restart_edit = 'a';
9918 else if (eap->cmdidx == CMD_startreplace)
9919 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009921 restart_edit = 'V';
9922
9923 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009925 if (eap->cmdidx == CMD_startinsert)
9926 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 curwin->w_curswant = 0; /* avoid MAXCOL */
9928 }
9929}
9930
9931/*
9932 * ":stopinsert"
9933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 static void
9935ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009936 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937{
9938 restart_edit = 0;
9939 stop_insert_mode = TRUE;
9940}
9941#endif
9942
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009943#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9944/*
9945 * Execute normal mode command "cmd".
9946 * "remap" can be REMAP_NONE or REMAP_YES.
9947 */
9948 void
9949exec_normal_cmd(cmd, remap, silent)
9950 char_u *cmd;
9951 int remap;
9952 int silent;
9953{
9954 oparg_T oa;
9955
9956 /*
9957 * Stuff the argument into the typeahead buffer.
9958 * Execute normal_cmd() until there is no typeahead left.
9959 */
9960 clear_oparg(&oa);
9961 finish_op = FALSE;
9962 ins_typebuf(cmd, remap, 0, TRUE, silent);
9963 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9964 && !got_int)
9965 {
9966 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009967 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009968 }
9969}
9970#endif
9971
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972#ifdef FEAT_FIND_ID
9973 static void
9974ex_checkpath(eap)
9975 exarg_T *eap;
9976{
9977 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9978 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9979 (linenr_T)1, (linenr_T)MAXLNUM);
9980}
9981
9982#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9983/*
9984 * ":psearch"
9985 */
9986 static void
9987ex_psearch(eap)
9988 exarg_T *eap;
9989{
9990 g_do_tagpreview = p_pvh;
9991 ex_findpat(eap);
9992 g_do_tagpreview = 0;
9993}
9994#endif
9995
9996 static void
9997ex_findpat(eap)
9998 exarg_T *eap;
9999{
10000 int whole = TRUE;
10001 long n;
10002 char_u *p;
10003 int action;
10004
10005 switch (cmdnames[eap->cmdidx].cmd_name[2])
10006 {
10007 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10008 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10009 action = ACTION_GOTO;
10010 else
10011 action = ACTION_SHOW;
10012 break;
10013 case 'i': /* ":ilist" and ":dlist" */
10014 action = ACTION_SHOW_ALL;
10015 break;
10016 case 'u': /* ":ijump" and ":djump" */
10017 action = ACTION_GOTO;
10018 break;
10019 default: /* ":isplit" and ":dsplit" */
10020 action = ACTION_SPLIT;
10021 break;
10022 }
10023
10024 n = 1;
10025 if (vim_isdigit(*eap->arg)) /* get count */
10026 {
10027 n = getdigits(&eap->arg);
10028 eap->arg = skipwhite(eap->arg);
10029 }
10030 if (*eap->arg == '/') /* Match regexp, not just whole words */
10031 {
10032 whole = FALSE;
10033 ++eap->arg;
10034 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10035 if (*p)
10036 {
10037 *p++ = NUL;
10038 p = skipwhite(p);
10039
10040 /* Check for trailing illegal characters */
10041 if (!ends_excmd(*p))
10042 eap->errmsg = e_trailing;
10043 else
10044 eap->nextcmd = check_nextcmd(p);
10045 }
10046 }
10047 if (!eap->skip)
10048 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10049 whole, !eap->forceit,
10050 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10051 n, action, eap->line1, eap->line2);
10052}
10053#endif
10054
10055#ifdef FEAT_WINDOWS
10056
10057# ifdef FEAT_QUICKFIX
10058/*
10059 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10060 */
10061 static void
10062ex_ptag(eap)
10063 exarg_T *eap;
10064{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010065 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10067}
10068
10069/*
10070 * ":pedit"
10071 */
10072 static void
10073ex_pedit(eap)
10074 exarg_T *eap;
10075{
10076 win_T *curwin_save = curwin;
10077
10078 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010079 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 keep_help_flag = curwin_save->w_buffer->b_help;
10081 do_exedit(eap, NULL);
10082 keep_help_flag = FALSE;
10083 if (curwin != curwin_save && win_valid(curwin_save))
10084 {
10085 /* Return cursor to where we were */
10086 validate_cursor();
10087 redraw_later(VALID);
10088 win_enter(curwin_save, TRUE);
10089 }
10090 g_do_tagpreview = 0;
10091}
10092# endif
10093
10094/*
10095 * ":stag", ":stselect" and ":stjump".
10096 */
10097 static void
10098ex_stag(eap)
10099 exarg_T *eap;
10100{
10101 postponed_split = -1;
10102 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010103 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10105 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010106 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107}
10108#endif
10109
10110/*
10111 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10112 */
10113 static void
10114ex_tag(eap)
10115 exarg_T *eap;
10116{
10117 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10118}
10119
10120 static void
10121ex_tag_cmd(eap, name)
10122 exarg_T *eap;
10123 char_u *name;
10124{
10125 int cmd;
10126
10127 switch (name[1])
10128 {
10129 case 'j': cmd = DT_JUMP; /* ":tjump" */
10130 break;
10131 case 's': cmd = DT_SELECT; /* ":tselect" */
10132 break;
10133 case 'p': cmd = DT_PREV; /* ":tprevious" */
10134 break;
10135 case 'N': cmd = DT_PREV; /* ":tNext" */
10136 break;
10137 case 'n': cmd = DT_NEXT; /* ":tnext" */
10138 break;
10139 case 'o': cmd = DT_POP; /* ":pop" */
10140 break;
10141 case 'f': /* ":tfirst" */
10142 case 'r': cmd = DT_FIRST; /* ":trewind" */
10143 break;
10144 case 'l': cmd = DT_LAST; /* ":tlast" */
10145 break;
10146 default: /* ":tag" */
10147#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010148 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 {
10150 do_cstag(eap);
10151 return;
10152 }
10153#endif
10154 cmd = DT_TAG;
10155 break;
10156 }
10157
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010158 if (name[0] == 'l')
10159 {
10160#ifndef FEAT_QUICKFIX
10161 ex_ni(eap);
10162 return;
10163#else
10164 cmd = DT_LTAG;
10165#endif
10166 }
10167
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10169 eap->forceit, TRUE);
10170}
10171
10172/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010173 * Check "str" for starting with a special cmdline variable.
10174 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10175 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10176 */
10177 int
10178find_cmdline_var(src, usedlen)
10179 char_u *src;
10180 int *usedlen;
10181{
10182 int len;
10183 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010184 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010185 "%",
10186#define SPEC_PERC 0
10187 "#",
10188#define SPEC_HASH 1
10189 "<cword>", /* cursor word */
10190#define SPEC_CWORD 2
10191 "<cWORD>", /* cursor WORD */
10192#define SPEC_CCWORD 3
10193 "<cfile>", /* cursor path name */
10194#define SPEC_CFILE 4
10195 "<sfile>", /* ":so" file name */
10196#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010197 "<slnum>", /* ":so" file line number */
10198#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010199#ifdef FEAT_AUTOCMD
10200 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010201# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010202 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010203# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010204 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010205# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010206#endif
10207#ifdef FEAT_CLIENTSERVER
10208 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010209# ifdef FEAT_AUTOCMD
10210# define SPEC_CLIENT 10
10211# else
10212# define SPEC_CLIENT 7
10213# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010214#endif
10215 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010216
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010217 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010218 {
10219 len = (int)STRLEN(spec_str[i]);
10220 if (STRNCMP(src, spec_str[i], len) == 0)
10221 {
10222 *usedlen = len;
10223 return i;
10224 }
10225 }
10226 return -1;
10227}
10228
10229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 * Evaluate cmdline variables.
10231 *
10232 * change '%' to curbuf->b_ffname
10233 * '#' to curwin->w_altfile
10234 * '<cword>' to word under the cursor
10235 * '<cWORD>' to WORD under the cursor
10236 * '<cfile>' to path name under the cursor
10237 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010238 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 * '<afile>' to file name for autocommand
10240 * '<abuf>' to buffer number for autocommand
10241 * '<amatch>' to matching name for autocommand
10242 *
10243 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10244 * "" for error without a message) and NULL is returned.
10245 * Returns an allocated string if a valid match was found.
10246 * Returns NULL if no match was found. "usedlen" then still contains the
10247 * number of characters to skip.
10248 */
10249 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +000010250eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010252 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 int *usedlen; /* characters after src that are used */
10254 linenr_T *lnump; /* line number for :e command, or NULL */
10255 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010256 int *escaped; /* return value has escaped white space (can
10257 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258{
10259 int i;
10260 char_u *s;
10261 char_u *result;
10262 char_u *resultbuf = NULL;
10263 int resultlen;
10264 buf_T *buf;
10265 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10266 int spec_idx;
10267#ifdef FEAT_MODIFY_FNAME
10268 int skip_mod = FALSE;
10269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271
10272 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010273 if (escaped != NULL)
10274 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275
10276 /*
10277 * Check if there is something to do.
10278 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010279 spec_idx = find_cmdline_var(src, usedlen);
10280 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 {
10282 *usedlen = 1;
10283 return NULL;
10284 }
10285
10286 /*
10287 * Skip when preceded with a backslash "\%" and "\#".
10288 * Note: In "\\%" the % is also not recognized!
10289 */
10290 if (src > srcstart && src[-1] == '\\')
10291 {
10292 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010293 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 return NULL;
10295 }
10296
10297 /*
10298 * word or WORD under cursor
10299 */
10300 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10301 {
10302 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10303 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10304 if (resultlen == 0)
10305 {
10306 *errormsg = (char_u *)"";
10307 return NULL;
10308 }
10309 }
10310
10311 /*
10312 * '#': Alternate file name
10313 * '%': Current file name
10314 * File name under the cursor
10315 * File name for autocommand
10316 * and following modifiers
10317 */
10318 else
10319 {
10320 switch (spec_idx)
10321 {
10322 case SPEC_PERC: /* '%': current file */
10323 if (curbuf->b_fname == NULL)
10324 {
10325 result = (char_u *)"";
10326 valid = 0; /* Must have ":p:h" to be valid */
10327 }
10328 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 break;
10331
10332 case SPEC_HASH: /* '#' or "#99": alternate file */
10333 if (src[1] == '#') /* "##": the argument list */
10334 {
10335 result = arg_all();
10336 resultbuf = result;
10337 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010338 if (escaped != NULL)
10339 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340#ifdef FEAT_MODIFY_FNAME
10341 skip_mod = TRUE;
10342#endif
10343 break;
10344 }
10345 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010346 if (*s == '<') /* "#<99" uses v:oldfiles */
10347 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 i = (int)getdigits(&s);
10349 *usedlen = (int)(s - src); /* length of what we expand */
10350
Bram Moolenaard812df62008-11-09 12:46:09 +000010351 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010353 if (*usedlen < 2)
10354 {
10355 /* Should we give an error message for #<text? */
10356 *usedlen = 1;
10357 return NULL;
10358 }
10359#ifdef FEAT_EVAL
10360 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10361 (long)i);
10362 if (result == NULL)
10363 {
10364 *errormsg = (char_u *)"";
10365 return NULL;
10366 }
10367#else
10368 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 }
10372 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010373 {
10374 buf = buflist_findnr(i);
10375 if (buf == NULL)
10376 {
10377 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10378 return NULL;
10379 }
10380 if (lnump != NULL)
10381 *lnump = ECMD_LAST;
10382 if (buf->b_fname == NULL)
10383 {
10384 result = (char_u *)"";
10385 valid = 0; /* Must have ":p:h" to be valid */
10386 }
10387 else
10388 result = buf->b_fname;
10389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 break;
10391
10392#ifdef FEAT_SEARCHPATH
10393 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010394 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 if (result == NULL)
10396 {
10397 *errormsg = (char_u *)"";
10398 return NULL;
10399 }
10400 resultbuf = result; /* remember allocated string */
10401 break;
10402#endif
10403
10404#ifdef FEAT_AUTOCMD
10405 case SPEC_AFILE: /* file name for autocommand */
10406 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010407 if (result != NULL && !autocmd_fname_full)
10408 {
10409 /* Still need to turn the fname into a full path. It is
10410 * postponed to avoid a delay when <afile> is not used. */
10411 autocmd_fname_full = TRUE;
10412 result = FullName_save(autocmd_fname, FALSE);
10413 vim_free(autocmd_fname);
10414 autocmd_fname = result;
10415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 if (result == NULL)
10417 {
10418 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10419 return NULL;
10420 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010421 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 break;
10423
10424 case SPEC_ABUF: /* buffer number for autocommand */
10425 if (autocmd_bufnr <= 0)
10426 {
10427 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10428 return NULL;
10429 }
10430 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10431 result = strbuf;
10432 break;
10433
10434 case SPEC_AMATCH: /* match name for autocommand */
10435 result = autocmd_match;
10436 if (result == NULL)
10437 {
10438 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10439 return NULL;
10440 }
10441 break;
10442
10443#endif
10444 case SPEC_SFILE: /* file name for ":so" command */
10445 result = sourcing_name;
10446 if (result == NULL)
10447 {
10448 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10449 return NULL;
10450 }
10451 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010452 case SPEC_SLNUM: /* line in file for ":so" command */
10453 if (sourcing_name == NULL || sourcing_lnum == 0)
10454 {
10455 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10456 return NULL;
10457 }
10458 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10459 result = strbuf;
10460 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461#if defined(FEAT_CLIENTSERVER)
10462 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010463 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10464 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465 result = strbuf;
10466 break;
10467#endif
10468 }
10469
10470 resultlen = (int)STRLEN(result); /* length of new string */
10471 if (src[*usedlen] == '<') /* remove the file name extension */
10472 {
10473 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 resultlen = (int)(s - result);
10476 }
10477#ifdef FEAT_MODIFY_FNAME
10478 else if (!skip_mod)
10479 {
10480 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10481 &resultlen);
10482 if (result == NULL)
10483 {
10484 *errormsg = (char_u *)"";
10485 return NULL;
10486 }
10487 }
10488#endif
10489 }
10490
10491 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10492 {
10493 if (valid != VALID_HEAD + VALID_PATH)
10494 /* xgettext:no-c-format */
10495 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10496 else
10497 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10498 result = NULL;
10499 }
10500 else
10501 result = vim_strnsave(result, resultlen);
10502 vim_free(resultbuf);
10503 return result;
10504}
10505
10506/*
10507 * Concatenate all files in the argument list, separated by spaces, and return
10508 * it in one allocated string.
10509 * Spaces and backslashes in the file names are escaped with a backslash.
10510 * Returns NULL when out of memory.
10511 */
10512 static char_u *
10513arg_all()
10514{
10515 int len;
10516 int idx;
10517 char_u *retval = NULL;
10518 char_u *p;
10519
10520 /*
10521 * Do this loop two times:
10522 * first time: compute the total length
10523 * second time: concatenate the names
10524 */
10525 for (;;)
10526 {
10527 len = 0;
10528 for (idx = 0; idx < ARGCOUNT; ++idx)
10529 {
10530 p = alist_name(&ARGLIST[idx]);
10531 if (p != NULL)
10532 {
10533 if (len > 0)
10534 {
10535 /* insert a space in between names */
10536 if (retval != NULL)
10537 retval[len] = ' ';
10538 ++len;
10539 }
10540 for ( ; *p != NUL; ++p)
10541 {
10542 if (*p == ' ' || *p == '\\')
10543 {
10544 /* insert a backslash */
10545 if (retval != NULL)
10546 retval[len] = '\\';
10547 ++len;
10548 }
10549 if (retval != NULL)
10550 retval[len] = *p;
10551 ++len;
10552 }
10553 }
10554 }
10555
10556 /* second time: break here */
10557 if (retval != NULL)
10558 {
10559 retval[len] = NUL;
10560 break;
10561 }
10562
10563 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010564 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 if (retval == NULL)
10566 break;
10567 }
10568
10569 return retval;
10570}
10571
10572#if defined(FEAT_AUTOCMD) || defined(PROTO)
10573/*
10574 * Expand the <sfile> string in "arg".
10575 *
10576 * Returns an allocated string, or NULL for any error.
10577 */
10578 char_u *
10579expand_sfile(arg)
10580 char_u *arg;
10581{
10582 char_u *errormsg;
10583 int len;
10584 char_u *result;
10585 char_u *newres;
10586 char_u *repl;
10587 int srclen;
10588 char_u *p;
10589
10590 result = vim_strsave(arg);
10591 if (result == NULL)
10592 return NULL;
10593
10594 for (p = result; *p; )
10595 {
10596 if (STRNCMP(p, "<sfile>", 7) != 0)
10597 ++p;
10598 else
10599 {
10600 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010601 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 if (errormsg != NULL)
10603 {
10604 if (*errormsg)
10605 emsg(errormsg);
10606 vim_free(result);
10607 return NULL;
10608 }
10609 if (repl == NULL) /* no match (cannot happen) */
10610 {
10611 p += srclen;
10612 continue;
10613 }
10614 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10615 newres = alloc(len);
10616 if (newres == NULL)
10617 {
10618 vim_free(repl);
10619 vim_free(result);
10620 return NULL;
10621 }
10622 mch_memmove(newres, result, (size_t)(p - result));
10623 STRCPY(newres + (p - result), repl);
10624 len = (int)STRLEN(newres);
10625 STRCAT(newres, p + srclen);
10626 vim_free(repl);
10627 vim_free(result);
10628 result = newres;
10629 p = newres + len; /* continue after the match */
10630 }
10631 }
10632
10633 return result;
10634}
10635#endif
10636
10637#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010638static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10639 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10641static frame_T *ses_skipframe __ARGS((frame_T *fr));
10642static int ses_do_frame __ARGS((frame_T *fr));
10643static int ses_do_win __ARGS((win_T *wp));
10644static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10645static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10646static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10647
10648/*
10649 * Write openfile commands for the current buffers to an .exrc file.
10650 * Return FAIL on error, OK otherwise.
10651 */
10652 static int
10653makeopens(fd, dirnow)
10654 FILE *fd;
10655 char_u *dirnow; /* Current directory name */
10656{
10657 buf_T *buf;
10658 int only_save_windows = TRUE;
10659 int nr;
10660 int cnr = 1;
10661 int restore_size = TRUE;
10662 win_T *wp;
10663 char_u *sname;
10664 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010665 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010666 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010667 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010668 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010669 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010670 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671
10672 if (ssop_flags & SSOP_BUFFERS)
10673 only_save_windows = FALSE; /* Save ALL buffers */
10674
10675 /*
10676 * Begin by setting the this_session variable, and then other
10677 * sessionable variables.
10678 */
10679#ifdef FEAT_EVAL
10680 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10681 return FAIL;
10682 if (ssop_flags & SSOP_GLOBALS)
10683 if (store_session_globals(fd) == FAIL)
10684 return FAIL;
10685#endif
10686
10687 /*
10688 * Close all windows but one.
10689 */
10690 if (put_line(fd, "silent only") == FAIL)
10691 return FAIL;
10692
10693 /*
10694 * Now a :cd command to the session directory or the current directory
10695 */
10696 if (ssop_flags & SSOP_SESDIR)
10697 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010698 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10699 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 return FAIL;
10701 }
10702 else if (ssop_flags & SSOP_CURDIR)
10703 {
10704 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10705 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010706 || fputs("cd ", fd) < 0
10707 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10708 || put_eol(fd) == FAIL)
10709 {
10710 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713 vim_free(sname);
10714 }
10715
10716 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010717 * If there is an empty, unnamed buffer we will wipe it out later.
10718 * Remember the buffer number.
10719 */
10720 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10721 return FAIL;
10722 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10723 return FAIL;
10724 if (put_line(fd, "endif") == FAIL)
10725 return FAIL;
10726
10727 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 * Now save the current files, current buffer first.
10729 */
10730 if (put_line(fd, "set shortmess=aoO") == FAIL)
10731 return FAIL;
10732
10733 /* Now put the other buffers into the buffer list */
10734 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10735 {
10736 if (!(only_save_windows && buf->b_nwindows == 0)
10737 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10738 && buf->b_fname != NULL
10739 && buf->b_p_bl)
10740 {
10741 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10742 : buf->b_wininfo->wi_fpos.lnum) < 0
10743 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10744 return FAIL;
10745 }
10746 }
10747
10748 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010749 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10751 return FAIL;
10752
10753 if (ssop_flags & SSOP_RESIZE)
10754 {
10755 /* Note: after the restore we still check it worked!*/
10756 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10757 || put_eol(fd) == FAIL)
10758 return FAIL;
10759 }
10760
10761#ifdef FEAT_GUI
10762 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10763 {
10764 int x, y;
10765
10766 if (gui_mch_get_winpos(&x, &y) == OK)
10767 {
10768 /* Note: after the restore we still check it worked!*/
10769 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10770 return FAIL;
10771 }
10772 }
10773#endif
10774
10775 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010776 * When there are two or more tabpages and 'showtabline' is 1 the tabline
10777 * will be displayed when creating the next tab. That resizes the windows
10778 * in the first tab, which may cause problems. Set 'showtabline' to 2
10779 * temporarily to avoid that.
10780 */
10781 if (p_stal == 1 && first_tabpage->tp_next != NULL)
10782 {
10783 if (put_line(fd, "set stal=2") == FAIL)
10784 return FAIL;
10785 restore_stal = TRUE;
10786 }
10787
10788 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010789 * May repeat putting Windows for each tab, when "tabpages" is in
10790 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010791 * Don't use goto_tabpage(), it may change directory and trigger
10792 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010794 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010795 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010796 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010798 int need_tabnew = FALSE;
10799
Bram Moolenaar18144c82006-04-12 21:52:12 +000010800 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010802 tabpage_T *tp = find_tabpage(tabnr);
10803
10804 if (tp == NULL)
10805 break; /* done all tab pages */
10806 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010807 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010808 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010809 tab_topframe = topframe;
10810 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010811 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010812 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010813 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010814 tab_topframe = tp->tp_topframe;
10815 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010816 if (tabnr > 1)
10817 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819
Bram Moolenaar18144c82006-04-12 21:52:12 +000010820 /*
10821 * Before creating the window layout, try loading one file. If this
10822 * is aborted we don't end up with a number of useless windows.
10823 * This may have side effects! (e.g., compressed or network file).
10824 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010825 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010826 {
10827 if (ses_do_win(wp)
10828 && wp->w_buffer->b_ffname != NULL
10829 && !wp->w_buffer->b_help
10830#ifdef FEAT_QUICKFIX
10831 && !bt_nofile(wp->w_buffer)
10832#endif
10833 )
10834 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010835 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010836 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10837 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010838 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010839 if (!wp->w_arg_idx_invalid)
10840 edited_win = wp;
10841 break;
10842 }
10843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010845 /* If no file got edited create an empty tab page. */
10846 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10847 return FAIL;
10848
Bram Moolenaar18144c82006-04-12 21:52:12 +000010849 /*
10850 * Save current window layout.
10851 */
10852 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010854 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010856 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10857 return FAIL;
10858 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10859 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860
Bram Moolenaar18144c82006-04-12 21:52:12 +000010861 /*
10862 * Check if window sizes can be restored (no windows omitted).
10863 * Remember the window number of the current window after restoring.
10864 */
10865 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010866 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010867 {
10868 if (ses_do_win(wp))
10869 ++nr;
10870 else
10871 restore_size = FALSE;
10872 if (curwin == wp)
10873 cnr = nr;
10874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875
Bram Moolenaar18144c82006-04-12 21:52:12 +000010876 /* Go to the first window. */
10877 if (put_line(fd, "wincmd t") == FAIL)
10878 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010879
Bram Moolenaar18144c82006-04-12 21:52:12 +000010880 /*
10881 * If more than one window, see if sizes can be restored.
10882 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10883 * resized when moving between windows.
10884 * Do this before restoring the view, so that the topline and the
10885 * cursor can be set. This is done again below.
10886 */
10887 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10888 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010889 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010890 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891
Bram Moolenaar18144c82006-04-12 21:52:12 +000010892 /*
10893 * Restore the view of the window (options, file, cursor, etc.).
10894 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010895 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010896 {
10897 if (!ses_do_win(wp))
10898 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010899 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10900 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010901 return FAIL;
10902 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10903 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010904 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010905 }
10906
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010907 /* The argument index in the first tab page is zero, need to set it in
10908 * each window. For further tab pages it's the window where we do
10909 * "tabedit". */
10910 cur_arg_idx = next_arg_idx;
10911
Bram Moolenaar18144c82006-04-12 21:52:12 +000010912 /*
10913 * Restore cursor to the current window if it's not the first one.
10914 */
10915 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10916 || put_eol(fd) == FAIL))
10917 return FAIL;
10918
10919 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010920 * Restore window sizes again after jumping around in windows, because
10921 * the current window has a minimum size while others may not.
10922 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010923 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010924 return FAIL;
10925
Bram Moolenaar18144c82006-04-12 21:52:12 +000010926 /* Don't continue in another tab page when doing only the current one
10927 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010928 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010929 break;
10930 }
10931
10932 if (ssop_flags & SSOP_TABPAGES)
10933 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010934 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10935 || put_eol(fd) == FAIL)
10936 return FAIL;
10937 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010938 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
10939 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010940
Bram Moolenaar9c102382006-05-03 21:26:49 +000010941 /*
10942 * Wipe out an empty unnamed buffer we started in.
10943 */
10944 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10945 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010946 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010947 return FAIL;
10948 if (put_line(fd, "endif") == FAIL)
10949 return FAIL;
10950 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10951 return FAIL;
10952
10953 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10954 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10955 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10956 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957
10958 /*
10959 * Lastly, execute the x.vim file if it exists.
10960 */
10961 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10962 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010963 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 || put_line(fd, "endif") == FAIL)
10965 return FAIL;
10966
10967 return OK;
10968}
10969
10970 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010971ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 FILE *fd;
10973 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010974 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975{
10976 int n = 0;
10977 win_T *wp;
10978
10979 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10980 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010981 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 {
10983 if (!ses_do_win(wp))
10984 continue;
10985 ++n;
10986
10987 /* restore height when not full height */
10988 if (wp->w_height + wp->w_status_height < topframe->fr_height
10989 && (fprintf(fd,
10990 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10991 n, (long)wp->w_height, Rows / 2, Rows) < 0
10992 || put_eol(fd) == FAIL))
10993 return FAIL;
10994
10995 /* restore width when not full width */
10996 if (wp->w_width < Columns && (fprintf(fd,
10997 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10998 n, (long)wp->w_width, Columns / 2, Columns) < 0
10999 || put_eol(fd) == FAIL))
11000 return FAIL;
11001 }
11002 }
11003 else
11004 {
11005 /* Just equalise window sizes */
11006 if (put_line(fd, "wincmd =") == FAIL)
11007 return FAIL;
11008 }
11009 return OK;
11010}
11011
11012/*
11013 * Write commands to "fd" to recursively create windows for frame "fr",
11014 * horizontally and vertically split.
11015 * After the commands the last window in the frame is the current window.
11016 * Returns FAIL when writing the commands to "fd" fails.
11017 */
11018 static int
11019ses_win_rec(fd, fr)
11020 FILE *fd;
11021 frame_T *fr;
11022{
11023 frame_T *frc;
11024 int count = 0;
11025
11026 if (fr->fr_layout != FR_LEAF)
11027 {
11028 /* Find first frame that's not skipped and then create a window for
11029 * each following one (first frame is already there). */
11030 frc = ses_skipframe(fr->fr_child);
11031 if (frc != NULL)
11032 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11033 {
11034 /* Make window as big as possible so that we have lots of room
11035 * to split. */
11036 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11037 || put_line(fd, fr->fr_layout == FR_COL
11038 ? "split" : "vsplit") == FAIL)
11039 return FAIL;
11040 ++count;
11041 }
11042
11043 /* Go back to the first window. */
11044 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11045 ? "%dwincmd k" : "%dwincmd h", count) < 0
11046 || put_eol(fd) == FAIL))
11047 return FAIL;
11048
11049 /* Recursively create frames/windows in each window of this column or
11050 * row. */
11051 frc = ses_skipframe(fr->fr_child);
11052 while (frc != NULL)
11053 {
11054 ses_win_rec(fd, frc);
11055 frc = ses_skipframe(frc->fr_next);
11056 /* Go to next window. */
11057 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11058 return FAIL;
11059 }
11060 }
11061 return OK;
11062}
11063
11064/*
11065 * Skip frames that don't contain windows we want to save in the Session.
11066 * Returns NULL when there none.
11067 */
11068 static frame_T *
11069ses_skipframe(fr)
11070 frame_T *fr;
11071{
11072 frame_T *frc;
11073
11074 for (frc = fr; frc != NULL; frc = frc->fr_next)
11075 if (ses_do_frame(frc))
11076 break;
11077 return frc;
11078}
11079
11080/*
11081 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11082 * the Session.
11083 */
11084 static int
11085ses_do_frame(fr)
11086 frame_T *fr;
11087{
11088 frame_T *frc;
11089
11090 if (fr->fr_layout == FR_LEAF)
11091 return ses_do_win(fr->fr_win);
11092 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11093 if (ses_do_frame(frc))
11094 return TRUE;
11095 return FALSE;
11096}
11097
11098/*
11099 * Return non-zero if window "wp" is to be stored in the Session.
11100 */
11101 static int
11102ses_do_win(wp)
11103 win_T *wp;
11104{
11105 if (wp->w_buffer->b_fname == NULL
11106#ifdef FEAT_QUICKFIX
11107 /* When 'buftype' is "nofile" can't restore the window contents. */
11108 || bt_nofile(wp->w_buffer)
11109#endif
11110 )
11111 return (ssop_flags & SSOP_BLANK);
11112 if (wp->w_buffer->b_help)
11113 return (ssop_flags & SSOP_HELP);
11114 return TRUE;
11115}
11116
11117/*
11118 * Write commands to "fd" to restore the view of a window.
11119 * Caller must make sure 'scrolloff' is zero.
11120 */
11121 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011122put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 FILE *fd;
11124 win_T *wp;
11125 int add_edit; /* add ":edit" command to view */
11126 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011127 int current_arg_idx; /* current argument index of the window, use
11128 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129{
11130 win_T *save_curwin;
11131 int f;
11132 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011133 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134
11135 /* Always restore cursor position for ":mksession". For ":mkview" only
11136 * when 'viewoptions' contains "cursor". */
11137 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11138
11139 /*
11140 * Local argument list.
11141 */
11142 if (wp->w_alist == &global_alist)
11143 {
11144 if (put_line(fd, "argglobal") == FAIL)
11145 return FAIL;
11146 }
11147 else
11148 {
11149 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11150 flagp == &vop_flags
11151 || !(*flagp & SSOP_CURDIR)
11152 || wp->w_localdir != NULL, flagp) == FAIL)
11153 return FAIL;
11154 }
11155
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011156 /* Only when part of a session: restore the argument index. Some
11157 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011158 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011159 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011161 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162 || put_eol(fd) == FAIL)
11163 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011164 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 }
11166
11167 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011168 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 {
11170 /*
11171 * Load the file.
11172 */
11173 if (wp->w_buffer->b_ffname != NULL
11174#ifdef FEAT_QUICKFIX
11175 && !bt_nofile(wp->w_buffer)
11176#endif
11177 )
11178 {
11179 /*
11180 * Editing a file in this buffer: use ":edit file".
11181 * This may have side effects! (e.g., compressed or network file).
11182 */
11183 if (fputs("edit ", fd) < 0
11184 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11185 return FAIL;
11186 }
11187 else
11188 {
11189 /* No file in this buffer, just make it empty. */
11190 if (put_line(fd, "enew") == FAIL)
11191 return FAIL;
11192#ifdef FEAT_QUICKFIX
11193 if (wp->w_buffer->b_ffname != NULL)
11194 {
11195 /* The buffer does have a name, but it's not a file name. */
11196 if (fputs("file ", fd) < 0
11197 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11198 return FAIL;
11199 }
11200#endif
11201 do_cursor = FALSE;
11202 }
11203 }
11204
11205 /*
11206 * Local mappings and abbreviations.
11207 */
11208 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11209 && makemap(fd, wp->w_buffer) == FAIL)
11210 return FAIL;
11211
11212 /*
11213 * Local options. Need to go to the window temporarily.
11214 * Store only local values when using ":mkview" and when ":mksession" is
11215 * used and 'sessionoptions' doesn't include "options".
11216 * Some folding options are always stored when "folds" is included,
11217 * otherwise the folds would not be restored correctly.
11218 */
11219 save_curwin = curwin;
11220 curwin = wp;
11221 curbuf = curwin->w_buffer;
11222 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11223 f = makeset(fd, OPT_LOCAL,
11224 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11225#ifdef FEAT_FOLDING
11226 else if (*flagp & SSOP_FOLDS)
11227 f = makefoldset(fd);
11228#endif
11229 else
11230 f = OK;
11231 curwin = save_curwin;
11232 curbuf = curwin->w_buffer;
11233 if (f == FAIL)
11234 return FAIL;
11235
11236#ifdef FEAT_FOLDING
11237 /*
11238 * Save Folds when 'buftype' is empty and for help files.
11239 */
11240 if ((*flagp & SSOP_FOLDS)
11241 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011242# ifdef FEAT_QUICKFIX
11243 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11244# endif
11245 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246 {
11247 if (put_folds(fd, wp) == FAIL)
11248 return FAIL;
11249 }
11250#endif
11251
11252 /*
11253 * Set the cursor after creating folds, since that moves the cursor.
11254 */
11255 if (do_cursor)
11256 {
11257
11258 /* Restore the cursor line in the file and relatively in the
11259 * window. Don't use "G", it changes the jumplist. */
11260 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11261 (long)wp->w_cursor.lnum,
11262 (long)(wp->w_cursor.lnum - wp->w_topline),
11263 (long)wp->w_height / 2, (long)wp->w_height) < 0
11264 || put_eol(fd) == FAIL
11265 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11266 || put_line(fd, "exe s:l") == FAIL
11267 || put_line(fd, "normal! zt") == FAIL
11268 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11269 || put_eol(fd) == FAIL)
11270 return FAIL;
11271 /* Restore the cursor column and left offset when not wrapping. */
11272 if (wp->w_cursor.col == 0)
11273 {
11274 if (put_line(fd, "normal! 0") == FAIL)
11275 return FAIL;
11276 }
11277 else
11278 {
11279 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11280 {
11281 if (fprintf(fd,
11282 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011283 (long)wp->w_virtcol + 1,
11284 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 (long)wp->w_width / 2, (long)wp->w_width) < 0
11286 || put_eol(fd) == FAIL
11287 || put_line(fd, "if s:c > 0") == FAIL
11288 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011289 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11290 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291 || put_eol(fd) == FAIL
11292 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011293 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 || put_eol(fd) == FAIL
11295 || put_line(fd, "endif") == FAIL)
11296 return FAIL;
11297 }
11298 else
11299 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011300 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301 || put_eol(fd) == FAIL)
11302 return FAIL;
11303 }
11304 }
11305 }
11306
11307 /*
11308 * Local directory.
11309 */
11310 if (wp->w_localdir != NULL)
11311 {
11312 if (fputs("lcd ", fd) < 0
11313 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11314 || put_eol(fd) == FAIL)
11315 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011316 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317 }
11318
11319 return OK;
11320}
11321
11322/*
11323 * Write an argument list to the session file.
11324 * Returns FAIL if writing fails.
11325 */
11326 static int
11327ses_arglist(fd, cmd, gap, fullname, flagp)
11328 FILE *fd;
11329 char *cmd;
11330 garray_T *gap;
11331 int fullname; /* TRUE: use full path name */
11332 unsigned *flagp;
11333{
11334 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011335 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 char_u *s;
11337
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011338 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11339 return FAIL;
11340 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 return FAIL;
11342 for (i = 0; i < gap->ga_len; ++i)
11343 {
11344 /* NULL file names are skipped (only happens when out of memory). */
11345 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11346 if (s != NULL)
11347 {
11348 if (fullname)
11349 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011350 buf = alloc(MAXPATHL);
11351 if (buf != NULL)
11352 {
11353 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11354 s = buf;
11355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011357 if (fputs("argadd ", fd) < 0
11358 || ses_put_fname(fd, s, flagp) == FAIL
11359 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011360 {
11361 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011363 }
11364 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 }
11366 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011367 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368}
11369
11370/*
11371 * Write a buffer name to the session file.
11372 * Also ends the line.
11373 * Returns FAIL if writing fails.
11374 */
11375 static int
11376ses_fname(fd, buf, flagp)
11377 FILE *fd;
11378 buf_T *buf;
11379 unsigned *flagp;
11380{
11381 char_u *name;
11382
11383 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011384 * the session file will be sourced.
11385 * Don't do this for ":mkview", we don't know the current directory.
11386 * Don't do this after ":lcd", we don't keep track of what the current
11387 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388 if (buf->b_sfname != NULL
11389 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011390 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011391#ifdef FEAT_AUTOCHDIR
11392 && !p_acd
11393#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011394 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 name = buf->b_sfname;
11396 else
11397 name = buf->b_ffname;
11398 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11399 return FAIL;
11400 return OK;
11401}
11402
11403/*
11404 * Write a file name to the session file.
11405 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11406 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011407 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 */
11409 static int
11410ses_put_fname(fd, name, flagp)
11411 FILE *fd;
11412 char_u *name;
11413 unsigned *flagp;
11414{
11415 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011416 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418
11419 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011420 if (sname == NULL)
11421 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011423 if (*flagp & SSOP_SLASH)
11424 {
11425 /* change all backslashes to forward slashes */
11426 for (p = sname; *p != NUL; mb_ptr_adv(p))
11427 if (*p == '\\')
11428 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011430
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011431 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011432 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011434 if (p == NULL)
11435 return FAIL;
11436
11437 /* write the result */
11438 if (fputs((char *)p, fd) < 0)
11439 retval = FAIL;
11440
11441 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442 return retval;
11443}
11444
11445/*
11446 * ":loadview [nr]"
11447 */
11448 static void
11449ex_loadview(eap)
11450 exarg_T *eap;
11451{
11452 char_u *fname;
11453
11454 fname = get_view_file(*eap->arg);
11455 if (fname != NULL)
11456 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011457 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458 vim_free(fname);
11459 }
11460}
11461
11462/*
11463 * Get the name of the view file for the current buffer.
11464 */
11465 static char_u *
11466get_view_file(c)
11467 int c;
11468{
11469 int len = 0;
11470 char_u *p, *s;
11471 char_u *retval;
11472 char_u *sname;
11473
11474 if (curbuf->b_ffname == NULL)
11475 {
11476 EMSG(_(e_noname));
11477 return NULL;
11478 }
11479 sname = home_replace_save(NULL, curbuf->b_ffname);
11480 if (sname == NULL)
11481 return NULL;
11482
11483 /*
11484 * We want a file name without separators, because we're not going to make
11485 * a directory.
11486 * "normal" path separator -> "=+"
11487 * "=" -> "=="
11488 * ":" path separator -> "=-"
11489 */
11490 for (p = sname; *p; ++p)
11491 if (*p == '=' || vim_ispathsep(*p))
11492 ++len;
11493 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11494 if (retval != NULL)
11495 {
11496 STRCPY(retval, p_vdir);
11497 add_pathsep(retval);
11498 s = retval + STRLEN(retval);
11499 for (p = sname; *p; ++p)
11500 {
11501 if (*p == '=')
11502 {
11503 *s++ = '=';
11504 *s++ = '=';
11505 }
11506 else if (vim_ispathsep(*p))
11507 {
11508 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011509#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510 if (*p == ':')
11511 *s++ = '-';
11512 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011514 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515 }
11516 else
11517 *s++ = *p;
11518 }
11519 *s++ = '=';
11520 *s++ = c;
11521 STRCPY(s, ".vim");
11522 }
11523
11524 vim_free(sname);
11525 return retval;
11526}
11527
11528#endif /* FEAT_SESSION */
11529
11530/*
11531 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11532 * Return FAIL for a write error.
11533 */
11534 int
11535put_eol(fd)
11536 FILE *fd;
11537{
11538 if (
11539#ifdef USE_CRNL
11540 (
11541# ifdef MKSESSION_NL
11542 !mksession_nl &&
11543# endif
11544 (putc('\r', fd) < 0)) ||
11545#endif
11546 (putc('\n', fd) < 0))
11547 return FAIL;
11548 return OK;
11549}
11550
11551/*
11552 * Write a line to "fd".
11553 * Return FAIL for a write error.
11554 */
11555 int
11556put_line(fd, s)
11557 FILE *fd;
11558 char *s;
11559{
11560 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11561 return FAIL;
11562 return OK;
11563}
11564
11565#ifdef FEAT_VIMINFO
11566/*
11567 * ":rviminfo" and ":wviminfo".
11568 */
11569 static void
11570ex_viminfo(eap)
11571 exarg_T *eap;
11572{
11573 char_u *save_viminfo;
11574
11575 save_viminfo = p_viminfo;
11576 if (*p_viminfo == NUL)
11577 p_viminfo = (char_u *)"'100";
11578 if (eap->cmdidx == CMD_rviminfo)
11579 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011580 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11581 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582 EMSG(_("E195: Cannot open viminfo file for reading"));
11583 }
11584 else
11585 write_viminfo(eap->arg, eap->forceit);
11586 p_viminfo = save_viminfo;
11587}
11588#endif
11589
11590#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011591/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011592 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011593 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595 void
11596dialog_msg(buff, format, fname)
11597 char_u *buff;
11598 char *format;
11599 char_u *fname;
11600{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601 if (fname == NULL)
11602 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011603 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604}
11605#endif
11606
11607/*
11608 * ":behave {mswin,xterm}"
11609 */
11610 static void
11611ex_behave(eap)
11612 exarg_T *eap;
11613{
11614 if (STRCMP(eap->arg, "mswin") == 0)
11615 {
11616 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11617 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11618 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11619 set_option_value((char_u *)"keymodel", 0L,
11620 (char_u *)"startsel,stopsel", 0);
11621 }
11622 else if (STRCMP(eap->arg, "xterm") == 0)
11623 {
11624 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11625 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11626 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11627 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11628 }
11629 else
11630 EMSG2(_(e_invarg2), eap->arg);
11631}
11632
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011633#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11634/*
11635 * Function given to ExpandGeneric() to obtain the possible arguments of the
11636 * ":behave {mswin,xterm}" command.
11637 */
11638 char_u *
11639get_behave_arg(xp, idx)
11640 expand_T *xp UNUSED;
11641 int idx;
11642{
11643 if (idx == 0)
11644 return (char_u *)"mswin";
11645 if (idx == 1)
11646 return (char_u *)"xterm";
11647 return NULL;
11648}
11649#endif
11650
Bram Moolenaar071d4272004-06-13 20:20:40 +000011651#ifdef FEAT_AUTOCMD
11652static int filetype_detect = FALSE;
11653static int filetype_plugin = FALSE;
11654static int filetype_indent = FALSE;
11655
11656/*
11657 * ":filetype [plugin] [indent] {on,off,detect}"
11658 * on: Load the filetype.vim file to install autocommands for file types.
11659 * off: Load the ftoff.vim file to remove all autocommands for file types.
11660 * plugin on: load filetype.vim and ftplugin.vim
11661 * plugin off: load ftplugof.vim
11662 * indent on: load filetype.vim and indent.vim
11663 * indent off: load indoff.vim
11664 */
11665 static void
11666ex_filetype(eap)
11667 exarg_T *eap;
11668{
11669 char_u *arg = eap->arg;
11670 int plugin = FALSE;
11671 int indent = FALSE;
11672
11673 if (*eap->arg == NUL)
11674 {
11675 /* Print current status. */
11676 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11677 filetype_detect ? "ON" : "OFF",
11678 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11679 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11680 return;
11681 }
11682
11683 /* Accept "plugin" and "indent" in any order. */
11684 for (;;)
11685 {
11686 if (STRNCMP(arg, "plugin", 6) == 0)
11687 {
11688 plugin = TRUE;
11689 arg = skipwhite(arg + 6);
11690 continue;
11691 }
11692 if (STRNCMP(arg, "indent", 6) == 0)
11693 {
11694 indent = TRUE;
11695 arg = skipwhite(arg + 6);
11696 continue;
11697 }
11698 break;
11699 }
11700 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11701 {
11702 if (*arg == 'o' || !filetype_detect)
11703 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011704 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 filetype_detect = TRUE;
11706 if (plugin)
11707 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011708 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709 filetype_plugin = TRUE;
11710 }
11711 if (indent)
11712 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011713 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714 filetype_indent = TRUE;
11715 }
11716 }
11717 if (*arg == 'd')
11718 {
11719 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011720 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721 }
11722 }
11723 else if (STRCMP(arg, "off") == 0)
11724 {
11725 if (plugin || indent)
11726 {
11727 if (plugin)
11728 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011729 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730 filetype_plugin = FALSE;
11731 }
11732 if (indent)
11733 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011734 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 filetype_indent = FALSE;
11736 }
11737 }
11738 else
11739 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011740 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741 filetype_detect = FALSE;
11742 }
11743 }
11744 else
11745 EMSG2(_(e_invarg2), arg);
11746}
11747
11748/*
11749 * ":setfiletype {name}"
11750 */
11751 static void
11752ex_setfiletype(eap)
11753 exarg_T *eap;
11754{
11755 if (!did_filetype)
11756 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11757}
11758#endif
11759
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760 static void
11761ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011762 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763{
11764#ifdef FEAT_DIGRAPHS
11765 if (*eap->arg != NUL)
11766 putdigraph(eap->arg);
11767 else
11768 listdigraphs();
11769#else
11770 EMSG(_("E196: No digraphs in this version"));
11771#endif
11772}
11773
11774 static void
11775ex_set(eap)
11776 exarg_T *eap;
11777{
11778 int flags = 0;
11779
11780 if (eap->cmdidx == CMD_setlocal)
11781 flags = OPT_LOCAL;
11782 else if (eap->cmdidx == CMD_setglobal)
11783 flags = OPT_GLOBAL;
11784#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11785 if (cmdmod.browse && flags == 0)
11786 ex_options(eap);
11787 else
11788#endif
11789 (void)do_set(eap->arg, flags);
11790}
11791
11792#ifdef FEAT_SEARCH_EXTRA
11793/*
11794 * ":nohlsearch"
11795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796 static void
11797ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011798 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011800 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011801 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802}
11803
11804/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011805 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806 * Sets nextcmd to the start of the next command, if any. Also called when
11807 * skipping commands to find the next command.
11808 */
11809 static void
11810ex_match(eap)
11811 exarg_T *eap;
11812{
11813 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011814 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815 char_u *end;
11816 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011817 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011818
11819 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011820 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011821 else
11822 {
11823 EMSG(e_invcmd);
11824 return;
11825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826
11827 /* First clear any old pattern. */
11828 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011829 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830
11831 if (ends_excmd(*eap->arg))
11832 end = eap->arg;
11833 else if ((STRNICMP(eap->arg, "none", 4) == 0
11834 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11835 end = eap->arg + 4;
11836 else
11837 {
11838 p = skiptowhite(eap->arg);
11839 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011840 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 p = skipwhite(p);
11842 if (*p == NUL)
11843 {
11844 /* There must be two arguments. */
11845 EMSG2(_(e_invarg2), eap->arg);
11846 return;
11847 }
11848 end = skip_regexp(p + 1, *p, TRUE, NULL);
11849 if (!eap->skip)
11850 {
11851 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11852 {
11853 eap->errmsg = e_trailing;
11854 return;
11855 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011856 if (*end != *p)
11857 {
11858 EMSG2(_(e_invarg2), p);
11859 return;
11860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861
11862 c = *end;
11863 *end = NUL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011864 match_add(curwin, g, p + 1, 10, id, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011865 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011866 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867 }
11868 }
11869 eap->nextcmd = find_nextcmd(end);
11870}
11871#endif
11872
11873#ifdef FEAT_CRYPT
11874/*
11875 * ":X": Get crypt key
11876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 static void
11878ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011879 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010011881 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020011882 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011883}
11884#endif
11885
11886#ifdef FEAT_FOLDING
11887 static void
11888ex_fold(eap)
11889 exarg_T *eap;
11890{
11891 if (foldManualAllowed(TRUE))
11892 foldCreate(eap->line1, eap->line2);
11893}
11894
11895 static void
11896ex_foldopen(eap)
11897 exarg_T *eap;
11898{
11899 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11900 eap->forceit, FALSE);
11901}
11902
11903 static void
11904ex_folddo(eap)
11905 exarg_T *eap;
11906{
11907 linenr_T lnum;
11908
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011909#ifdef FEAT_CLIPBOARD
11910 start_global_changes();
11911#endif
11912
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 /* First set the marks for all lines closed/open. */
11914 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11915 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11916 ml_setmarked(lnum);
11917
11918 /* Execute the command on the marked lines. */
11919 global_exe(eap->arg);
11920 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011921#ifdef FEAT_CLIPBOARD
11922 end_global_changes();
11923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924}
11925#endif