blob: eebdcffb483dd53055c265a747d3f26217bac653 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 Moolenaarf1d6ccf2014-12-08 04:16:44 +010030 int uc_addr_type; /* The command's address type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010031# ifdef FEAT_EVAL
32 scid_T uc_scriptID; /* SID where the command was defined */
33# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000034 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010035# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000036# endif
37} ucmd_T;
38
39#define UC_BUFFER 1 /* -buffer: local to current buffer */
40
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000041static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
44#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
45
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static void do_ucmd(exarg_T *eap);
47static void ex_command(exarg_T *eap);
48static void ex_delcommand(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# ifdef FEAT_CMDL_COMPL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010050static char_u *get_user_command_name(int idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051# endif
52
Bram Moolenaar958636c2014-10-21 20:01:58 +020053/* Wether a command index indicates a user command. */
54# define IS_USER_CMDIDX(idx) ((int)(idx) < 0)
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056#else
57# define ex_command ex_ni
58# define ex_comclear ex_ni
59# define ex_delcommand ex_ni
Bram Moolenaar958636c2014-10-21 20:01:58 +020060/* Wether a command index indicates a user command. */
61# define IS_USER_CMDIDX(idx) (FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#endif
63
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010064static int compute_buffer_local_count(int addr_type, int lnum, int local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010066static char_u *do_one_cmd(char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010068static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000069static int if_level = 0; /* depth in :if */
70#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void append_command(char_u *cmd);
72static char_u *find_command(exarg_T *eap, int *full);
Bram Moolenaar071d4272004-06-13 20:20:40 +000073
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010074static void ex_abbreviate(exarg_T *eap);
75static void ex_map(exarg_T *eap);
76static void ex_unmap(exarg_T *eap);
77static void ex_mapclear(exarg_T *eap);
78static void ex_abclear(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifndef FEAT_MENU
80# define ex_emenu ex_ni
81# define ex_menu ex_ni
82# define ex_menutranslate ex_ni
83#endif
84#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static void ex_autocmd(exarg_T *eap);
86static void ex_doautocmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#else
88# define ex_autocmd ex_ni
89# define ex_doautocmd ex_ni
90# define ex_doautoall ex_ni
91#endif
92#ifdef FEAT_LISTCMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010093static void ex_bunload(exarg_T *eap);
94static void ex_buffer(exarg_T *eap);
95static void ex_bmodified(exarg_T *eap);
96static void ex_bnext(exarg_T *eap);
97static void ex_bprevious(exarg_T *eap);
98static void ex_brewind(exarg_T *eap);
99static void ex_blast(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#else
101# define ex_bunload ex_ni
102# define ex_buffer ex_ni
103# define ex_bmodified ex_ni
104# define ex_bnext ex_ni
105# define ex_bprevious ex_ni
106# define ex_brewind ex_ni
107# define ex_blast ex_ni
108# define buflist_list ex_ni
109# define ex_checktime ex_ni
110#endif
111#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
112# define ex_buffer_all ex_ni
113#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static char_u *getargcmd(char_u **);
115static char_u *skip_cmd_arg(char_u *p, int rembs);
116static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifndef FEAT_QUICKFIX
118# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# define ex_cc ex_ni
121# define ex_cnext ex_ni
122# define ex_cfile ex_ni
123# define qf_list ex_ni
124# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200125# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000127# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
129#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
130# define ex_cclose ex_ni
131# define ex_copen ex_ni
132# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +0200133# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000135#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
136# define ex_cexpr ex_ni
137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static int check_more(int, int);
Bram Moolenaarded27822017-01-02 14:27:34 +0100140static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100141static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200142#if !defined(FEAT_PERL) \
143 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
144 || !defined(FEAT_TCL) \
145 || !defined(FEAT_RUBY) \
146 || !defined(FEAT_LUA) \
147 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000148# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100149static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100151static char_u *invalid_range(exarg_T *eap);
152static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000153#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100154static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000155#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100156static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
157static void ex_highlight(exarg_T *eap);
158static void ex_colorscheme(exarg_T *eap);
159static void ex_quit(exarg_T *eap);
160static void ex_cquit(exarg_T *eap);
161static void ex_quit_all(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100163static void ex_close(exarg_T *eap);
164static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
165static void ex_only(exarg_T *eap);
166static void ex_resize(exarg_T *eap);
167static void ex_stag(exarg_T *eap);
168static void ex_tabclose(exarg_T *eap);
169static void ex_tabonly(exarg_T *eap);
170static void ex_tabnext(exarg_T *eap);
171static void ex_tabmove(exarg_T *eap);
172static void ex_tabs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#else
174# define ex_close ex_ni
175# define ex_only ex_ni
176# define ex_all ex_ni
177# define ex_resize ex_ni
178# define ex_splitview ex_ni
179# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000180# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000181# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000182# define ex_tabs ex_ni
183# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000184# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#endif
186#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100187static void ex_pclose(exarg_T *eap);
188static void ex_ptag(exarg_T *eap);
189static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#else
191# define ex_pclose ex_ni
192# define ex_ptag ex_ni
193# define ex_pedit ex_ni
194#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100195static void ex_hide(exarg_T *eap);
196static void ex_stop(exarg_T *eap);
197static void ex_exit(exarg_T *eap);
198static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100200static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#else
202# define ex_goto ex_ni
203#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100204static void ex_shell(exarg_T *eap);
205static void ex_preserve(exarg_T *eap);
206static void ex_recover(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#ifndef FEAT_LISTCMDS
208# define ex_argedit ex_ni
209# define ex_argadd ex_ni
210# define ex_argdelete ex_ni
211# define ex_listdo ex_ni
212#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100213static void ex_mode(exarg_T *eap);
214static void ex_wrongmodifier(exarg_T *eap);
215static void ex_find(exarg_T *eap);
216static void ex_open(exarg_T *eap);
217static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
219# define ex_drop ex_ni
220#endif
221#ifndef FEAT_GUI
222# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#endif
225#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228# define ex_tearoff ex_ni
229#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000230#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100231static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#else
233# define ex_popup ex_ni
234#endif
235#ifndef FEAT_GUI_MSWIN
236# define ex_simalt ex_ni
237#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000238#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# define gui_mch_find_dialog ex_ni
240# define gui_mch_replace_dialog ex_ni
241#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000242#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243# define ex_helpfind ex_ni
244#endif
245#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100246# define ex_cscope ex_ni
247# define ex_scscope ex_ni
248# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#endif
250#ifndef FEAT_SYN_HL
251# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200252# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000253#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200254#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200255# define ex_syntime ex_ni
256#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000257#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000258# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000259# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000260# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000261# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000262# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000263#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200264#ifndef FEAT_PERSISTENT_UNDO
265# define ex_rundo ex_ni
266# define ex_wundo ex_ni
267#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200268#ifndef FEAT_LUA
269# define ex_lua ex_script_ni
270# define ex_luado ex_ni
271# define ex_luafile ex_ni
272#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000273#ifndef FEAT_MZSCHEME
274# define ex_mzscheme ex_script_ni
275# define ex_mzfile ex_ni
276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifndef FEAT_PERL
278# define ex_perl ex_script_ni
279# define ex_perldo ex_ni
280#endif
281#ifndef FEAT_PYTHON
282# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200283# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_pyfile ex_ni
285#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200286#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200287# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200288# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200289# define ex_py3file ex_ni
290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291#ifndef FEAT_TCL
292# define ex_tcl ex_script_ni
293# define ex_tcldo ex_ni
294# define ex_tclfile ex_ni
295#endif
296#ifndef FEAT_RUBY
297# define ex_ruby ex_script_ni
298# define ex_rubydo ex_ni
299# define ex_rubyfile ex_ni
300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301#ifndef FEAT_KEYMAP
302# define ex_loadkeymap ex_ni
303#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100304static void ex_swapname(exarg_T *eap);
305static void ex_syncbind(exarg_T *eap);
306static void ex_read(exarg_T *eap);
307static void ex_pwd(exarg_T *eap);
308static void ex_equal(exarg_T *eap);
309static void ex_sleep(exarg_T *eap);
310static void do_exmap(exarg_T *eap, int isabbrev);
311static void ex_winsize(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100313static void ex_wincmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314#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 Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#else
320# define ex_winpos ex_ni
321#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100322static void ex_operators(exarg_T *eap);
323static void ex_put(exarg_T *eap);
324static void ex_copymove(exarg_T *eap);
325static void ex_submagic(exarg_T *eap);
326static void ex_join(exarg_T *eap);
327static void ex_at(exarg_T *eap);
328static void ex_bang(exarg_T *eap);
329static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200330#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100331static void ex_wundo(exarg_T *eap);
332static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200333#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100334static void ex_redo(exarg_T *eap);
335static void ex_later(exarg_T *eap);
336static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100337static void ex_redrawstatus(exarg_T *eap);
338static void close_redir(void);
339static void ex_mkrc(exarg_T *eap);
340static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static char_u *uc_fun_cmd(void);
343static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100345static void ex_startinsert(exarg_T *eap);
346static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100348static void ex_checkpath(exarg_T *eap);
349static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#else
351# define ex_findpat ex_ni
352# define ex_checkpath ex_ni
353#endif
354#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100355static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356#else
357# define ex_psearch ex_ni
358#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100359static void ex_tag(exarg_T *eap);
360static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361#ifndef FEAT_EVAL
362# define ex_scriptnames ex_ni
363# define ex_finish ex_ni
364# define ex_echo ex_ni
365# define ex_echohl ex_ni
366# define ex_execute ex_ni
367# define ex_call ex_ni
368# define ex_if ex_ni
369# define ex_endif ex_ni
370# define ex_else ex_ni
371# define ex_while ex_ni
372# define ex_continue ex_ni
373# define ex_break ex_ni
374# define ex_endwhile ex_ni
375# define ex_throw ex_ni
376# define ex_try ex_ni
377# define ex_catch ex_ni
378# define ex_finally ex_ni
379# define ex_endtry ex_ni
380# define ex_endfunction ex_ni
381# define ex_let ex_ni
382# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000383# define ex_lockvar ex_ni
384# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385# define ex_function ex_ni
386# define ex_delfunction ex_ni
387# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000388# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100390static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100392static int makeopens(FILE *fd, char_u *dirnow);
393static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
394static void ex_loadview(exarg_T *eap);
395static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000396static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397#else
398# define ex_loadview ex_ni
399#endif
400#ifndef FEAT_EVAL
401# define ex_compiler ex_ni
402#endif
403#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100404static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405#else
406# define ex_viminfo ex_ni
407#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100408static void ex_behave(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100410static void ex_filetype(exarg_T *eap);
411static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412#else
413# define ex_filetype ex_ni
414# define ex_setfiletype ex_ni
415#endif
416#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000417# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418# define ex_diffpatch ex_ni
419# define ex_diffgetput ex_ni
420# define ex_diffsplit ex_ni
421# define ex_diffthis ex_ni
422# define ex_diffupdate ex_ni
423#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100424static void ex_digraphs(exarg_T *eap);
425static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
427# define ex_options ex_ni
428#endif
429#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100430static void ex_nohlsearch(exarg_T *eap);
431static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432#else
433# define ex_nohlsearch ex_ni
434# define ex_match ex_ni
435#endif
436#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100437static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438#else
439# define ex_X ex_ni
440#endif
441#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100442static void ex_fold(exarg_T *eap);
443static void ex_foldopen(exarg_T *eap);
444static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445#else
446# define ex_fold ex_ni
447# define ex_foldopen ex_ni
448# define ex_folddo ex_ni
449#endif
450#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
451 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
452# define ex_language ex_ni
453#endif
454#ifndef FEAT_SIGNS
455# define ex_sign ex_ni
456#endif
457#ifndef FEAT_SUN_WORKSHOP
458# define ex_wsverb ex_ni
459#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000460#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200461# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000462# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200463# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465
466#ifndef FEAT_EVAL
467# define ex_debug ex_ni
468# define ex_breakadd ex_ni
469# define ex_debuggreedy ex_ni
470# define ex_breakdel ex_ni
471# define ex_breaklist ex_ni
472#endif
473
474#ifndef FEAT_CMDHIST
475# define ex_history ex_ni
476#endif
477#ifndef FEAT_JUMPLIST
478# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200479# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480# define ex_changes ex_ni
481#endif
482
Bram Moolenaar05159a02005-02-26 23:04:13 +0000483#ifndef FEAT_PROFILE
484# define ex_profile ex_ni
485#endif
486
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487/*
488 * Declare cmdnames[].
489 */
490#define DO_DECLARE_EXCMD
491#include "ex_cmds.h"
492
493/*
494 * Table used to quickly search for a command, based on its first character.
495 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000496static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497{
498 CMD_append,
499 CMD_buffer,
500 CMD_change,
501 CMD_delete,
502 CMD_edit,
503 CMD_file,
504 CMD_global,
505 CMD_help,
506 CMD_insert,
507 CMD_join,
508 CMD_k,
509 CMD_list,
510 CMD_move,
511 CMD_next,
512 CMD_open,
513 CMD_print,
514 CMD_quit,
515 CMD_read,
516 CMD_substitute,
517 CMD_t,
518 CMD_undo,
519 CMD_vglobal,
520 CMD_write,
521 CMD_xit,
522 CMD_yank,
523 CMD_z,
524 CMD_bang
525};
526
527static char_u dollar_command[2] = {'$', 0};
528
529
530#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000531/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532typedef struct
533{
534 char_u *line; /* command line */
535 linenr_T lnum; /* sourcing_lnum of the line */
536} wcmd_T;
537
538/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000539 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000541 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000543struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
545 garray_T *lines_gap; /* growarray with line info */
546 int current_line; /* last read line from growarray */
547 int repeating; /* TRUE when looping a second time */
548 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100549 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 void *cookie;
551};
552
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100553static char_u *get_loop_line(int c, void *cookie, int indent);
554static int store_loop_line(garray_T *gap, char_u *line);
555static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000556
557/* Struct to save a few things while debugging. Used in do_cmdline() only. */
558struct dbg_stuff
559{
560 int trylevel;
561 int force_abort;
562 except_T *caught_stack;
563 char_u *vv_exception;
564 char_u *vv_throwpoint;
565 int did_emsg;
566 int got_int;
567 int did_throw;
568 int need_rethrow;
569 int check_cstack;
570 except_T *current_exception;
571};
572
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100573static void save_dbg_stuff(struct dbg_stuff *dsp);
574static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000575
576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100577save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000578{
579 dsp->trylevel = trylevel; trylevel = 0;
580 dsp->force_abort = force_abort; force_abort = FALSE;
581 dsp->caught_stack = caught_stack; caught_stack = NULL;
582 dsp->vv_exception = v_exception(NULL);
583 dsp->vv_throwpoint = v_throwpoint(NULL);
584
585 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
586 dsp->did_emsg = did_emsg; did_emsg = FALSE;
587 dsp->got_int = got_int; got_int = FALSE;
588 dsp->did_throw = did_throw; did_throw = FALSE;
589 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
590 dsp->check_cstack = check_cstack; check_cstack = FALSE;
591 dsp->current_exception = current_exception; current_exception = NULL;
592}
593
594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100595restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000596{
597 suppress_errthrow = FALSE;
598 trylevel = dsp->trylevel;
599 force_abort = dsp->force_abort;
600 caught_stack = dsp->caught_stack;
601 (void)v_exception(dsp->vv_exception);
602 (void)v_throwpoint(dsp->vv_throwpoint);
603 did_emsg = dsp->did_emsg;
604 got_int = dsp->got_int;
605 did_throw = dsp->did_throw;
606 need_rethrow = dsp->need_rethrow;
607 check_cstack = dsp->check_cstack;
608 current_exception = dsp->current_exception;
609}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610#endif
611
612
613/*
614 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
615 * command is given.
616 */
617 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100618do_exmode(
619 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 int save_msg_scroll;
622 int prev_msg_row;
623 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000624 int changedtick;
625
626 if (improved)
627 exmode_active = EXMODE_VIM;
628 else
629 exmode_active = EXMODE_NORMAL;
630 State = NORMAL;
631
632 /* When using ":global /pat/ visual" and then "Q" we return to continue
633 * the :global command. */
634 if (global_busy)
635 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636
637 save_msg_scroll = msg_scroll;
638 ++RedrawingDisabled; /* don't redisplay the window */
639 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640#ifdef FEAT_GUI
641 /* Ignore scrollbar and mouse events in Ex mode */
642 ++hold_gui_events;
643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
645 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
646 while (exmode_active)
647 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000648 /* Check for a ":normal" command and no more characters left. */
649 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
650 {
651 exmode_active = FALSE;
652 break;
653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 msg_scroll = TRUE;
655 need_wait_return = FALSE;
656 ex_pressedreturn = FALSE;
657 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000658 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 prev_msg_row = msg_row;
660 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 if (improved)
662 {
663 cmdline_row = msg_row;
664 do_cmdline(NULL, getexline, NULL, 0);
665 }
666 else
667 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
668 lines_left = Rows - 1;
669
Bram Moolenaardf177f62005-02-22 08:39:57 +0000670 if ((prev_line != curwin->w_cursor.lnum
671 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000673 if (curbuf->b_ml.ml_flags & ML_EMPTY)
674 EMSG(_(e_emptybuf));
675 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000677 if (ex_pressedreturn)
678 {
679 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000680 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000681 msg_row = prev_msg_row;
682 if (prev_msg_row == Rows - 1)
683 msg_row--;
684 }
685 msg_col = 0;
686 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
687 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000690 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
691 {
692 if (curbuf->b_ml.ml_flags & ML_EMPTY)
693 EMSG(_(e_emptybuf));
694 else
695 EMSG(_("E501: At end-of-file"));
696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 }
698
699#ifdef FEAT_GUI
700 --hold_gui_events;
701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 --RedrawingDisabled;
703 --no_wait_return;
704 update_screen(CLEAR);
705 need_wait_return = FALSE;
706 msg_scroll = save_msg_scroll;
707}
708
709/*
710 * Execute a simple command line. Used for translated commands like "*".
711 */
712 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100713do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714{
715 return do_cmdline(cmd, NULL, NULL,
716 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
717}
718
719/*
720 * do_cmdline(): execute one Ex command line
721 *
722 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100723 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 * 2. Split up in parts separated with '|'.
725 *
726 * This function can be called recursively!
727 *
728 * flags:
729 * DOCMD_VERBOSE - The command will be included in the error message.
730 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100731 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 * DOCMD_KEYTYPED - Don't reset KeyTyped.
733 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
734 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
735 *
736 * return FAIL if cmdline could not be executed, OK otherwise
737 */
738 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100739do_cmdline(
740 char_u *cmdline,
741 char_u *(*fgetline)(int, void *, int),
742 void *cookie, /* argument for fgetline() */
743 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
745 char_u *next_cmdline; /* next cmd to execute */
746 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100747 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 static int recursive = 0; /* recursive depth */
749 int msg_didout_before_start = 0;
750 int count = 0; /* line number count */
751 int did_inc = FALSE; /* incremented RedrawingDisabled */
752 int retval = OK;
753#ifdef FEAT_EVAL
754 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000755 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 int current_line = 0; /* active line in lines_ga */
757 char_u *fname = NULL; /* function or script name */
758 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
759 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000760 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 int initial_trylevel;
762 struct msglist **saved_msg_list = NULL;
763 struct msglist *private_msg_list;
764
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100765 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100766 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000768 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100772# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773# define cmd_cookie cookie
774#endif
775 static int call_depth = 0; /* recursiveness */
776
777#ifdef FEAT_EVAL
778 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
779 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000780 * This ensures that the do_errthrow() call in do_one_cmd() does not
781 * combine the messages stored by an earlier invocation of do_one_cmd()
782 * with the command name of the later one. This would happen when
783 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 saved_msg_list = msg_list;
785 msg_list = &private_msg_list;
786 private_msg_list = NULL;
787#endif
788
789 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100790 * here. The value of 200 allows nested function calls, ":source", etc.
791 * Allow 200 or 'maxfuncdepth', whatever is larger. */
792 if (call_depth >= 200 && call_depth >= p_mfd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {
794 EMSG(_("E169: Command too recursive"));
795#ifdef FEAT_EVAL
796 /* When converting to an exception, we do not include the command name
797 * since this is not an error of the specific command. */
798 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
799 msg_list = saved_msg_list;
800#endif
801 return FAIL;
802 }
803 ++call_depth;
804
805#ifdef FEAT_EVAL
806 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000807 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 cstack.cs_trylevel = 0;
809 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000810 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
812
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100813 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
815 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100816 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 ++ex_nesting_level;
819
820 /* Get the function or script name and the address where the next breakpoint
821 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000822 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 {
824 fname = func_name(real_cookie);
825 breakpoint = func_breakpoint(real_cookie);
826 dbg_tick = func_dbg_tick(real_cookie);
827 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100828 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 {
830 fname = sourcing_name;
831 breakpoint = source_breakpoint(real_cookie);
832 dbg_tick = source_dbg_tick(real_cookie);
833 }
834
835 /*
836 * Initialize "force_abort" and "suppress_errthrow" at the top level.
837 */
838 if (!recursive)
839 {
840 force_abort = FALSE;
841 suppress_errthrow = FALSE;
842 }
843
844 /*
845 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000846 * exception handling (used when debugging). Otherwise clear it to avoid
847 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000849 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000850 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000851 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200852 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853
854 initial_trylevel = trylevel;
855
856 /*
857 * "did_throw" will be set to TRUE when an exception is being thrown.
858 */
859 did_throw = FALSE;
860#endif
861 /*
862 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000863 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 * If force_abort is set, we cancel everything.
865 */
866 did_emsg = FALSE;
867
868 /*
869 * KeyTyped is only set when calling vgetc(). Reset it here when not
870 * calling vgetc() (sourced command lines).
871 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100872 if (!(flags & DOCMD_KEYTYPED)
873 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 KeyTyped = FALSE;
875
876 /*
877 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000878 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 * - for multiple commands on one line, separated with '|'
880 * - when repeating until there are no more lines (for ":source")
881 */
882 next_cmdline = cmdline;
883 do
884 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000885#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100886 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000887#endif
888
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000889 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 if (next_cmdline == NULL
891#ifdef FEAT_EVAL
892 && !force_abort
893 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000894 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895#endif
896 )
897 did_emsg = FALSE;
898
899 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000900 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100901 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 * 3. If a line is given: Make a copy, so we can mess with it.
903 */
904
905#ifdef FEAT_EVAL
906 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000907 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 {
909 /* Each '|' separated command is stored separately in lines_ga, to
910 * be able to jump to it. Don't use next_cmdline now. */
911 vim_free(cmdline_copy);
912 cmdline_copy = NULL;
913
914 /* Check if a function has returned or, unless it has an unclosed
915 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000916 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000918# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000919 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000920 func_line_end(real_cookie);
921# endif
922 if (func_has_ended(real_cookie))
923 {
924 retval = FAIL;
925 break;
926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000928#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000929 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100930 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000931 script_line_end();
932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933
934 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100935 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 {
937 retval = FAIL;
938 break;
939 }
940
941 /* If breakpoints have been added/deleted need to check for it. */
942 if (breakpoint != NULL && dbg_tick != NULL
943 && *dbg_tick != debug_tick)
944 {
945 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100946 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 fname, sourcing_lnum);
948 *dbg_tick = debug_tick;
949 }
950
951 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
952 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
953
954 /* Did we encounter a breakpoint? */
955 if (breakpoint != NULL && *breakpoint != 0
956 && *breakpoint <= sourcing_lnum)
957 {
958 dbg_breakpoint(fname, sourcing_lnum);
959 /* Find next breakpoint. */
960 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100961 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 fname, sourcing_lnum);
963 *dbg_tick = debug_tick;
964 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000965# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000966 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000967 {
968 if (getline_is_func)
969 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100970 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000971 script_line_start();
972 }
973# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 }
975
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000976 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000978 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100979 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 * below, so that it stores lines in or reads them from
981 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000982 * while/for loop. */
983 cmd_getline = get_loop_line;
984 cmd_cookie = (void *)&cmd_loop_cookie;
985 cmd_loop_cookie.lines_gap = &lines_ga;
986 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100987 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000988 cmd_loop_cookie.cookie = cookie;
989 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 }
991 else
992 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100993 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 cmd_cookie = cookie;
995 }
996#endif
997
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100998 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 if (next_cmdline == NULL)
1000 {
1001 /*
1002 * Need to set msg_didout for the first line after an ":if",
1003 * otherwise the ":if" will be overwritten.
1004 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001005 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001007 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008#ifdef FEAT_EVAL
1009 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1010#else
1011 0
1012#endif
1013 )) == NULL)
1014 {
1015 /* Don't call wait_return for aborted command line. The NULL
1016 * returned for the end of a sourced file or executed function
1017 * doesn't do this. */
1018 if (KeyTyped && !(flags & DOCMD_REPEAT))
1019 need_wait_return = FALSE;
1020 retval = FAIL;
1021 break;
1022 }
1023 used_getline = TRUE;
1024
1025 /*
1026 * Keep the first typed line. Clear it when more lines are typed.
1027 */
1028 if (flags & DOCMD_KEEPLINE)
1029 {
1030 vim_free(repeat_cmdline);
1031 if (count == 0)
1032 repeat_cmdline = vim_strsave(next_cmdline);
1033 else
1034 repeat_cmdline = NULL;
1035 }
1036 }
1037
1038 /* 3. Make a copy of the command so we can mess with it. */
1039 else if (cmdline_copy == NULL)
1040 {
1041 next_cmdline = vim_strsave(next_cmdline);
1042 if (next_cmdline == NULL)
1043 {
1044 EMSG(_(e_outofmem));
1045 retval = FAIL;
1046 break;
1047 }
1048 }
1049 cmdline_copy = next_cmdline;
1050
1051#ifdef FEAT_EVAL
1052 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001053 * Save the current line when inside a ":while" or ":for", and when
1054 * the command looks like a ":while" or ":for", because we may need it
1055 * later. When there is a '|' and another command, it is stored
1056 * separately, because we need to be able to jump back to it from an
1057 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001059 if (current_line == lines_ga.ga_len
1060 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001062 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 {
1064 retval = FAIL;
1065 break;
1066 }
1067 }
1068 did_endif = FALSE;
1069#endif
1070
1071 if (count++ == 0)
1072 {
1073 /*
1074 * All output from the commands is put below each other, without
1075 * waiting for a return. Don't do this when executing commands
1076 * from a script or when being called recursive (e.g. for ":e
1077 * +command file").
1078 */
1079 if (!(flags & DOCMD_NOWAIT) && !recursive)
1080 {
1081 msg_didout_before_start = msg_didout;
1082 msg_didany = FALSE; /* no output yet */
1083 msg_start();
1084 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001085 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 ++RedrawingDisabled;
1087 did_inc = TRUE;
1088 }
1089 }
1090
1091 if (p_verbose >= 15 && sourcing_name != NULL)
1092 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001094 verbose_enter_scroll();
1095
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 smsg((char_u *)_("line %ld: %s"),
1097 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001098 if (msg_silent == 0)
1099 msg_puts((char_u *)"\n"); /* don't overwrite this */
1100
1101 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 --no_wait_return;
1103 }
1104
1105 /*
1106 * 2. Execute one '|' separated command.
1107 * do_one_cmd() will return NULL if there is no trailing '|'.
1108 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1109 */
1110 ++recursive;
1111 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1112#ifdef FEAT_EVAL
1113 &cstack,
1114#endif
1115 cmd_getline, cmd_cookie);
1116 --recursive;
1117
1118#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001119 if (cmd_cookie == (void *)&cmd_loop_cookie)
1120 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001122 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123#endif
1124
1125 if (next_cmdline == NULL)
1126 {
1127 vim_free(cmdline_copy);
1128 cmdline_copy = NULL;
1129#ifdef FEAT_CMDHIST
1130 /*
1131 * If the command was typed, remember it for the ':' register.
1132 * Do this AFTER executing the command to make :@: work.
1133 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001134 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 && new_last_cmdline != NULL)
1136 {
1137 vim_free(last_cmdline);
1138 last_cmdline = new_last_cmdline;
1139 new_last_cmdline = NULL;
1140 }
1141#endif
1142 }
1143 else
1144 {
1145 /* need to copy the command after the '|' to cmdline_copy, for the
1146 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001147 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 next_cmdline = cmdline_copy;
1149 }
1150
1151
1152#ifdef FEAT_EVAL
1153 /* reset did_emsg for a function that is not aborted by an error */
1154 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001155 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 && !func_has_abort(real_cookie))
1157 did_emsg = FALSE;
1158
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001159 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {
1161 ++current_line;
1162
1163 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001164 * An ":endwhile", ":endfor" and ":continue" is handled here.
1165 * If we were executing commands, jump back to the ":while" or
1166 * ":for".
1167 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001169 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001171 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001173 /* Jump back to the matching ":while" or ":for". Be careful
1174 * not to use a cs_line[] from an entry that isn't a ":while"
1175 * or ":for": It would make "current_line" invalid and can
1176 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 if (!did_emsg && !got_int && !did_throw
1178 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001179 && (cstack.cs_flags[cstack.cs_idx]
1180 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 && cstack.cs_line[cstack.cs_idx] >= 0
1182 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1183 {
1184 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001185 /* remember we jumped there */
1186 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 line_breakcheck(); /* check if CTRL-C typed */
1188
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001189 /* Check for the next breakpoint at or after the ":while"
1190 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 if (breakpoint != NULL)
1192 {
1193 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001194 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 fname,
1196 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1197 *dbg_tick = debug_tick;
1198 }
1199 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001202 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001204 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1205 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 }
1207 }
1208
1209 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001210 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001212 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001214 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1216 }
1217 }
1218
1219 /*
1220 * When not inside any ":while" loop, clear remembered lines.
1221 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001222 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 {
1224 if (lines_ga.ga_len > 0)
1225 {
1226 sourcing_lnum =
1227 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1228 free_cmdlines(&lines_ga);
1229 }
1230 current_line = 0;
1231 }
1232
1233 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001234 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1235 * being restored at the ":endtry". Reset them here and set the
1236 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1237 * This includes the case where a missing ":endif", ":endwhile" or
1238 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001240 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001242 cstack.cs_lflags &= ~CSL_HAD_FINA;
1243 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1244 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 did_throw ? (void *)current_exception : NULL);
1246 did_emsg = got_int = did_throw = FALSE;
1247 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1248 }
1249
1250 /* Update global "trylevel" for recursive calls to do_cmdline() from
1251 * within this loop. */
1252 trylevel = initial_trylevel + cstack.cs_trylevel;
1253
1254 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001255 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 * files) is aborted because of an error, an interrupt, or an uncaught
1257 * exception, cancel everything. If it is left normally, reset
1258 * force_abort to get the non-EH compatible abortion behavior for
1259 * the rest of the script.
1260 */
1261 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1262 force_abort = FALSE;
1263
1264 /* Convert an interrupt to an exception if appropriate. */
1265 (void)do_intthrow(&cstack);
1266#endif /* FEAT_EVAL */
1267
1268 }
1269 /*
1270 * Continue executing command lines when:
1271 * - no CTRL-C typed, no aborting error, no exception thrown or try
1272 * conditionals need to be checked for executing finally clauses or
1273 * catching an interrupt exception
1274 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001275 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 * looping for ":source" command or function call.
1277 */
1278 while (!((got_int
1279#ifdef FEAT_EVAL
1280 || (did_emsg && force_abort) || did_throw
1281#endif
1282 )
1283#ifdef FEAT_EVAL
1284 && cstack.cs_trylevel == 0
1285#endif
1286 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001287 && !(did_emsg
1288#ifdef FEAT_EVAL
1289 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001290 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001291 * the :endtry to be missed. */
1292 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1293#endif
1294 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001295 && (getline_equal(fgetline, cookie, getexmodeline)
1296 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 && (next_cmdline != NULL
1298#ifdef FEAT_EVAL
1299 || cstack.cs_idx >= 0
1300#endif
1301 || (flags & DOCMD_REPEAT)));
1302
1303 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001304 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305#ifdef FEAT_EVAL
1306 free_cmdlines(&lines_ga);
1307 ga_clear(&lines_ga);
1308
1309 if (cstack.cs_idx >= 0)
1310 {
1311 /*
1312 * If a sourced file or executed function ran to its end, report the
1313 * unclosed conditional.
1314 */
1315 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001316 && ((getline_equal(fgetline, cookie, getsourceline)
1317 && !source_finished(fgetline, cookie))
1318 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 && !func_has_ended(real_cookie))))
1320 {
1321 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1322 EMSG(_(e_endtry));
1323 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1324 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001325 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1326 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 else
1328 EMSG(_(e_endif));
1329 }
1330
1331 /*
1332 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1333 * ":endtry" in a sourced file or executed function. If the try
1334 * conditional is in its finally clause, ignore anything pending.
1335 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001336 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 */
1338 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001339 {
1340 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1341
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001342 if (idx >= 0)
1343 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001344 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1345 &cstack.cs_looplevel);
1346 }
1347 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 trylevel = initial_trylevel;
1349 }
1350
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001351 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1352 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001354 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 ? (char_u *)"endfunction" : (char_u *)NULL);
1356
1357 if (trylevel == 0)
1358 {
1359 /*
1360 * When an exception is being thrown out of the outermost try
1361 * conditional, discard the uncaught exception, disable the conversion
1362 * of interrupts or errors to exceptions, and ensure that no more
1363 * commands are executed.
1364 */
1365 if (did_throw)
1366 {
1367 void *p = NULL;
1368 char_u *saved_sourcing_name;
1369 int saved_sourcing_lnum;
1370 struct msglist *messages = NULL, *next;
1371
1372 /*
1373 * If the uncaught exception is a user exception, report it as an
1374 * error. If it is an error exception, display the saved error
1375 * message now. For an interrupt exception, do nothing; the
1376 * interrupt message is given elsewhere.
1377 */
1378 switch (current_exception->type)
1379 {
1380 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001381 vim_snprintf((char *)IObuff, IOSIZE,
1382 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 current_exception->value);
1384 p = vim_strsave(IObuff);
1385 break;
1386 case ET_ERROR:
1387 messages = current_exception->messages;
1388 current_exception->messages = NULL;
1389 break;
1390 case ET_INTERRUPT:
1391 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 }
1393
1394 saved_sourcing_name = sourcing_name;
1395 saved_sourcing_lnum = sourcing_lnum;
1396 sourcing_name = current_exception->throw_name;
1397 sourcing_lnum = current_exception->throw_lnum;
1398 current_exception->throw_name = NULL;
1399
1400 discard_current_exception(); /* uses IObuff if 'verbose' */
1401 suppress_errthrow = TRUE;
1402 force_abort = TRUE;
1403
1404 if (messages != NULL)
1405 {
1406 do
1407 {
1408 next = messages->next;
1409 emsg(messages->msg);
1410 vim_free(messages->msg);
1411 vim_free(messages);
1412 messages = next;
1413 }
1414 while (messages != NULL);
1415 }
1416 else if (p != NULL)
1417 {
1418 emsg(p);
1419 vim_free(p);
1420 }
1421 vim_free(sourcing_name);
1422 sourcing_name = saved_sourcing_name;
1423 sourcing_lnum = saved_sourcing_lnum;
1424 }
1425
1426 /*
1427 * On an interrupt or an aborting error not converted to an exception,
1428 * disable the conversion of errors to exceptions. (Interrupts are not
1429 * converted any more, here.) This enables also the interrupt message
1430 * when force_abort is set and did_emsg unset in case of an interrupt
1431 * from a finally clause after an error.
1432 */
1433 else if (got_int || (did_emsg && force_abort))
1434 suppress_errthrow = TRUE;
1435 }
1436
1437 /*
1438 * The current cstack will be freed when do_cmdline() returns. An uncaught
1439 * exception will have to be rethrown in the previous cstack. If a function
1440 * has just returned or a script file was just finished and the previous
1441 * cstack belongs to the same function or, respectively, script file, it
1442 * will have to be checked for finally clauses to be executed due to the
1443 * ":return" or ":finish". This is done in do_one_cmd().
1444 */
1445 if (did_throw)
1446 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001447 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001449 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 && ex_nesting_level > func_level(real_cookie) + 1))
1451 {
1452 if (!did_throw)
1453 check_cstack = TRUE;
1454 }
1455 else
1456 {
1457 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001458 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 --ex_nesting_level;
1460 /*
1461 * Go to debug mode when returning from a function in which we are
1462 * single-stepping.
1463 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001464 if ((getline_equal(fgetline, cookie, getsourceline)
1465 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001467 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 ? (char_u *)_("End of sourced file")
1469 : (char_u *)_("End of function"));
1470 }
1471
1472 /*
1473 * Restore the exception environment (done after returning from the
1474 * debugger).
1475 */
1476 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001477 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478
1479 msg_list = saved_msg_list;
1480#endif /* FEAT_EVAL */
1481
1482 /*
1483 * If there was too much output to fit on the command line, ask the user to
1484 * hit return before redrawing the screen. With the ":global" command we do
1485 * this only once after the command is finished.
1486 */
1487 if (did_inc)
1488 {
1489 --RedrawingDisabled;
1490 --no_wait_return;
1491 msg_scroll = FALSE;
1492
1493 /*
1494 * When just finished an ":if"-":else" which was typed, no need to
1495 * wait for hit-return. Also for an error situation.
1496 */
1497 if (retval == FAIL
1498#ifdef FEAT_EVAL
1499 || (did_endif && KeyTyped && !did_emsg)
1500#endif
1501 )
1502 {
1503 need_wait_return = FALSE;
1504 msg_didany = FALSE; /* don't wait when restarting edit */
1505 }
1506 else if (need_wait_return)
1507 {
1508 /*
1509 * The msg_start() above clears msg_didout. The wait_return we do
1510 * here should not overwrite the command that may be shown before
1511 * doing that.
1512 */
1513 msg_didout |= msg_didout_before_start;
1514 wait_return(FALSE);
1515 }
1516 }
1517
Bram Moolenaar2a942252012-11-28 23:03:07 +01001518#ifdef FEAT_EVAL
1519 did_endif = FALSE; /* in case do_cmdline used recursively */
1520#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 /*
1522 * Reset if_level, in case a sourced script file contains more ":if" than
1523 * ":endif" (could be ":if x | foo | endif").
1524 */
1525 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001526#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001527
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 --call_depth;
1529 return retval;
1530}
1531
1532#ifdef FEAT_EVAL
1533/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001534 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 */
1536 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001537get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001539 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 wcmd_T *wp;
1541 char_u *line;
1542
1543 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1544 {
1545 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001546 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001548 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 if (cp->getline == NULL)
1550 line = getcmdline(c, 0L, indent);
1551 else
1552 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001553 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 ++cp->current_line;
1555
1556 return line;
1557 }
1558
1559 KeyTyped = FALSE;
1560 ++cp->current_line;
1561 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1562 sourcing_lnum = wp->lnum;
1563 return vim_strsave(wp->line);
1564}
1565
1566/*
1567 * Store a line in "gap" so that a ":while" loop can execute it again.
1568 */
1569 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001570store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
1572 if (ga_grow(gap, 1) == FAIL)
1573 return FAIL;
1574 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1575 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1576 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 return OK;
1578}
1579
1580/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001581 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 */
1583 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001584free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 while (gap->ga_len > 0)
1587 {
1588 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1589 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 }
1591}
1592#endif
1593
1594/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001595 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1596 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001599getline_equal(
1600 char_u *(*fgetline)(int, void *, int),
1601 void *cookie UNUSED, /* argument for fgetline() */
1602 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
1604#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001605 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001606 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607
Bram Moolenaar89d40322006-08-29 15:30:07 +00001608 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001609 * function that's originally used to obtain the lines. This may be
1610 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001611 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001612 cp = (struct loop_cookie *)cookie;
1613 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {
1615 gp = cp->getline;
1616 cp = cp->cookie;
1617 }
1618 return gp == func;
1619#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001620 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621#endif
1622}
1623
1624#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1625/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001626 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 * getline function. Otherwise return "cookie".
1628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001630getline_cookie(
1631 char_u *(*fgetline)(int, void *, int) UNUSED,
1632 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
1634# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001635 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001636 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
Bram Moolenaar89d40322006-08-29 15:30:07 +00001638 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001639 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001641 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001642 cp = (struct loop_cookie *)cookie;
1643 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 {
1645 gp = cp->getline;
1646 cp = cp->cookie;
1647 }
1648 return cp;
1649# else
1650 return cookie;
1651# endif
1652}
1653#endif
1654
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001655
1656/*
1657 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1658 * ":bwipeout", etc.
1659 * Returns the buffer number.
1660 */
1661 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001662compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001663{
1664 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001665 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001666 int count = offset;
1667
1668 buf = firstbuf;
1669 while (buf->b_next != NULL && buf->b_fnum < lnum)
1670 buf = buf->b_next;
1671 while (count != 0)
1672 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001673 count += (offset < 0) ? 1 : -1;
1674 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1675 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001676 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001677 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001678 if (addr_type == ADDR_LOADED_BUFFERS)
1679 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001680 while (buf->b_ml.ml_mfp == NULL)
1681 {
1682 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1683 if (nextbuf == NULL)
1684 break;
1685 buf = nextbuf;
1686 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001687 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001688 /* we might have gone too far, last buffer is not loadedd */
1689 if (addr_type == ADDR_LOADED_BUFFERS)
1690 while (buf->b_ml.ml_mfp == NULL)
1691 {
1692 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1693 if (nextbuf == NULL)
1694 break;
1695 buf = nextbuf;
1696 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001697 return buf->b_fnum;
1698}
1699
Bram Moolenaarf240e182014-11-27 18:33:02 +01001700#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001701static int current_win_nr(win_T *win);
1702static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001703
1704 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001705current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001706{
1707 win_T *wp;
1708 int nr = 0;
1709
Bram Moolenaar29323592016-07-24 22:04:11 +02001710 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001711 {
1712 ++nr;
1713 if (wp == win)
1714 break;
1715 }
1716 return nr;
1717}
1718
1719 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001720current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001721{
1722 tabpage_T *tp;
1723 int nr = 0;
1724
Bram Moolenaar29323592016-07-24 22:04:11 +02001725 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001726 {
1727 ++nr;
1728 if (tp == tab)
1729 break;
1730 }
1731 return nr;
1732}
1733
1734# define CURRENT_WIN_NR current_win_nr(curwin)
1735# define LAST_WIN_NR current_win_nr(NULL)
1736# define CURRENT_TAB_NR current_tab_nr(curtab)
1737# define LAST_TAB_NR current_tab_nr(NULL)
1738#else
1739# define CURRENT_WIN_NR 1
1740# define LAST_WIN_NR 1
1741# define CURRENT_TAB_NR 1
1742# define LAST_TAB_NR 1
1743#endif
1744
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001745
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746/*
1747 * Execute one Ex command.
1748 *
1749 * If 'sourcing' is TRUE, the command will be included in the error message.
1750 *
1751 * 1. skip comment lines and leading space
1752 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001753 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001754 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001755 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001756 * 6. parse arguments
1757 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001759 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 *
1761 * This function may be called recursively!
1762 */
1763#if (_MSC_VER == 1200)
1764/*
Bram Moolenaared203462004-06-16 11:19:22 +00001765 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001767 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#endif
1769 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001770do_one_cmd(
1771 char_u **cmdlinep,
1772 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001774 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001776 char_u *(*fgetline)(int, void *, int),
1777 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
1779 char_u *p;
1780 linenr_T lnum;
1781 long n;
1782 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001783 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 exarg_T ea; /* Ex command arguments */
1785 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001786 int save_msg_scroll = msg_scroll;
1787 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001789#ifdef HAVE_SANDBOX
1790 int did_sandbox = FALSE;
1791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 cmdmod_T save_cmdmod;
1793 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001794 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001795 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796
1797 vim_memset(&ea, 0, sizeof(ea));
1798 ea.line1 = 1;
1799 ea.line2 = 1;
1800#ifdef FEAT_EVAL
1801 ++ex_nesting_level;
1802#endif
1803
Bram Moolenaar21691f82012-12-05 19:13:18 +01001804 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 if (quitmore
1806#ifdef FEAT_EVAL
1807 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001808 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001809#endif
1810#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001811 /* avoid that an autocommand, e.g. QuitPre, does this */
1812 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813#endif
1814 )
1815 --quitmore;
1816
1817 /*
1818 * Reset browse, confirm, etc.. They are restored when returning, for
1819 * recursive calls.
1820 */
1821 save_cmdmod = cmdmod;
1822 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1823
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001824 /* "#!anything" is handled like a comment. */
1825 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1826 goto doend;
1827
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 /*
1829 * Repeat until no more command modifiers are found.
1830 */
1831 ea.cmd = *cmdlinep;
1832 for (;;)
1833 {
1834/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001835 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 */
1837 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1838 ++ea.cmd;
1839
1840 /* in ex mode, an empty line works like :+ */
1841 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001842 && (getline_equal(fgetline, cookie, getexmodeline)
1843 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001844 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 {
1846 ea.cmd = (char_u *)"+";
1847 ex_pressedreturn = TRUE;
1848 }
1849
1850 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001851 if (*ea.cmd == '"')
1852 goto doend;
1853 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001854 {
1855 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
1859/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001860 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001862 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 switch (*p)
1864 {
1865 /* When adding an entry, also modify cmd_exists(). */
1866 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1867 break;
1868#ifdef FEAT_WINDOWS
1869 cmdmod.split |= WSP_ABOVE;
1870#endif
1871 continue;
1872
1873 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1874 {
1875#ifdef FEAT_WINDOWS
1876 cmdmod.split |= WSP_BELOW;
1877#endif
1878 continue;
1879 }
1880 if (checkforcmd(&ea.cmd, "browse", 3))
1881 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001882#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 cmdmod.browse = TRUE;
1884#endif
1885 continue;
1886 }
1887 if (!checkforcmd(&ea.cmd, "botright", 2))
1888 break;
1889#ifdef FEAT_WINDOWS
1890 cmdmod.split |= WSP_BOT;
1891#endif
1892 continue;
1893
1894 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1895 break;
1896#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1897 cmdmod.confirm = TRUE;
1898#endif
1899 continue;
1900
1901 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1902 {
1903 cmdmod.keepmarks = TRUE;
1904 continue;
1905 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001906 if (checkforcmd(&ea.cmd, "keepalt", 5))
1907 {
1908 cmdmod.keepalt = TRUE;
1909 continue;
1910 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001911 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1912 {
1913 cmdmod.keeppatterns = TRUE;
1914 continue;
1915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1917 break;
1918 cmdmod.keepjumps = TRUE;
1919 continue;
1920
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001921 case 'f': /* only accept ":filter {pat} cmd" */
1922 {
1923 char_u *reg_pat;
1924
1925 if (!checkforcmd(&p, "filter", 4)
1926 || *p == NUL || ends_excmd(*p))
1927 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001928 if (*p == '!')
1929 {
1930 cmdmod.filter_force = TRUE;
1931 p = skipwhite(p + 1);
1932 if (*p == NUL || ends_excmd(*p))
1933 break;
1934 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001935 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1936 if (p == NULL || *p == NUL)
1937 break;
1938 cmdmod.filter_regmatch.regprog =
1939 vim_regcomp(reg_pat, RE_MAGIC);
1940 if (cmdmod.filter_regmatch.regprog == NULL)
1941 break;
1942 ea.cmd = p;
1943 continue;
1944 }
1945
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 /* ":hide" and ":hide | cmd" are not modifiers */
1947 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1948 || *p == NUL || ends_excmd(*p))
1949 break;
1950 ea.cmd = p;
1951 cmdmod.hide = TRUE;
1952 continue;
1953
1954 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1955 {
1956 cmdmod.lockmarks = TRUE;
1957 continue;
1958 }
1959
1960 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1961 break;
1962#ifdef FEAT_WINDOWS
1963 cmdmod.split |= WSP_ABOVE;
1964#endif
1965 continue;
1966
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001967 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001968 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001969#ifdef FEAT_AUTOCMD
1970 if (cmdmod.save_ei == NULL)
1971 {
1972 /* Set 'eventignore' to "all". Restore the
1973 * existing option value later. */
1974 cmdmod.save_ei = vim_strsave(p_ei);
1975 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001976 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001977 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001978#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001979 continue;
1980 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001981 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001982 break;
1983 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001984 continue;
1985
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1987 break;
1988#ifdef FEAT_WINDOWS
1989 cmdmod.split |= WSP_BELOW;
1990#endif
1991 continue;
1992
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001993 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1994 {
1995#ifdef HAVE_SANDBOX
1996 if (!did_sandbox)
1997 ++sandbox;
1998 did_sandbox = TRUE;
1999#endif
2000 continue;
2001 }
2002 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002004 if (save_msg_silent == -1)
2005 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
2008 {
2009 /* ":silent!", but not "silent !cmd" */
2010 ea.cmd = skipwhite(ea.cmd + 1);
2011 ++emsg_silent;
2012 ++did_esilent;
2013 }
2014 continue;
2015
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002016 case 't': if (checkforcmd(&p, "tab", 3))
2017 {
2018#ifdef FEAT_WINDOWS
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002019 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01002020 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002021 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002022 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002023 else
2024 {
2025 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2026 {
2027 errormsg = (char_u *)_(e_invrange);
2028 goto doend;
2029 }
2030 cmdmod.tab = tabnr + 1;
2031 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002032 ea.cmd = p;
2033#endif
2034 continue;
2035 }
2036 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 break;
2038#ifdef FEAT_WINDOWS
2039 cmdmod.split |= WSP_TOP;
2040#endif
2041 continue;
2042
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002043 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2044 break;
2045 if (save_msg_silent == -1)
2046 save_msg_silent = msg_silent;
2047 msg_silent = 0;
2048 continue;
2049
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2051 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002052#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 cmdmod.split |= WSP_VERT;
2054#endif
2055 continue;
2056 }
2057 if (!checkforcmd(&p, "verbose", 4))
2058 break;
2059 if (verbose_save < 0)
2060 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002061 if (vim_isdigit(*ea.cmd))
2062 p_verbose = atoi((char *)ea.cmd);
2063 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 p_verbose = 1;
2065 ea.cmd = p;
2066 continue;
2067 }
2068 break;
2069 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002070 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
2072#ifdef FEAT_EVAL
2073 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2074 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2075#else
2076 ea.skip = (if_level > 0);
2077#endif
2078
2079#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002080# ifdef FEAT_PROFILE
2081 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002082 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002083 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002084 if (getline_equal(fgetline, cookie, get_func_line))
2085 func_line_exec(getline_cookie(fgetline, cookie));
2086 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002087 script_line_exec();
2088 }
2089#endif
2090
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 /* May go to debug mode. If this happens and the ">quit" debug command is
2092 * used, throw an interrupt exception and skip the next command. */
2093 dbg_check_breakpoint(&ea);
2094 if (!ea.skip && got_int)
2095 {
2096 ea.skip = TRUE;
2097 (void)do_intthrow(cstack);
2098 }
2099#endif
2100
2101/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002102 * 3. Skip over the range to find the command. Let "p" point to after it.
2103 *
2104 * We need the command to know what kind of range it uses.
2105 */
2106 cmd = ea.cmd;
2107 ea.cmd = skip_range(ea.cmd, NULL);
2108 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2109 ea.cmd = skipwhite(ea.cmd + 1);
2110 p = find_command(&ea, NULL);
2111
2112/*
2113 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 *
2115 * where 'addr' is:
2116 *
2117 * % (entire file)
2118 * $ [+-NUM]
2119 * 'x [+-NUM] (where x denotes a currently defined mark)
2120 * . [+-NUM]
2121 * [+-NUM]..
2122 * NUM
2123 *
2124 * The ea.cmd pointer is updated to point to the first character following the
2125 * range spec. If an initial address is found, but no second, the upper bound
2126 * is equal to the lower.
2127 */
2128
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002129 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002130 if (!IS_USER_CMDIDX(ea.cmdidx))
2131 {
2132 if (ea.cmdidx != CMD_SIZE)
2133 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2134 else
2135 ea.addr_type = ADDR_LINES;
2136
2137#ifdef FEAT_WINDOWS
2138 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002139 if (ea.cmdidx == CMD_wincmd && p != NULL)
2140 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002141#endif
2142 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002143
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002145 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 for (;;)
2147 {
2148 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002149 switch (ea.addr_type)
2150 {
2151 case ADDR_LINES:
2152 /* default is current line number */
2153 ea.line2 = curwin->w_cursor.lnum;
2154 break;
2155 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002156 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002157 ea.line2 = lnum;
2158 break;
2159 case ADDR_ARGUMENTS:
2160 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002161 if (ea.line2 > ARGCOUNT)
2162 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002163 break;
2164 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002165 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002166 ea.line2 = curbuf->b_fnum;
2167 break;
2168 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002169 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002170 ea.line2 = lnum;
2171 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002172#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002173 case ADDR_QUICKFIX:
2174 ea.line2 = qf_get_cur_valid_idx(&ea);
2175 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002176#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002179 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002180 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 if (ea.cmd == NULL) /* error detected */
2182 goto doend;
2183 if (lnum == MAXLNUM)
2184 {
2185 if (*ea.cmd == '%') /* '%' - all lines */
2186 {
2187 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002188 switch (ea.addr_type)
2189 {
2190 case ADDR_LINES:
2191 ea.line1 = 1;
2192 ea.line2 = curbuf->b_ml.ml_line_count;
2193 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002194 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002195 {
2196 buf_T *buf = firstbuf;
2197
2198 while (buf->b_next != NULL
2199 && buf->b_ml.ml_mfp == NULL)
2200 buf = buf->b_next;
2201 ea.line1 = buf->b_fnum;
2202 buf = lastbuf;
2203 while (buf->b_prev != NULL
2204 && buf->b_ml.ml_mfp == NULL)
2205 buf = buf->b_prev;
2206 ea.line2 = buf->b_fnum;
2207 break;
2208 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002209 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002210 ea.line1 = firstbuf->b_fnum;
2211 ea.line2 = lastbuf->b_fnum;
2212 break;
2213 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002214 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002215 if (IS_USER_CMDIDX(ea.cmdidx))
2216 {
2217 ea.line1 = 1;
2218 ea.line2 = ea.addr_type == ADDR_WINDOWS
2219 ? LAST_WIN_NR : LAST_TAB_NR;
2220 }
2221 else
2222 {
2223 /* there is no Vim command which uses '%' and
2224 * ADDR_WINDOWS or ADDR_TABS */
2225 errormsg = (char_u *)_(e_invrange);
2226 goto doend;
2227 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002228 break;
2229 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002230 if (ARGCOUNT == 0)
2231 ea.line1 = ea.line2 = 0;
2232 else
2233 {
2234 ea.line1 = 1;
2235 ea.line2 = ARGCOUNT;
2236 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002237 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002238#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002239 case ADDR_QUICKFIX:
2240 ea.line1 = 1;
2241 ea.line2 = qf_get_size(&ea);
2242 if (ea.line2 == 0)
2243 ea.line2 = 1;
2244 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002245#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 ++ea.addr_count;
2248 }
2249 /* '*' - visual area */
2250 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2251 {
2252 pos_T *fp;
2253
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002254 if (ea.addr_type != ADDR_LINES)
2255 {
2256 errormsg = (char_u *)_(e_invrange);
2257 goto doend;
2258 }
2259
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 ++ea.cmd;
2261 if (!ea.skip)
2262 {
2263 fp = getmark('<', FALSE);
2264 if (check_mark(fp) == FAIL)
2265 goto doend;
2266 ea.line1 = fp->lnum;
2267 fp = getmark('>', FALSE);
2268 if (check_mark(fp) == FAIL)
2269 goto doend;
2270 ea.line2 = fp->lnum;
2271 ++ea.addr_count;
2272 }
2273 }
2274 }
2275 else
2276 ea.line2 = lnum;
2277 ea.addr_count++;
2278
2279 if (*ea.cmd == ';')
2280 {
2281 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002282 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002284 /* don't leave the cursor on an illegal line */
2285 check_cursor_lnum();
2286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 }
2288 else if (*ea.cmd != ',')
2289 break;
2290 ++ea.cmd;
2291 }
2292
2293 /* One address given: set start and end lines */
2294 if (ea.addr_count == 1)
2295 {
2296 ea.line1 = ea.line2;
2297 /* ... but only implicit: really no address given */
2298 if (lnum == MAXLNUM)
2299 ea.addr_count = 0;
2300 }
2301
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002303 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 */
2305
2306 /*
2307 * Skip ':' and any white space
2308 */
2309 ea.cmd = skipwhite(ea.cmd);
2310 while (*ea.cmd == ':')
2311 ea.cmd = skipwhite(ea.cmd + 1);
2312
2313 /*
2314 * If we got a line, but no command, then go to the line.
2315 * If we find a '|' or '\n' we set ea.nextcmd.
2316 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002317 if (*ea.cmd == NUL || *ea.cmd == '"'
2318 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
2320 /*
2321 * strange vi behaviour:
2322 * ":3" jumps to line 3
2323 * ":3|..." prints line 3
2324 * ":|" prints current line
2325 */
2326 if (ea.skip) /* skip this if inside :if */
2327 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002328 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
2330 ea.cmdidx = CMD_print;
2331 ea.argt = RANGE+COUNT+TRLBAR;
2332 if ((errormsg = invalid_range(&ea)) == NULL)
2333 {
2334 correct_range(&ea);
2335 ex_print(&ea);
2336 }
2337 }
2338 else if (ea.addr_count != 0)
2339 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002340 if (ea.line2 > curbuf->b_ml.ml_line_count)
2341 {
2342 /* With '-' in 'cpoptions' a line number past the file is an
2343 * error, otherwise put it at the end of the file. */
2344 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2345 ea.line2 = -1;
2346 else
2347 ea.line2 = curbuf->b_ml.ml_line_count;
2348 }
2349
2350 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002351 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 else
2353 {
2354 if (ea.line2 == 0)
2355 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 else
2357 curwin->w_cursor.lnum = ea.line2;
2358 beginline(BL_SOL | BL_FIX);
2359 }
2360 }
2361 goto doend;
2362 }
2363
Bram Moolenaard5005162014-08-22 23:05:54 +02002364#ifdef FEAT_AUTOCMD
2365 /* If this looks like an undefined user command and there are CmdUndefined
2366 * autocommands defined, trigger the matching autocommands. */
2367 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2368 && ASCII_ISUPPER(*ea.cmd)
2369 && has_cmdundefined())
2370 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002371 int ret;
2372
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002373 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002374 while (ASCII_ISALNUM(*p))
2375 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002376 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002377 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2378 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002379 /* If the autocommands did something and didn't cause an error, try
2380 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002381 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002382 }
2383#endif
2384
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385#ifdef FEAT_USR_CMDS
2386 if (p == NULL)
2387 {
2388 if (!ea.skip)
2389 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2390 goto doend;
2391 }
2392 /* Check for wrong commands. */
2393 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2394 {
2395 errormsg = uc_fun_cmd();
2396 goto doend;
2397 }
2398#endif
2399 if (ea.cmdidx == CMD_SIZE)
2400 {
2401 if (!ea.skip)
2402 {
2403 STRCPY(IObuff, _("E492: Not an editor command"));
2404 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002405 {
2406 /* If the modifier was parsed OK the error must be in the
2407 * following command */
2408 if (after_modifier != NULL)
2409 append_command(after_modifier);
2410 else
2411 append_command(*cmdlinep);
2412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002414 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416 goto doend;
2417 }
2418
Bram Moolenaar958636c2014-10-21 20:01:58 +02002419 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2420 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002421#ifdef HAVE_EX_SCRIPT_NI
2422 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2423#endif
2424 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
2426#ifndef FEAT_EVAL
2427 /*
2428 * When the expression evaluation is disabled, recognize the ":if" and
2429 * ":endif" commands and ignore everything in between it.
2430 */
2431 if (ea.cmdidx == CMD_if)
2432 ++if_level;
2433 if (if_level)
2434 {
2435 if (ea.cmdidx == CMD_endif)
2436 --if_level;
2437 goto doend;
2438 }
2439
2440#endif
2441
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002442 /* forced commands */
2443 if (*p == '!' && ea.cmdidx != CMD_substitute
2444 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
2446 ++p;
2447 ea.forceit = TRUE;
2448 }
2449 else
2450 ea.forceit = FALSE;
2451
2452/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002453 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002455 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002456 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457
2458 if (!ea.skip)
2459 {
2460#ifdef HAVE_SANDBOX
2461 if (sandbox != 0 && !(ea.argt & SBOXOK))
2462 {
2463 /* Command not allowed in sandbox. */
2464 errormsg = (char_u *)_(e_sandbox);
2465 goto doend;
2466 }
2467#endif
2468 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2469 {
2470 /* Command not allowed in non-'modifiable' buffer */
2471 errormsg = (char_u *)_(e_modifiable);
2472 goto doend;
2473 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002474
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002475 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002476 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002478 /* Command not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02002479 errormsg = get_text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 goto doend;
2481 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002482#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002483 /* Disallow editing another buffer when "curbuf_lock" is set.
2484 * Do allow ":edit" (check for argument later).
2485 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002486 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002487 && ea.cmdidx != CMD_edit
2488 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002489 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002490 && curbuf_locked())
2491 goto doend;
2492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
2494 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2495 {
2496 /* no range allowed */
2497 errormsg = (char_u *)_(e_norange);
2498 goto doend;
2499 }
2500 }
2501
2502 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2503 {
2504 errormsg = (char_u *)_(e_nobang);
2505 goto doend;
2506 }
2507
2508 /*
2509 * Don't complain about the range if it is not used
2510 * (could happen if line_count is accidentally set to 0).
2511 */
2512 if (!ea.skip && !ni)
2513 {
2514 /*
2515 * If the range is backwards, ask for confirmation and, if given, swap
2516 * ea.line1 & ea.line2 so it's forwards again.
2517 * When global command is busy, don't ask, will fail below.
2518 */
2519 if (!global_busy && ea.line1 > ea.line2)
2520 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002521 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002523 if (sourcing || exmode_active)
2524 {
2525 errormsg = (char_u *)_("E493: Backwards range given");
2526 goto doend;
2527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 if (ask_yesno((char_u *)
2529 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002530 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 }
2532 lnum = ea.line1;
2533 ea.line1 = ea.line2;
2534 ea.line2 = lnum;
2535 }
2536 if ((errormsg = invalid_range(&ea)) != NULL)
2537 goto doend;
2538 }
2539
2540 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2541 ea.line2 = 1;
2542
2543 correct_range(&ea);
2544
2545#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002546 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2547 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 {
2549 /* Put the first line at the start of a closed fold, put the last line
2550 * at the end of a closed fold. */
2551 (void)hasFolding(ea.line1, &ea.line1, NULL);
2552 (void)hasFolding(ea.line2, NULL, &ea.line2);
2553 }
2554#endif
2555
2556#ifdef FEAT_QUICKFIX
2557 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002558 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 * option here, so things like % get expanded.
2560 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002561 p = replace_makeprg(&ea, p, cmdlinep);
2562 if (p == NULL)
2563 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564#endif
2565
2566 /*
2567 * Skip to start of argument.
2568 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2569 */
2570 if (ea.cmdidx == CMD_bang)
2571 ea.arg = p;
2572 else
2573 ea.arg = skipwhite(p);
2574
2575 /*
2576 * Check for "++opt=val" argument.
2577 * Must be first, allow ":w ++enc=utf8 !cmd"
2578 */
2579 if (ea.argt & ARGOPT)
2580 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2581 if (getargopt(&ea) == FAIL && !ni)
2582 {
2583 errormsg = (char_u *)_(e_invarg);
2584 goto doend;
2585 }
2586
2587 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2588 {
2589 if (*ea.arg == '>') /* append */
2590 {
2591 if (*++ea.arg != '>') /* typed wrong */
2592 {
2593 errormsg = (char_u *)_("E494: Use w or w>>");
2594 goto doend;
2595 }
2596 ea.arg = skipwhite(ea.arg + 1);
2597 ea.append = TRUE;
2598 }
2599 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2600 {
2601 ++ea.arg;
2602 ea.usefilter = TRUE;
2603 }
2604 }
2605
2606 if (ea.cmdidx == CMD_read)
2607 {
2608 if (ea.forceit)
2609 {
2610 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2611 ea.forceit = FALSE;
2612 }
2613 else if (*ea.arg == '!') /* :r !filter */
2614 {
2615 ++ea.arg;
2616 ea.usefilter = TRUE;
2617 }
2618 }
2619
2620 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2621 {
2622 ea.amount = 1;
2623 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2624 {
2625 ++ea.arg;
2626 ++ea.amount;
2627 }
2628 ea.arg = skipwhite(ea.arg);
2629 }
2630
2631 /*
2632 * Check for "+command" argument, before checking for next command.
2633 * Don't do this for ":read !cmd" and ":write !cmd".
2634 */
2635 if ((ea.argt & EDITCMD) && !ea.usefilter)
2636 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2637
2638 /*
2639 * Check for '|' to separate commands and '"' to start comments.
2640 * Don't do this for ":read !cmd" and ":write !cmd".
2641 */
2642 if ((ea.argt & TRLBAR) && !ea.usefilter)
2643 separate_nextcmd(&ea);
2644
2645 /*
2646 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002647 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2648 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002650 else if (ea.cmdidx == CMD_bang
2651 || ea.cmdidx == CMD_global
2652 || ea.cmdidx == CMD_vglobal
2653 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 {
2655 for (p = ea.arg; *p; ++p)
2656 {
2657 /* Remove one backslash before a newline, so that it's possible to
2658 * pass a newline to the shell and also a newline that is preceded
2659 * with a backslash. This makes it impossible to end a shell
2660 * command in a backslash, but that doesn't appear useful.
2661 * Halving the number of backslashes is incompatible with previous
2662 * versions. */
2663 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002664 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 else if (*p == '\n')
2666 {
2667 ea.nextcmd = p + 1;
2668 *p = NUL;
2669 break;
2670 }
2671 }
2672 }
2673
2674 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2675 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002676 buf_T *buf;
2677
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002679 switch (ea.addr_type)
2680 {
2681 case ADDR_LINES:
2682 ea.line2 = curbuf->b_ml.ml_line_count;
2683 break;
2684 case ADDR_LOADED_BUFFERS:
2685 buf = firstbuf;
2686 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2687 buf = buf->b_next;
2688 ea.line1 = buf->b_fnum;
2689 buf = lastbuf;
2690 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2691 buf = buf->b_prev;
2692 ea.line2 = buf->b_fnum;
2693 break;
2694 case ADDR_BUFFERS:
2695 ea.line1 = firstbuf->b_fnum;
2696 ea.line2 = lastbuf->b_fnum;
2697 break;
2698 case ADDR_WINDOWS:
2699 ea.line2 = LAST_WIN_NR;
2700 break;
2701 case ADDR_TABS:
2702 ea.line2 = LAST_TAB_NR;
2703 break;
2704 case ADDR_ARGUMENTS:
2705 if (ARGCOUNT == 0)
2706 ea.line1 = ea.line2 = 0;
2707 else
2708 ea.line2 = ARGCOUNT;
2709 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002710#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002711 case ADDR_QUICKFIX:
2712 ea.line2 = qf_get_size(&ea);
2713 if (ea.line2 == 0)
2714 ea.line2 = 1;
2715 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002716#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 }
2719
2720 /* accept numbered register only when no count allowed (:put) */
2721 if ( (ea.argt & REGSTR)
2722 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002723 /* Do not allow register = for user commands */
2724 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2726 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002727#ifndef FEAT_CLIPBOARD
2728 /* check these explicitly for a more specific error message */
2729 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002731 errormsg = (char_u *)_(e_invalidreg);
2732 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 }
2734#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002735 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2736 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002737 {
2738 ea.regname = *ea.arg++;
2739#ifdef FEAT_EVAL
2740 /* for '=' register: accept the rest of the line as an expression */
2741 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2742 {
2743 set_expr_line(vim_strsave(ea.arg));
2744 ea.arg += STRLEN(ea.arg);
2745 }
2746#endif
2747 ea.arg = skipwhite(ea.arg);
2748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 }
2750
2751 /*
2752 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2753 * count, it's a buffer name.
2754 */
2755 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2756 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2757 || vim_iswhite(*p)))
2758 {
2759 n = getdigits(&ea.arg);
2760 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002761 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 {
2763 errormsg = (char_u *)_(e_zerocount);
2764 goto doend;
2765 }
2766 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2767 {
2768 ea.line2 = n;
2769 if (ea.addr_count == 0)
2770 ea.addr_count = 1;
2771 }
2772 else
2773 {
2774 ea.line1 = ea.line2;
2775 ea.line2 += n - 1;
2776 ++ea.addr_count;
2777 /*
2778 * Be vi compatible: no error message for out of range.
2779 */
2780 if (ea.line2 > curbuf->b_ml.ml_line_count)
2781 ea.line2 = curbuf->b_ml.ml_line_count;
2782 }
2783 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002784
2785 /*
2786 * Check for flags: 'l', 'p' and '#'.
2787 */
2788 if (ea.argt & EXFLAGS)
2789 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002791 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002792 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 {
2794 errormsg = (char_u *)_(e_trailing);
2795 goto doend;
2796 }
2797
2798 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2799 {
2800 errormsg = (char_u *)_(e_argreq);
2801 goto doend;
2802 }
2803
2804#ifdef FEAT_EVAL
2805 /*
2806 * Skip the command when it's not going to be executed.
2807 * The commands like :if, :endif, etc. always need to be executed.
2808 * Also make an exception for commands that handle a trailing command
2809 * themselves.
2810 */
2811 if (ea.skip)
2812 {
2813 switch (ea.cmdidx)
2814 {
2815 /* commands that need evaluation */
2816 case CMD_while:
2817 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002818 case CMD_for:
2819 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 case CMD_if:
2821 case CMD_elseif:
2822 case CMD_else:
2823 case CMD_endif:
2824 case CMD_try:
2825 case CMD_catch:
2826 case CMD_finally:
2827 case CMD_endtry:
2828 case CMD_function:
2829 break;
2830
2831 /* Commands that handle '|' themselves. Check: A command should
2832 * either have the TRLBAR flag, appear in this list or appear in
2833 * the list at ":help :bar". */
2834 case CMD_aboveleft:
2835 case CMD_and:
2836 case CMD_belowright:
2837 case CMD_botright:
2838 case CMD_browse:
2839 case CMD_call:
2840 case CMD_confirm:
2841 case CMD_delfunction:
2842 case CMD_djump:
2843 case CMD_dlist:
2844 case CMD_dsearch:
2845 case CMD_dsplit:
2846 case CMD_echo:
2847 case CMD_echoerr:
2848 case CMD_echomsg:
2849 case CMD_echon:
2850 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002851 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 case CMD_help:
2853 case CMD_hide:
2854 case CMD_ijump:
2855 case CMD_ilist:
2856 case CMD_isearch:
2857 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002858 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 case CMD_keepjumps:
2860 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002861 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 case CMD_leftabove:
2863 case CMD_let:
2864 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002865 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002867 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002868 case CMD_noautocmd:
2869 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 case CMD_perl:
2871 case CMD_psearch:
2872 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002873 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002874 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 case CMD_return:
2876 case CMD_rightbelow:
2877 case CMD_ruby:
2878 case CMD_silent:
2879 case CMD_smagic:
2880 case CMD_snomagic:
2881 case CMD_substitute:
2882 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002883 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 case CMD_tcl:
2885 case CMD_throw:
2886 case CMD_tilde:
2887 case CMD_topleft:
2888 case CMD_unlet:
2889 case CMD_verbose:
2890 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002891 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 break;
2893
2894 default: goto doend;
2895 }
2896 }
2897#endif
2898
2899 if (ea.argt & XFILE)
2900 {
2901 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2902 goto doend;
2903 }
2904
2905#ifdef FEAT_LISTCMDS
2906 /*
2907 * Accept buffer name. Cannot be used at the same time with a buffer
2908 * number. Don't do this for a user command.
2909 */
2910 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002911 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 {
2913 /*
2914 * :bdelete, :bwipeout and :bunload take several arguments, separated
2915 * by spaces: find next space (skipping over escaped characters).
2916 * The others take one argument: ignore trailing spaces.
2917 */
2918 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2919 || ea.cmdidx == CMD_bunload)
2920 p = skiptowhite_esc(ea.arg);
2921 else
2922 {
2923 p = ea.arg + STRLEN(ea.arg);
2924 while (p > ea.arg && vim_iswhite(p[-1]))
2925 --p;
2926 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002927 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2928 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 if (ea.line2 < 0) /* failed */
2930 goto doend;
2931 ea.addr_count = 1;
2932 ea.arg = skipwhite(p);
2933 }
2934#endif
2935
2936/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002937 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 *
2939 * The "ea" structure holds the arguments that can be used.
2940 */
2941 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002942 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 ea.cookie = cookie;
2944#ifdef FEAT_EVAL
2945 ea.cstack = cstack;
2946#endif
2947
2948#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002949 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 {
2951 /*
2952 * Execute a user-defined command.
2953 */
2954 do_ucmd(&ea);
2955 }
2956 else
2957#endif
2958 {
2959 /*
2960 * Call the function to execute the command.
2961 */
2962 ea.errmsg = NULL;
2963 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2964 if (ea.errmsg != NULL)
2965 errormsg = (char_u *)_(ea.errmsg);
2966 }
2967
2968#ifdef FEAT_EVAL
2969 /*
2970 * If the command just executed called do_cmdline(), any throw or ":return"
2971 * or ":finish" encountered there must also check the cstack of the still
2972 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2973 * exception, or reanimate a returned function or finished script file and
2974 * return or finish it again.
2975 */
2976 if (need_rethrow)
2977 do_throw(cstack);
2978 else if (check_cstack)
2979 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002980 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002982 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 && current_func_returned())
2984 do_return(&ea, TRUE, FALSE, NULL);
2985 }
2986 need_rethrow = check_cstack = FALSE;
2987#endif
2988
2989doend:
2990 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2991 curwin->w_cursor.lnum = 1;
2992
2993 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2994 {
2995 if (sourcing)
2996 {
2997 if (errormsg != IObuff)
2998 {
2999 STRCPY(IObuff, errormsg);
3000 errormsg = IObuff;
3001 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003002 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 }
3004 emsg(errormsg);
3005 }
3006#ifdef FEAT_EVAL
3007 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02003008 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
3009 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010#endif
3011
3012 if (verbose_save >= 0)
3013 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003014#ifdef FEAT_AUTOCMD
3015 if (cmdmod.save_ei != NULL)
3016 {
3017 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003018 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
3019 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003020 free_string_option(cmdmod.save_ei);
3021 }
3022#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003023 if (cmdmod.filter_regmatch.regprog != NULL)
3024 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025
3026 cmdmod = save_cmdmod;
3027
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003028 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 {
3030 /* messages could be enabled for a serious error, need to check if the
3031 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00003032 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003033 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 emsg_silent -= did_esilent;
3035 if (emsg_silent < 0)
3036 emsg_silent = 0;
3037 /* Restore msg_scroll, it's set by file I/O commands, even when no
3038 * message is actually displayed. */
3039 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003040
3041 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3042 * somewhere in the line. Put it back in the first column. */
3043 if (redirecting())
3044 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 }
3046
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003047#ifdef HAVE_SANDBOX
3048 if (did_sandbox)
3049 --sandbox;
3050#endif
3051
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3053 ea.nextcmd = NULL;
3054
3055#ifdef FEAT_EVAL
3056 --ex_nesting_level;
3057#endif
3058
3059 return ea.nextcmd;
3060}
3061#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003062 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063#endif
3064
3065/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003066 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 * If there is a match advance "pp" to the argument and return TRUE.
3068 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003069 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003070checkforcmd(
3071 char_u **pp, /* start of command */
3072 char *cmd, /* name of command */
3073 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074{
3075 int i;
3076
3077 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003078 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 break;
3080 if (i >= len && !isalpha((*pp)[i]))
3081 {
3082 *pp = skipwhite(*pp + i);
3083 return TRUE;
3084 }
3085 return FALSE;
3086}
3087
3088/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003089 * Append "cmd" to the error message in IObuff.
3090 * Takes care of limiting the length and handling 0xa0, which would be
3091 * invisible otherwise.
3092 */
3093 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003094append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003095{
3096 char_u *s = cmd;
3097 char_u *d;
3098
3099 STRCAT(IObuff, ": ");
3100 d = IObuff + STRLEN(IObuff);
3101 while (*s != NUL && d - IObuff < IOSIZE - 7)
3102 {
3103 if (
3104#ifdef FEAT_MBYTE
3105 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3106#endif
3107 *s == 0xa0)
3108 {
3109 s +=
3110#ifdef FEAT_MBYTE
3111 enc_utf8 ? 2 :
3112#endif
3113 1;
3114 STRCPY(d, "<a0>");
3115 d += 4;
3116 }
3117 else
3118 MB_COPY_CHAR(s, d);
3119 }
3120 *d = NUL;
3121}
3122
3123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003125 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003127 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 * Returns NULL for an ambiguous user command.
3129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003131find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132{
3133 int len;
3134 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003135 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136
3137 /*
3138 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003139 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 * - the 'k' command can directly be followed by any character.
3141 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003142 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003144 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 */
3146 p = eap->cmd;
3147 if (*p == 'k')
3148 {
3149 eap->cmdidx = CMD_k;
3150 ++p;
3151 }
3152 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003153 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3154 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 || p[1] == 'g'
3156 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3157 || p[1] == 'I'
3158 || (p[1] == 'r' && p[2] != 'e')))
3159 {
3160 eap->cmdidx = CMD_substitute;
3161 ++p;
3162 }
3163 else
3164 {
3165 while (ASCII_ISALPHA(*p))
3166 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003167 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003168 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003169 while (ASCII_ISALNUM(*p))
3170 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003171
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 /* check for non-alpha command */
3173 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3174 ++p;
3175 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003176 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3177 {
3178 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3179 * :delete with the 'l' flag. Same for 'p'. */
3180 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003181 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003182 break;
3183 if (i == len - 1)
3184 {
3185 --len;
3186 if (p[-1] == 'l')
3187 eap->flags |= EXFLAG_LIST;
3188 else
3189 eap->flags |= EXFLAG_PRINT;
3190 }
3191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192
3193 if (ASCII_ISLOWER(*eap->cmd))
3194 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3195 else
3196 eap->cmdidx = cmdidxs[26];
3197
3198 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3199 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3200 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3201 (size_t)len) == 0)
3202 {
3203#ifdef FEAT_EVAL
3204 if (full != NULL
3205 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3206 *full = TRUE;
3207#endif
3208 break;
3209 }
3210
3211#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003212 /* Look for a user defined command as a last resort. Let ":Print" be
3213 * overruled by a user defined command. */
3214 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3215 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003217 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 while (ASCII_ISALNUM(*p))
3219 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003220 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 }
3222#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003223 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 eap->cmdidx = CMD_SIZE;
3225 }
3226
3227 return p;
3228}
3229
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003230#ifdef FEAT_USR_CMDS
3231/*
3232 * Search for a user command that matches "eap->cmd".
3233 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3234 * Return a pointer to just after the command.
3235 * Return NULL if there is no matching command.
3236 */
3237 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003238find_ucmd(
3239 exarg_T *eap,
3240 char_u *p, /* end of the command (possibly including count) */
3241 int *full, /* set to TRUE for a full match */
3242 expand_T *xp, /* used for completion, NULL otherwise */
3243 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003244{
3245 int len = (int)(p - eap->cmd);
3246 int j, k, matchlen = 0;
3247 ucmd_T *uc;
3248 int found = FALSE;
3249 int possible = FALSE;
3250 char_u *cp, *np; /* Point into typed cmd and test name */
3251 garray_T *gap;
3252 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3253 only full match global is accepted. */
3254
3255 /*
3256 * Look for buffer-local user commands first, then global ones.
3257 */
3258 gap = &curbuf->b_ucmds;
3259 for (;;)
3260 {
3261 for (j = 0; j < gap->ga_len; ++j)
3262 {
3263 uc = USER_CMD_GA(gap, j);
3264 cp = eap->cmd;
3265 np = uc->uc_name;
3266 k = 0;
3267 while (k < len && *np != NUL && *cp++ == *np++)
3268 k++;
3269 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3270 {
3271 /* If finding a second match, the command is ambiguous. But
3272 * not if a buffer-local command wasn't a full match and a
3273 * global command is a full match. */
3274 if (k == len && found && *np != NUL)
3275 {
3276 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003277 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003278 amb_local = TRUE;
3279 }
3280
3281 if (!found || (k == len && *np == NUL))
3282 {
3283 /* If we matched up to a digit, then there could
3284 * be another command including the digit that we
3285 * should use instead.
3286 */
3287 if (k == len)
3288 found = TRUE;
3289 else
3290 possible = TRUE;
3291
3292 if (gap == &ucmds)
3293 eap->cmdidx = CMD_USER;
3294 else
3295 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003296 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003297 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003298 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003299
3300# ifdef FEAT_CMDL_COMPL
3301 if (compl != NULL)
3302 *compl = uc->uc_compl;
3303# ifdef FEAT_EVAL
3304 if (xp != NULL)
3305 {
3306 xp->xp_arg = uc->uc_compl_arg;
3307 xp->xp_scriptID = uc->uc_scriptID;
3308 }
3309# endif
3310# endif
3311 /* Do not search for further abbreviations
3312 * if this is an exact match. */
3313 matchlen = k;
3314 if (k == len && *np == NUL)
3315 {
3316 if (full != NULL)
3317 *full = TRUE;
3318 amb_local = FALSE;
3319 break;
3320 }
3321 }
3322 }
3323 }
3324
3325 /* Stop if we found a full match or searched all. */
3326 if (j < gap->ga_len || gap == &ucmds)
3327 break;
3328 gap = &ucmds;
3329 }
3330
3331 /* Only found ambiguous matches. */
3332 if (amb_local)
3333 {
3334 if (xp != NULL)
3335 xp->xp_context = EXPAND_UNSUCCESSFUL;
3336 return NULL;
3337 }
3338
3339 /* The match we found may be followed immediately by a number. Move "p"
3340 * back to point to it. */
3341 if (found || possible)
3342 return p + (matchlen - len);
3343 return p;
3344}
3345#endif
3346
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003348static struct cmdmod
3349{
3350 char *name;
3351 int minlen;
3352 int has_count; /* :123verbose :3tab */
3353} cmdmods[] = {
3354 {"aboveleft", 3, FALSE},
3355 {"belowright", 3, FALSE},
3356 {"botright", 2, FALSE},
3357 {"browse", 3, FALSE},
3358 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003359 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003360 {"hide", 3, FALSE},
3361 {"keepalt", 5, FALSE},
3362 {"keepjumps", 5, FALSE},
3363 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003364 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003365 {"leftabove", 5, FALSE},
3366 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003367 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003368 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003369 {"rightbelow", 6, FALSE},
3370 {"sandbox", 3, FALSE},
3371 {"silent", 3, FALSE},
3372 {"tab", 3, TRUE},
3373 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003374 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003375 {"verbose", 4, TRUE},
3376 {"vertical", 4, FALSE},
3377};
3378
3379/*
3380 * Return length of a command modifier (including optional count).
3381 * Return zero when it's not a modifier.
3382 */
3383 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003384modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003385{
3386 int i, j;
3387 char_u *p = cmd;
3388
3389 if (VIM_ISDIGIT(*cmd))
3390 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003391 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003392 {
3393 for (j = 0; p[j] != NUL; ++j)
3394 if (p[j] != cmdmods[i].name[j])
3395 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003396 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003397 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003398 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003399 }
3400 return 0;
3401}
3402
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403/*
3404 * Return > 0 if an Ex command "name" exists.
3405 * Return 2 if there is an exact match.
3406 * Return 3 if there is an ambiguous match.
3407 */
3408 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003409cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410{
3411 exarg_T ea;
3412 int full = FALSE;
3413 int i;
3414 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003415 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416
3417 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003418 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 {
3420 for (j = 0; name[j] != NUL; ++j)
3421 if (name[j] != cmdmods[i].name[j])
3422 break;
3423 if (name[j] == NUL && j >= cmdmods[i].minlen)
3424 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3425 }
3426
Bram Moolenaara9587612006-05-04 21:47:50 +00003427 /* Check built-in commands and user defined commands.
3428 * For ":2match" and ":3match" we need to skip the number. */
3429 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003431 p = find_command(&ea, &full);
3432 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003434 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3435 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003436 if (*skipwhite(p) != NUL)
3437 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3439}
3440#endif
3441
3442/*
3443 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3444 * we don't need/want deleted. Maybe this could be done better if we didn't
3445 * repeat all this stuff. The only problem is that they may not stay
3446 * perfectly compatible with each other, but then the command line syntax
3447 * probably won't change that much -- webb.
3448 */
3449 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003450set_one_cmd_context(
3451 expand_T *xp,
3452 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
3454 char_u *p;
3455 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003456 int len = 0;
3457 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3459 int compl = EXPAND_NOTHING;
3460#endif
3461#ifdef FEAT_CMDL_COMPL
3462 int delim;
3463#endif
3464 int forceit = FALSE;
3465 int usefilter = FALSE; /* filter instead of file name */
3466
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003467 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 xp->xp_pattern = buff;
3469 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003470 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471
3472/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003473 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 */
3475 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3476 ;
3477 xp->xp_pattern = cmd;
3478
3479 if (*cmd == NUL)
3480 return NULL;
3481 if (*cmd == '"') /* ignore comment lines */
3482 {
3483 xp->xp_context = EXPAND_NOTHING;
3484 return NULL;
3485 }
3486
3487/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003488 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 */
3490 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 xp->xp_pattern = cmd;
3492 if (*cmd == NUL)
3493 return NULL;
3494 if (*cmd == '"')
3495 {
3496 xp->xp_context = EXPAND_NOTHING;
3497 return NULL;
3498 }
3499
3500 if (*cmd == '|' || *cmd == '\n')
3501 return cmd + 1; /* There's another command */
3502
3503 /*
3504 * Isolate the command and search for it in the command table.
3505 * Exceptions:
3506 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003507 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3509 */
3510 if (*cmd == 'k' && cmd[1] != 'e')
3511 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003512 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 p = cmd + 1;
3514 }
3515 else
3516 {
3517 p = cmd;
3518 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3519 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003520 /* a user command may contain digits */
3521 if (ASCII_ISUPPER(cmd[0]))
3522 while (ASCII_ISALNUM(*p) || *p == '*')
3523 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003524 /* for python 3.x: ":py3*" commands completion */
3525 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003526 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003527 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003528 while (ASCII_ISALPHA(*p) || *p == '*')
3529 ++p;
3530 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003531 /* check for non-alpha command */
3532 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3533 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003534 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003536 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 {
3538 xp->xp_context = EXPAND_UNSUCCESSFUL;
3539 return NULL;
3540 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003541 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003542 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3543 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3544 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 break;
3546
3547#ifdef FEAT_USR_CMDS
3548 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3550 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551#endif
3552 }
3553
3554 /*
3555 * If the cursor is touching the command, and it ends in an alpha-numeric
3556 * character, complete the command name.
3557 */
3558 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3559 return NULL;
3560
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003561 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 {
3563 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3564 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003565 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 p = cmd + 1;
3567 }
3568#ifdef FEAT_USR_CMDS
3569 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3570 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003571 ea.cmd = cmd;
3572 p = find_ucmd(&ea, p, NULL, xp,
3573# if defined(FEAT_CMDL_COMPL)
3574 &compl
3575# else
3576 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003578 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003579 if (p == NULL)
3580 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 }
3582#endif
3583 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003584 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
3586 /* Not still touching the command and it was an illegal one */
3587 xp->xp_context = EXPAND_UNSUCCESSFUL;
3588 return NULL;
3589 }
3590
3591 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3592
3593 if (*p == '!') /* forced commands */
3594 {
3595 forceit = TRUE;
3596 ++p;
3597 }
3598
3599/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003600 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003602 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003603 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
3605 arg = skipwhite(p);
3606
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003607 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 {
3609 if (*arg == '>') /* append */
3610 {
3611 if (*++arg == '>')
3612 ++arg;
3613 arg = skipwhite(arg);
3614 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003615 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
3617 ++arg;
3618 usefilter = TRUE;
3619 }
3620 }
3621
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003622 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
3624 usefilter = forceit; /* :r! filter if forced */
3625 if (*arg == '!') /* :r !filter */
3626 {
3627 ++arg;
3628 usefilter = TRUE;
3629 }
3630 }
3631
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003632 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
3634 while (*arg == *cmd) /* allow any number of '>' or '<' */
3635 ++arg;
3636 arg = skipwhite(arg);
3637 }
3638
3639 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003640 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 {
3642 /* Check if we're in the +command */
3643 p = arg + 1;
3644 arg = skip_cmd_arg(arg, FALSE);
3645
3646 /* Still touching the command after '+'? */
3647 if (*arg == NUL)
3648 return p;
3649
3650 /* Skip space(s) after +command to get to the real argument */
3651 arg = skipwhite(arg);
3652 }
3653
3654 /*
3655 * Check for '|' to separate commands and '"' to start comments.
3656 * Don't do this for ":read !cmd" and ":write !cmd".
3657 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003658 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
3660 p = arg;
3661 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003662 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 p += 2;
3664 while (*p)
3665 {
3666 if (*p == Ctrl_V)
3667 {
3668 if (p[1] != NUL)
3669 ++p;
3670 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003671 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 || *p == '|' || *p == '\n')
3673 {
3674 if (*(p - 1) != '\\')
3675 {
3676 if (*p == '|' || *p == '\n')
3677 return p + 1;
3678 return NULL; /* It's a comment */
3679 }
3680 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003681 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 }
3683 }
3684
3685 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003686 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 vim_strchr((char_u *)"|\"", *arg) == NULL)
3688 return NULL;
3689
3690 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003691 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003693 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003694 while (*p && p < buff + len)
3695 {
3696 if (*p == ' ' || *p == TAB)
3697 {
3698 /* argument starts after a space */
3699 xp->xp_pattern = ++p;
3700 }
3701 else
3702 {
3703 if (*p == '\\' && *(p + 1) != NUL)
3704 ++p; /* skip over escaped character */
3705 mb_ptr_adv(p);
3706 }
3707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003709 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003711 int c;
3712 int in_quote = FALSE;
3713 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714
3715 /*
3716 * Allow spaces within back-quotes to count as part of the argument
3717 * being expanded.
3718 */
3719 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003720 p = xp->xp_pattern;
3721 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003723#ifdef FEAT_MBYTE
3724 if (has_mbyte)
3725 c = mb_ptr2char(p);
3726 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003728 c = *p;
3729 if (c == '\\' && p[1] != NUL)
3730 ++p;
3731 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 {
3733 if (!in_quote)
3734 {
3735 xp->xp_pattern = p;
3736 bow = p + 1;
3737 }
3738 in_quote = !in_quote;
3739 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003740 /* An argument can contain just about everything, except
3741 * characters that end the command and white space. */
3742 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003743#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003744 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003745#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003746 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003747 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003748 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003749 while (*p != NUL)
3750 {
3751#ifdef FEAT_MBYTE
3752 if (has_mbyte)
3753 c = mb_ptr2char(p);
3754 else
3755#endif
3756 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003757 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003758 break;
3759#ifdef FEAT_MBYTE
3760 if (has_mbyte)
3761 len = (*mb_ptr2len)(p);
3762 else
3763#endif
3764 len = 1;
3765 mb_ptr_adv(p);
3766 }
3767 if (in_quote)
3768 bow = p;
3769 else
3770 xp->xp_pattern = p;
3771 p -= len;
3772 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003773 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
3775
3776 /*
3777 * If we are still inside the quotes, and we passed a space, just
3778 * expand from there.
3779 */
3780 if (bow != NULL && in_quote)
3781 xp->xp_pattern = bow;
3782 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003783
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003784 /* For a shell command more chars need to be escaped. */
3785 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003786 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003787#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003788 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003789#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003790 /* When still after the command name expand executables. */
3791 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003792 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794
3795 /* Check for environment variable */
3796 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003797#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 || *xp->xp_pattern == '%'
3799#endif
3800 )
3801 {
3802 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3803 if (!vim_isIDc(*p))
3804 break;
3805 if (*p == NUL)
3806 {
3807 xp->xp_context = EXPAND_ENV_VARS;
3808 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003809#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3810 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003811 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003812 compl = EXPAND_ENV_VARS;
3813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 }
3815 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003816#if defined(FEAT_CMDL_COMPL)
3817 /* Check for user names */
3818 if (*xp->xp_pattern == '~')
3819 {
3820 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3821 ;
3822 /* Complete ~user only if it partially matches a user name.
3823 * A full match ~user<Tab> will be replaced by user's home
3824 * directory i.e. something like ~user<Tab> -> /home/user/ */
3825 if (*p == NUL && p > xp->xp_pattern + 1
3826 && match_user(xp->xp_pattern + 1) == 1)
3827 {
3828 xp->xp_context = EXPAND_USER;
3829 ++xp->xp_pattern;
3830 }
3831 }
3832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
3834
3835/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003836 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003838 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003840 case CMD_find:
3841 case CMD_sfind:
3842 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003843 if (xp->xp_context == EXPAND_FILES)
3844 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003845 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 case CMD_cd:
3847 case CMD_chdir:
3848 case CMD_lcd:
3849 case CMD_lchdir:
3850 if (xp->xp_context == EXPAND_FILES)
3851 xp->xp_context = EXPAND_DIRECTORIES;
3852 break;
3853 case CMD_help:
3854 xp->xp_context = EXPAND_HELP;
3855 xp->xp_pattern = arg;
3856 break;
3857
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003858 /* Command modifiers: return the argument.
3859 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003861 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 case CMD_belowright:
3863 case CMD_botright:
3864 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003865 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003866 case CMD_cdo:
3867 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003869 case CMD_debug:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003870 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 case CMD_folddoclosed:
3872 case CMD_folddoopen:
3873 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003874 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 case CMD_keepjumps:
3876 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003877 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003878 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003880 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003882 case CMD_noautocmd:
3883 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003885 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003887 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003888 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 case CMD_topleft:
3890 case CMD_verbose:
3891 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003892 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 return arg;
3894
Bram Moolenaar4f688582007-07-24 12:34:30 +00003895#ifdef FEAT_CMDL_COMPL
3896# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 case CMD_match:
3898 if (*arg == NUL || !ends_excmd(*arg))
3899 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003900 /* also complete "None" */
3901 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 arg = skipwhite(skiptowhite(arg));
3903 if (*arg != NUL)
3904 {
3905 xp->xp_context = EXPAND_NOTHING;
3906 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3907 }
3908 }
3909 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003910# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912/*
3913 * All completion for the +cmdline_compl feature goes here.
3914 */
3915
3916# ifdef FEAT_USR_CMDS
3917 case CMD_command:
3918 /* Check for attributes */
3919 while (*arg == '-')
3920 {
3921 arg++; /* Skip "-" */
3922 p = skiptowhite(arg);
3923 if (*p == NUL)
3924 {
3925 /* Cursor is still in the attribute */
3926 p = vim_strchr(arg, '=');
3927 if (p == NULL)
3928 {
3929 /* No "=", so complete attribute names */
3930 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3931 xp->xp_pattern = arg;
3932 return NULL;
3933 }
3934
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003935 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 * their arguments as well.
3937 */
3938 if (STRNICMP(arg, "complete", p - arg) == 0)
3939 {
3940 xp->xp_context = EXPAND_USER_COMPLETE;
3941 xp->xp_pattern = p + 1;
3942 return NULL;
3943 }
3944 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3945 {
3946 xp->xp_context = EXPAND_USER_NARGS;
3947 xp->xp_pattern = p + 1;
3948 return NULL;
3949 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003950 else if (STRNICMP(arg, "addr", p - arg) == 0)
3951 {
3952 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3953 xp->xp_pattern = p + 1;
3954 return NULL;
3955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 return NULL;
3957 }
3958 arg = skipwhite(p);
3959 }
3960
3961 /* After the attributes comes the new command name */
3962 p = skiptowhite(arg);
3963 if (*p == NUL)
3964 {
3965 xp->xp_context = EXPAND_USER_COMMANDS;
3966 xp->xp_pattern = arg;
3967 break;
3968 }
3969
3970 /* And finally comes a normal command */
3971 return skipwhite(p);
3972
3973 case CMD_delcommand:
3974 xp->xp_context = EXPAND_USER_COMMANDS;
3975 xp->xp_pattern = arg;
3976 break;
3977# endif
3978
3979 case CMD_global:
3980 case CMD_vglobal:
3981 delim = *arg; /* get the delimiter */
3982 if (delim)
3983 ++arg; /* skip delimiter if there is one */
3984
3985 while (arg[0] != NUL && arg[0] != delim)
3986 {
3987 if (arg[0] == '\\' && arg[1] != NUL)
3988 ++arg;
3989 ++arg;
3990 }
3991 if (arg[0] != NUL)
3992 return arg + 1;
3993 break;
3994 case CMD_and:
3995 case CMD_substitute:
3996 delim = *arg;
3997 if (delim)
3998 {
3999 /* skip "from" part */
4000 ++arg;
4001 arg = skip_regexp(arg, delim, p_magic, NULL);
4002 }
4003 /* skip "to" part */
4004 while (arg[0] != NUL && arg[0] != delim)
4005 {
4006 if (arg[0] == '\\' && arg[1] != NUL)
4007 ++arg;
4008 ++arg;
4009 }
4010 if (arg[0] != NUL) /* skip delimiter */
4011 ++arg;
4012 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4013 ++arg;
4014 if (arg[0] != NUL)
4015 return arg;
4016 break;
4017 case CMD_isearch:
4018 case CMD_dsearch:
4019 case CMD_ilist:
4020 case CMD_dlist:
4021 case CMD_ijump:
4022 case CMD_psearch:
4023 case CMD_djump:
4024 case CMD_isplit:
4025 case CMD_dsplit:
4026 arg = skipwhite(skipdigits(arg)); /* skip count */
4027 if (*arg == '/') /* Match regexp, not just whole words */
4028 {
4029 for (++arg; *arg && *arg != '/'; arg++)
4030 if (*arg == '\\' && arg[1] != NUL)
4031 arg++;
4032 if (*arg)
4033 {
4034 arg = skipwhite(arg + 1);
4035
4036 /* Check for trailing illegal characters */
4037 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4038 xp->xp_context = EXPAND_NOTHING;
4039 else
4040 return arg;
4041 }
4042 }
4043 break;
4044#ifdef FEAT_AUTOCMD
4045 case CMD_autocmd:
4046 return set_context_in_autocmd(xp, arg, FALSE);
4047
4048 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004049 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 return set_context_in_autocmd(xp, arg, TRUE);
4051#endif
4052 case CMD_set:
4053 set_context_in_set_cmd(xp, arg, 0);
4054 break;
4055 case CMD_setglobal:
4056 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4057 break;
4058 case CMD_setlocal:
4059 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4060 break;
4061 case CMD_tag:
4062 case CMD_stag:
4063 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004064 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 case CMD_tselect:
4066 case CMD_stselect:
4067 case CMD_ptselect:
4068 case CMD_tjump:
4069 case CMD_stjump:
4070 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004071 if (*p_wop != NUL)
4072 xp->xp_context = EXPAND_TAGS_LISTFILES;
4073 else
4074 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 xp->xp_pattern = arg;
4076 break;
4077 case CMD_augroup:
4078 xp->xp_context = EXPAND_AUGROUP;
4079 xp->xp_pattern = arg;
4080 break;
4081#ifdef FEAT_SYN_HL
4082 case CMD_syntax:
4083 set_context_in_syntax_cmd(xp, arg);
4084 break;
4085#endif
4086#ifdef FEAT_EVAL
4087 case CMD_let:
4088 case CMD_if:
4089 case CMD_elseif:
4090 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004091 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 case CMD_echo:
4093 case CMD_echon:
4094 case CMD_execute:
4095 case CMD_echomsg:
4096 case CMD_echoerr:
4097 case CMD_call:
4098 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004099 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 break;
4101
4102 case CMD_unlet:
4103 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4104 arg = xp->xp_pattern + 1;
4105 xp->xp_context = EXPAND_USER_VARS;
4106 xp->xp_pattern = arg;
4107 break;
4108
4109 case CMD_function:
4110 case CMD_delfunction:
4111 xp->xp_context = EXPAND_USER_FUNC;
4112 xp->xp_pattern = arg;
4113 break;
4114
4115 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004116 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 break;
4118#endif
4119 case CMD_highlight:
4120 set_context_in_highlight_cmd(xp, arg);
4121 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004122#ifdef FEAT_CSCOPE
4123 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004124 case CMD_lcscope:
4125 case CMD_scscope:
4126 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004127 break;
4128#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004129#ifdef FEAT_SIGNS
4130 case CMD_sign:
4131 set_context_in_sign_cmd(xp, arg);
4132 break;
4133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134#ifdef FEAT_LISTCMDS
4135 case CMD_bdelete:
4136 case CMD_bwipeout:
4137 case CMD_bunload:
4138 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4139 arg = xp->xp_pattern + 1;
4140 /*FALLTHROUGH*/
4141 case CMD_buffer:
4142 case CMD_sbuffer:
4143 case CMD_checktime:
4144 xp->xp_context = EXPAND_BUFFERS;
4145 xp->xp_pattern = arg;
4146 break;
4147#endif
4148#ifdef FEAT_USR_CMDS
4149 case CMD_USER:
4150 case CMD_USER_BUF:
4151 if (compl != EXPAND_NOTHING)
4152 {
4153 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004154 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 {
4156# ifdef FEAT_MENU
4157 if (compl == EXPAND_MENUS)
4158 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4159# endif
4160 if (compl == EXPAND_COMMANDS)
4161 return arg;
4162 if (compl == EXPAND_MAPPINGS)
4163 return set_context_in_map_cmd(xp, (char_u *)"map",
4164 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004165 /* Find start of last argument. */
4166 p = arg;
4167 while (*p)
4168 {
4169 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004170 /* argument starts after a space */
4171 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004172 else if (*p == '\\' && *(p + 1) != NUL)
4173 ++p; /* skip over escaped character */
4174 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 xp->xp_pattern = arg;
4177 }
4178 xp->xp_context = compl;
4179 }
4180 break;
4181#endif
4182 case CMD_map: case CMD_noremap:
4183 case CMD_nmap: case CMD_nnoremap:
4184 case CMD_vmap: case CMD_vnoremap:
4185 case CMD_omap: case CMD_onoremap:
4186 case CMD_imap: case CMD_inoremap:
4187 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004188 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004189 case CMD_smap: case CMD_snoremap:
4190 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004192 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 case CMD_unmap:
4194 case CMD_nunmap:
4195 case CMD_vunmap:
4196 case CMD_ounmap:
4197 case CMD_iunmap:
4198 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004199 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004200 case CMD_sunmap:
4201 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004203 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 case CMD_abbreviate: case CMD_noreabbrev:
4205 case CMD_cabbrev: case CMD_cnoreabbrev:
4206 case CMD_iabbrev: case CMD_inoreabbrev:
4207 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004208 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 case CMD_unabbreviate:
4210 case CMD_cunabbrev:
4211 case CMD_iunabbrev:
4212 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004213 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214#ifdef FEAT_MENU
4215 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4216 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4217 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4218 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4219 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4220 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4221 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4222 case CMD_tmenu: case CMD_tunmenu:
4223 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4224 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4225#endif
4226
4227 case CMD_colorscheme:
4228 xp->xp_context = EXPAND_COLORS;
4229 xp->xp_pattern = arg;
4230 break;
4231
4232 case CMD_compiler:
4233 xp->xp_context = EXPAND_COMPILER;
4234 xp->xp_pattern = arg;
4235 break;
4236
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004237 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004238 xp->xp_context = EXPAND_OWNSYNTAX;
4239 xp->xp_pattern = arg;
4240 break;
4241
4242 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004243 xp->xp_context = EXPAND_FILETYPE;
4244 xp->xp_pattern = arg;
4245 break;
4246
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004247 case CMD_packadd:
4248 xp->xp_context = EXPAND_PACKADD;
4249 xp->xp_pattern = arg;
4250 break;
4251
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4253 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4254 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004255 p = skiptowhite(arg);
4256 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 {
4258 xp->xp_context = EXPAND_LANGUAGE;
4259 xp->xp_pattern = arg;
4260 }
4261 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004262 {
4263 if ( STRNCMP(arg, "messages", p - arg) == 0
4264 || STRNCMP(arg, "ctype", p - arg) == 0
4265 || STRNCMP(arg, "time", p - arg) == 0)
4266 {
4267 xp->xp_context = EXPAND_LOCALES;
4268 xp->xp_pattern = skipwhite(p);
4269 }
4270 else
4271 xp->xp_context = EXPAND_NOTHING;
4272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 break;
4274#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004275#if defined(FEAT_PROFILE)
4276 case CMD_profile:
4277 set_context_in_profile_cmd(xp, arg);
4278 break;
4279#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004280 case CMD_behave:
4281 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004282 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004283 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004285 case CMD_messages:
4286 xp->xp_context = EXPAND_MESSAGES;
4287 xp->xp_pattern = arg;
4288 break;
4289
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004290#if defined(FEAT_CMDHIST)
4291 case CMD_history:
4292 xp->xp_context = EXPAND_HISTORY;
4293 xp->xp_pattern = arg;
4294 break;
4295#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004296#if defined(FEAT_PROFILE)
4297 case CMD_syntime:
4298 xp->xp_context = EXPAND_SYNTIME;
4299 xp->xp_pattern = arg;
4300 break;
4301#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004302
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303#endif /* FEAT_CMDL_COMPL */
4304
4305 default:
4306 break;
4307 }
4308 return NULL;
4309}
4310
4311/*
4312 * skip a range specifier of the form: addr [,addr] [;addr] ..
4313 *
4314 * Backslashed delimiters after / or ? will be skipped, and commands will
4315 * not be expanded between /'s and ?'s or after "'".
4316 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004317 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 * Returns the "cmd" pointer advanced to beyond the range.
4319 */
4320 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004321skip_range(
4322 char_u *cmd,
4323 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004325 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326
Bram Moolenaardf177f62005-02-22 08:39:57 +00004327 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 {
4329 if (*cmd == '\'')
4330 {
4331 if (*++cmd == NUL && ctx != NULL)
4332 *ctx = EXPAND_NOTHING;
4333 }
4334 else if (*cmd == '/' || *cmd == '?')
4335 {
4336 delim = *cmd++;
4337 while (*cmd != NUL && *cmd != delim)
4338 if (*cmd++ == '\\' && *cmd != NUL)
4339 ++cmd;
4340 if (*cmd == NUL && ctx != NULL)
4341 *ctx = EXPAND_NOTHING;
4342 }
4343 if (*cmd != NUL)
4344 ++cmd;
4345 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004346
4347 /* Skip ":" and white space. */
4348 while (*cmd == ':')
4349 cmd = skipwhite(cmd + 1);
4350
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 return cmd;
4352}
4353
4354/*
4355 * get a single EX address
4356 *
4357 * Set ptr to the next character after the part that was interpreted.
4358 * Set ptr to NULL when an error is encountered.
4359 *
4360 * Return MAXLNUM when no Ex address was found.
4361 */
4362 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004363get_address(
4364 exarg_T *eap UNUSED,
4365 char_u **ptr,
4366 int addr_type, /* flag: one of ADDR_LINES, ... */
4367 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004368 int to_other_file, /* flag: may jump to other file */
4369 int address_count) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370{
4371 int c;
4372 int i;
4373 long n;
4374 char_u *cmd;
4375 pos_T pos;
4376 pos_T *fp;
4377 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004378 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379
4380 cmd = skipwhite(*ptr);
4381 lnum = MAXLNUM;
4382 do
4383 {
4384 switch (*cmd)
4385 {
4386 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004387 ++cmd;
4388 switch (addr_type)
4389 {
4390 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 lnum = curwin->w_cursor.lnum;
4392 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004393 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004394 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004395 break;
4396 case ADDR_ARGUMENTS:
4397 lnum = curwin->w_arg_idx + 1;
4398 break;
4399 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004400 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004401 lnum = curbuf->b_fnum;
4402 break;
4403 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004404 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004405 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004406#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004407 case ADDR_QUICKFIX:
4408 lnum = qf_get_cur_valid_idx(eap);
4409 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004410#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004411 }
4412 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413
4414 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004415 ++cmd;
4416 switch (addr_type)
4417 {
4418 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 lnum = curbuf->b_ml.ml_line_count;
4420 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004421 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004422 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004423 break;
4424 case ADDR_ARGUMENTS:
4425 lnum = ARGCOUNT;
4426 break;
4427 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004428 buf = lastbuf;
4429 while (buf->b_ml.ml_mfp == NULL)
4430 {
4431 if (buf->b_prev == NULL)
4432 break;
4433 buf = buf->b_prev;
4434 }
4435 lnum = buf->b_fnum;
4436 break;
4437 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004438 lnum = lastbuf->b_fnum;
4439 break;
4440 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004441 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004442 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004443#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004444 case ADDR_QUICKFIX:
4445 lnum = qf_get_size(eap);
4446 if (lnum == 0)
4447 lnum = 1;
4448 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004449#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004450 }
4451 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
4453 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004454 if (*++cmd == NUL)
4455 {
4456 cmd = NULL;
4457 goto error;
4458 }
4459 if (addr_type != ADDR_LINES)
4460 {
4461 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004462 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004463 goto error;
4464 }
4465 if (skip)
4466 ++cmd;
4467 else
4468 {
4469 /* Only accept a mark in another file when it is
4470 * used by itself: ":'M". */
4471 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4472 ++cmd;
4473 if (fp == (pos_T *)-1)
4474 /* Jumped to another file. */
4475 lnum = curwin->w_cursor.lnum;
4476 else
4477 {
4478 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 {
4480 cmd = NULL;
4481 goto error;
4482 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004483 lnum = fp->lnum;
4484 }
4485 }
4486 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487
4488 case '/':
4489 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004490 c = *cmd++;
4491 if (addr_type != ADDR_LINES)
4492 {
4493 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004494 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004495 goto error;
4496 }
4497 if (skip) /* skip "/pat/" */
4498 {
4499 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4500 if (*cmd == c)
4501 ++cmd;
4502 }
4503 else
4504 {
4505 pos = curwin->w_cursor; /* save curwin->w_cursor */
4506 /*
4507 * When '/' or '?' follows another address, start
4508 * from there.
4509 */
4510 if (lnum != MAXLNUM)
4511 curwin->w_cursor.lnum = lnum;
4512 /*
4513 * Start a forward search at the end of the line.
4514 * Start a backward search at the start of the line.
4515 * This makes sure we never match in the current
4516 * line, and can match anywhere in the
4517 * next/previous line.
4518 */
4519 if (c == '/')
4520 curwin->w_cursor.col = MAXCOL;
4521 else
4522 curwin->w_cursor.col = 0;
4523 searchcmdlen = 0;
4524 if (!do_search(NULL, c, cmd, 1L,
4525 SEARCH_HIS | SEARCH_MSG, NULL))
4526 {
4527 curwin->w_cursor = pos;
4528 cmd = NULL;
4529 goto error;
4530 }
4531 lnum = curwin->w_cursor.lnum;
4532 curwin->w_cursor = pos;
4533 /* adjust command string pointer */
4534 cmd += searchcmdlen;
4535 }
4536 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
4538 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004539 ++cmd;
4540 if (addr_type != ADDR_LINES)
4541 {
4542 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004543 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004544 goto error;
4545 }
4546 if (*cmd == '&')
4547 i = RE_SUBST;
4548 else if (*cmd == '?' || *cmd == '/')
4549 i = RE_SEARCH;
4550 else
4551 {
4552 EMSG(_(e_backslash));
4553 cmd = NULL;
4554 goto error;
4555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004557 if (!skip)
4558 {
4559 /*
4560 * When search follows another address, start from
4561 * there.
4562 */
4563 if (lnum != MAXLNUM)
4564 pos.lnum = lnum;
4565 else
4566 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004568 /*
4569 * Start the search just like for the above
4570 * do_search().
4571 */
4572 if (*cmd != '?')
4573 pos.col = MAXCOL;
4574 else
4575 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004576#ifdef FEAT_VIRTUALEDIT
4577 pos.coladd = 0;
4578#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004579 if (searchit(curwin, curbuf, &pos,
4580 *cmd == '?' ? BACKWARD : FORWARD,
4581 (char_u *)"", 1L, SEARCH_MSG,
4582 i, (linenr_T)0, NULL) != FAIL)
4583 lnum = pos.lnum;
4584 else
4585 {
4586 cmd = NULL;
4587 goto error;
4588 }
4589 }
4590 ++cmd;
4591 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
4593 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004594 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4595 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
4597
4598 for (;;)
4599 {
4600 cmd = skipwhite(cmd);
4601 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4602 break;
4603
4604 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004605 {
4606 switch (addr_type)
4607 {
4608 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004609 /* "+1" is same as ".+1" */
4610 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004611 break;
4612 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004613 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004614 break;
4615 case ADDR_ARGUMENTS:
4616 lnum = curwin->w_arg_idx + 1;
4617 break;
4618 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004619 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004620 lnum = curbuf->b_fnum;
4621 break;
4622 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004623 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004624 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004625#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004626 case ADDR_QUICKFIX:
4627 lnum = qf_get_cur_valid_idx(eap);
4628 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004629#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004630 }
4631 }
4632
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (VIM_ISDIGIT(*cmd))
4634 i = '+'; /* "number" is same as "+number" */
4635 else
4636 i = *cmd++;
4637 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4638 n = 1;
4639 else
4640 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004641 if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004642 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004643 lnum = compute_buffer_local_count(
4644 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004646 {
4647#ifdef FEAT_FOLDING
4648 /* Relative line addressing, need to adjust for folded lines
4649 * now, but only do it after the first address. */
4650 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4651 && address_count >= 2)
4652 (void)hasFolding(lnum, NULL, &lnum);
4653#endif
4654 if (i == '-')
4655 lnum -= n;
4656 else
4657 lnum += n;
4658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 }
4660 } while (*cmd == '/' || *cmd == '?');
4661
4662error:
4663 *ptr = cmd;
4664 return lnum;
4665}
4666
4667/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004668 * Get flags from an Ex command argument.
4669 */
4670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004671get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004672{
4673 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4674 {
4675 if (*eap->arg == 'l')
4676 eap->flags |= EXFLAG_LIST;
4677 else if (*eap->arg == 'p')
4678 eap->flags |= EXFLAG_PRINT;
4679 else
4680 eap->flags |= EXFLAG_NR;
4681 eap->arg = skipwhite(eap->arg + 1);
4682 }
4683}
4684
4685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 * Function called for command which is Not Implemented. NI!
4687 */
4688 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004689ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690{
4691 if (!eap->skip)
4692 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4693}
4694
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004695#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696/*
4697 * Function called for script command which is Not Implemented. NI!
4698 * Skips over ":perl <<EOF" constructs.
4699 */
4700 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004701ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702{
4703 if (!eap->skip)
4704 ex_ni(eap);
4705 else
4706 vim_free(script_get(eap, eap->arg));
4707}
4708#endif
4709
4710/*
4711 * Check range in Ex command for validity.
4712 * Return NULL when valid, error message when invalid.
4713 */
4714 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004715invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004717 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 if ( eap->line1 < 0
4719 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004720 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004722
4723 if (eap->argt & RANGE)
4724 {
4725 switch(eap->addr_type)
4726 {
4727 case ADDR_LINES:
4728 if (!(eap->argt & NOTADR)
4729 && eap->line2 > curbuf->b_ml.ml_line_count
4730#ifdef FEAT_DIFF
4731 + (eap->cmdidx == CMD_diffget)
4732#endif
4733 )
4734 return (char_u *)_(e_invrange);
4735 break;
4736 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004737 /* add 1 if ARGCOUNT is 0 */
4738 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004739 return (char_u *)_(e_invrange);
4740 break;
4741 case ADDR_BUFFERS:
4742 if (eap->line1 < firstbuf->b_fnum
4743 || eap->line2 > lastbuf->b_fnum)
4744 return (char_u *)_(e_invrange);
4745 break;
4746 case ADDR_LOADED_BUFFERS:
4747 buf = firstbuf;
4748 while (buf->b_ml.ml_mfp == NULL)
4749 {
4750 if (buf->b_next == NULL)
4751 return (char_u *)_(e_invrange);
4752 buf = buf->b_next;
4753 }
4754 if (eap->line1 < buf->b_fnum)
4755 return (char_u *)_(e_invrange);
4756 buf = lastbuf;
4757 while (buf->b_ml.ml_mfp == NULL)
4758 {
4759 if (buf->b_prev == NULL)
4760 return (char_u *)_(e_invrange);
4761 buf = buf->b_prev;
4762 }
4763 if (eap->line2 > buf->b_fnum)
4764 return (char_u *)_(e_invrange);
4765 break;
4766 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004767 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004768 return (char_u *)_(e_invrange);
4769 break;
4770 case ADDR_TABS:
4771 if (eap->line2 > LAST_TAB_NR)
4772 return (char_u *)_(e_invrange);
4773 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004774#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004775 case ADDR_QUICKFIX:
4776 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4777 return (char_u *)_(e_invrange);
4778 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004779#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004780 }
4781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 return NULL;
4783}
4784
4785/*
4786 * Correct the range for zero line number, if required.
4787 */
4788 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004789correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790{
4791 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4792 {
4793 if (eap->line1 == 0)
4794 eap->line1 = 1;
4795 if (eap->line2 == 0)
4796 eap->line2 = 1;
4797 }
4798}
4799
Bram Moolenaar748bf032005-02-02 23:04:36 +00004800#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004801static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004802
4803/*
4804 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4805 * pattern. Otherwise return eap->arg.
4806 */
4807 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004808skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004809{
4810 char_u *p = eap->arg;
4811
Bram Moolenaara37420f2006-02-04 22:37:47 +00004812 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4813 || eap->cmdidx == CMD_vimgrepadd
4814 || eap->cmdidx == CMD_lvimgrepadd
4815 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004816 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004817 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004818 if (p == NULL)
4819 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004820 }
4821 return p;
4822}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004823
4824/*
4825 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4826 * in the command line, so that things like % get expanded.
4827 */
4828 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004829replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004830{
4831 char_u *new_cmdline;
4832 char_u *program;
4833 char_u *pos;
4834 char_u *ptr;
4835 int len;
4836 int i;
4837
4838 /*
4839 * Don't do it when ":vimgrep" is used for ":grep".
4840 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004841 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4842 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4843 || eap->cmdidx == CMD_grepadd
4844 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004845 && !grep_internal(eap->cmdidx))
4846 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004847 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4848 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004849 {
4850 if (*curbuf->b_p_gp == NUL)
4851 program = p_gp;
4852 else
4853 program = curbuf->b_p_gp;
4854 }
4855 else
4856 {
4857 if (*curbuf->b_p_mp == NUL)
4858 program = p_mp;
4859 else
4860 program = curbuf->b_p_mp;
4861 }
4862
4863 p = skipwhite(p);
4864
4865 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4866 {
4867 /* replace $* by given arguments */
4868 i = 1;
4869 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4870 ++i;
4871 len = (int)STRLEN(p);
4872 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4873 if (new_cmdline == NULL)
4874 return NULL; /* out of memory */
4875 ptr = new_cmdline;
4876 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4877 {
4878 i = (int)(pos - program);
4879 STRNCPY(ptr, program, i);
4880 STRCPY(ptr += i, p);
4881 ptr += len;
4882 program = pos + 2;
4883 }
4884 STRCPY(ptr, program);
4885 }
4886 else
4887 {
4888 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4889 if (new_cmdline == NULL)
4890 return NULL; /* out of memory */
4891 STRCPY(new_cmdline, program);
4892 STRCAT(new_cmdline, " ");
4893 STRCAT(new_cmdline, p);
4894 }
4895 msg_make(p);
4896
4897 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4898 vim_free(*cmdlinep);
4899 *cmdlinep = new_cmdline;
4900 p = new_cmdline;
4901 }
4902 return p;
4903}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004904#endif
4905
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906/*
4907 * Expand file name in Ex command argument.
4908 * Return FAIL for failure, OK otherwise.
4909 */
4910 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004911expand_filename(
4912 exarg_T *eap,
4913 char_u **cmdlinep,
4914 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915{
4916 int has_wildcards; /* need to expand wildcards */
4917 char_u *repl;
4918 int srclen;
4919 char_u *p;
4920 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004921 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922
Bram Moolenaar748bf032005-02-02 23:04:36 +00004923#ifdef FEAT_QUICKFIX
4924 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4925 p = skip_grep_pat(eap);
4926#else
4927 p = eap->arg;
4928#endif
4929
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 /*
4931 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4932 * the file name contains a wildcard it should not cause expanding.
4933 * (it will be expanded anyway if there is a wildcard before replacing).
4934 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004935 has_wildcards = mch_has_wildcard(p);
4936 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004938#ifdef FEAT_EVAL
4939 /* Skip over `=expr`, wildcards in it are not expanded. */
4940 if (p[0] == '`' && p[1] == '=')
4941 {
4942 p += 2;
4943 (void)skip_expr(&p);
4944 if (*p == '`')
4945 ++p;
4946 continue;
4947 }
4948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 /*
4950 * Quick check if this cannot be the start of a special string.
4951 * Also removes backslash before '%', '#' and '<'.
4952 */
4953 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4954 {
4955 ++p;
4956 continue;
4957 }
4958
4959 /*
4960 * Try to find a match at this position.
4961 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004962 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4963 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 if (*errormsgp != NULL) /* error detected */
4965 return FAIL;
4966 if (repl == NULL) /* no match found */
4967 {
4968 p += srclen;
4969 continue;
4970 }
4971
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004972 /* Wildcards won't be expanded below, the replacement is taken
4973 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4974 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4975 {
4976 char_u *l = repl;
4977
4978 repl = expand_env_save(repl);
4979 vim_free(l);
4980 }
4981
Bram Moolenaar63b92542007-03-27 14:57:09 +00004982 /* Need to escape white space et al. with a backslash.
4983 * Don't do this for:
4984 * - replacement that already has been escaped: "##"
4985 * - shell commands (may have to use quotes instead).
4986 * - non-unix systems when there is a single argument (spaces don't
4987 * separate arguments then).
4988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004990 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 && eap->cmdidx != CMD_bang
4992 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004993 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004995 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004997 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998#ifndef UNIX
4999 && !(eap->argt & NOSPC)
5000#endif
5001 )
5002 {
5003 char_u *l;
5004#ifdef BACKSLASH_IN_FILENAME
5005 /* Don't escape a backslash here, because rem_backslash() doesn't
5006 * remove it later. */
5007 static char_u *nobslash = (char_u *)" \t\"|";
5008# define ESCAPE_CHARS nobslash
5009#else
5010# define ESCAPE_CHARS escape_chars
5011#endif
5012
5013 for (l = repl; *l; ++l)
5014 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5015 {
5016 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5017 if (l != NULL)
5018 {
5019 vim_free(repl);
5020 repl = l;
5021 }
5022 break;
5023 }
5024 }
5025
5026 /* For a shell command a '!' must be escaped. */
5027 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005028 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 {
5030 char_u *l;
5031
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005032 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 if (l != NULL)
5034 {
5035 vim_free(repl);
5036 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
5038 }
5039
5040 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5041 vim_free(repl);
5042 if (p == NULL)
5043 return FAIL;
5044 }
5045
5046 /*
5047 * One file argument: Expand wildcards.
5048 * Don't do this with ":r !command" or ":w !command".
5049 */
5050 if ((eap->argt & NOSPC) && !eap->usefilter)
5051 {
5052 /*
5053 * May do this twice:
5054 * 1. Replace environment variables.
5055 * 2. Replace any other wildcards, remove backslashes.
5056 */
5057 for (n = 1; n <= 2; ++n)
5058 {
5059 if (n == 2)
5060 {
5061#ifdef UNIX
5062 /*
5063 * Only for Unix we check for more than one file name.
5064 * For other systems spaces are considered to be part
5065 * of the file name.
5066 * Only check here if there is no wildcard, otherwise
5067 * ExpandOne() will check for errors. This allows
5068 * ":e `ls ve*.c`" on Unix.
5069 */
5070 if (!has_wildcards)
5071 for (p = eap->arg; *p; ++p)
5072 {
5073 /* skip escaped characters */
5074 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5075 ++p;
5076 else if (vim_iswhite(*p))
5077 {
5078 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5079 return FAIL;
5080 }
5081 }
5082#endif
5083
5084 /*
5085 * Halve the number of backslashes (this is Vi compatible).
5086 * For Unix and OS/2, when wildcards are expanded, this is
5087 * done by ExpandOne() below.
5088 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005089#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 if (!has_wildcards)
5091#endif
5092 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
5094
5095 if (has_wildcards)
5096 {
5097 if (n == 1)
5098 {
5099 /*
5100 * First loop: May expand environment variables. This
5101 * can be done much faster with expand_env() than with
5102 * something else (e.g., calling a shell).
5103 * After expanding environment variables, check again
5104 * if there are still wildcards present.
5105 */
5106 if (vim_strchr(eap->arg, '$') != NULL
5107 || vim_strchr(eap->arg, '~') != NULL)
5108 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005109 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005110 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 has_wildcards = mch_has_wildcard(NameBuff);
5112 p = NameBuff;
5113 }
5114 else
5115 p = NULL;
5116 }
5117 else /* n == 2 */
5118 {
5119 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005120 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121
5122 ExpandInit(&xpc);
5123 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005124 if (p_wic)
5125 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005127 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 if (p == NULL)
5129 return FAIL;
5130 }
5131 if (p != NULL)
5132 {
5133 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5134 p, cmdlinep);
5135 if (n == 2) /* p came from ExpandOne() */
5136 vim_free(p);
5137 }
5138 }
5139 }
5140 }
5141 return OK;
5142}
5143
5144/*
5145 * Replace part of the command line, keeping eap->cmd, eap->arg and
5146 * eap->nextcmd correct.
5147 * "src" points to the part that is to be replaced, of length "srclen".
5148 * "repl" is the replacement string.
5149 * Returns a pointer to the character after the replaced string.
5150 * Returns NULL for failure.
5151 */
5152 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005153repl_cmdline(
5154 exarg_T *eap,
5155 char_u *src,
5156 int srclen,
5157 char_u *repl,
5158 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159{
5160 int len;
5161 int i;
5162 char_u *new_cmdline;
5163
5164 /*
5165 * The new command line is build in new_cmdline[].
5166 * First allocate it.
5167 * Careful: a "+cmd" argument may have been NUL terminated.
5168 */
5169 len = (int)STRLEN(repl);
5170 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005171 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5173 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5174 return NULL; /* out of memory! */
5175
5176 /*
5177 * Copy the stuff before the expanded part.
5178 * Copy the expanded stuff.
5179 * Copy what came after the expanded part.
5180 * Copy the next commands, if there are any.
5181 */
5182 i = (int)(src - *cmdlinep); /* length of part before match */
5183 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005184
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 mch_memmove(new_cmdline + i, repl, (size_t)len);
5186 i += len; /* remember the end of the string */
5187 STRCPY(new_cmdline + i, src + srclen);
5188 src = new_cmdline + i; /* remember where to continue */
5189
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005190 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 {
5192 i = (int)STRLEN(new_cmdline) + 1;
5193 STRCPY(new_cmdline + i, eap->nextcmd);
5194 eap->nextcmd = new_cmdline + i;
5195 }
5196 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5197 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5198 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5199 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5200 vim_free(*cmdlinep);
5201 *cmdlinep = new_cmdline;
5202
5203 return src;
5204}
5205
5206/*
5207 * Check for '|' to separate commands and '"' to start comments.
5208 */
5209 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005210separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211{
5212 char_u *p;
5213
Bram Moolenaar86b68352004-12-27 21:59:20 +00005214#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005215 p = skip_grep_pat(eap);
5216#else
5217 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005218#endif
5219
5220 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {
5222 if (*p == Ctrl_V)
5223 {
5224 if (eap->argt & (USECTRLV | XFILE))
5225 ++p; /* skip CTRL-V and next char */
5226 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005227 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005228 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 if (*p == NUL) /* stop at NUL after CTRL-V */
5230 break;
5231 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005232
5233#ifdef FEAT_EVAL
5234 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005235 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005236 {
5237 p += 2;
5238 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005239 }
5240#endif
5241
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 /* Check for '"': start of comment or '|': next command */
5243 /* :@" and :*" do not start a comment!
5244 * :redir @" doesn't either. */
5245 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5246 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5247 || p != eap->arg)
5248 && (eap->cmdidx != CMD_redir
5249 || p != eap->arg + 1 || p[-1] != '@'))
5250 || *p == '|' || *p == '\n')
5251 {
5252 /*
5253 * We remove the '\' before the '|', unless USECTRLV is used
5254 * AND 'b' is present in 'cpoptions'.
5255 */
5256 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5257 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5258 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005259 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 --p;
5261 }
5262 else
5263 {
5264 eap->nextcmd = check_nextcmd(p);
5265 *p = NUL;
5266 break;
5267 }
5268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5272 del_trailing_spaces(eap->arg);
5273}
5274
5275/*
5276 * get + command from ex argument
5277 */
5278 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005279getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280{
5281 char_u *arg = *argp;
5282 char_u *command = NULL;
5283
5284 if (*arg == '+') /* +[command] */
5285 {
5286 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005287 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 command = dollar_command;
5289 else
5290 {
5291 command = arg;
5292 arg = skip_cmd_arg(command, TRUE);
5293 if (*arg != NUL)
5294 *arg++ = NUL; /* terminate command with NUL */
5295 }
5296
5297 arg = skipwhite(arg); /* skip over spaces */
5298 *argp = arg;
5299 }
5300 return command;
5301}
5302
5303/*
5304 * Find end of "+command" argument. Skip over "\ " and "\\".
5305 */
5306 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005307skip_cmd_arg(
5308 char_u *p,
5309 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310{
5311 while (*p && !vim_isspace(*p))
5312 {
5313 if (*p == '\\' && p[1] != NUL)
5314 {
5315 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005316 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 else
5318 ++p;
5319 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005320 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 }
5322 return p;
5323}
5324
5325/*
5326 * Get "++opt=arg" argument.
5327 * Return FAIL or OK.
5328 */
5329 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005330getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331{
5332 char_u *arg = eap->arg + 2;
5333 int *pp = NULL;
5334#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005335 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 char_u *p;
5337#endif
5338
5339 /* ":edit ++[no]bin[ary] file" */
5340 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5341 {
5342 if (*arg == 'n')
5343 {
5344 arg += 2;
5345 eap->force_bin = FORCE_NOBIN;
5346 }
5347 else
5348 eap->force_bin = FORCE_BIN;
5349 if (!checkforcmd(&arg, "binary", 3))
5350 return FAIL;
5351 eap->arg = skipwhite(arg);
5352 return OK;
5353 }
5354
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005355 /* ":read ++edit file" */
5356 if (STRNCMP(arg, "edit", 4) == 0)
5357 {
5358 eap->read_edit = TRUE;
5359 eap->arg = skipwhite(arg + 4);
5360 return OK;
5361 }
5362
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 if (STRNCMP(arg, "ff", 2) == 0)
5364 {
5365 arg += 2;
5366 pp = &eap->force_ff;
5367 }
5368 else if (STRNCMP(arg, "fileformat", 10) == 0)
5369 {
5370 arg += 10;
5371 pp = &eap->force_ff;
5372 }
5373#ifdef FEAT_MBYTE
5374 else if (STRNCMP(arg, "enc", 3) == 0)
5375 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005376 if (STRNCMP(arg, "encoding", 8) == 0)
5377 arg += 8;
5378 else
5379 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 pp = &eap->force_enc;
5381 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005382 else if (STRNCMP(arg, "bad", 3) == 0)
5383 {
5384 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005385 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387#endif
5388
5389 if (pp == NULL || *arg != '=')
5390 return FAIL;
5391
5392 ++arg;
5393 *pp = (int)(arg - eap->cmd);
5394 arg = skip_cmd_arg(arg, FALSE);
5395 eap->arg = skipwhite(arg);
5396 *arg = NUL;
5397
5398#ifdef FEAT_MBYTE
5399 if (pp == &eap->force_ff)
5400 {
5401#endif
5402 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5403 return FAIL;
5404#ifdef FEAT_MBYTE
5405 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005406 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 {
5408 /* Make 'fileencoding' lower case. */
5409 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5410 *p = TOLOWER_ASC(*p);
5411 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005412 else
5413 {
5414 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5415 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005416 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005417 if (STRICMP(p, "keep") == 0)
5418 eap->bad_char = BAD_KEEP;
5419 else if (STRICMP(p, "drop") == 0)
5420 eap->bad_char = BAD_DROP;
5421 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5422 eap->bad_char = *p;
5423 else
5424 return FAIL;
5425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426#endif
5427
5428 return OK;
5429}
5430
5431/*
5432 * ":abbreviate" and friends.
5433 */
5434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005435ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436{
5437 do_exmap(eap, TRUE); /* almost the same as mapping */
5438}
5439
5440/*
5441 * ":map" and friends.
5442 */
5443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005444ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445{
5446 /*
5447 * If we are sourcing .exrc or .vimrc in current directory we
5448 * print the mappings for security reasons.
5449 */
5450 if (secure)
5451 {
5452 secure = 2;
5453 msg_outtrans(eap->cmd);
5454 msg_putchar('\n');
5455 }
5456 do_exmap(eap, FALSE);
5457}
5458
5459/*
5460 * ":unmap" and friends.
5461 */
5462 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005463ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464{
5465 do_exmap(eap, FALSE);
5466}
5467
5468/*
5469 * ":mapclear" and friends.
5470 */
5471 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005472ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473{
5474 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5475}
5476
5477/*
5478 * ":abclear" and friends.
5479 */
5480 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005481ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482{
5483 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5484}
5485
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005486#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005488ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489{
5490 /*
5491 * Disallow auto commands from .exrc and .vimrc in current
5492 * directory for security reasons.
5493 */
5494 if (secure)
5495 {
5496 secure = 2;
5497 eap->errmsg = e_curdir;
5498 }
5499 else if (eap->cmdidx == CMD_autocmd)
5500 do_autocmd(eap->arg, eap->forceit);
5501 else
5502 do_augroup(eap->arg, eap->forceit);
5503}
5504
5505/*
5506 * ":doautocmd": Apply the automatic commands to the current buffer.
5507 */
5508 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005509ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005511 char_u *arg = eap->arg;
5512 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005513 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005514
Bram Moolenaar1610d052016-06-09 22:53:01 +02005515 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5516 /* Only when there is no <nomodeline>. */
5517 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005518 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519}
5520#endif
5521
5522#ifdef FEAT_LISTCMDS
5523/*
5524 * :[N]bunload[!] [N] [bufname] unload buffer
5525 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5526 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5527 */
5528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005529ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530{
5531 eap->errmsg = do_bufdel(
5532 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5533 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5534 : DOBUF_UNLOAD, eap->arg,
5535 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5536}
5537
5538/*
5539 * :[N]buffer [N] to buffer N
5540 * :[N]sbuffer [N] to buffer N
5541 */
5542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005543ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544{
5545 if (*eap->arg)
5546 eap->errmsg = e_trailing;
5547 else
5548 {
5549 if (eap->addr_count == 0) /* default is current buffer */
5550 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5551 else
5552 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005553 if (eap->do_ecmd_cmd != NULL)
5554 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 }
5556}
5557
5558/*
5559 * :[N]bmodified [N] to next mod. buffer
5560 * :[N]sbmodified [N] to next mod. buffer
5561 */
5562 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005563ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564{
5565 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005566 if (eap->do_ecmd_cmd != NULL)
5567 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568}
5569
5570/*
5571 * :[N]bnext [N] to next buffer
5572 * :[N]sbnext [N] split and to next buffer
5573 */
5574 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005575ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576{
5577 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005578 if (eap->do_ecmd_cmd != NULL)
5579 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580}
5581
5582/*
5583 * :[N]bNext [N] to previous buffer
5584 * :[N]bprevious [N] to previous buffer
5585 * :[N]sbNext [N] split and to previous buffer
5586 * :[N]sbprevious [N] split and to previous buffer
5587 */
5588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005589ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590{
5591 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005592 if (eap->do_ecmd_cmd != NULL)
5593 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594}
5595
5596/*
5597 * :brewind to first buffer
5598 * :bfirst to first buffer
5599 * :sbrewind split and to first buffer
5600 * :sbfirst split and to first buffer
5601 */
5602 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005603ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604{
5605 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005606 if (eap->do_ecmd_cmd != NULL)
5607 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608}
5609
5610/*
5611 * :blast to last buffer
5612 * :sblast split and to last buffer
5613 */
5614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005615ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616{
5617 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005618 if (eap->do_ecmd_cmd != NULL)
5619 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620}
5621#endif
5622
5623 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005624ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625{
5626 return (c == NUL || c == '|' || c == '"' || c == '\n');
5627}
5628
5629#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5630 || defined(PROTO)
5631/*
5632 * Return the next command, after the first '|' or '\n'.
5633 * Return NULL if not found.
5634 */
5635 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005636find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 while (*p != '|' && *p != '\n')
5639 {
5640 if (*p == NUL)
5641 return NULL;
5642 ++p;
5643 }
5644 return (p + 1);
5645}
5646#endif
5647
5648/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005649 * Check if *p is a separator between Ex commands, skipping over white space.
5650 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 */
5652 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005653check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005655 char_u *s = skipwhite(p);
5656
5657 if (*s == '|' || *s == '\n')
5658 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 else
5660 return NULL;
5661}
5662
5663/*
5664 * - if there are more files to edit
5665 * - and this is the last window
5666 * - and forceit not used
5667 * - and not repeated twice on a row
5668 * return FAIL and give error message if 'message' TRUE
5669 * return OK otherwise
5670 */
5671 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005672check_more(
5673 int message, /* when FALSE check only, no messages */
5674 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675{
5676 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5677
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005678 if (!forceit && only_one_window()
5679 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 {
5681 if (message)
5682 {
5683#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5684 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5685 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005686 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687
5688 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005689 vim_strncpy(buff,
5690 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005691 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005693 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 _("%d more files to edit. Quit anyway?"), n);
5695 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5696 return OK;
5697 return FAIL;
5698 }
5699#endif
5700 if (n == 1)
5701 EMSG(_("E173: 1 more file to edit"));
5702 else
5703 EMSGN(_("E173: %ld more files to edit"), n);
5704 quitmore = 2; /* next try to quit is allowed */
5705 }
5706 return FAIL;
5707 }
5708 return OK;
5709}
5710
5711#ifdef FEAT_CMDL_COMPL
5712/*
5713 * Function given to ExpandGeneric() to obtain the list of command names.
5714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005716get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717{
5718 if (idx >= (int)CMD_SIZE)
5719# ifdef FEAT_USR_CMDS
5720 return get_user_command_name(idx);
5721# else
5722 return NULL;
5723# endif
5724 return cmdnames[idx].cmd_name;
5725}
5726#endif
5727
5728#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005729static int uc_add_command(char_u *name, size_t name_len, char_u *rep, long argt, long def, int flags, int compl, char_u *compl_arg, int addr_type, int force);
5730static void uc_list(char_u *name, size_t name_len);
5731static int uc_scan_attr(char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg, int* attr_type_arg);
5732static char_u *uc_split_args(char_u *arg, size_t *lenp);
5733static size_t uc_check_code(char_u *code, size_t len, char_u *buf, ucmd_T *cmd, exarg_T *eap, char_u **split_buf, size_t *split_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
5735 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005736uc_add_command(
5737 char_u *name,
5738 size_t name_len,
5739 char_u *rep,
5740 long argt,
5741 long def,
5742 int flags,
5743 int compl,
5744 char_u *compl_arg,
5745 int addr_type,
5746 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747{
5748 ucmd_T *cmd = NULL;
5749 char_u *p;
5750 int i;
5751 int cmp = 1;
5752 char_u *rep_buf = NULL;
5753 garray_T *gap;
5754
Bram Moolenaar9c102382006-05-03 21:26:49 +00005755 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 if (rep_buf == NULL)
5757 {
5758 /* Can't replace termcodes - try using the string as is */
5759 rep_buf = vim_strsave(rep);
5760
5761 /* Give up if out of memory */
5762 if (rep_buf == NULL)
5763 return FAIL;
5764 }
5765
5766 /* get address of growarray: global or in curbuf */
5767 if (flags & UC_BUFFER)
5768 {
5769 gap = &curbuf->b_ucmds;
5770 if (gap->ga_itemsize == 0)
5771 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5772 }
5773 else
5774 gap = &ucmds;
5775
5776 /* Search for the command in the already defined commands. */
5777 for (i = 0; i < gap->ga_len; ++i)
5778 {
5779 size_t len;
5780
5781 cmd = USER_CMD_GA(gap, i);
5782 len = STRLEN(cmd->uc_name);
5783 cmp = STRNCMP(name, cmd->uc_name, name_len);
5784 if (cmp == 0)
5785 {
5786 if (name_len < len)
5787 cmp = -1;
5788 else if (name_len > len)
5789 cmp = 1;
5790 }
5791
5792 if (cmp == 0)
5793 {
5794 if (!force)
5795 {
5796 EMSG(_("E174: Command already exists: add ! to replace it"));
5797 goto fail;
5798 }
5799
5800 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005801 cmd->uc_rep = NULL;
5802#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5803 vim_free(cmd->uc_compl_arg);
5804 cmd->uc_compl_arg = NULL;
5805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 break;
5807 }
5808
5809 /* Stop as soon as we pass the name to add */
5810 if (cmp < 0)
5811 break;
5812 }
5813
5814 /* Extend the array unless we're replacing an existing command */
5815 if (cmp != 0)
5816 {
5817 if (ga_grow(gap, 1) != OK)
5818 goto fail;
5819 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5820 goto fail;
5821
5822 cmd = USER_CMD_GA(gap, i);
5823 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5824
5825 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826
5827 cmd->uc_name = p;
5828 }
5829
5830 cmd->uc_rep = rep_buf;
5831 cmd->uc_argt = argt;
5832 cmd->uc_def = def;
5833 cmd->uc_compl = compl;
5834#ifdef FEAT_EVAL
5835 cmd->uc_scriptID = current_SID;
5836# ifdef FEAT_CMDL_COMPL
5837 cmd->uc_compl_arg = compl_arg;
5838# endif
5839#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005840 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841
5842 return OK;
5843
5844fail:
5845 vim_free(rep_buf);
5846#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5847 vim_free(compl_arg);
5848#endif
5849 return FAIL;
5850}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005853#if defined(FEAT_USR_CMDS)
5854static struct
5855{
5856 int expand;
5857 char *name;
5858} addr_type_complete[] =
5859{
5860 {ADDR_ARGUMENTS, "arguments"},
5861 {ADDR_LINES, "lines"},
5862 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5863 {ADDR_TABS, "tabs"},
5864 {ADDR_BUFFERS, "buffers"},
5865 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005866 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005867 {-1, NULL}
5868};
5869#endif
5870
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005871#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872/*
5873 * List of names for completion for ":command" with the EXPAND_ flag.
5874 * Must be alphabetical for completion.
5875 */
5876static struct
5877{
5878 int expand;
5879 char *name;
5880} command_complete[] =
5881{
5882 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005883 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005885 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005887 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005888#if defined(FEAT_CSCOPE)
5889 {EXPAND_CSCOPE, "cscope"},
5890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5892 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005893 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894#endif
5895 {EXPAND_DIRECTORIES, "dir"},
5896 {EXPAND_ENV_VARS, "environment"},
5897 {EXPAND_EVENTS, "event"},
5898 {EXPAND_EXPRESSION, "expression"},
5899 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005900 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005901 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 {EXPAND_FUNCTIONS, "function"},
5903 {EXPAND_HELP, "help"},
5904 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005905#if defined(FEAT_CMDHIST)
5906 {EXPAND_HISTORY, "history"},
5907#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005908#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005909 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005910 {EXPAND_LOCALES, "locale"},
5911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 {EXPAND_MAPPINGS, "mapping"},
5913 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005914 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005915 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005916#if defined(FEAT_PROFILE)
5917 {EXPAND_SYNTIME, "syntime"},
5918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005920 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005921 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005922#if defined(FEAT_SIGNS)
5923 {EXPAND_SIGN, "sign"},
5924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {EXPAND_TAGS, "tag"},
5926 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005927 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 {EXPAND_USER_VARS, "var"},
5929 {0, NULL}
5930};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005933#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005935uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936{
5937 int i, j;
5938 int found = FALSE;
5939 ucmd_T *cmd;
5940 int len;
5941 long a;
5942 garray_T *gap;
5943
5944 gap = &curbuf->b_ucmds;
5945 for (;;)
5946 {
5947 for (i = 0; i < gap->ga_len; ++i)
5948 {
5949 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005950 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951
Bram Moolenaard29459b2016-08-26 22:29:11 +02005952 /* Skip commands which don't match the requested prefix and
5953 * commands filtered out. */
5954 if (STRNCMP(name, cmd->uc_name, name_len) != 0
5955 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 continue;
5957
5958 /* Put out the title first time */
5959 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005960 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 found = TRUE;
5962 msg_putchar('\n');
5963 if (got_int)
5964 break;
5965
5966 /* Special cases */
5967 msg_putchar(a & BANG ? '!' : ' ');
5968 msg_putchar(a & REGSTR ? '"' : ' ');
5969 msg_putchar(gap != &ucmds ? 'b' : ' ');
5970 msg_putchar(' ');
5971
5972 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5973 len = (int)STRLEN(cmd->uc_name) + 4;
5974
5975 do {
5976 msg_putchar(' ');
5977 ++len;
5978 } while (len < 16);
5979
5980 len = 0;
5981
5982 /* Arguments */
5983 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5984 {
5985 case 0: IObuff[len++] = '0'; break;
5986 case (EXTRA): IObuff[len++] = '*'; break;
5987 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5988 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5989 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5990 }
5991
5992 do {
5993 IObuff[len++] = ' ';
5994 } while (len < 5);
5995
5996 /* Range */
5997 if (a & (RANGE|COUNT))
5998 {
5999 if (a & COUNT)
6000 {
6001 /* -count=N */
6002 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6003 len += (int)STRLEN(IObuff + len);
6004 }
6005 else if (a & DFLALL)
6006 IObuff[len++] = '%';
6007 else if (cmd->uc_def >= 0)
6008 {
6009 /* -range=N */
6010 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6011 len += (int)STRLEN(IObuff + len);
6012 }
6013 else
6014 IObuff[len++] = '.';
6015 }
6016
6017 do {
6018 IObuff[len++] = ' ';
6019 } while (len < 11);
6020
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006021 /* Address Type */
6022 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6023 if (addr_type_complete[j].expand != ADDR_LINES
6024 && addr_type_complete[j].expand == cmd->uc_addr_type)
6025 {
6026 STRCPY(IObuff + len, addr_type_complete[j].name);
6027 len += (int)STRLEN(IObuff + len);
6028 break;
6029 }
6030
6031 do {
6032 IObuff[len++] = ' ';
6033 } while (len < 21);
6034
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 /* Completion */
6036 for (j = 0; command_complete[j].expand != 0; ++j)
6037 if (command_complete[j].expand == cmd->uc_compl)
6038 {
6039 STRCPY(IObuff + len, command_complete[j].name);
6040 len += (int)STRLEN(IObuff + len);
6041 break;
6042 }
6043
6044 do {
6045 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006046 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047
6048 IObuff[len] = '\0';
6049 msg_outtrans(IObuff);
6050
6051 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006052#ifdef FEAT_EVAL
6053 if (p_verbose > 0)
6054 last_set_msg(cmd->uc_scriptID);
6055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 out_flush();
6057 ui_breakcheck();
6058 if (got_int)
6059 break;
6060 }
6061 if (gap == &ucmds || i < gap->ga_len)
6062 break;
6063 gap = &ucmds;
6064 }
6065
6066 if (!found)
6067 MSG(_("No user-defined commands found"));
6068}
6069
6070 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006071uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072{
6073 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6074 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6075 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6076 0xb9, 0x7f, 0};
6077 int i;
6078
6079 for (i = 0; fcmd[i]; ++i)
6080 IObuff[i] = fcmd[i] - 0x40;
6081 IObuff[i] = 0;
6082 return IObuff;
6083}
6084
6085 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006086uc_scan_attr(
6087 char_u *attr,
6088 size_t len,
6089 long *argt,
6090 long *def,
6091 int *flags,
6092 int *compl,
6093 char_u **compl_arg,
6094 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095{
6096 char_u *p;
6097
6098 if (len == 0)
6099 {
6100 EMSG(_("E175: No attribute specified"));
6101 return FAIL;
6102 }
6103
6104 /* First, try the simple attributes (no arguments) */
6105 if (STRNICMP(attr, "bang", len) == 0)
6106 *argt |= BANG;
6107 else if (STRNICMP(attr, "buffer", len) == 0)
6108 *flags |= UC_BUFFER;
6109 else if (STRNICMP(attr, "register", len) == 0)
6110 *argt |= REGSTR;
6111 else if (STRNICMP(attr, "bar", len) == 0)
6112 *argt |= TRLBAR;
6113 else
6114 {
6115 int i;
6116 char_u *val = NULL;
6117 size_t vallen = 0;
6118 size_t attrlen = len;
6119
6120 /* Look for the attribute name - which is the part before any '=' */
6121 for (i = 0; i < (int)len; ++i)
6122 {
6123 if (attr[i] == '=')
6124 {
6125 val = &attr[i + 1];
6126 vallen = len - i - 1;
6127 attrlen = i;
6128 break;
6129 }
6130 }
6131
6132 if (STRNICMP(attr, "nargs", attrlen) == 0)
6133 {
6134 if (vallen == 1)
6135 {
6136 if (*val == '0')
6137 /* Do nothing - this is the default */;
6138 else if (*val == '1')
6139 *argt |= (EXTRA | NOSPC | NEEDARG);
6140 else if (*val == '*')
6141 *argt |= EXTRA;
6142 else if (*val == '?')
6143 *argt |= (EXTRA | NOSPC);
6144 else if (*val == '+')
6145 *argt |= (EXTRA | NEEDARG);
6146 else
6147 goto wrong_nargs;
6148 }
6149 else
6150 {
6151wrong_nargs:
6152 EMSG(_("E176: Invalid number of arguments"));
6153 return FAIL;
6154 }
6155 }
6156 else if (STRNICMP(attr, "range", attrlen) == 0)
6157 {
6158 *argt |= RANGE;
6159 if (vallen == 1 && *val == '%')
6160 *argt |= DFLALL;
6161 else if (val != NULL)
6162 {
6163 p = val;
6164 if (*def >= 0)
6165 {
6166two_count:
6167 EMSG(_("E177: Count cannot be specified twice"));
6168 return FAIL;
6169 }
6170
6171 *def = getdigits(&p);
6172 *argt |= (ZEROR | NOTADR);
6173
6174 if (p != val + vallen || vallen == 0)
6175 {
6176invalid_count:
6177 EMSG(_("E178: Invalid default value for count"));
6178 return FAIL;
6179 }
6180 }
6181 }
6182 else if (STRNICMP(attr, "count", attrlen) == 0)
6183 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006184 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185
6186 if (val != NULL)
6187 {
6188 p = val;
6189 if (*def >= 0)
6190 goto two_count;
6191
6192 *def = getdigits(&p);
6193
6194 if (p != val + vallen)
6195 goto invalid_count;
6196 }
6197
6198 if (*def < 0)
6199 *def = 0;
6200 }
6201 else if (STRNICMP(attr, "complete", attrlen) == 0)
6202 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 if (val == NULL)
6204 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006205 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 return FAIL;
6207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006209 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6210 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006213 else if (STRNICMP(attr, "addr", attrlen) == 0)
6214 {
6215 *argt |= RANGE;
6216 if (val == NULL)
6217 {
6218 EMSG(_("E179: argument required for -addr"));
6219 return FAIL;
6220 }
6221 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6222 == FAIL)
6223 return FAIL;
6224 if (addr_type_arg != ADDR_LINES)
6225 *argt |= (ZEROR | NOTADR) ;
6226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 else
6228 {
6229 char_u ch = attr[len];
6230 attr[len] = '\0';
6231 EMSG2(_("E181: Invalid attribute: %s"), attr);
6232 attr[len] = ch;
6233 return FAIL;
6234 }
6235 }
6236
6237 return OK;
6238}
6239
Bram Moolenaara850a712009-01-28 14:42:59 +00006240/*
6241 * ":command ..."
6242 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006244ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245{
6246 char_u *name;
6247 char_u *end;
6248 char_u *p;
6249 long argt = 0;
6250 long def = -1;
6251 int flags = 0;
6252 int compl = EXPAND_NOTHING;
6253 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006254 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006256 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257
6258 p = eap->arg;
6259
6260 /* Check for attributes */
6261 while (*p == '-')
6262 {
6263 ++p;
6264 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006265 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 == FAIL)
6267 return;
6268 p = skipwhite(end);
6269 }
6270
6271 /* Get the name (if any) and skip to the following argument */
6272 name = p;
6273 if (ASCII_ISALPHA(*p))
6274 while (ASCII_ISALNUM(*p))
6275 ++p;
6276 if (!ends_excmd(*p) && !vim_iswhite(*p))
6277 {
6278 EMSG(_("E182: Invalid command name"));
6279 return;
6280 }
6281 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006282 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283
6284 /* If there is nothing after the name, and no attributes were specified,
6285 * we are listing commands
6286 */
6287 p = skipwhite(end);
6288 if (!has_attr && ends_excmd(*p))
6289 {
6290 uc_list(name, end - name);
6291 }
6292 else if (!ASCII_ISUPPER(*name))
6293 {
6294 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6295 return;
6296 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006297 else if ((name_len == 1 && *name == 'X')
6298 || (name_len <= 4
6299 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6300 {
6301 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6302 return;
6303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 else
6305 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006306 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307}
6308
6309/*
6310 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 * Clear all user commands, global and for current buffer.
6312 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006313 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006314ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315{
6316 uc_clear(&ucmds);
6317 uc_clear(&curbuf->b_ucmds);
6318}
6319
6320/*
6321 * Clear all user commands for "gap".
6322 */
6323 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006324uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325{
6326 int i;
6327 ucmd_T *cmd;
6328
6329 for (i = 0; i < gap->ga_len; ++i)
6330 {
6331 cmd = USER_CMD_GA(gap, i);
6332 vim_free(cmd->uc_name);
6333 vim_free(cmd->uc_rep);
6334# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6335 vim_free(cmd->uc_compl_arg);
6336# endif
6337 }
6338 ga_clear(gap);
6339}
6340
6341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006342ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343{
6344 int i = 0;
6345 ucmd_T *cmd = NULL;
6346 int cmp = -1;
6347 garray_T *gap;
6348
6349 gap = &curbuf->b_ucmds;
6350 for (;;)
6351 {
6352 for (i = 0; i < gap->ga_len; ++i)
6353 {
6354 cmd = USER_CMD_GA(gap, i);
6355 cmp = STRCMP(eap->arg, cmd->uc_name);
6356 if (cmp <= 0)
6357 break;
6358 }
6359 if (gap == &ucmds || cmp == 0)
6360 break;
6361 gap = &ucmds;
6362 }
6363
6364 if (cmp != 0)
6365 {
6366 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6367 return;
6368 }
6369
6370 vim_free(cmd->uc_name);
6371 vim_free(cmd->uc_rep);
6372# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6373 vim_free(cmd->uc_compl_arg);
6374# endif
6375
6376 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
6378 if (i < gap->ga_len)
6379 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6380}
6381
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006382/*
6383 * split and quote args for <f-args>
6384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006386uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387{
6388 char_u *buf;
6389 char_u *p;
6390 char_u *q;
6391 int len;
6392
6393 /* Precalculate length */
6394 p = arg;
6395 len = 2; /* Initial and final quotes */
6396
6397 while (*p)
6398 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006399 if (p[0] == '\\' && p[1] == '\\')
6400 {
6401 len += 2;
6402 p += 2;
6403 }
6404 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 {
6406 len += 1;
6407 p += 2;
6408 }
6409 else if (*p == '\\' || *p == '"')
6410 {
6411 len += 2;
6412 p += 1;
6413 }
6414 else if (vim_iswhite(*p))
6415 {
6416 p = skipwhite(p);
6417 if (*p == NUL)
6418 break;
6419 len += 3; /* "," */
6420 }
6421 else
6422 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006423#ifdef FEAT_MBYTE
6424 int charlen = (*mb_ptr2len)(p);
6425 len += charlen;
6426 p += charlen;
6427#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 ++len;
6429 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 }
6432 }
6433
6434 buf = alloc(len + 1);
6435 if (buf == NULL)
6436 {
6437 *lenp = 0;
6438 return buf;
6439 }
6440
6441 p = arg;
6442 q = buf;
6443 *q++ = '"';
6444 while (*p)
6445 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006446 if (p[0] == '\\' && p[1] == '\\')
6447 {
6448 *q++ = '\\';
6449 *q++ = '\\';
6450 p += 2;
6451 }
6452 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 {
6454 *q++ = p[1];
6455 p += 2;
6456 }
6457 else if (*p == '\\' || *p == '"')
6458 {
6459 *q++ = '\\';
6460 *q++ = *p++;
6461 }
6462 else if (vim_iswhite(*p))
6463 {
6464 p = skipwhite(p);
6465 if (*p == NUL)
6466 break;
6467 *q++ = '"';
6468 *q++ = ',';
6469 *q++ = '"';
6470 }
6471 else
6472 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006473 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 }
6475 }
6476 *q++ = '"';
6477 *q = 0;
6478
6479 *lenp = len;
6480 return buf;
6481}
6482
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006483 static size_t
6484add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6485{
6486 size_t result;
6487
6488 result = STRLEN(mod_str);
6489 if (*multi_mods)
6490 result += 1;
6491 if (buf != NULL)
6492 {
6493 if (*multi_mods)
6494 STRCAT(buf, " ");
6495 STRCAT(buf, mod_str);
6496 }
6497
6498 *multi_mods = 1;
6499
6500 return result;
6501}
6502
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503/*
6504 * Check for a <> code in a user command.
6505 * "code" points to the '<'. "len" the length of the <> (inclusive).
6506 * "buf" is where the result is to be added.
6507 * "split_buf" points to a buffer used for splitting, caller should free it.
6508 * "split_len" is the length of what "split_buf" contains.
6509 * Returns the length of the replacement, which has been added to "buf".
6510 * Returns -1 if there was no match, and only the "<" has been copied.
6511 */
6512 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006513uc_check_code(
6514 char_u *code,
6515 size_t len,
6516 char_u *buf,
6517 ucmd_T *cmd, /* the user command we're expanding */
6518 exarg_T *eap, /* ex arguments */
6519 char_u **split_buf,
6520 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521{
6522 size_t result = 0;
6523 char_u *p = code + 1;
6524 size_t l = len - 2;
6525 int quote = 0;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006526 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_MODS,
6527 ct_REGISTER, ct_LT, ct_NONE } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528
6529 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6530 {
6531 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6532 p += 2;
6533 l -= 2;
6534 }
6535
Bram Moolenaar371d5402006-03-20 21:47:49 +00006536 ++l;
6537 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006539 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006541 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006543 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006545 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006547 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006549 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006551 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006553 else if (STRNICMP(p, "mods>", l) == 0)
6554 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555
6556 switch (type)
6557 {
6558 case ct_ARGS:
6559 /* Simple case first */
6560 if (*eap->arg == NUL)
6561 {
6562 if (quote == 1)
6563 {
6564 result = 2;
6565 if (buf != NULL)
6566 STRCPY(buf, "''");
6567 }
6568 else
6569 result = 0;
6570 break;
6571 }
6572
6573 /* When specified there is a single argument don't split it.
6574 * Works for ":Cmd %" when % is "a b c". */
6575 if ((eap->argt & NOSPC) && quote == 2)
6576 quote = 1;
6577
6578 switch (quote)
6579 {
6580 case 0: /* No quoting, no splitting */
6581 result = STRLEN(eap->arg);
6582 if (buf != NULL)
6583 STRCPY(buf, eap->arg);
6584 break;
6585 case 1: /* Quote, but don't split */
6586 result = STRLEN(eap->arg) + 2;
6587 for (p = eap->arg; *p; ++p)
6588 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006589#ifdef FEAT_MBYTE
6590 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6591 /* DBCS can contain \ in a trail byte, skip the
6592 * double-byte character. */
6593 ++p;
6594 else
6595#endif
6596 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 ++result;
6598 }
6599
6600 if (buf != NULL)
6601 {
6602 *buf++ = '"';
6603 for (p = eap->arg; *p; ++p)
6604 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006605#ifdef FEAT_MBYTE
6606 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6607 /* DBCS can contain \ in a trail byte, copy the
6608 * double-byte character to avoid escaping. */
6609 *buf++ = *p++;
6610 else
6611#endif
6612 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 *buf++ = '\\';
6614 *buf++ = *p;
6615 }
6616 *buf = '"';
6617 }
6618
6619 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006620 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 /* This is hard, so only do it once, and cache the result */
6622 if (*split_buf == NULL)
6623 *split_buf = uc_split_args(eap->arg, split_len);
6624
6625 result = *split_len;
6626 if (buf != NULL && result != 0)
6627 STRCPY(buf, *split_buf);
6628
6629 break;
6630 }
6631 break;
6632
6633 case ct_BANG:
6634 result = eap->forceit ? 1 : 0;
6635 if (quote)
6636 result += 2;
6637 if (buf != NULL)
6638 {
6639 if (quote)
6640 *buf++ = '"';
6641 if (eap->forceit)
6642 *buf++ = '!';
6643 if (quote)
6644 *buf = '"';
6645 }
6646 break;
6647
6648 case ct_LINE1:
6649 case ct_LINE2:
6650 case ct_COUNT:
6651 {
6652 char num_buf[20];
6653 long num = (type == ct_LINE1) ? eap->line1 :
6654 (type == ct_LINE2) ? eap->line2 :
6655 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6656 size_t num_len;
6657
6658 sprintf(num_buf, "%ld", num);
6659 num_len = STRLEN(num_buf);
6660 result = num_len;
6661
6662 if (quote)
6663 result += 2;
6664
6665 if (buf != NULL)
6666 {
6667 if (quote)
6668 *buf++ = '"';
6669 STRCPY(buf, num_buf);
6670 buf += num_len;
6671 if (quote)
6672 *buf = '"';
6673 }
6674
6675 break;
6676 }
6677
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006678 case ct_MODS:
6679 {
6680 int multi_mods = 0;
6681 typedef struct {
6682 int *varp;
6683 char *name;
6684 } mod_entry_T;
6685 static mod_entry_T mod_entries[] = {
6686#ifdef FEAT_BROWSE_CMD
6687 {&cmdmod.browse, "browse"},
6688#endif
6689#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6690 {&cmdmod.confirm, "confirm"},
6691#endif
6692 {&cmdmod.hide, "hide"},
6693 {&cmdmod.keepalt, "keepalt"},
6694 {&cmdmod.keepjumps, "keepjumps"},
6695 {&cmdmod.keepmarks, "keepmarks"},
6696 {&cmdmod.keeppatterns, "keeppatterns"},
6697 {&cmdmod.lockmarks, "lockmarks"},
6698 {&cmdmod.noswapfile, "noswapfile"},
6699 {NULL, NULL}
6700 };
6701 int i;
6702
6703 result = quote ? 2 : 0;
6704 if (buf != NULL)
6705 {
6706 if (quote)
6707 *buf++ = '"';
6708 *buf = '\0';
6709 }
6710
6711#ifdef FEAT_WINDOWS
6712 /* :aboveleft and :leftabove */
6713 if (cmdmod.split & WSP_ABOVE)
6714 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6715 /* :belowright and :rightbelow */
6716 if (cmdmod.split & WSP_BELOW)
6717 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6718 /* :botright */
6719 if (cmdmod.split & WSP_BOT)
6720 result += add_cmd_modifier(buf, "botright", &multi_mods);
6721#endif
6722
6723 /* the modifiers that are simple flags */
6724 for (i = 0; mod_entries[i].varp != NULL; ++i)
6725 if (*mod_entries[i].varp)
6726 result += add_cmd_modifier(buf, mod_entries[i].name,
6727 &multi_mods);
6728
6729 /* TODO: How to support :noautocmd? */
6730#ifdef HAVE_SANDBOX
6731 /* TODO: How to support :sandbox?*/
6732#endif
6733 /* :silent */
6734 if (msg_silent > 0)
6735 result += add_cmd_modifier(buf,
6736 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6737#ifdef FEAT_WINDOWS
6738 /* :tab */
6739 if (cmdmod.tab > 0)
6740 result += add_cmd_modifier(buf, "tab", &multi_mods);
6741 /* :topleft */
6742 if (cmdmod.split & WSP_TOP)
6743 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6744#endif
6745 /* TODO: How to support :unsilent?*/
6746 /* :verbose */
6747 if (p_verbose > 0)
6748 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6749#ifdef FEAT_WINDOWS
6750 /* :vertical */
6751 if (cmdmod.split & WSP_VERT)
6752 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6753#endif
6754 if (quote && buf != NULL)
6755 {
6756 buf += result - 2;
6757 *buf = '"';
6758 }
6759 break;
6760 }
6761
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 case ct_REGISTER:
6763 result = eap->regname ? 1 : 0;
6764 if (quote)
6765 result += 2;
6766 if (buf != NULL)
6767 {
6768 if (quote)
6769 *buf++ = '\'';
6770 if (eap->regname)
6771 *buf++ = eap->regname;
6772 if (quote)
6773 *buf = '\'';
6774 }
6775 break;
6776
6777 case ct_LT:
6778 result = 1;
6779 if (buf != NULL)
6780 *buf = '<';
6781 break;
6782
6783 default:
6784 /* Not recognized: just copy the '<' and return -1. */
6785 result = (size_t)-1;
6786 if (buf != NULL)
6787 *buf = '<';
6788 break;
6789 }
6790
6791 return result;
6792}
6793
6794 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006795do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796{
6797 char_u *buf;
6798 char_u *p;
6799 char_u *q;
6800
6801 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006802 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006803 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 size_t len, totlen;
6805
6806 size_t split_len = 0;
6807 char_u *split_buf = NULL;
6808 ucmd_T *cmd;
6809#ifdef FEAT_EVAL
6810 scid_T save_current_SID = current_SID;
6811#endif
6812
6813 if (eap->cmdidx == CMD_USER)
6814 cmd = USER_CMD(eap->useridx);
6815 else
6816 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6817
6818 /*
6819 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006820 * First round: "buf" is NULL, compute length, allocate "buf".
6821 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 */
6823 buf = NULL;
6824 for (;;)
6825 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006826 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006827 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006829
6830 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006832 start = vim_strchr(p, '<');
6833 if (start != NULL)
6834 end = vim_strchr(start + 1, '>');
6835 if (buf != NULL)
6836 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006837 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6838 ;
6839 if (*ksp == K_SPECIAL
6840 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006841 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6842# ifdef FEAT_GUI
6843 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6844# endif
6845 ))
6846 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006847 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006848 * KS_SPECIAL KE_FILLER, like for mappings, but
6849 * do_cmdline() doesn't handle that, so convert it back.
6850 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6851 len = ksp - p;
6852 if (len > 0)
6853 {
6854 mch_memmove(q, p, len);
6855 q += len;
6856 }
6857 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6858 p = ksp + 3;
6859 continue;
6860 }
6861 }
6862
6863 /* break if there no <item> is found */
6864 if (start == NULL || end == NULL)
6865 break;
6866
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 /* Include the '>' */
6868 ++end;
6869
6870 /* Take everything up to the '<' */
6871 len = start - p;
6872 if (buf == NULL)
6873 totlen += len;
6874 else
6875 {
6876 mch_memmove(q, p, len);
6877 q += len;
6878 }
6879
6880 len = uc_check_code(start, end - start, q, cmd, eap,
6881 &split_buf, &split_len);
6882 if (len == (size_t)-1)
6883 {
6884 /* no match, continue after '<' */
6885 p = start + 1;
6886 len = 1;
6887 }
6888 else
6889 p = end;
6890 if (buf == NULL)
6891 totlen += len;
6892 else
6893 q += len;
6894 }
6895 if (buf != NULL) /* second time here, finished */
6896 {
6897 STRCPY(q, p);
6898 break;
6899 }
6900
6901 totlen += STRLEN(p); /* Add on the trailing characters */
6902 buf = alloc((unsigned)(totlen + 1));
6903 if (buf == NULL)
6904 {
6905 vim_free(split_buf);
6906 return;
6907 }
6908 }
6909
6910#ifdef FEAT_EVAL
6911 current_SID = cmd->uc_scriptID;
6912#endif
6913 (void)do_cmdline(buf, eap->getline, eap->cookie,
6914 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6915#ifdef FEAT_EVAL
6916 current_SID = save_current_SID;
6917#endif
6918 vim_free(buf);
6919 vim_free(split_buf);
6920}
6921
6922# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6923 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006924get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925{
6926 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6927}
6928
6929/*
6930 * Function given to ExpandGeneric() to obtain the list of user command names.
6931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006933get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934{
6935 if (idx < curbuf->b_ucmds.ga_len)
6936 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6937 idx -= curbuf->b_ucmds.ga_len;
6938 if (idx < ucmds.ga_len)
6939 return USER_CMD(idx)->uc_name;
6940 return NULL;
6941}
6942
6943/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006944 * Function given to ExpandGeneric() to obtain the list of user address type names.
6945 */
6946 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006947get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006948{
6949 return (char_u *)addr_type_complete[idx].name;
6950}
6951
6952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 * Function given to ExpandGeneric() to obtain the list of user command
6954 * attributes.
6955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006957get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958{
6959 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006960 {"addr", "bang", "bar", "buffer", "complete",
6961 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006963 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 return NULL;
6965 return (char_u *)user_cmd_flags[idx];
6966}
6967
6968/*
6969 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006972get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973{
6974 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6975
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006976 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 return NULL;
6978 return (char_u *)user_cmd_nargs[idx];
6979}
6980
6981/*
6982 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006985get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986{
6987 return (char_u *)command_complete[idx].name;
6988}
6989# endif /* FEAT_CMDL_COMPL */
6990
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006991/*
6992 * Parse address type argument
6993 */
6994 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006995parse_addr_type_arg(
6996 char_u *value,
6997 int vallen,
6998 long *argt,
6999 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007000{
7001 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007002
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007003 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7004 {
7005 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7006 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7007 if (a && b)
7008 {
7009 *addr_type_arg = addr_type_complete[i].expand;
7010 break;
7011 }
7012 }
7013
7014 if (addr_type_complete[i].expand == -1)
7015 {
7016 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007017
7018 for (i = 0; err[i] != NUL && !vim_iswhite(err[i]); i++)
7019 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007020 err[i] = NUL;
7021 EMSG2(_("E180: Invalid address type value: %s"), err);
7022 return FAIL;
7023 }
7024
7025 if (*addr_type_arg != ADDR_LINES)
7026 *argt |= NOTADR;
7027
7028 return OK;
7029}
7030
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031#endif /* FEAT_USR_CMDS */
7032
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007033#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7034/*
7035 * Parse a completion argument "value[vallen]".
7036 * The detected completion goes in "*complp", argument type in "*argt".
7037 * When there is an argument, for function and user defined completion, it's
7038 * copied to allocated memory and stored in "*compl_arg".
7039 * Returns FAIL if something is wrong.
7040 */
7041 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007042parse_compl_arg(
7043 char_u *value,
7044 int vallen,
7045 int *complp,
7046 long *argt,
7047 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007048{
7049 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007050# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007051 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007052# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007053 int i;
7054 int valend = vallen;
7055
7056 /* Look for any argument part - which is the part after any ',' */
7057 for (i = 0; i < vallen; ++i)
7058 {
7059 if (value[i] == ',')
7060 {
7061 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007062# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007063 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007064# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007065 valend = i;
7066 break;
7067 }
7068 }
7069
7070 for (i = 0; command_complete[i].expand != 0; ++i)
7071 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007072 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007073 && STRNCMP(value, command_complete[i].name, valend) == 0)
7074 {
7075 *complp = command_complete[i].expand;
7076 if (command_complete[i].expand == EXPAND_BUFFERS)
7077 *argt |= BUFNAME;
7078 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7079 || command_complete[i].expand == EXPAND_FILES)
7080 *argt |= XFILE;
7081 break;
7082 }
7083 }
7084
7085 if (command_complete[i].expand == 0)
7086 {
7087 EMSG2(_("E180: Invalid complete value: %s"), value);
7088 return FAIL;
7089 }
7090
7091# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7092 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7093 && arg != NULL)
7094# else
7095 if (arg != NULL)
7096# endif
7097 {
7098 EMSG(_("E468: Completion argument only allowed for custom completion"));
7099 return FAIL;
7100 }
7101
7102# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7103 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7104 && arg == NULL)
7105 {
7106 EMSG(_("E467: Custom completion requires a function argument"));
7107 return FAIL;
7108 }
7109
7110 if (arg != NULL)
7111 *compl_arg = vim_strnsave(arg, (int)arglen);
7112# endif
7113 return OK;
7114}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007115
7116 int
7117cmdcomplete_str_to_type(char_u *complete_str)
7118{
7119 int i;
7120
7121 for (i = 0; command_complete[i].expand != 0; ++i)
7122 if (STRCMP(complete_str, command_complete[i].name) == 0)
7123 return command_complete[i].expand;
7124
7125 return EXPAND_NOTHING;
7126}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007127#endif
7128
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007130ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131{
Bram Moolenaare6850792010-05-14 15:28:44 +02007132 if (*eap->arg == NUL)
7133 {
7134#ifdef FEAT_EVAL
7135 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7136 char_u *p = NULL;
7137
7138 if (expr != NULL)
7139 {
7140 ++emsg_off;
7141 p = eval_to_string(expr, NULL, FALSE);
7142 --emsg_off;
7143 vim_free(expr);
7144 }
7145 if (p != NULL)
7146 {
7147 MSG(p);
7148 vim_free(p);
7149 }
7150 else
7151 MSG("default");
7152#else
7153 MSG(_("unknown"));
7154#endif
7155 }
7156 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007157 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158}
7159
7160 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007161ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162{
7163 if (*eap->arg == NUL && eap->cmd[2] == '!')
7164 MSG(_("Greetings, Vim user!"));
7165 do_highlight(eap->arg, eap->forceit, FALSE);
7166}
7167
7168
7169/*
7170 * Call this function if we thought we were going to exit, but we won't
7171 * (because of an error). May need to restore the terminal mode.
7172 */
7173 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007174not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
7176 exiting = FALSE;
7177 settmode(TMODE_RAW);
7178}
7179
7180/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007181 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 */
7183 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007184ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007186#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007187 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007188#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007189
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190#ifdef FEAT_CMDWIN
7191 if (cmdwin_type != 0)
7192 {
7193 cmdwin_result = Ctrl_C;
7194 return;
7195 }
7196#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007197 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007198 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007199 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007200 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007201 return;
7202 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007203#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007204 if (eap->addr_count > 0)
7205 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007206 int wnr = eap->line2;
7207
7208 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7209 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007210 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007211 }
7212 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007213#endif
7214#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007215 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007216#endif
7217
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007218#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007219 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007220 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007221 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007222 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007223 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007224 return;
7225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226
7227#ifdef FEAT_NETBEANS_INTG
7228 netbeansForcedQuit = eap->forceit;
7229#endif
7230
7231 /*
7232 * If there are more files or windows we won't exit.
7233 */
7234 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7235 exiting = TRUE;
7236 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007237 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7238 | (eap->forceit ? CCGD_FORCEIT : 0)
7239 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007241 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 {
7243 not_exiting();
7244 }
7245 else
7246 {
7247#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007248 /* quit last window
7249 * Note: only_one_window() returns true, even so a help window is
7250 * still open. In that case only quit, if no address has been
7251 * specified. Example:
7252 * :h|wincmd w|1q - don't quit
7253 * :h|wincmd w|q - quit
7254 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007255 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256#endif
7257 getout(0);
7258#ifdef FEAT_WINDOWS
7259# ifdef FEAT_GUI
7260 need_mouse_correct = TRUE;
7261# endif
7262 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007263 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264#endif
7265 }
7266}
7267
7268/*
7269 * ":cquit".
7270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007272ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273{
7274 getout(1); /* this does not always pass on the exit code to the Manx
7275 compiler. why? */
7276}
7277
7278/*
7279 * ":qall": try to quit all windows
7280 */
7281 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007282ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283{
7284# ifdef FEAT_CMDWIN
7285 if (cmdwin_type != 0)
7286 {
7287 if (eap->forceit)
7288 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7289 else
7290 cmdwin_result = K_XF2;
7291 return;
7292 }
7293# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007294
7295 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007296 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007297 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007298 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007299 return;
7300 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007301#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007302 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7303 /* Refuse to quit when locked or when the buffer in the last window is
7304 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007305 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007306 return;
7307#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007308
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007310 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 getout(0);
7312 not_exiting();
7313}
7314
Bram Moolenaard9967712006-03-11 21:18:15 +00007315#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316/*
7317 * ":close": close current window, unless it is the last one
7318 */
7319 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007320ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007322 win_T *win;
7323 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324# ifdef FEAT_CMDWIN
7325 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007326 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 else
7328# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007329 if (!text_locked()
7330#ifdef FEAT_AUTOCMD
7331 && !curbuf_locked()
7332#endif
7333 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007334 {
7335 if (eap->addr_count == 0)
7336 ex_win_close(eap->forceit, curwin, NULL);
7337 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007338 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007339 {
7340 winnr++;
7341 if (winnr == eap->line2)
7342 break;
7343 }
7344 if (win == NULL)
7345 win = lastwin;
7346 ex_win_close(eap->forceit, win, NULL);
7347 }
7348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349}
7350
Bram Moolenaard9967712006-03-11 21:18:15 +00007351# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007352/*
7353 * ":pclose": Close any preview window.
7354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007356ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007357{
7358 win_T *win;
7359
Bram Moolenaar29323592016-07-24 22:04:11 +02007360 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007361 if (win->w_p_pvw)
7362 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007363 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007364 break;
7365 }
7366}
Bram Moolenaard9967712006-03-11 21:18:15 +00007367# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007368
Bram Moolenaarf740b292006-02-16 22:11:02 +00007369/*
7370 * Close window "win" and take care of handling closing the last window for a
7371 * modified buffer.
7372 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007373 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007374ex_win_close(
7375 int forceit,
7376 win_T *win,
7377 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378{
7379 int need_hide;
7380 buf_T *buf = win->w_buffer;
7381
7382 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007383 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007385# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 if ((p_confirm || cmdmod.confirm) && p_write)
7387 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007388 bufref_T bufref;
7389
7390 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007392 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 return;
7394 need_hide = FALSE;
7395 }
7396 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007397# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 {
7399 EMSG(_(e_nowrtmsg));
7400 return;
7401 }
7402 }
7403
Bram Moolenaard9967712006-03-11 21:18:15 +00007404# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007406# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007407
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007409 if (tp == NULL)
7410 win_close(win, !need_hide && !P_HID(buf));
7411 else
7412 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413}
7414
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007416 * ":tabclose": close current tab page, unless it is the last one.
7417 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 */
7419 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007420ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007422 tabpage_T *tp;
7423
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007424# ifdef FEAT_CMDWIN
7425 if (cmdwin_type != 0)
7426 cmdwin_result = K_IGNORE;
7427 else
7428# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007429 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007430 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007431 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007433 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007434 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007435 tp = find_tabpage((int)eap->line2);
7436 if (tp == NULL)
7437 {
7438 beep_flush();
7439 return;
7440 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007441 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007442 {
7443 tabpage_close_other(tp, eap->forceit);
7444 return;
7445 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007446 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007447 if (!text_locked()
7448#ifdef FEAT_AUTOCMD
7449 && !curbuf_locked()
7450#endif
7451 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007452 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007454}
7455
7456/*
7457 * ":tabonly": close all tab pages except the current one
7458 */
7459 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007460ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007461{
7462 tabpage_T *tp;
7463 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007464
7465# ifdef FEAT_CMDWIN
7466 if (cmdwin_type != 0)
7467 cmdwin_result = K_IGNORE;
7468 else
7469# endif
7470 if (first_tabpage->tp_next == NULL)
7471 MSG(_("Already only one tab page"));
7472 else
7473 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007474 if (eap->addr_count > 0)
7475 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007476 /* Repeat this up to a 1000 times, because autocommands may mess
7477 * up the lists. */
7478 for (done = 0; done < 1000; ++done)
7479 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007480 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007481 if (tp->tp_topframe != topframe)
7482 {
7483 tabpage_close_other(tp, eap->forceit);
7484 /* if we failed to close it quit */
7485 if (valid_tabpage(tp))
7486 done = 1000;
7487 /* start over, "tp" is now invalid */
7488 break;
7489 }
7490 if (first_tabpage->tp_next == NULL)
7491 break;
7492 }
7493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495
7496/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007497 * Close the current tab page.
7498 */
7499 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007500tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007501{
7502 /* First close all the windows but the current one. If that worked then
7503 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007504 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007505 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007506 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007507 ex_win_close(forceit, curwin, NULL);
7508# ifdef FEAT_GUI
7509 need_mouse_correct = TRUE;
7510# endif
7511}
7512
7513/*
7514 * Close tab page "tp", which is not the current tab page.
7515 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007516 * Also takes care of the tab pages line disappearing when closing the
7517 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007518 */
7519 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007520tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007521{
7522 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007523 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007524 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007525
7526 /* Limit to 1000 windows, autocommands may add a window while we close
7527 * one. OK, so I'm paranoid... */
7528 while (++done < 1000)
7529 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007530 wp = tp->tp_firstwin;
7531 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007532
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007533 /* Autocommands may delete the tab page under our fingers and we may
7534 * fail to close a window with a modified buffer. */
7535 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007536 break;
7537 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007538
Bram Moolenaar29323592016-07-24 22:04:11 +02007539#ifdef FEAT_AUTOCMD
7540 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7541#endif
7542
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007543 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007544 if (h != tabline_height())
7545 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007546}
7547
7548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 * ":only".
7550 */
7551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007552ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007554 win_T *wp;
7555 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556# ifdef FEAT_GUI
7557 need_mouse_correct = TRUE;
7558# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007559 if (eap->addr_count > 0)
7560 {
7561 wnr = eap->line2;
7562 for (wp = firstwin; --wnr > 0; )
7563 {
7564 if (wp->w_next == NULL)
7565 break;
7566 else
7567 wp = wp->w_next;
7568 }
7569 win_goto(wp);
7570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 close_others(TRUE, eap->forceit);
7572}
7573
7574/*
7575 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007576 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007578 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007579ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580{
7581 if (eap->addr_count == 0)
7582 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007583 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584}
7585#endif /* FEAT_WINDOWS */
7586
7587 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007588ex_hide(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007590 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591#ifdef FEAT_WINDOWS
Bram Moolenaar2256c992016-11-15 21:17:07 +01007592 if (!eap->skip)
7593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594# ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007595 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596# endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007597 if (eap->addr_count == 0)
7598 win_close(curwin, FALSE); /* don't free buffer */
7599 else
7600 {
7601 int winnr = 0;
7602 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007603
Bram Moolenaar2256c992016-11-15 21:17:07 +01007604 FOR_ALL_WINDOWS(win)
7605 {
7606 winnr++;
7607 if (winnr == eap->line2)
7608 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007609 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007610 if (win == NULL)
7611 win = lastwin;
7612 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616}
7617
7618/*
7619 * ":stop" and ":suspend": Suspend Vim.
7620 */
7621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007622ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623{
7624 /*
7625 * Disallow suspending for "rvim".
7626 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007627 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 {
7629 if (!eap->forceit)
7630 autowrite_all();
7631 windgoto((int)Rows - 1, 0);
7632 out_char('\n');
7633 out_flush();
7634 stoptermcap();
7635 out_flush(); /* needed for SUN to restore xterm buffer */
7636#ifdef FEAT_TITLE
7637 mch_restore_title(3); /* restore window titles */
7638#endif
7639 ui_suspend(); /* call machine specific function */
7640#ifdef FEAT_TITLE
7641 maketitle();
7642 resettitle(); /* force updating the title */
7643#endif
7644 starttermcap();
7645 scroll_start(); /* scroll screen before redrawing */
7646 redraw_later_clear();
7647 shell_resized(); /* may have resized window */
7648 }
7649}
7650
7651/*
7652 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7653 */
7654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007655ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656{
7657#ifdef FEAT_CMDWIN
7658 if (cmdwin_type != 0)
7659 {
7660 cmdwin_result = Ctrl_C;
7661 return;
7662 }
7663#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007664 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007665 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007666 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007667 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007668 return;
7669 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007670#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007671 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7672 /* Refuse to quit when locked or when the buffer in the last window is
7673 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007674 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007675 return;
7676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677
7678 /*
7679 * if more files or windows we won't exit
7680 */
7681 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7682 exiting = TRUE;
7683 if ( ((eap->cmdidx == CMD_wq
7684 || curbufIsChanged())
7685 && do_write(eap) == FAIL)
7686 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007687 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 {
7689 not_exiting();
7690 }
7691 else
7692 {
7693#ifdef FEAT_WINDOWS
7694 if (only_one_window()) /* quit last window, exit Vim */
7695#endif
7696 getout(0);
7697#ifdef FEAT_WINDOWS
7698# ifdef FEAT_GUI
7699 need_mouse_correct = TRUE;
7700# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007701 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 win_close(curwin, !P_HID(curwin->w_buffer));
7703#endif
7704 }
7705}
7706
7707/*
7708 * ":print", ":list", ":number".
7709 */
7710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007711ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007713 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7714 EMSG(_(e_emptybuf));
7715 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007717 for ( ;!got_int; ui_breakcheck())
7718 {
7719 print_line(eap->line1,
7720 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7721 || (eap->flags & EXFLAG_NR)),
7722 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7723 if (++eap->line1 > eap->line2)
7724 break;
7725 out_flush(); /* show one line at a time */
7726 }
7727 setpcmark();
7728 /* put cursor at last line */
7729 curwin->w_cursor.lnum = eap->line2;
7730 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 }
7732
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734}
7735
7736#ifdef FEAT_BYTEOFF
7737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007738ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739{
7740 goto_byte(eap->line2);
7741}
7742#endif
7743
7744/*
7745 * ":shell".
7746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007748ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749{
7750 do_shell(NULL, 0);
7751}
7752
7753#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7754 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007755 || defined(FEAT_GUI_MSWIN) \
7756 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 || defined(PROTO)
7758
7759/*
7760 * Handle a file drop. The code is here because a drop is *nearly* like an
7761 * :args command, but not quite (we have a list of exact filenames, so we
7762 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7763 * code is very similar to :args and hence needs access to a lot of the static
7764 * functions in this file.
7765 *
7766 * The list should be allocated using alloc(), as should each item in the
7767 * list. This function takes over responsibility for freeing the list.
7768 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007769 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007770 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 */
7772 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007773handle_drop(
7774 int filec, /* the number of files dropped */
7775 char_u **filev, /* the list of files dropped */
7776 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777{
7778 exarg_T ea;
7779 int save_msg_scroll = msg_scroll;
7780
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007781 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007782 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007784#ifdef FEAT_AUTOCMD
7785 if (curbuf_locked())
7786 return;
7787#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007788 /* When the screen is being updated we should not change buffers and
7789 * windows structures, it may cause freed memory to be used. */
7790 if (updating_screen)
7791 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792
7793 /* Check whether the current buffer is changed. If so, we will need
7794 * to split the current window or data could be lost.
7795 * We don't need to check if the 'hidden' option is set, as in this
7796 * case the buffer won't be lost.
7797 */
7798 if (!P_HID(curbuf) && !split)
7799 {
7800 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007801 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 --emsg_off;
7803 }
7804 if (split)
7805 {
7806# ifdef FEAT_WINDOWS
7807 if (win_split(0, 0) == FAIL)
7808 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007809 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810
7811 /* When splitting the window, create a new alist. Otherwise the
7812 * existing one is overwritten. */
7813 alist_unlink(curwin->w_alist);
7814 alist_new();
7815# else
7816 return; /* can't split, always fail */
7817# endif
7818 }
7819
7820 /*
7821 * Set up the new argument list.
7822 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007823 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824
7825 /*
7826 * Move to the first file.
7827 */
7828 /* Fake up a minimal "next" command for do_argfile() */
7829 vim_memset(&ea, 0, sizeof(ea));
7830 ea.cmd = (char_u *)"next";
7831 do_argfile(&ea, 0);
7832
7833 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7834 * mode that is not needed here. */
7835 need_start_insertmode = FALSE;
7836
7837 /* Restore msg_scroll, otherwise a following command may cause scrolling
7838 * unexpectedly. The screen will be redrawn by the caller, thus
7839 * msg_scroll being set by displaying a message is irrelevant. */
7840 msg_scroll = save_msg_scroll;
7841}
7842#endif
7843
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844/*
7845 * Clear an argument list: free all file names and reset it to zero entries.
7846 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007847 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007848alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849{
7850 while (--al->al_ga.ga_len >= 0)
7851 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7852 ga_clear(&al->al_ga);
7853}
7854
7855/*
7856 * Init an argument list.
7857 */
7858 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007859alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860{
7861 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7862}
7863
7864#if defined(FEAT_WINDOWS) || defined(PROTO)
7865
7866/*
7867 * Remove a reference from an argument list.
7868 * Ignored when the argument list is the global one.
7869 * If the argument list is no longer used by any window, free it.
7870 */
7871 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007872alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873{
7874 if (al != &global_alist && --al->al_refcount <= 0)
7875 {
7876 alist_clear(al);
7877 vim_free(al);
7878 }
7879}
7880
7881# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7882/*
7883 * Create a new argument list and use it for the current window.
7884 */
7885 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007886alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887{
7888 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7889 if (curwin->w_alist == NULL)
7890 {
7891 curwin->w_alist = &global_alist;
7892 ++global_alist.al_refcount;
7893 }
7894 else
7895 {
7896 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007897 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 alist_init(curwin->w_alist);
7899 }
7900}
7901# endif
7902#endif
7903
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007904#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905/*
7906 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007907 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7908 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 */
7910 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007911alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912{
7913 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007914 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 char_u **new_arg_files;
7916 int new_arg_file_count;
7917 char_u *save_p_su = p_su;
7918 int i;
7919
7920 /* Don't use 'suffixes' here. This should work like the shell did the
7921 * expansion. Also, the vimrc file isn't read yet, thus the user
7922 * can't set the options. */
7923 p_su = empty_option;
7924 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7925 if (old_arg_files != NULL)
7926 {
7927 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007928 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7929 old_arg_count = GARGCOUNT;
7930 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007932 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 && new_arg_file_count > 0)
7934 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007935 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7936 TRUE, fnum_list, fnum_len);
7937 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 }
7940 p_su = save_p_su;
7941}
7942#endif
7943
7944/*
7945 * Set the argument list for the current window.
7946 * Takes over the allocated files[] and the allocated fnames in it.
7947 */
7948 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007949alist_set(
7950 alist_T *al,
7951 int count,
7952 char_u **files,
7953 int use_curbuf,
7954 int *fnum_list,
7955 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956{
7957 int i;
7958
7959 alist_clear(al);
7960 if (ga_grow(&al->al_ga, count) == OK)
7961 {
7962 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007963 {
7964 if (got_int)
7965 {
7966 /* When adding many buffers this can take a long time. Allow
7967 * interrupting here. */
7968 while (i < count)
7969 vim_free(files[i++]);
7970 break;
7971 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007972
7973 /* May set buffer name of a buffer previously used for the
7974 * argument list, so that it's re-used by alist_add. */
7975 if (fnum_list != NULL && i < fnum_len)
7976 buf_set_name(fnum_list[i], files[i]);
7977
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007979 ui_breakcheck();
7980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 vim_free(files);
7982 }
7983 else
7984 FreeWild(count, files);
7985#ifdef FEAT_WINDOWS
7986 if (al == &global_alist)
7987#endif
7988 arg_had_last = FALSE;
7989}
7990
7991/*
7992 * Add file "fname" to argument list "al".
7993 * "fname" must have been allocated and "al" must have been checked for room.
7994 */
7995 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007996alist_add(
7997 alist_T *al,
7998 char_u *fname,
7999 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000{
8001 if (fname == NULL) /* don't add NULL file names */
8002 return;
8003#ifdef BACKSLASH_IN_FILENAME
8004 slash_adjust(fname);
8005#endif
8006 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8007 if (set_fnum > 0)
8008 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8009 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8010 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011}
8012
8013#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8014/*
8015 * Adjust slashes in file names. Called after 'shellslash' was set.
8016 */
8017 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008018alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019{
8020 int i;
8021# ifdef FEAT_WINDOWS
8022 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008023 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024# endif
8025
8026 for (i = 0; i < GARGCOUNT; ++i)
8027 if (GARGLIST[i].ae_fname != NULL)
8028 slash_adjust(GARGLIST[i].ae_fname);
8029# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00008030 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 if (wp->w_alist != &global_alist)
8032 for (i = 0; i < WARGCOUNT(wp); ++i)
8033 if (WARGLIST(wp)[i].ae_fname != NULL)
8034 slash_adjust(WARGLIST(wp)[i].ae_fname);
8035# endif
8036}
8037#endif
8038
8039/*
8040 * ":preserve".
8041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008043ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008045 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 ml_preserve(curbuf, TRUE);
8047}
8048
8049/*
8050 * ":recover".
8051 */
8052 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008053ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054{
8055 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8056 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008057 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8058 | CCGD_MULTWIN
8059 | (eap->forceit ? CCGD_FORCEIT : 0)
8060 | CCGD_EXCMD)
8061
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 && (*eap->arg == NUL
8063 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8064 ml_recover();
8065 recoverymode = FALSE;
8066}
8067
8068/*
8069 * Command modifier used in a wrong way.
8070 */
8071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008072ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073{
8074 eap->errmsg = e_invcmd;
8075}
8076
8077#ifdef FEAT_WINDOWS
8078/*
8079 * :sview [+command] file split window with new file, read-only
8080 * :split [[+command] file] split window with current or new file
8081 * :vsplit [[+command] file] split window vertically with current or new file
8082 * :new [[+command] file] split window with no or new file
8083 * :vnew [[+command] file] split vertically window with no or new file
8084 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008085 *
8086 * :tabedit open new Tab page with empty window
8087 * :tabedit [+command] file open new Tab page and edit "file"
8088 * :tabnew [[+command] file] just like :tabedit
8089 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 */
8091 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008092ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008094 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008095# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008097# endif
8098# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008100# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008102# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008104# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008106# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008108 * quickfix windows. But it's OK when doing ":tab split". */
8109 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {
8111 if (eap->cmdidx == CMD_split)
8112 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 if (eap->cmdidx == CMD_vsplit)
8114 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008116# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008118# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008119 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {
8121 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8122 FNAME_MESS, TRUE, curbuf->b_ffname);
8123 if (fname == NULL)
8124 goto theend;
8125 eap->arg = fname;
8126 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008127# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008129# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008131# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 && eap->cmdidx != CMD_new)
8135 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008136# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008137 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008138# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008139 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008140# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008141 au_has_group((char_u *)"FileExplorer"))
8142 {
8143 /* No browsing supported but we do have the file explorer:
8144 * Edit the directory. */
8145 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8146 eap->arg = (char_u *)".";
8147 }
8148 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008149# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008150 {
8151 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008153 if (fname == NULL)
8154 goto theend;
8155 eap->arg = fname;
8156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 }
8158 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008159# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008161 /*
8162 * Either open new tab page or split the window.
8163 */
8164 if (eap->cmdidx == CMD_tabedit
8165 || eap->cmdidx == CMD_tabfind
8166 || eap->cmdidx == CMD_tabnew)
8167 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008168 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8169 : eap->addr_count == 0 ? 0
8170 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008171 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008172 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008173
8174 /* set the alternate buffer for the window we came from */
8175 if (curwin != old_curwin
8176 && win_valid(old_curwin)
8177 && old_curwin->w_buffer != curbuf
8178 && !cmdmod.keepalt)
8179 old_curwin->w_alt_fnum = curbuf->b_fnum;
8180 }
8181 }
8182 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8184 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008185# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 /* Reset 'scrollbind' when editing another file, but keep it when
8187 * doing ":split" without arguments. */
8188 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008189# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008191# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008193 {
8194 RESET_BINDING(curwin);
8195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 else
8197 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008198# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 do_exedit(eap, old_curwin);
8200 }
8201
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008202# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008204# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008206# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207theend:
8208 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008209# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008211
8212/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008213 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008214 */
8215 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008216tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008217{
8218 exarg_T ea;
8219
8220 vim_memset(&ea, 0, sizeof(ea));
8221 ea.cmdidx = CMD_tabnew;
8222 ea.cmd = (char_u *)"tabn";
8223 ea.arg = (char_u *)"";
8224 ex_splitview(&ea);
8225}
8226
8227/*
8228 * :tabnext command
8229 */
8230 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008231ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008232{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008233 switch (eap->cmdidx)
8234 {
8235 case CMD_tabfirst:
8236 case CMD_tabrewind:
8237 goto_tabpage(1);
8238 break;
8239 case CMD_tablast:
8240 goto_tabpage(9999);
8241 break;
8242 case CMD_tabprevious:
8243 case CMD_tabNext:
8244 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8245 break;
8246 default: /* CMD_tabnext */
8247 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8248 break;
8249 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008250}
8251
8252/*
8253 * :tabmove command
8254 */
8255 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008256ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008257{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008258 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008259
8260 if (eap->arg && *eap->arg != NUL)
8261 {
8262 char_u *p = eap->arg;
8263 int relative = 0; /* argument +N/-N means: move N places to the
8264 * right/left relative to the current position. */
8265
8266 if (*eap->arg == '-')
8267 {
8268 relative = -1;
8269 p = eap->arg + 1;
8270 }
8271 else if (*eap->arg == '+')
8272 {
8273 relative = 1;
8274 p = eap->arg + 1;
8275 }
8276 else
8277 p = eap->arg;
8278
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008279 if (relative == 0)
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008280 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008281 if (STRCMP(p, "$") == 0)
8282 tab_number = LAST_TAB_NR;
8283 else if (p == skipdigits(p))
8284 {
8285 /* No numbers as argument. */
8286 eap->errmsg = e_invarg;
8287 return;
8288 }
8289 else
8290 tab_number = getdigits(&p);
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008291 }
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008292 else
8293 {
8294 if (*p != NUL)
8295 tab_number = getdigits(&p);
8296 else
8297 tab_number = 1;
8298 tab_number = tab_number * relative + tabpage_index(curtab);
8299 if (relative == -1)
8300 --tab_number;
8301 }
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008302 }
8303 else if (eap->addr_count != 0)
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008304 {
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008305 tab_number = eap->line2;
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008306 if (**eap->cmdlinep == '-')
8307 --tab_number;
8308 }
8309 else
8310 tab_number = LAST_TAB_NR;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008311
8312 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008313}
8314
8315/*
8316 * :tabs command: List tabs and their contents.
8317 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008319ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008320{
8321 tabpage_T *tp;
8322 win_T *wp;
8323 int tabcount = 1;
8324
8325 msg_start();
8326 msg_scroll = TRUE;
8327 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8328 {
8329 msg_putchar('\n');
8330 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8331 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8332 out_flush(); /* output one line at a time */
8333 ui_breakcheck();
8334
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008335 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008336 wp = firstwin;
8337 else
8338 wp = tp->tp_firstwin;
8339 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8340 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008341 msg_putchar('\n');
8342 msg_putchar(wp == curwin ? '>' : ' ');
8343 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008344 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8345 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008346 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008347 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008348 else
8349 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8350 IObuff, IOSIZE, TRUE);
8351 msg_outtrans(IObuff);
8352 out_flush(); /* output one line at a time */
8353 ui_breakcheck();
8354 }
8355 }
8356}
8357
8358#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359
8360/*
8361 * ":mode": Set screen mode.
8362 * If no argument given, just get the screen size and redraw.
8363 */
8364 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008365ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366{
8367 if (*eap->arg == NUL)
8368 shell_resized();
8369 else
8370 mch_screenmode(eap->arg);
8371}
8372
8373#ifdef FEAT_WINDOWS
8374/*
8375 * ":resize".
8376 * set, increment or decrement current window height
8377 */
8378 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008379ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380{
8381 int n;
8382 win_T *wp = curwin;
8383
8384 if (eap->addr_count > 0)
8385 {
8386 n = eap->line2;
8387 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8388 ;
8389 }
8390
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008391# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008393# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 if (cmdmod.split & WSP_VERT)
8396 {
8397 if (*eap->arg == '-' || *eap->arg == '+')
8398 n += W_WIDTH(curwin);
8399 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8400 n = 9999;
8401 win_setwidth_win((int)n, wp);
8402 }
8403 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 {
8405 if (*eap->arg == '-' || *eap->arg == '+')
8406 n += curwin->w_height;
8407 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8408 n = 9999;
8409 win_setheight_win((int)n, wp);
8410 }
8411}
8412#endif
8413
8414/*
8415 * ":find [+command] <file>" command.
8416 */
8417 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008418ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419{
8420#ifdef FEAT_SEARCHPATH
8421 char_u *fname;
8422 int count;
8423
8424 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8425 TRUE, curbuf->b_ffname);
8426 if (eap->addr_count > 0)
8427 {
8428 /* Repeat finding the file "count" times. This matters when it
8429 * appears several times in the path. */
8430 count = eap->line2;
8431 while (fname != NULL && --count > 0)
8432 {
8433 vim_free(fname);
8434 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8435 FALSE, curbuf->b_ffname);
8436 }
8437 }
8438
8439 if (fname != NULL)
8440 {
8441 eap->arg = fname;
8442#endif
8443 do_exedit(eap, NULL);
8444#ifdef FEAT_SEARCHPATH
8445 vim_free(fname);
8446 }
8447#endif
8448}
8449
8450/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008451 * ":open" simulation: for now just work like ":visual".
8452 */
8453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008454ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008455{
8456 regmatch_T regmatch;
8457 char_u *p;
8458
8459 curwin->w_cursor.lnum = eap->line2;
8460 beginline(BL_SOL | BL_FIX);
8461 if (*eap->arg == '/')
8462 {
8463 /* ":open /pattern/": put cursor in column found with pattern */
8464 ++eap->arg;
8465 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8466 *p = NUL;
8467 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8468 if (regmatch.regprog != NULL)
8469 {
8470 regmatch.rm_ic = p_ic;
8471 p = ml_get_curline();
8472 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008473 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008474 else
8475 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008476 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008477 }
8478 /* Move to the NUL, ignore any other arguments. */
8479 eap->arg += STRLEN(eap->arg);
8480 }
8481 check_cursor();
8482
8483 eap->cmdidx = CMD_visual;
8484 do_exedit(eap, NULL);
8485}
8486
8487/*
8488 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 */
8490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008491ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492{
8493 do_exedit(eap, NULL);
8494}
8495
8496/*
8497 * ":edit <file>" command and alikes.
8498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008500do_exedit(
8501 exarg_T *eap,
8502 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503{
8504 int n;
8505#ifdef FEAT_WINDOWS
8506 int need_hide;
8507#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008508 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509
8510 /*
8511 * ":vi" command ends Ex mode.
8512 */
8513 if (exmode_active && (eap->cmdidx == CMD_visual
8514 || eap->cmdidx == CMD_view))
8515 {
8516 exmode_active = FALSE;
8517 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008518 {
8519 /* Special case: ":global/pat/visual\NLvi-commands" */
8520 if (global_busy)
8521 {
8522 int rd = RedrawingDisabled;
8523 int nwr = no_wait_return;
8524 int ms = msg_scroll;
8525#ifdef FEAT_GUI
8526 int he = hold_gui_events;
8527#endif
8528
8529 if (eap->nextcmd != NULL)
8530 {
8531 stuffReadbuff(eap->nextcmd);
8532 eap->nextcmd = NULL;
8533 }
8534
8535 if (exmode_was != EXMODE_VIM)
8536 settmode(TMODE_RAW);
8537 RedrawingDisabled = 0;
8538 no_wait_return = 0;
8539 need_wait_return = FALSE;
8540 msg_scroll = 0;
8541#ifdef FEAT_GUI
8542 hold_gui_events = 0;
8543#endif
8544 must_redraw = CLEAR;
8545
8546 main_loop(FALSE, TRUE);
8547
8548 RedrawingDisabled = rd;
8549 no_wait_return = nwr;
8550 msg_scroll = ms;
8551#ifdef FEAT_GUI
8552 hold_gui_events = he;
8553#endif
8554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 }
8558
8559 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008560 || eap->cmdidx == CMD_tabnew
8561 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008562#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 || eap->cmdidx == CMD_vnew
8564#endif
8565 ) && *eap->arg == NUL)
8566 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008567 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 setpcmark();
8569 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008570 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8571 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 }
8573 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008574#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 && eap->cmdidx != CMD_vsplit
8576#endif
8577 )
8578 || *eap->arg != NUL
8579#ifdef FEAT_BROWSE
8580 || cmdmod.browse
8581#endif
8582 )
8583 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008584#ifdef FEAT_AUTOCMD
8585 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8586 * can bring us here, others are stopped earlier. */
8587 if (*eap->arg != NUL && curbuf_locked())
8588 return;
8589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 n = readonlymode;
8591 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8592 readonlymode = TRUE;
8593 else if (eap->cmdidx == CMD_enew)
8594 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8595 empty buffer */
8596 setpcmark();
8597 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8598 NULL, eap,
8599 /* ":edit" goes to first line if Vi compatible */
8600 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8601 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8602 ? ECMD_ONE : eap->do_ecmd_lnum,
8603 (P_HID(curbuf) ? ECMD_HIDE : 0)
8604 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008605 /* after a split we can use an existing buffer */
8606 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607#ifdef FEAT_LISTCMDS
8608 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8609#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008610 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 {
8612 /* Editing the file failed. If the window was split, close it. */
8613#ifdef FEAT_WINDOWS
8614 if (old_curwin != NULL)
8615 {
8616 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8617 if (!need_hide || P_HID(curbuf))
8618 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008619# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8620 cleanup_T cs;
8621
8622 /* Reset the error/interrupt/exception state here so that
8623 * aborting() returns FALSE when closing a window. */
8624 enter_cleanup(&cs);
8625# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626# ifdef FEAT_GUI
8627 need_mouse_correct = TRUE;
8628# endif
8629 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008630
8631# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8632 /* Restore the error/interrupt/exception state if not
8633 * discarded by a new aborting error, interrupt, or
8634 * uncaught exception. */
8635 leave_cleanup(&cs);
8636# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 }
8638 }
8639#endif
8640 }
8641 else if (readonlymode && curbuf->b_nwindows == 1)
8642 {
8643 /* When editing an already visited buffer, 'readonly' won't be set
8644 * but the previous value is kept. With ":view" and ":sview" we
8645 * want the file to be readonly, except when another window is
8646 * editing the same buffer. */
8647 curbuf->b_p_ro = TRUE;
8648 }
8649 readonlymode = n;
8650 }
8651 else
8652 {
8653 if (eap->do_ecmd_cmd != NULL)
8654 do_cmdline_cmd(eap->do_ecmd_cmd);
8655#ifdef FEAT_TITLE
8656 n = curwin->w_arg_idx_invalid;
8657#endif
8658 check_arg_idx(curwin);
8659#ifdef FEAT_TITLE
8660 if (n != curwin->w_arg_idx_invalid)
8661 maketitle();
8662#endif
8663 }
8664
8665#ifdef FEAT_WINDOWS
8666 /*
8667 * if ":split file" worked, set alternate file name in old window to new
8668 * file
8669 */
8670 if (old_curwin != NULL
8671 && *eap->arg != NUL
8672 && curwin != old_curwin
8673 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008674 && old_curwin->w_buffer != curbuf
8675 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 old_curwin->w_alt_fnum = curbuf->b_fnum;
8677#endif
8678
8679 ex_no_reprint = TRUE;
8680}
8681
8682#ifndef FEAT_GUI
8683/*
8684 * ":gui" and ":gvim" when there is no GUI.
8685 */
8686 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008687ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688{
8689 eap->errmsg = e_nogvim;
8690}
8691#endif
8692
8693#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8694 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008695ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696{
8697 gui_make_tearoff(eap->arg);
8698}
8699#endif
8700
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008701#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008703ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008705 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706}
8707#endif
8708
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008710ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711{
8712 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8713 MSG(_("No swap file"));
8714 else
8715 msg(curbuf->b_ml.ml_mfp->mf_fname);
8716}
8717
8718/*
8719 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8720 * offset.
8721 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8722 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008724ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725{
8726#ifdef FEAT_SCROLLBIND
8727 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008728 win_T *save_curwin = curwin;
8729 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 long topline;
8731 long y;
8732 linenr_T old_linenr = curwin->w_cursor.lnum;
8733
8734 setpcmark();
8735
8736 /*
8737 * determine max topline
8738 */
8739 if (curwin->w_p_scb)
8740 {
8741 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008742 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 {
8744 if (wp->w_p_scb && wp->w_buffer)
8745 {
8746 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8747 if (topline > y)
8748 topline = y;
8749 }
8750 }
8751 if (topline < 1)
8752 topline = 1;
8753 }
8754 else
8755 {
8756 topline = 1;
8757 }
8758
8759
8760 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008761 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008763 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 {
8765 if (curwin->w_p_scb)
8766 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008767 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 y = topline - curwin->w_topline;
8769 if (y > 0)
8770 scrollup(y, TRUE);
8771 else
8772 scrolldown(-y, TRUE);
8773 curwin->w_scbind_pos = topline;
8774 redraw_later(VALID);
8775 cursor_correct();
8776#ifdef FEAT_WINDOWS
8777 curwin->w_redr_status = TRUE;
8778#endif
8779 }
8780 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008781 curwin = save_curwin;
8782 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 if (curwin->w_p_scb)
8784 {
8785 did_syncbind = TRUE;
8786 checkpcmark();
8787 if (old_linenr != curwin->w_cursor.lnum)
8788 {
8789 char_u ctrl_o[2];
8790
8791 ctrl_o[0] = Ctrl_O;
8792 ctrl_o[1] = 0;
8793 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8794 }
8795 }
8796#endif
8797}
8798
8799
8800 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008801ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008803 int i;
8804 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8805 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806
8807 if (eap->usefilter) /* :r!cmd */
8808 do_bang(1, eap, FALSE, FALSE, TRUE);
8809 else
8810 {
8811 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8812 return;
8813
8814#ifdef FEAT_BROWSE
8815 if (cmdmod.browse)
8816 {
8817 char_u *browseFile;
8818
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008819 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 NULL, NULL, NULL, curbuf);
8821 if (browseFile != NULL)
8822 {
8823 i = readfile(browseFile, NULL,
8824 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8825 vim_free(browseFile);
8826 }
8827 else
8828 i = OK;
8829 }
8830 else
8831#endif
8832 if (*eap->arg == NUL)
8833 {
8834 if (check_fname() == FAIL) /* check for no file name */
8835 return;
8836 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8837 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8838 }
8839 else
8840 {
8841 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8842 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8843 i = readfile(eap->arg, NULL,
8844 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8845
8846 }
8847 if (i == FAIL)
8848 {
8849#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8850 if (!aborting())
8851#endif
8852 EMSG2(_(e_notopen), eap->arg);
8853 }
8854 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008855 {
8856 if (empty && exmode_active)
8857 {
8858 /* Delete the empty line that remains. Historically ex does
8859 * this but vi doesn't. */
8860 if (eap->line2 == 0)
8861 lnum = curbuf->b_ml.ml_line_count;
8862 else
8863 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008864 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008865 {
8866 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008867 if (curwin->w_cursor.lnum > 1
8868 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008869 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008870 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008871 }
8872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 }
8876}
8877
Bram Moolenaarea408852005-06-25 22:49:46 +00008878static char_u *prev_dir = NULL;
8879
8880#if defined(EXITFREE) || defined(PROTO)
8881 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008882free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008883{
8884 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008885 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008886
8887 vim_free(globaldir);
8888 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008889}
8890#endif
8891
Bram Moolenaarf4258302013-06-02 18:20:17 +02008892/*
8893 * Deal with the side effects of changing the current directory.
8894 * When "local" is TRUE then this was after an ":lcd" command.
8895 */
8896 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008897post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008898{
8899 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008900 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008901 if (local)
8902 {
8903 /* If still in global directory, need to remember current
8904 * directory as global directory. */
8905 if (globaldir == NULL && prev_dir != NULL)
8906 globaldir = vim_strsave(prev_dir);
8907 /* Remember this local directory for the window. */
8908 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8909 curwin->w_localdir = vim_strsave(NameBuff);
8910 }
8911 else
8912 {
8913 /* We are now in the global directory, no need to remember its
8914 * name. */
8915 vim_free(globaldir);
8916 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008917 }
8918
8919 shorten_fnames(TRUE);
8920}
8921
Bram Moolenaarea408852005-06-25 22:49:46 +00008922
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923/*
8924 * ":cd", ":lcd", ":chdir" and ":lchdir".
8925 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008926 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008927ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 char_u *new_dir;
8930 char_u *tofree;
8931
8932 new_dir = eap->arg;
8933#if !defined(UNIX) && !defined(VMS)
8934 /* for non-UNIX ":cd" means: print current directory */
8935 if (*new_dir == NUL)
8936 ex_pwd(NULL);
8937 else
8938#endif
8939 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008940#ifdef FEAT_AUTOCMD
8941 if (allbuf_locked())
8942 return;
8943#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008944 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8945 && !eap->forceit)
8946 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008947 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008948 return;
8949 }
8950
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 /* ":cd -": Change to previous directory */
8952 if (STRCMP(new_dir, "-") == 0)
8953 {
8954 if (prev_dir == NULL)
8955 {
8956 EMSG(_("E186: No previous directory"));
8957 return;
8958 }
8959 new_dir = prev_dir;
8960 }
8961
8962 /* Save current directory for next ":cd -" */
8963 tofree = prev_dir;
8964 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8965 prev_dir = vim_strsave(NameBuff);
8966 else
8967 prev_dir = NULL;
8968
8969#if defined(UNIX) || defined(VMS)
8970 /* for UNIX ":cd" means: go to home directory */
8971 if (*new_dir == NUL)
8972 {
8973 /* use NameBuff for home directory name */
8974# ifdef VMS
8975 char_u *p;
8976
8977 p = mch_getenv((char_u *)"SYS$LOGIN");
8978 if (p == NULL || *p == NUL) /* empty is the same as not set */
8979 NameBuff[0] = NUL;
8980 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008981 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982# else
8983 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8984# endif
8985 new_dir = NameBuff;
8986 }
8987#endif
8988 if (new_dir == NULL || vim_chdir(new_dir))
8989 EMSG(_(e_failed));
8990 else
8991 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008992 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993
8994 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008995 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 ex_pwd(eap);
8997 }
8998 vim_free(tofree);
8999 }
9000}
9001
9002/*
9003 * ":pwd".
9004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009006ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007{
9008 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9009 {
9010#ifdef BACKSLASH_IN_FILENAME
9011 slash_adjust(NameBuff);
9012#endif
9013 msg(NameBuff);
9014 }
9015 else
9016 EMSG(_("E187: Unknown"));
9017}
9018
9019/*
9020 * ":=".
9021 */
9022 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009023ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024{
9025 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009026 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027}
9028
9029 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009030ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009032 int n;
9033 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034
9035 if (cursor_valid())
9036 {
9037 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9038 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01009039 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009041
9042 len = eap->line2;
9043 switch (*eap->arg)
9044 {
9045 case 'm': break;
9046 case NUL: len *= 1000L; break;
9047 default: EMSG2(_(e_invarg2), eap->arg); return;
9048 }
9049 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050}
9051
9052/*
9053 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9054 */
9055 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009056do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057{
9058 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009059 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060
9061 cursor_on();
9062 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009063 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009065 wait_now = msec - done > 1000L ? 1000L : msec - done;
9066#ifdef FEAT_TIMERS
9067 {
9068 long due_time = check_due_timer();
9069
9070 if (due_time > 0 && due_time < wait_now)
9071 wait_now = due_time;
9072 }
9073#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009074#ifdef FEAT_JOB_CHANNEL
9075 if (has_any_channel() && wait_now > 100L)
9076 wait_now = 100L;
9077#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009078 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009079#ifdef FEAT_JOB_CHANNEL
9080 if (has_any_channel())
9081 ui_breakcheck_force(TRUE);
9082 else
9083#endif
9084 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009085#ifdef MESSAGE_QUEUE
9086 /* Process the netbeans and clientserver messages that may have been
9087 * received in the call to ui_breakcheck() when the GUI is in use. This
9088 * may occur when running a test case. */
9089 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 }
9092}
9093
9094 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009095do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096{
9097 int mode;
9098 char_u *cmdp;
9099
9100 cmdp = eap->cmd;
9101 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9102
9103 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9104 eap->arg, mode, isabbrev))
9105 {
9106 case 1: EMSG(_(e_invarg));
9107 break;
9108 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9109 break;
9110 }
9111}
9112
9113/*
9114 * ":winsize" command (obsolete).
9115 */
9116 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009117ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118{
9119 int w, h;
9120 char_u *arg = eap->arg;
9121 char_u *p;
9122
9123 w = getdigits(&arg);
9124 arg = skipwhite(arg);
9125 p = arg;
9126 h = getdigits(&arg);
9127 if (*p != NUL && *arg == NUL)
9128 set_shellsize(w, h, TRUE);
9129 else
9130 EMSG(_("E465: :winsize requires two number arguments"));
9131}
9132
9133#ifdef FEAT_WINDOWS
9134 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009135ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136{
9137 int xchar = NUL;
9138 char_u *p;
9139
9140 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9141 {
9142 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9143 if (eap->arg[1] == NUL)
9144 {
9145 EMSG(_(e_invarg));
9146 return;
9147 }
9148 xchar = eap->arg[1];
9149 p = eap->arg + 2;
9150 }
9151 else
9152 p = eap->arg + 1;
9153
9154 eap->nextcmd = check_nextcmd(p);
9155 p = skipwhite(p);
9156 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9157 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009158 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 {
9160 /* Pass flags on for ":vertical wincmd ]". */
9161 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009162 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9164 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009165 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 }
9167}
9168#endif
9169
Bram Moolenaar843ee412004-06-30 16:16:41 +00009170#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171/*
9172 * ":winpos".
9173 */
9174 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009175ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176{
9177 int x, y;
9178 char_u *arg = eap->arg;
9179 char_u *p;
9180
9181 if (*arg == NUL)
9182 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009183# if defined(FEAT_GUI) || defined(MSWIN)
9184# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009186# else
9187 if (mch_get_winpos(&x, &y) != FAIL)
9188# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 {
9190 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9191 msg(IObuff);
9192 }
9193 else
9194# endif
9195 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9196 }
9197 else
9198 {
9199 x = getdigits(&arg);
9200 arg = skipwhite(arg);
9201 p = arg;
9202 y = getdigits(&arg);
9203 if (*p == NUL || *arg != NUL)
9204 {
9205 EMSG(_("E466: :winpos requires two number arguments"));
9206 return;
9207 }
9208# ifdef FEAT_GUI
9209 if (gui.in_use)
9210 gui_mch_set_winpos(x, y);
9211 else if (gui.starting)
9212 {
9213 /* Remember the coordinates for when the window is opened. */
9214 gui_win_x = x;
9215 gui_win_y = y;
9216 }
9217# ifdef HAVE_TGETENT
9218 else
9219# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009220# else
9221# ifdef MSWIN
9222 mch_set_winpos(x, y);
9223# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224# endif
9225# ifdef HAVE_TGETENT
9226 if (*T_CWP)
9227 term_set_winpos(x, y);
9228# endif
9229 }
9230}
9231#endif
9232
9233/*
9234 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9235 */
9236 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009237ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238{
9239 oparg_T oa;
9240
9241 clear_oparg(&oa);
9242 oa.regname = eap->regname;
9243 oa.start.lnum = eap->line1;
9244 oa.end.lnum = eap->line2;
9245 oa.line_count = eap->line2 - eap->line1 + 1;
9246 oa.motion_type = MLINE;
9247#ifdef FEAT_VIRTUALEDIT
9248 virtual_op = FALSE;
9249#endif
9250 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9251 {
9252 setpcmark();
9253 curwin->w_cursor.lnum = eap->line1;
9254 beginline(BL_SOL | BL_FIX);
9255 }
9256
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009257 if (VIsual_active)
9258 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009259
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 switch (eap->cmdidx)
9261 {
9262 case CMD_delete:
9263 oa.op_type = OP_DELETE;
9264 op_delete(&oa);
9265 break;
9266
9267 case CMD_yank:
9268 oa.op_type = OP_YANK;
9269 (void)op_yank(&oa, FALSE, TRUE);
9270 break;
9271
9272 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009273 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009275 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9276#else
9277 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009279 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 oa.op_type = OP_RSHIFT;
9281 else
9282 oa.op_type = OP_LSHIFT;
9283 op_shift(&oa, FALSE, eap->amount);
9284 break;
9285 }
9286#ifdef FEAT_VIRTUALEDIT
9287 virtual_op = MAYBE;
9288#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009289 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290}
9291
9292/*
9293 * ":put".
9294 */
9295 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009296ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297{
9298 /* ":0put" works like ":1put!". */
9299 if (eap->line2 == 0)
9300 {
9301 eap->line2 = 1;
9302 eap->forceit = TRUE;
9303 }
9304 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009305 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9306 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307}
9308
9309/*
9310 * Handle ":copy" and ":move".
9311 */
9312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009313ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314{
9315 long n;
9316
Bram Moolenaarded27822017-01-02 14:27:34 +01009317 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 if (eap->arg == NULL) /* error detected */
9319 {
9320 eap->nextcmd = NULL;
9321 return;
9322 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009323 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324
9325 /*
9326 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9327 */
9328 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9329 {
9330 EMSG(_(e_invaddr));
9331 return;
9332 }
9333
9334 if (eap->cmdidx == CMD_move)
9335 {
9336 if (do_move(eap->line1, eap->line2, n) == FAIL)
9337 return;
9338 }
9339 else
9340 ex_copy(eap->line1, eap->line2, n);
9341 u_clearline();
9342 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009343 ex_may_print(eap);
9344}
9345
9346/*
9347 * Print the current line if flags were given to the Ex command.
9348 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009349 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009350ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009351{
9352 if (eap->flags != 0)
9353 {
9354 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9355 (eap->flags & EXFLAG_LIST));
9356 ex_no_reprint = TRUE;
9357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358}
9359
9360/*
9361 * ":smagic" and ":snomagic".
9362 */
9363 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009364ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365{
9366 int magic_save = p_magic;
9367
9368 p_magic = (eap->cmdidx == CMD_smagic);
9369 do_sub(eap);
9370 p_magic = magic_save;
9371}
9372
9373/*
9374 * ":join".
9375 */
9376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009377ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378{
9379 curwin->w_cursor.lnum = eap->line1;
9380 if (eap->line1 == eap->line2)
9381 {
9382 if (eap->addr_count >= 2) /* :2,2join does nothing */
9383 return;
9384 if (eap->line2 == curbuf->b_ml.ml_line_count)
9385 {
9386 beep_flush();
9387 return;
9388 }
9389 ++eap->line2;
9390 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009391 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009393 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394}
9395
9396/*
9397 * ":[addr]@r" or ":[addr]*r": execute register
9398 */
9399 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009400ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401{
9402 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009403 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404
9405 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009406 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407
9408#ifdef USE_ON_FLY_SCROLL
9409 dont_scroll = TRUE; /* disallow scrolling here */
9410#endif
9411
9412 /* get the register name. No name means to use the previous one */
9413 c = *eap->arg;
9414 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9415 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009416 /* Put the register in the typeahead buffer with the "silent" flag. */
9417 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9418 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009419 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 else
9423 {
9424 int save_efr = exec_from_reg;
9425
9426 exec_from_reg = TRUE;
9427
9428 /*
9429 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009430 * Continue until the stuff buffer is empty and all added characters
9431 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009433 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9435
9436 exec_from_reg = save_efr;
9437 }
9438}
9439
9440/*
9441 * ":!".
9442 */
9443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009444ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445{
9446 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9447}
9448
9449/*
9450 * ":undo".
9451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009453ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009455 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009456 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009457 else
9458 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459}
9460
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009461#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009462 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009463ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009464{
9465 char_u hash[UNDO_HASH_SIZE];
9466
9467 u_compute_hash(hash);
9468 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9469}
9470
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009471 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009472ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009473{
9474 char_u hash[UNDO_HASH_SIZE];
9475
9476 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009477 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009478}
9479#endif
9480
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481/*
9482 * ":redo".
9483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009485ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486{
9487 u_redo(1);
9488}
9489
9490/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009491 * ":earlier" and ":later".
9492 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009493 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009494ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009495{
9496 long count = 0;
9497 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009498 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009499 char_u *p = eap->arg;
9500
9501 if (*p == NUL)
9502 count = 1;
9503 else if (isdigit(*p))
9504 {
9505 count = getdigits(&p);
9506 switch (*p)
9507 {
9508 case 's': ++p; sec = TRUE; break;
9509 case 'm': ++p; sec = TRUE; count *= 60; break;
9510 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009511 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9512 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009513 }
9514 }
9515
9516 if (*p != NUL)
9517 EMSG2(_(e_invarg2), eap->arg);
9518 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009519 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9520 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009521}
9522
9523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 * ":redir": start/stop redirection.
9525 */
9526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009527ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528{
9529 char *mode;
9530 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009531 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532
Bram Moolenaarba768492016-07-08 15:32:54 +02009533#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009534 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009535 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009536 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009537 return;
9538 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009539#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009540
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 if (STRICMP(eap->arg, "END") == 0)
9542 close_redir();
9543 else
9544 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009545 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009547 ++arg;
9548 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009550 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 mode = "a";
9552 }
9553 else
9554 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009555 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556
9557 close_redir();
9558
9559 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009560 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 if (fname == NULL)
9562 return;
9563#ifdef FEAT_BROWSE
9564 if (cmdmod.browse)
9565 {
9566 char_u *browseFile;
9567
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009568 browseFile = do_browse(BROWSE_SAVE,
9569 (char_u *)_("Save Redirection"),
9570 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 if (browseFile == NULL)
9572 return; /* operation cancelled */
9573 vim_free(fname);
9574 fname = browseFile;
9575 eap->forceit = TRUE; /* since dialog already asked */
9576 }
9577#endif
9578
9579 redir_fd = open_exfile(fname, eap->forceit, mode);
9580 vim_free(fname);
9581 }
9582#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009583 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 {
9585 /* redirect to a register a-z (resp. A-Z for appending) */
9586 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009587 ++arg;
9588 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009590 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009591 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009593 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009595 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009596 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009597 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009598 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009600 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009601 if (*arg == '>')
9602 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009603 /* Make register empty when not using @A-@Z and the
9604 * command is valid. */
9605 if (*arg == NUL && !isupper(redir_reg))
9606 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009608 }
9609 if (*arg != NUL)
9610 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009611 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009612 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009613 }
9614 }
9615 else if (*arg == '=' && arg[1] == '>')
9616 {
9617 int append;
9618
9619 /* redirect to a variable */
9620 close_redir();
9621 arg += 2;
9622
9623 if (*arg == '>')
9624 {
9625 ++arg;
9626 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 }
9628 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009629 append = FALSE;
9630
9631 if (var_redir_start(skipwhite(arg), append) == OK)
9632 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 }
9634#endif
9635
9636 /* TODO: redirect to a buffer */
9637
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009639 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009641
9642 /* Make sure redirection is not off. Can happen for cmdline completion
9643 * that indirectly invokes a command to catch its output. */
9644 if (redir_fd != NULL
9645#ifdef FEAT_EVAL
9646 || redir_reg || redir_vname
9647#endif
9648 )
9649 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650}
9651
9652/*
9653 * ":redraw": force redraw
9654 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009655 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009656ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657{
9658 int r = RedrawingDisabled;
9659 int p = p_lz;
9660
9661 RedrawingDisabled = 0;
9662 p_lz = FALSE;
9663 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009664 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665#ifdef FEAT_TITLE
9666 if (need_maketitle)
9667 maketitle();
9668#endif
9669 RedrawingDisabled = r;
9670 p_lz = p;
9671
9672 /* Reset msg_didout, so that a message that's there is overwritten. */
9673 msg_didout = FALSE;
9674 msg_col = 0;
9675
Bram Moolenaar56667a52013-07-17 11:54:28 +02009676 /* No need to wait after an intentional redraw. */
9677 need_wait_return = FALSE;
9678
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 out_flush();
9680}
9681
9682/*
9683 * ":redrawstatus": force redraw of status line(s)
9684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009686ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687{
9688#if defined(FEAT_WINDOWS)
9689 int r = RedrawingDisabled;
9690 int p = p_lz;
9691
9692 RedrawingDisabled = 0;
9693 p_lz = FALSE;
9694 if (eap->forceit)
9695 status_redraw_all();
9696 else
9697 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009698 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 RedrawingDisabled = r;
9700 p_lz = p;
9701 out_flush();
9702#endif
9703}
9704
9705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009706close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707{
9708 if (redir_fd != NULL)
9709 {
9710 fclose(redir_fd);
9711 redir_fd = NULL;
9712 }
9713#ifdef FEAT_EVAL
9714 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009715 if (redir_vname)
9716 {
9717 var_redir_stop();
9718 redir_vname = 0;
9719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720#endif
9721}
9722
9723#if defined(FEAT_SESSION) && defined(USE_CRNL)
9724# define MKSESSION_NL
9725static int mksession_nl = FALSE; /* use NL only in put_eol() */
9726#endif
9727
9728/*
9729 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9730 */
9731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009732ex_mkrc(
9733 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734{
9735 FILE *fd;
9736 int failed = FALSE;
9737 char_u *fname;
9738#ifdef FEAT_BROWSE
9739 char_u *browseFile = NULL;
9740#endif
9741#ifdef FEAT_SESSION
9742 int view_session = FALSE;
9743 int using_vdir = FALSE; /* using 'viewdir'? */
9744 char_u *viewFile = NULL;
9745 unsigned *flagp;
9746#endif
9747
9748 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9749 {
9750#ifdef FEAT_SESSION
9751 view_session = TRUE;
9752#else
9753 ex_ni(eap);
9754 return;
9755#endif
9756 }
9757
9758#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009759 /* Use the short file name until ":lcd" is used. We also don't use the
9760 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009761 did_lcd = FALSE;
9762
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9764 if (eap->cmdidx == CMD_mkview
9765 && (*eap->arg == NUL
9766 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9767 {
9768 eap->forceit = TRUE;
9769 fname = get_view_file(*eap->arg);
9770 if (fname == NULL)
9771 return;
9772 viewFile = fname;
9773 using_vdir = TRUE;
9774 }
9775 else
9776#endif
9777 if (*eap->arg != NUL)
9778 fname = eap->arg;
9779 else if (eap->cmdidx == CMD_mkvimrc)
9780 fname = (char_u *)VIMRC_FILE;
9781#ifdef FEAT_SESSION
9782 else if (eap->cmdidx == CMD_mksession)
9783 fname = (char_u *)SESSION_FILE;
9784#endif
9785 else
9786 fname = (char_u *)EXRC_FILE;
9787
9788#ifdef FEAT_BROWSE
9789 if (cmdmod.browse)
9790 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009791 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792# ifdef FEAT_SESSION
9793 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9794 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9795# endif
9796 (char_u *)_("Save Setup"),
9797 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9798 if (browseFile == NULL)
9799 goto theend;
9800 fname = browseFile;
9801 eap->forceit = TRUE; /* since dialog already asked */
9802 }
9803#endif
9804
9805#if defined(FEAT_SESSION) && defined(vim_mkdir)
9806 /* When using 'viewdir' may have to create the directory. */
9807 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009808 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809#endif
9810
9811 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9812 if (fd != NULL)
9813 {
9814#ifdef FEAT_SESSION
9815 if (eap->cmdidx == CMD_mkview)
9816 flagp = &vop_flags;
9817 else
9818 flagp = &ssop_flags;
9819#endif
9820
9821#ifdef MKSESSION_NL
9822 /* "unix" in 'sessionoptions': use NL line separator */
9823 if (view_session && (*flagp & SSOP_UNIX))
9824 mksession_nl = TRUE;
9825#endif
9826
9827 /* Write the version command for :mkvimrc */
9828 if (eap->cmdidx == CMD_mkvimrc)
9829 (void)put_line(fd, "version 6.0");
9830
9831#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009832 if (eap->cmdidx == CMD_mksession)
9833 {
9834 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9835 failed = TRUE;
9836 }
9837
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 if (eap->cmdidx != CMD_mkview)
9839#endif
9840 {
9841 /* Write setting 'compatible' first, because it has side effects.
9842 * For that same reason only do it when needed. */
9843 if (p_cp)
9844 (void)put_line(fd, "if !&cp | set cp | endif");
9845 else
9846 (void)put_line(fd, "if &cp | set nocp | endif");
9847 }
9848
9849#ifdef FEAT_SESSION
9850 if (!view_session
9851 || (eap->cmdidx == CMD_mksession
9852 && (*flagp & SSOP_OPTIONS)))
9853#endif
9854 failed |= (makemap(fd, NULL) == FAIL
9855 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9856
9857#ifdef FEAT_SESSION
9858 if (!failed && view_session)
9859 {
9860 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9861 failed = TRUE;
9862 if (eap->cmdidx == CMD_mksession)
9863 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009864 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865
Bram Moolenaard9462e32011-04-11 21:35:11 +02009866 dirnow = alloc(MAXPATHL);
9867 if (dirnow == NULL)
9868 failed = TRUE;
9869 else
9870 {
9871 /*
9872 * Change to session file's dir.
9873 */
9874 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009876 *dirnow = NUL;
9877 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9878 {
9879 if (vim_chdirfile(fname) == OK)
9880 shorten_fnames(TRUE);
9881 }
9882 else if (*dirnow != NUL
9883 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9884 {
9885 if (mch_chdir((char *)globaldir) == 0)
9886 shorten_fnames(TRUE);
9887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888
Bram Moolenaard9462e32011-04-11 21:35:11 +02009889 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890
Bram Moolenaard9462e32011-04-11 21:35:11 +02009891 /* restore original dir */
9892 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009894 {
9895 if (mch_chdir((char *)dirnow) != 0)
9896 EMSG(_(e_prev_dir));
9897 shorten_fnames(TRUE);
9898 }
9899 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 }
9901 }
9902 else
9903 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009904 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9905 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 }
9907 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9908 == FAIL)
9909 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009910 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9911 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009912 if (eap->cmdidx == CMD_mksession)
9913 {
9914 if (put_line(fd, "unlet SessionLoad") == FAIL)
9915 failed = TRUE;
9916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 }
9918#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009919 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9920 failed = TRUE;
9921
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 failed |= fclose(fd);
9923
9924 if (failed)
9925 EMSG(_(e_write));
9926#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9927 else if (eap->cmdidx == CMD_mksession)
9928 {
9929 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009930 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931
Bram Moolenaard9462e32011-04-11 21:35:11 +02009932 tbuf = alloc(MAXPATHL);
9933 if (tbuf != NULL)
9934 {
9935 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9936 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9937 vim_free(tbuf);
9938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 }
9940#endif
9941#ifdef MKSESSION_NL
9942 mksession_nl = FALSE;
9943#endif
9944 }
9945
9946#ifdef FEAT_BROWSE
9947theend:
9948 vim_free(browseFile);
9949#endif
9950#ifdef FEAT_SESSION
9951 vim_free(viewFile);
9952#endif
9953}
9954
Bram Moolenaardf177f62005-02-22 08:39:57 +00009955#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9956 || defined(PROTO)
9957 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009958vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009959{
9960 if (vim_mkdir(name, prot) != 0)
9961 {
9962 EMSG2(_("E739: Cannot create directory: %s"), name);
9963 return FAIL;
9964 }
9965 return OK;
9966}
9967#endif
9968
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969/*
9970 * Open a file for writing for an Ex command, with some checks.
9971 * Return file descriptor, or NULL on failure.
9972 */
9973 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009974open_exfile(
9975 char_u *fname,
9976 int forceit,
9977 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978{
9979 FILE *fd;
9980
9981#ifdef UNIX
9982 /* with Unix it is possible to open a directory */
9983 if (mch_isdir(fname))
9984 {
9985 EMSG2(_(e_isadir2), fname);
9986 return NULL;
9987 }
9988#endif
9989 if (!forceit && *mode != 'a' && vim_fexists(fname))
9990 {
9991 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9992 return NULL;
9993 }
9994
9995 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9996 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9997
9998 return fd;
9999}
10000
10001/*
10002 * ":mark" and ":k".
10003 */
10004 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010005ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006{
10007 pos_T pos;
10008
10009 if (*eap->arg == NUL) /* No argument? */
10010 EMSG(_(e_argreq));
10011 else if (eap->arg[1] != NUL) /* more than one character? */
10012 EMSG(_(e_trailing));
10013 else
10014 {
10015 pos = curwin->w_cursor; /* save curwin->w_cursor */
10016 curwin->w_cursor.lnum = eap->line2;
10017 beginline(BL_WHITE | BL_FIX);
10018 if (setmark(*eap->arg) == FAIL) /* set mark */
10019 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10020 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10021 }
10022}
10023
10024/*
10025 * Update w_topline, w_leftcol and the cursor position.
10026 */
10027 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010028update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029{
10030 check_cursor(); /* put cursor on valid line */
10031 update_topline();
10032 if (!curwin->w_p_wrap)
10033 validate_cursor();
10034 update_curswant();
10035}
10036
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037/*
10038 * ":normal[!] {commands}": Execute normal mode commands.
10039 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010040 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010041ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 int save_msg_scroll = msg_scroll;
10044 int save_restart_edit = restart_edit;
10045 int save_msg_didout = msg_didout;
10046 int save_State = State;
10047 tasave_T tabuf;
10048 int save_insertmode = p_im;
10049 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010050 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051#ifdef FEAT_MBYTE
10052 char_u *arg = NULL;
10053 int l;
10054 char_u *p;
10055#endif
10056
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010057 if (ex_normal_lock > 0)
10058 {
10059 EMSG(_(e_secure));
10060 return;
10061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 if (ex_normal_busy >= p_mmd)
10063 {
10064 EMSG(_("E192: Recursive use of :normal too deep"));
10065 return;
10066 }
10067 ++ex_normal_busy;
10068
10069 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10070 restart_edit = 0; /* don't go to Insert mode */
10071 p_im = FALSE; /* don't use 'insertmode' */
10072
10073#ifdef FEAT_MBYTE
10074 /*
10075 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10076 * this for the K_SPECIAL leading byte, otherwise special keys will not
10077 * work.
10078 */
10079 if (has_mbyte)
10080 {
10081 int len = 0;
10082
10083 /* Count the number of characters to be escaped. */
10084 for (p = eap->arg; *p != NUL; ++p)
10085 {
10086# ifdef FEAT_GUI
10087 if (*p == CSI) /* leadbyte CSI */
10088 len += 2;
10089# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010090 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10092# ifdef FEAT_GUI
10093 || *p == CSI
10094# endif
10095 )
10096 len += 2;
10097 }
10098 if (len > 0)
10099 {
10100 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10101 if (arg != NULL)
10102 {
10103 len = 0;
10104 for (p = eap->arg; *p != NUL; ++p)
10105 {
10106 arg[len++] = *p;
10107# ifdef FEAT_GUI
10108 if (*p == CSI)
10109 {
10110 arg[len++] = KS_EXTRA;
10111 arg[len++] = (int)KE_CSI;
10112 }
10113# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010114 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 {
10116 arg[len++] = *++p;
10117 if (*p == K_SPECIAL)
10118 {
10119 arg[len++] = KS_SPECIAL;
10120 arg[len++] = KE_FILLER;
10121 }
10122# ifdef FEAT_GUI
10123 else if (*p == CSI)
10124 {
10125 arg[len++] = KS_EXTRA;
10126 arg[len++] = (int)KE_CSI;
10127 }
10128# endif
10129 }
10130 arg[len] = NUL;
10131 }
10132 }
10133 }
10134 }
10135#endif
10136
10137 /*
10138 * Save the current typeahead. This is required to allow using ":normal"
10139 * from an event handler and makes sure we don't hang when the argument
10140 * ends with half a command.
10141 */
10142 save_typeahead(&tabuf);
10143 if (tabuf.typebuf_valid)
10144 {
10145 /*
10146 * Repeat the :normal command for each line in the range. When no
10147 * range given, execute it just once, without positioning the cursor
10148 * first.
10149 */
10150 do
10151 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 if (eap->addr_count != 0)
10153 {
10154 curwin->w_cursor.lnum = eap->line1++;
10155 curwin->w_cursor.col = 0;
10156 }
10157
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010158 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159#ifdef FEAT_MBYTE
10160 arg != NULL ? arg :
10161#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010162 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 }
10164 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10165 }
10166
10167 /* Might not return to the main loop when in an event handler. */
10168 update_topline_cursor();
10169
10170 /* Restore the previous typeahead. */
10171 restore_typeahead(&tabuf);
10172
10173 --ex_normal_busy;
10174 msg_scroll = save_msg_scroll;
10175 restart_edit = save_restart_edit;
10176 p_im = save_insertmode;
10177 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010178 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10180
10181 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010182 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010184#ifdef FEAT_MOUSE
10185 setmouse();
10186#endif
10187#ifdef CURSOR_SHAPE
10188 ui_cursor_shape(); /* may show different cursor shape */
10189#endif
10190
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191#ifdef FEAT_MBYTE
10192 vim_free(arg);
10193#endif
10194}
10195
10196/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010197 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 */
10199 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010200ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010202 if (eap->forceit)
10203 {
10204 coladvance((colnr_T)MAXCOL);
10205 curwin->w_curswant = MAXCOL;
10206 curwin->w_set_curswant = FALSE;
10207 }
10208
Bram Moolenaara40c5002005-01-09 21:16:21 +000010209 /* Ignore the command when already in Insert mode. Inserting an
10210 * expression register that invokes a function can do this. */
10211 if (State & INSERT)
10212 return;
10213
Bram Moolenaar64969662005-12-14 21:59:55 +000010214 if (eap->cmdidx == CMD_startinsert)
10215 restart_edit = 'a';
10216 else if (eap->cmdidx == CMD_startreplace)
10217 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010219 restart_edit = 'V';
10220
10221 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010223 if (eap->cmdidx == CMD_startinsert)
10224 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 curwin->w_curswant = 0; /* avoid MAXCOL */
10226 }
10227}
10228
10229/*
10230 * ":stopinsert"
10231 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010233ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234{
10235 restart_edit = 0;
10236 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010237 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010240/*
10241 * Execute normal mode command "cmd".
10242 * "remap" can be REMAP_NONE or REMAP_YES.
10243 */
10244 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010245exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010246{
Bram Moolenaar25281632016-01-21 23:32:32 +010010247 /* Stuff the argument into the typeahead buffer. */
10248 ins_typebuf(cmd, remap, 0, TRUE, silent);
10249 exec_normal(FALSE);
10250}
Bram Moolenaar25281632016-01-21 23:32:32 +010010251
Bram Moolenaar25281632016-01-21 23:32:32 +010010252/*
10253 * Execute normal_cmd() until there is no typeahead left.
10254 */
10255 void
10256exec_normal(int was_typed)
10257{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010258 oparg_T oa;
10259
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010260 clear_oparg(&oa);
10261 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010262 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10263 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010264 {
10265 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010266 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010267 }
10268}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010269
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270#ifdef FEAT_FIND_ID
10271 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010272ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273{
10274 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10275 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10276 (linenr_T)1, (linenr_T)MAXLNUM);
10277}
10278
10279#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10280/*
10281 * ":psearch"
10282 */
10283 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010284ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285{
10286 g_do_tagpreview = p_pvh;
10287 ex_findpat(eap);
10288 g_do_tagpreview = 0;
10289}
10290#endif
10291
10292 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010293ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294{
10295 int whole = TRUE;
10296 long n;
10297 char_u *p;
10298 int action;
10299
10300 switch (cmdnames[eap->cmdidx].cmd_name[2])
10301 {
10302 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10303 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10304 action = ACTION_GOTO;
10305 else
10306 action = ACTION_SHOW;
10307 break;
10308 case 'i': /* ":ilist" and ":dlist" */
10309 action = ACTION_SHOW_ALL;
10310 break;
10311 case 'u': /* ":ijump" and ":djump" */
10312 action = ACTION_GOTO;
10313 break;
10314 default: /* ":isplit" and ":dsplit" */
10315 action = ACTION_SPLIT;
10316 break;
10317 }
10318
10319 n = 1;
10320 if (vim_isdigit(*eap->arg)) /* get count */
10321 {
10322 n = getdigits(&eap->arg);
10323 eap->arg = skipwhite(eap->arg);
10324 }
10325 if (*eap->arg == '/') /* Match regexp, not just whole words */
10326 {
10327 whole = FALSE;
10328 ++eap->arg;
10329 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10330 if (*p)
10331 {
10332 *p++ = NUL;
10333 p = skipwhite(p);
10334
10335 /* Check for trailing illegal characters */
10336 if (!ends_excmd(*p))
10337 eap->errmsg = e_trailing;
10338 else
10339 eap->nextcmd = check_nextcmd(p);
10340 }
10341 }
10342 if (!eap->skip)
10343 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10344 whole, !eap->forceit,
10345 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10346 n, action, eap->line1, eap->line2);
10347}
10348#endif
10349
10350#ifdef FEAT_WINDOWS
10351
10352# ifdef FEAT_QUICKFIX
10353/*
10354 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10355 */
10356 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010357ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010359 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10361}
10362
10363/*
10364 * ":pedit"
10365 */
10366 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010367ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368{
10369 win_T *curwin_save = curwin;
10370
10371 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010372 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 keep_help_flag = curwin_save->w_buffer->b_help;
10374 do_exedit(eap, NULL);
10375 keep_help_flag = FALSE;
10376 if (curwin != curwin_save && win_valid(curwin_save))
10377 {
10378 /* Return cursor to where we were */
10379 validate_cursor();
10380 redraw_later(VALID);
10381 win_enter(curwin_save, TRUE);
10382 }
10383 g_do_tagpreview = 0;
10384}
10385# endif
10386
10387/*
10388 * ":stag", ":stselect" and ":stjump".
10389 */
10390 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010391ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392{
10393 postponed_split = -1;
10394 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010395 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10397 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010398 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399}
10400#endif
10401
10402/*
10403 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10404 */
10405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010406ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407{
10408 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10409}
10410
10411 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010412ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413{
10414 int cmd;
10415
10416 switch (name[1])
10417 {
10418 case 'j': cmd = DT_JUMP; /* ":tjump" */
10419 break;
10420 case 's': cmd = DT_SELECT; /* ":tselect" */
10421 break;
10422 case 'p': cmd = DT_PREV; /* ":tprevious" */
10423 break;
10424 case 'N': cmd = DT_PREV; /* ":tNext" */
10425 break;
10426 case 'n': cmd = DT_NEXT; /* ":tnext" */
10427 break;
10428 case 'o': cmd = DT_POP; /* ":pop" */
10429 break;
10430 case 'f': /* ":tfirst" */
10431 case 'r': cmd = DT_FIRST; /* ":trewind" */
10432 break;
10433 case 'l': cmd = DT_LAST; /* ":tlast" */
10434 break;
10435 default: /* ":tag" */
10436#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010437 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010439 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 return;
10441 }
10442#endif
10443 cmd = DT_TAG;
10444 break;
10445 }
10446
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010447 if (name[0] == 'l')
10448 {
10449#ifndef FEAT_QUICKFIX
10450 ex_ni(eap);
10451 return;
10452#else
10453 cmd = DT_LTAG;
10454#endif
10455 }
10456
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10458 eap->forceit, TRUE);
10459}
10460
10461/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010462 * Check "str" for starting with a special cmdline variable.
10463 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10464 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10465 */
10466 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010467find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010468{
10469 int len;
10470 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010471 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010472 "%",
10473#define SPEC_PERC 0
10474 "#",
10475#define SPEC_HASH 1
10476 "<cword>", /* cursor word */
10477#define SPEC_CWORD 2
10478 "<cWORD>", /* cursor WORD */
10479#define SPEC_CCWORD 3
10480 "<cfile>", /* cursor path name */
10481#define SPEC_CFILE 4
10482 "<sfile>", /* ":so" file name */
10483#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010484 "<slnum>", /* ":so" file line number */
10485#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010486#ifdef FEAT_AUTOCMD
10487 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010488# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010489 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010490# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010491 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010492# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010493#endif
10494#ifdef FEAT_CLIENTSERVER
10495 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010496# ifdef FEAT_AUTOCMD
10497# define SPEC_CLIENT 10
10498# else
10499# define SPEC_CLIENT 7
10500# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010501#endif
10502 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010503
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010504 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010505 {
10506 len = (int)STRLEN(spec_str[i]);
10507 if (STRNCMP(src, spec_str[i], len) == 0)
10508 {
10509 *usedlen = len;
10510 return i;
10511 }
10512 }
10513 return -1;
10514}
10515
10516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 * Evaluate cmdline variables.
10518 *
10519 * change '%' to curbuf->b_ffname
10520 * '#' to curwin->w_altfile
10521 * '<cword>' to word under the cursor
10522 * '<cWORD>' to WORD under the cursor
10523 * '<cfile>' to path name under the cursor
10524 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010525 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 * '<afile>' to file name for autocommand
10527 * '<abuf>' to buffer number for autocommand
10528 * '<amatch>' to matching name for autocommand
10529 *
10530 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10531 * "" for error without a message) and NULL is returned.
10532 * Returns an allocated string if a valid match was found.
10533 * Returns NULL if no match was found. "usedlen" then still contains the
10534 * number of characters to skip.
10535 */
10536 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010537eval_vars(
10538 char_u *src, /* pointer into commandline */
10539 char_u *srcstart, /* beginning of valid memory for src */
10540 int *usedlen, /* characters after src that are used */
10541 linenr_T *lnump, /* line number for :e command, or NULL */
10542 char_u **errormsg, /* pointer to error message */
10543 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010544 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545{
10546 int i;
10547 char_u *s;
10548 char_u *result;
10549 char_u *resultbuf = NULL;
10550 int resultlen;
10551 buf_T *buf;
10552 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10553 int spec_idx;
10554#ifdef FEAT_MODIFY_FNAME
10555 int skip_mod = FALSE;
10556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558
10559 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010560 if (escaped != NULL)
10561 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562
10563 /*
10564 * Check if there is something to do.
10565 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010566 spec_idx = find_cmdline_var(src, usedlen);
10567 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568 {
10569 *usedlen = 1;
10570 return NULL;
10571 }
10572
10573 /*
10574 * Skip when preceded with a backslash "\%" and "\#".
10575 * Note: In "\\%" the % is also not recognized!
10576 */
10577 if (src > srcstart && src[-1] == '\\')
10578 {
10579 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010580 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581 return NULL;
10582 }
10583
10584 /*
10585 * word or WORD under cursor
10586 */
10587 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10588 {
10589 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10590 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10591 if (resultlen == 0)
10592 {
10593 *errormsg = (char_u *)"";
10594 return NULL;
10595 }
10596 }
10597
10598 /*
10599 * '#': Alternate file name
10600 * '%': Current file name
10601 * File name under the cursor
10602 * File name for autocommand
10603 * and following modifiers
10604 */
10605 else
10606 {
10607 switch (spec_idx)
10608 {
10609 case SPEC_PERC: /* '%': current file */
10610 if (curbuf->b_fname == NULL)
10611 {
10612 result = (char_u *)"";
10613 valid = 0; /* Must have ":p:h" to be valid */
10614 }
10615 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 break;
10618
10619 case SPEC_HASH: /* '#' or "#99": alternate file */
10620 if (src[1] == '#') /* "##": the argument list */
10621 {
10622 result = arg_all();
10623 resultbuf = result;
10624 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010625 if (escaped != NULL)
10626 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627#ifdef FEAT_MODIFY_FNAME
10628 skip_mod = TRUE;
10629#endif
10630 break;
10631 }
10632 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010633 if (*s == '<') /* "#<99" uses v:oldfiles */
10634 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635 i = (int)getdigits(&s);
10636 *usedlen = (int)(s - src); /* length of what we expand */
10637
Bram Moolenaard812df62008-11-09 12:46:09 +000010638 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010640 if (*usedlen < 2)
10641 {
10642 /* Should we give an error message for #<text? */
10643 *usedlen = 1;
10644 return NULL;
10645 }
10646#ifdef FEAT_EVAL
10647 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10648 (long)i);
10649 if (result == NULL)
10650 {
10651 *errormsg = (char_u *)"";
10652 return NULL;
10653 }
10654#else
10655 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 }
10659 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010660 {
10661 buf = buflist_findnr(i);
10662 if (buf == NULL)
10663 {
10664 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10665 return NULL;
10666 }
10667 if (lnump != NULL)
10668 *lnump = ECMD_LAST;
10669 if (buf->b_fname == NULL)
10670 {
10671 result = (char_u *)"";
10672 valid = 0; /* Must have ":p:h" to be valid */
10673 }
10674 else
10675 result = buf->b_fname;
10676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 break;
10678
10679#ifdef FEAT_SEARCHPATH
10680 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010681 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 if (result == NULL)
10683 {
10684 *errormsg = (char_u *)"";
10685 return NULL;
10686 }
10687 resultbuf = result; /* remember allocated string */
10688 break;
10689#endif
10690
10691#ifdef FEAT_AUTOCMD
10692 case SPEC_AFILE: /* file name for autocommand */
10693 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010694 if (result != NULL && !autocmd_fname_full)
10695 {
10696 /* Still need to turn the fname into a full path. It is
10697 * postponed to avoid a delay when <afile> is not used. */
10698 autocmd_fname_full = TRUE;
10699 result = FullName_save(autocmd_fname, FALSE);
10700 vim_free(autocmd_fname);
10701 autocmd_fname = result;
10702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 if (result == NULL)
10704 {
10705 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10706 return NULL;
10707 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010708 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 break;
10710
10711 case SPEC_ABUF: /* buffer number for autocommand */
10712 if (autocmd_bufnr <= 0)
10713 {
10714 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10715 return NULL;
10716 }
10717 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10718 result = strbuf;
10719 break;
10720
10721 case SPEC_AMATCH: /* match name for autocommand */
10722 result = autocmd_match;
10723 if (result == NULL)
10724 {
10725 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10726 return NULL;
10727 }
10728 break;
10729
10730#endif
10731 case SPEC_SFILE: /* file name for ":so" command */
10732 result = sourcing_name;
10733 if (result == NULL)
10734 {
10735 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10736 return NULL;
10737 }
10738 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010739 case SPEC_SLNUM: /* line in file for ":so" command */
10740 if (sourcing_name == NULL || sourcing_lnum == 0)
10741 {
10742 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10743 return NULL;
10744 }
10745 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10746 result = strbuf;
10747 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748#if defined(FEAT_CLIENTSERVER)
10749 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010750 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10751 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 result = strbuf;
10753 break;
10754#endif
10755 }
10756
10757 resultlen = (int)STRLEN(result); /* length of new string */
10758 if (src[*usedlen] == '<') /* remove the file name extension */
10759 {
10760 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 resultlen = (int)(s - result);
10763 }
10764#ifdef FEAT_MODIFY_FNAME
10765 else if (!skip_mod)
10766 {
10767 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10768 &resultlen);
10769 if (result == NULL)
10770 {
10771 *errormsg = (char_u *)"";
10772 return NULL;
10773 }
10774 }
10775#endif
10776 }
10777
10778 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10779 {
10780 if (valid != VALID_HEAD + VALID_PATH)
10781 /* xgettext:no-c-format */
10782 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10783 else
10784 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10785 result = NULL;
10786 }
10787 else
10788 result = vim_strnsave(result, resultlen);
10789 vim_free(resultbuf);
10790 return result;
10791}
10792
10793/*
10794 * Concatenate all files in the argument list, separated by spaces, and return
10795 * it in one allocated string.
10796 * Spaces and backslashes in the file names are escaped with a backslash.
10797 * Returns NULL when out of memory.
10798 */
10799 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010800arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801{
10802 int len;
10803 int idx;
10804 char_u *retval = NULL;
10805 char_u *p;
10806
10807 /*
10808 * Do this loop two times:
10809 * first time: compute the total length
10810 * second time: concatenate the names
10811 */
10812 for (;;)
10813 {
10814 len = 0;
10815 for (idx = 0; idx < ARGCOUNT; ++idx)
10816 {
10817 p = alist_name(&ARGLIST[idx]);
10818 if (p != NULL)
10819 {
10820 if (len > 0)
10821 {
10822 /* insert a space in between names */
10823 if (retval != NULL)
10824 retval[len] = ' ';
10825 ++len;
10826 }
10827 for ( ; *p != NUL; ++p)
10828 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010829 if (*p == ' '
10830#ifndef BACKSLASH_IN_FILENAME
10831 || *p == '\\'
10832#endif
10833 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 {
10835 /* insert a backslash */
10836 if (retval != NULL)
10837 retval[len] = '\\';
10838 ++len;
10839 }
10840 if (retval != NULL)
10841 retval[len] = *p;
10842 ++len;
10843 }
10844 }
10845 }
10846
10847 /* second time: break here */
10848 if (retval != NULL)
10849 {
10850 retval[len] = NUL;
10851 break;
10852 }
10853
10854 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010855 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 if (retval == NULL)
10857 break;
10858 }
10859
10860 return retval;
10861}
10862
10863#if defined(FEAT_AUTOCMD) || defined(PROTO)
10864/*
10865 * Expand the <sfile> string in "arg".
10866 *
10867 * Returns an allocated string, or NULL for any error.
10868 */
10869 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010870expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871{
10872 char_u *errormsg;
10873 int len;
10874 char_u *result;
10875 char_u *newres;
10876 char_u *repl;
10877 int srclen;
10878 char_u *p;
10879
10880 result = vim_strsave(arg);
10881 if (result == NULL)
10882 return NULL;
10883
10884 for (p = result; *p; )
10885 {
10886 if (STRNCMP(p, "<sfile>", 7) != 0)
10887 ++p;
10888 else
10889 {
10890 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010891 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 if (errormsg != NULL)
10893 {
10894 if (*errormsg)
10895 emsg(errormsg);
10896 vim_free(result);
10897 return NULL;
10898 }
10899 if (repl == NULL) /* no match (cannot happen) */
10900 {
10901 p += srclen;
10902 continue;
10903 }
10904 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10905 newres = alloc(len);
10906 if (newres == NULL)
10907 {
10908 vim_free(repl);
10909 vim_free(result);
10910 return NULL;
10911 }
10912 mch_memmove(newres, result, (size_t)(p - result));
10913 STRCPY(newres + (p - result), repl);
10914 len = (int)STRLEN(newres);
10915 STRCAT(newres, p + srclen);
10916 vim_free(repl);
10917 vim_free(result);
10918 result = newres;
10919 p = newres + len; /* continue after the match */
10920 }
10921 }
10922
10923 return result;
10924}
10925#endif
10926
10927#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010010928static int ses_winsizes(FILE *fd, int restore_size,
10929 win_T *tab_firstwin);
10930static int ses_win_rec(FILE *fd, frame_T *fr);
10931static frame_T *ses_skipframe(frame_T *fr);
10932static int ses_do_frame(frame_T *fr);
10933static int ses_do_win(win_T *wp);
10934static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
10935static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
10936static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937
10938/*
10939 * Write openfile commands for the current buffers to an .exrc file.
10940 * Return FAIL on error, OK otherwise.
10941 */
10942 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010943makeopens(
10944 FILE *fd,
10945 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946{
10947 buf_T *buf;
10948 int only_save_windows = TRUE;
10949 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 int restore_size = TRUE;
10951 win_T *wp;
10952 char_u *sname;
10953 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010954 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010955 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010956 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010957 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010958 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010959 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960
10961 if (ssop_flags & SSOP_BUFFERS)
10962 only_save_windows = FALSE; /* Save ALL buffers */
10963
10964 /*
10965 * Begin by setting the this_session variable, and then other
10966 * sessionable variables.
10967 */
10968#ifdef FEAT_EVAL
10969 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10970 return FAIL;
10971 if (ssop_flags & SSOP_GLOBALS)
10972 if (store_session_globals(fd) == FAIL)
10973 return FAIL;
10974#endif
10975
10976 /*
10977 * Close all windows but one.
10978 */
10979 if (put_line(fd, "silent only") == FAIL)
10980 return FAIL;
10981
10982 /*
10983 * Now a :cd command to the session directory or the current directory
10984 */
10985 if (ssop_flags & SSOP_SESDIR)
10986 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010987 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10988 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 return FAIL;
10990 }
10991 else if (ssop_flags & SSOP_CURDIR)
10992 {
10993 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10994 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010995 || fputs("cd ", fd) < 0
10996 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10997 || put_eol(fd) == FAIL)
10998 {
10999 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 vim_free(sname);
11003 }
11004
11005 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011006 * If there is an empty, unnamed buffer we will wipe it out later.
11007 * Remember the buffer number.
11008 */
11009 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11010 return FAIL;
11011 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11012 return FAIL;
11013 if (put_line(fd, "endif") == FAIL)
11014 return FAIL;
11015
11016 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 * Now save the current files, current buffer first.
11018 */
11019 if (put_line(fd, "set shortmess=aoO") == FAIL)
11020 return FAIL;
11021
11022 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011023 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 {
11025 if (!(only_save_windows && buf->b_nwindows == 0)
11026 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11027 && buf->b_fname != NULL
11028 && buf->b_p_bl)
11029 {
11030 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11031 : buf->b_wininfo->wi_fpos.lnum) < 0
11032 || ses_fname(fd, buf, &ssop_flags) == FAIL)
11033 return FAIL;
11034 }
11035 }
11036
11037 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011038 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11040 return FAIL;
11041
11042 if (ssop_flags & SSOP_RESIZE)
11043 {
11044 /* Note: after the restore we still check it worked!*/
11045 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11046 || put_eol(fd) == FAIL)
11047 return FAIL;
11048 }
11049
11050#ifdef FEAT_GUI
11051 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11052 {
11053 int x, y;
11054
11055 if (gui_mch_get_winpos(&x, &y) == OK)
11056 {
11057 /* Note: after the restore we still check it worked!*/
11058 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11059 return FAIL;
11060 }
11061 }
11062#endif
11063
11064 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011065 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11066 * will be displayed when creating the next tab. That resizes the windows
11067 * in the first tab, which may cause problems. Set 'showtabline' to 2
11068 * temporarily to avoid that.
11069 */
11070 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11071 {
11072 if (put_line(fd, "set stal=2") == FAIL)
11073 return FAIL;
11074 restore_stal = TRUE;
11075 }
11076
11077 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011078 * May repeat putting Windows for each tab, when "tabpages" is in
11079 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011080 * Don't use goto_tabpage(), it may change directory and trigger
11081 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011083 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011084 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011085 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011087 int need_tabnew = FALSE;
11088 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011089
Bram Moolenaar18144c82006-04-12 21:52:12 +000011090 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011092 tabpage_T *tp = find_tabpage(tabnr);
11093
11094 if (tp == NULL)
11095 break; /* done all tab pages */
11096 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011097 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011098 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011099 tab_topframe = topframe;
11100 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011101 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011102 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011103 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011104 tab_topframe = tp->tp_topframe;
11105 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011106 if (tabnr > 1)
11107 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109
Bram Moolenaar18144c82006-04-12 21:52:12 +000011110 /*
11111 * Before creating the window layout, try loading one file. If this
11112 * is aborted we don't end up with a number of useless windows.
11113 * This may have side effects! (e.g., compressed or network file).
11114 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011115 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011116 {
11117 if (ses_do_win(wp)
11118 && wp->w_buffer->b_ffname != NULL
11119 && !wp->w_buffer->b_help
11120#ifdef FEAT_QUICKFIX
11121 && !bt_nofile(wp->w_buffer)
11122#endif
11123 )
11124 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011125 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011126 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11127 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011128 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011129 if (!wp->w_arg_idx_invalid)
11130 edited_win = wp;
11131 break;
11132 }
11133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011135 /* If no file got edited create an empty tab page. */
11136 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11137 return FAIL;
11138
Bram Moolenaar18144c82006-04-12 21:52:12 +000011139 /*
11140 * Save current window layout.
11141 */
11142 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011144 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011146 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11147 return FAIL;
11148 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11149 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150
Bram Moolenaar18144c82006-04-12 21:52:12 +000011151 /*
11152 * Check if window sizes can be restored (no windows omitted).
11153 * Remember the window number of the current window after restoring.
11154 */
11155 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011156 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011157 {
11158 if (ses_do_win(wp))
11159 ++nr;
11160 else
11161 restore_size = FALSE;
11162 if (curwin == wp)
11163 cnr = nr;
11164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165
Bram Moolenaar18144c82006-04-12 21:52:12 +000011166 /* Go to the first window. */
11167 if (put_line(fd, "wincmd t") == FAIL)
11168 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011169
Bram Moolenaar18144c82006-04-12 21:52:12 +000011170 /*
11171 * If more than one window, see if sizes can be restored.
11172 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11173 * resized when moving between windows.
11174 * Do this before restoring the view, so that the topline and the
11175 * cursor can be set. This is done again below.
11176 */
11177 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
11178 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011179 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011180 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181
Bram Moolenaar18144c82006-04-12 21:52:12 +000011182 /*
11183 * Restore the view of the window (options, file, cursor, etc.).
11184 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011185 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011186 {
11187 if (!ses_do_win(wp))
11188 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011189 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11190 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011191 return FAIL;
11192 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11193 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011194 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011195 }
11196
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011197 /* The argument index in the first tab page is zero, need to set it in
11198 * each window. For further tab pages it's the window where we do
11199 * "tabedit". */
11200 cur_arg_idx = next_arg_idx;
11201
Bram Moolenaar18144c82006-04-12 21:52:12 +000011202 /*
11203 * Restore cursor to the current window if it's not the first one.
11204 */
11205 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11206 || put_eol(fd) == FAIL))
11207 return FAIL;
11208
11209 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011210 * Restore window sizes again after jumping around in windows, because
11211 * the current window has a minimum size while others may not.
11212 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011213 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011214 return FAIL;
11215
Bram Moolenaar18144c82006-04-12 21:52:12 +000011216 /* Don't continue in another tab page when doing only the current one
11217 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011218 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011219 break;
11220 }
11221
11222 if (ssop_flags & SSOP_TABPAGES)
11223 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011224 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11225 || put_eol(fd) == FAIL)
11226 return FAIL;
11227 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011228 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11229 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011230
Bram Moolenaar9c102382006-05-03 21:26:49 +000011231 /*
11232 * Wipe out an empty unnamed buffer we started in.
11233 */
11234 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11235 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011236 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011237 return FAIL;
11238 if (put_line(fd, "endif") == FAIL)
11239 return FAIL;
11240 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11241 return FAIL;
11242
11243 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11244 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11245 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11246 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247
11248 /*
11249 * Lastly, execute the x.vim file if it exists.
11250 */
11251 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11252 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011253 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 || put_line(fd, "endif") == FAIL)
11255 return FAIL;
11256
11257 return OK;
11258}
11259
11260 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011261ses_winsizes(
11262 FILE *fd,
11263 int restore_size,
11264 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265{
11266 int n = 0;
11267 win_T *wp;
11268
11269 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11270 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011271 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 {
11273 if (!ses_do_win(wp))
11274 continue;
11275 ++n;
11276
11277 /* restore height when not full height */
11278 if (wp->w_height + wp->w_status_height < topframe->fr_height
11279 && (fprintf(fd,
11280 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11281 n, (long)wp->w_height, Rows / 2, Rows) < 0
11282 || put_eol(fd) == FAIL))
11283 return FAIL;
11284
11285 /* restore width when not full width */
11286 if (wp->w_width < Columns && (fprintf(fd,
11287 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11288 n, (long)wp->w_width, Columns / 2, Columns) < 0
11289 || put_eol(fd) == FAIL))
11290 return FAIL;
11291 }
11292 }
11293 else
11294 {
11295 /* Just equalise window sizes */
11296 if (put_line(fd, "wincmd =") == FAIL)
11297 return FAIL;
11298 }
11299 return OK;
11300}
11301
11302/*
11303 * Write commands to "fd" to recursively create windows for frame "fr",
11304 * horizontally and vertically split.
11305 * After the commands the last window in the frame is the current window.
11306 * Returns FAIL when writing the commands to "fd" fails.
11307 */
11308 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011309ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310{
11311 frame_T *frc;
11312 int count = 0;
11313
11314 if (fr->fr_layout != FR_LEAF)
11315 {
11316 /* Find first frame that's not skipped and then create a window for
11317 * each following one (first frame is already there). */
11318 frc = ses_skipframe(fr->fr_child);
11319 if (frc != NULL)
11320 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11321 {
11322 /* Make window as big as possible so that we have lots of room
11323 * to split. */
11324 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11325 || put_line(fd, fr->fr_layout == FR_COL
11326 ? "split" : "vsplit") == FAIL)
11327 return FAIL;
11328 ++count;
11329 }
11330
11331 /* Go back to the first window. */
11332 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11333 ? "%dwincmd k" : "%dwincmd h", count) < 0
11334 || put_eol(fd) == FAIL))
11335 return FAIL;
11336
11337 /* Recursively create frames/windows in each window of this column or
11338 * row. */
11339 frc = ses_skipframe(fr->fr_child);
11340 while (frc != NULL)
11341 {
11342 ses_win_rec(fd, frc);
11343 frc = ses_skipframe(frc->fr_next);
11344 /* Go to next window. */
11345 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11346 return FAIL;
11347 }
11348 }
11349 return OK;
11350}
11351
11352/*
11353 * Skip frames that don't contain windows we want to save in the Session.
11354 * Returns NULL when there none.
11355 */
11356 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011357ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358{
11359 frame_T *frc;
11360
11361 for (frc = fr; frc != NULL; frc = frc->fr_next)
11362 if (ses_do_frame(frc))
11363 break;
11364 return frc;
11365}
11366
11367/*
11368 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11369 * the Session.
11370 */
11371 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011372ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373{
11374 frame_T *frc;
11375
11376 if (fr->fr_layout == FR_LEAF)
11377 return ses_do_win(fr->fr_win);
11378 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11379 if (ses_do_frame(frc))
11380 return TRUE;
11381 return FALSE;
11382}
11383
11384/*
11385 * Return non-zero if window "wp" is to be stored in the Session.
11386 */
11387 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011388ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389{
11390 if (wp->w_buffer->b_fname == NULL
11391#ifdef FEAT_QUICKFIX
11392 /* When 'buftype' is "nofile" can't restore the window contents. */
11393 || bt_nofile(wp->w_buffer)
11394#endif
11395 )
11396 return (ssop_flags & SSOP_BLANK);
11397 if (wp->w_buffer->b_help)
11398 return (ssop_flags & SSOP_HELP);
11399 return TRUE;
11400}
11401
11402/*
11403 * Write commands to "fd" to restore the view of a window.
11404 * Caller must make sure 'scrolloff' is zero.
11405 */
11406 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011407put_view(
11408 FILE *fd,
11409 win_T *wp,
11410 int add_edit, /* add ":edit" command to view */
11411 unsigned *flagp, /* vop_flags or ssop_flags */
11412 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011413 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414{
11415 win_T *save_curwin;
11416 int f;
11417 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011418 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419
11420 /* Always restore cursor position for ":mksession". For ":mkview" only
11421 * when 'viewoptions' contains "cursor". */
11422 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11423
11424 /*
11425 * Local argument list.
11426 */
11427 if (wp->w_alist == &global_alist)
11428 {
11429 if (put_line(fd, "argglobal") == FAIL)
11430 return FAIL;
11431 }
11432 else
11433 {
11434 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11435 flagp == &vop_flags
11436 || !(*flagp & SSOP_CURDIR)
11437 || wp->w_localdir != NULL, flagp) == FAIL)
11438 return FAIL;
11439 }
11440
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011441 /* Only when part of a session: restore the argument index. Some
11442 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011443 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011444 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011446 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447 || put_eol(fd) == FAIL)
11448 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011449 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450 }
11451
11452 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011453 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 {
11455 /*
11456 * Load the file.
11457 */
11458 if (wp->w_buffer->b_ffname != NULL
11459#ifdef FEAT_QUICKFIX
11460 && !bt_nofile(wp->w_buffer)
11461#endif
11462 )
11463 {
11464 /*
11465 * Editing a file in this buffer: use ":edit file".
11466 * This may have side effects! (e.g., compressed or network file).
11467 */
11468 if (fputs("edit ", fd) < 0
11469 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11470 return FAIL;
11471 }
11472 else
11473 {
11474 /* No file in this buffer, just make it empty. */
11475 if (put_line(fd, "enew") == FAIL)
11476 return FAIL;
11477#ifdef FEAT_QUICKFIX
11478 if (wp->w_buffer->b_ffname != NULL)
11479 {
11480 /* The buffer does have a name, but it's not a file name. */
11481 if (fputs("file ", fd) < 0
11482 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11483 return FAIL;
11484 }
11485#endif
11486 do_cursor = FALSE;
11487 }
11488 }
11489
11490 /*
11491 * Local mappings and abbreviations.
11492 */
11493 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11494 && makemap(fd, wp->w_buffer) == FAIL)
11495 return FAIL;
11496
11497 /*
11498 * Local options. Need to go to the window temporarily.
11499 * Store only local values when using ":mkview" and when ":mksession" is
11500 * used and 'sessionoptions' doesn't include "options".
11501 * Some folding options are always stored when "folds" is included,
11502 * otherwise the folds would not be restored correctly.
11503 */
11504 save_curwin = curwin;
11505 curwin = wp;
11506 curbuf = curwin->w_buffer;
11507 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11508 f = makeset(fd, OPT_LOCAL,
11509 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11510#ifdef FEAT_FOLDING
11511 else if (*flagp & SSOP_FOLDS)
11512 f = makefoldset(fd);
11513#endif
11514 else
11515 f = OK;
11516 curwin = save_curwin;
11517 curbuf = curwin->w_buffer;
11518 if (f == FAIL)
11519 return FAIL;
11520
11521#ifdef FEAT_FOLDING
11522 /*
11523 * Save Folds when 'buftype' is empty and for help files.
11524 */
11525 if ((*flagp & SSOP_FOLDS)
11526 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011527# ifdef FEAT_QUICKFIX
11528 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11529# endif
11530 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531 {
11532 if (put_folds(fd, wp) == FAIL)
11533 return FAIL;
11534 }
11535#endif
11536
11537 /*
11538 * Set the cursor after creating folds, since that moves the cursor.
11539 */
11540 if (do_cursor)
11541 {
11542
11543 /* Restore the cursor line in the file and relatively in the
11544 * window. Don't use "G", it changes the jumplist. */
11545 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11546 (long)wp->w_cursor.lnum,
11547 (long)(wp->w_cursor.lnum - wp->w_topline),
11548 (long)wp->w_height / 2, (long)wp->w_height) < 0
11549 || put_eol(fd) == FAIL
11550 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11551 || put_line(fd, "exe s:l") == FAIL
11552 || put_line(fd, "normal! zt") == FAIL
11553 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11554 || put_eol(fd) == FAIL)
11555 return FAIL;
11556 /* Restore the cursor column and left offset when not wrapping. */
11557 if (wp->w_cursor.col == 0)
11558 {
11559 if (put_line(fd, "normal! 0") == FAIL)
11560 return FAIL;
11561 }
11562 else
11563 {
11564 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11565 {
11566 if (fprintf(fd,
11567 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011568 (long)wp->w_virtcol + 1,
11569 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570 (long)wp->w_width / 2, (long)wp->w_width) < 0
11571 || put_eol(fd) == FAIL
11572 || put_line(fd, "if s:c > 0") == FAIL
11573 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011574 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11575 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576 || put_eol(fd) == FAIL
11577 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011578 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579 || put_eol(fd) == FAIL
11580 || put_line(fd, "endif") == FAIL)
11581 return FAIL;
11582 }
11583 else
11584 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011585 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586 || put_eol(fd) == FAIL)
11587 return FAIL;
11588 }
11589 }
11590 }
11591
11592 /*
11593 * Local directory.
11594 */
11595 if (wp->w_localdir != NULL)
11596 {
11597 if (fputs("lcd ", fd) < 0
11598 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11599 || put_eol(fd) == FAIL)
11600 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011601 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602 }
11603
11604 return OK;
11605}
11606
11607/*
11608 * Write an argument list to the session file.
11609 * Returns FAIL if writing fails.
11610 */
11611 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011612ses_arglist(
11613 FILE *fd,
11614 char *cmd,
11615 garray_T *gap,
11616 int fullname, /* TRUE: use full path name */
11617 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618{
11619 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011620 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621 char_u *s;
11622
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011623 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11624 return FAIL;
11625 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626 return FAIL;
11627 for (i = 0; i < gap->ga_len; ++i)
11628 {
11629 /* NULL file names are skipped (only happens when out of memory). */
11630 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11631 if (s != NULL)
11632 {
11633 if (fullname)
11634 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011635 buf = alloc(MAXPATHL);
11636 if (buf != NULL)
11637 {
11638 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11639 s = buf;
11640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011642 if (fputs("argadd ", fd) < 0
11643 || ses_put_fname(fd, s, flagp) == FAIL
11644 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011645 {
11646 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011648 }
11649 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650 }
11651 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011652 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653}
11654
11655/*
11656 * Write a buffer name to the session file.
11657 * Also ends the line.
11658 * Returns FAIL if writing fails.
11659 */
11660 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011661ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662{
11663 char_u *name;
11664
11665 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011666 * the session file will be sourced.
11667 * Don't do this for ":mkview", we don't know the current directory.
11668 * Don't do this after ":lcd", we don't keep track of what the current
11669 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670 if (buf->b_sfname != NULL
11671 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011672 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011673#ifdef FEAT_AUTOCHDIR
11674 && !p_acd
11675#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011676 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 name = buf->b_sfname;
11678 else
11679 name = buf->b_ffname;
11680 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11681 return FAIL;
11682 return OK;
11683}
11684
11685/*
11686 * Write a file name to the session file.
11687 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11688 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011689 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690 */
11691 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011692ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693{
11694 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011695 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697
11698 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011699 if (sname == NULL)
11700 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011702 if (*flagp & SSOP_SLASH)
11703 {
11704 /* change all backslashes to forward slashes */
11705 for (p = sname; *p != NUL; mb_ptr_adv(p))
11706 if (*p == '\\')
11707 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011708 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011709
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011710 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011711 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011713 if (p == NULL)
11714 return FAIL;
11715
11716 /* write the result */
11717 if (fputs((char *)p, fd) < 0)
11718 retval = FAIL;
11719
11720 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721 return retval;
11722}
11723
11724/*
11725 * ":loadview [nr]"
11726 */
11727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011728ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011729{
11730 char_u *fname;
11731
11732 fname = get_view_file(*eap->arg);
11733 if (fname != NULL)
11734 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011735 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736 vim_free(fname);
11737 }
11738}
11739
11740/*
11741 * Get the name of the view file for the current buffer.
11742 */
11743 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011744get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011745{
11746 int len = 0;
11747 char_u *p, *s;
11748 char_u *retval;
11749 char_u *sname;
11750
11751 if (curbuf->b_ffname == NULL)
11752 {
11753 EMSG(_(e_noname));
11754 return NULL;
11755 }
11756 sname = home_replace_save(NULL, curbuf->b_ffname);
11757 if (sname == NULL)
11758 return NULL;
11759
11760 /*
11761 * We want a file name without separators, because we're not going to make
11762 * a directory.
11763 * "normal" path separator -> "=+"
11764 * "=" -> "=="
11765 * ":" path separator -> "=-"
11766 */
11767 for (p = sname; *p; ++p)
11768 if (*p == '=' || vim_ispathsep(*p))
11769 ++len;
11770 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11771 if (retval != NULL)
11772 {
11773 STRCPY(retval, p_vdir);
11774 add_pathsep(retval);
11775 s = retval + STRLEN(retval);
11776 for (p = sname; *p; ++p)
11777 {
11778 if (*p == '=')
11779 {
11780 *s++ = '=';
11781 *s++ = '=';
11782 }
11783 else if (vim_ispathsep(*p))
11784 {
11785 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011786#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787 if (*p == ':')
11788 *s++ = '-';
11789 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011791 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792 }
11793 else
11794 *s++ = *p;
11795 }
11796 *s++ = '=';
11797 *s++ = c;
11798 STRCPY(s, ".vim");
11799 }
11800
11801 vim_free(sname);
11802 return retval;
11803}
11804
11805#endif /* FEAT_SESSION */
11806
11807/*
11808 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11809 * Return FAIL for a write error.
11810 */
11811 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011812put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813{
11814 if (
11815#ifdef USE_CRNL
11816 (
11817# ifdef MKSESSION_NL
11818 !mksession_nl &&
11819# endif
11820 (putc('\r', fd) < 0)) ||
11821#endif
11822 (putc('\n', fd) < 0))
11823 return FAIL;
11824 return OK;
11825}
11826
11827/*
11828 * Write a line to "fd".
11829 * Return FAIL for a write error.
11830 */
11831 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011832put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833{
11834 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11835 return FAIL;
11836 return OK;
11837}
11838
11839#ifdef FEAT_VIMINFO
11840/*
11841 * ":rviminfo" and ":wviminfo".
11842 */
11843 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011844ex_viminfo(
11845 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846{
11847 char_u *save_viminfo;
11848
11849 save_viminfo = p_viminfo;
11850 if (*p_viminfo == NUL)
11851 p_viminfo = (char_u *)"'100";
11852 if (eap->cmdidx == CMD_rviminfo)
11853 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011854 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11855 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 EMSG(_("E195: Cannot open viminfo file for reading"));
11857 }
11858 else
11859 write_viminfo(eap->arg, eap->forceit);
11860 p_viminfo = save_viminfo;
11861}
11862#endif
11863
11864#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011865/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011866 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011867 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011870dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 if (fname == NULL)
11873 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011874 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875}
11876#endif
11877
11878/*
11879 * ":behave {mswin,xterm}"
11880 */
11881 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011882ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011883{
11884 if (STRCMP(eap->arg, "mswin") == 0)
11885 {
11886 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11887 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11888 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11889 set_option_value((char_u *)"keymodel", 0L,
11890 (char_u *)"startsel,stopsel", 0);
11891 }
11892 else if (STRCMP(eap->arg, "xterm") == 0)
11893 {
11894 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11895 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11896 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11897 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11898 }
11899 else
11900 EMSG2(_(e_invarg2), eap->arg);
11901}
11902
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011903#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11904/*
11905 * Function given to ExpandGeneric() to obtain the possible arguments of the
11906 * ":behave {mswin,xterm}" command.
11907 */
11908 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011909get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011910{
11911 if (idx == 0)
11912 return (char_u *)"mswin";
11913 if (idx == 1)
11914 return (char_u *)"xterm";
11915 return NULL;
11916}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020011917
11918/*
11919 * Function given to ExpandGeneric() to obtain the possible arguments of the
11920 * ":messages {clear}" command.
11921 */
11922 char_u *
11923get_messages_arg(expand_T *xp UNUSED, int idx)
11924{
11925 if (idx == 0)
11926 return (char_u *)"clear";
11927 return NULL;
11928}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011929#endif
11930
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931#ifdef FEAT_AUTOCMD
11932static int filetype_detect = FALSE;
11933static int filetype_plugin = FALSE;
11934static int filetype_indent = FALSE;
11935
11936/*
11937 * ":filetype [plugin] [indent] {on,off,detect}"
11938 * on: Load the filetype.vim file to install autocommands for file types.
11939 * off: Load the ftoff.vim file to remove all autocommands for file types.
11940 * plugin on: load filetype.vim and ftplugin.vim
11941 * plugin off: load ftplugof.vim
11942 * indent on: load filetype.vim and indent.vim
11943 * indent off: load indoff.vim
11944 */
11945 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011946ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947{
11948 char_u *arg = eap->arg;
11949 int plugin = FALSE;
11950 int indent = FALSE;
11951
11952 if (*eap->arg == NUL)
11953 {
11954 /* Print current status. */
11955 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11956 filetype_detect ? "ON" : "OFF",
11957 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11958 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11959 return;
11960 }
11961
11962 /* Accept "plugin" and "indent" in any order. */
11963 for (;;)
11964 {
11965 if (STRNCMP(arg, "plugin", 6) == 0)
11966 {
11967 plugin = TRUE;
11968 arg = skipwhite(arg + 6);
11969 continue;
11970 }
11971 if (STRNCMP(arg, "indent", 6) == 0)
11972 {
11973 indent = TRUE;
11974 arg = skipwhite(arg + 6);
11975 continue;
11976 }
11977 break;
11978 }
11979 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11980 {
11981 if (*arg == 'o' || !filetype_detect)
11982 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011983 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 filetype_detect = TRUE;
11985 if (plugin)
11986 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011987 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 filetype_plugin = TRUE;
11989 }
11990 if (indent)
11991 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011992 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993 filetype_indent = TRUE;
11994 }
11995 }
11996 if (*arg == 'd')
11997 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020011998 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011999 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000 }
12001 }
12002 else if (STRCMP(arg, "off") == 0)
12003 {
12004 if (plugin || indent)
12005 {
12006 if (plugin)
12007 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012008 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009 filetype_plugin = FALSE;
12010 }
12011 if (indent)
12012 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012013 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014 filetype_indent = FALSE;
12015 }
12016 }
12017 else
12018 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012019 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 filetype_detect = FALSE;
12021 }
12022 }
12023 else
12024 EMSG2(_(e_invarg2), arg);
12025}
12026
12027/*
12028 * ":setfiletype {name}"
12029 */
12030 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012031ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032{
12033 if (!did_filetype)
12034 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
12035}
12036#endif
12037
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012039ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040{
12041#ifdef FEAT_DIGRAPHS
12042 if (*eap->arg != NUL)
12043 putdigraph(eap->arg);
12044 else
12045 listdigraphs();
12046#else
12047 EMSG(_("E196: No digraphs in this version"));
12048#endif
12049}
12050
12051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012052ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053{
12054 int flags = 0;
12055
12056 if (eap->cmdidx == CMD_setlocal)
12057 flags = OPT_LOCAL;
12058 else if (eap->cmdidx == CMD_setglobal)
12059 flags = OPT_GLOBAL;
12060#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12061 if (cmdmod.browse && flags == 0)
12062 ex_options(eap);
12063 else
12064#endif
12065 (void)do_set(eap->arg, flags);
12066}
12067
12068#ifdef FEAT_SEARCH_EXTRA
12069/*
12070 * ":nohlsearch"
12071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012073ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012075 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012076 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077}
12078
12079/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012080 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012081 * Sets nextcmd to the start of the next command, if any. Also called when
12082 * skipping commands to find the next command.
12083 */
12084 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012085ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086{
12087 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012088 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089 char_u *end;
12090 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012091 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012092
12093 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012094 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012095 else
12096 {
12097 EMSG(e_invcmd);
12098 return;
12099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100
12101 /* First clear any old pattern. */
12102 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012103 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104
12105 if (ends_excmd(*eap->arg))
12106 end = eap->arg;
12107 else if ((STRNICMP(eap->arg, "none", 4) == 0
12108 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
12109 end = eap->arg + 4;
12110 else
12111 {
12112 p = skiptowhite(eap->arg);
12113 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012114 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115 p = skipwhite(p);
12116 if (*p == NUL)
12117 {
12118 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012119 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012120 EMSG2(_(e_invarg2), eap->arg);
12121 return;
12122 }
12123 end = skip_regexp(p + 1, *p, TRUE, NULL);
12124 if (!eap->skip)
12125 {
12126 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12127 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012128 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012129 eap->errmsg = e_trailing;
12130 return;
12131 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012132 if (*end != *p)
12133 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012134 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012135 EMSG2(_(e_invarg2), p);
12136 return;
12137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012138
12139 c = *end;
12140 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012141 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012142 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012143 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144 }
12145 }
12146 eap->nextcmd = find_nextcmd(end);
12147}
12148#endif
12149
12150#ifdef FEAT_CRYPT
12151/*
12152 * ":X": Get crypt key
12153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012155ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012157 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012158 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159}
12160#endif
12161
12162#ifdef FEAT_FOLDING
12163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012164ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165{
12166 if (foldManualAllowed(TRUE))
12167 foldCreate(eap->line1, eap->line2);
12168}
12169
12170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012171ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172{
12173 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12174 eap->forceit, FALSE);
12175}
12176
12177 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012178ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179{
12180 linenr_T lnum;
12181
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012182#ifdef FEAT_CLIPBOARD
12183 start_global_changes();
12184#endif
12185
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186 /* First set the marks for all lines closed/open. */
12187 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12188 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12189 ml_setmarked(lnum);
12190
12191 /* Execute the command on the marked lines. */
12192 global_exe(eap->arg);
12193 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012194#ifdef FEAT_CLIPBOARD
12195 end_global_changes();
12196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197}
12198#endif