blob: 3c51b3aea6422bd82590b71a85a978875e2d6df0 [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 Moolenaarf42dd3c2017-01-28 16:06:38 +0100291#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
292# define ex_pyx ex_script_ni
293# define ex_pyxdo ex_ni
294# define ex_pyxfile ex_ni
295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296#ifndef FEAT_TCL
297# define ex_tcl ex_script_ni
298# define ex_tcldo ex_ni
299# define ex_tclfile ex_ni
300#endif
301#ifndef FEAT_RUBY
302# define ex_ruby ex_script_ni
303# define ex_rubydo ex_ni
304# define ex_rubyfile ex_ni
305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#ifndef FEAT_KEYMAP
307# define ex_loadkeymap ex_ni
308#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100309static void ex_swapname(exarg_T *eap);
310static void ex_syncbind(exarg_T *eap);
311static void ex_read(exarg_T *eap);
312static void ex_pwd(exarg_T *eap);
313static void ex_equal(exarg_T *eap);
314static void ex_sleep(exarg_T *eap);
315static void do_exmap(exarg_T *eap, int isabbrev);
316static void ex_winsize(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_wincmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#else
320# define ex_wincmd ex_ni
321#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000322#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100323static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324#else
325# define ex_winpos ex_ni
326#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_operators(exarg_T *eap);
328static void ex_put(exarg_T *eap);
329static void ex_copymove(exarg_T *eap);
330static void ex_submagic(exarg_T *eap);
331static void ex_join(exarg_T *eap);
332static void ex_at(exarg_T *eap);
333static void ex_bang(exarg_T *eap);
334static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200335#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_wundo(exarg_T *eap);
337static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200338#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100339static void ex_redo(exarg_T *eap);
340static void ex_later(exarg_T *eap);
341static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static void ex_redrawstatus(exarg_T *eap);
343static void close_redir(void);
344static void ex_mkrc(exarg_T *eap);
345static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100347static char_u *uc_fun_cmd(void);
348static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100350static void ex_startinsert(exarg_T *eap);
351static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100353static void ex_checkpath(exarg_T *eap);
354static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355#else
356# define ex_findpat ex_ni
357# define ex_checkpath ex_ni
358#endif
359#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100360static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361#else
362# define ex_psearch ex_ni
363#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100364static void ex_tag(exarg_T *eap);
365static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366#ifndef FEAT_EVAL
367# define ex_scriptnames ex_ni
368# define ex_finish ex_ni
369# define ex_echo ex_ni
370# define ex_echohl ex_ni
371# define ex_execute ex_ni
372# define ex_call ex_ni
373# define ex_if ex_ni
374# define ex_endif ex_ni
375# define ex_else ex_ni
376# define ex_while ex_ni
377# define ex_continue ex_ni
378# define ex_break ex_ni
379# define ex_endwhile ex_ni
380# define ex_throw ex_ni
381# define ex_try ex_ni
382# define ex_catch ex_ni
383# define ex_finally ex_ni
384# define ex_endtry ex_ni
385# define ex_endfunction ex_ni
386# define ex_let ex_ni
387# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000388# define ex_lockvar ex_ni
389# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390# define ex_function ex_ni
391# define ex_delfunction ex_ni
392# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000393# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100395static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100397static int makeopens(FILE *fd, char_u *dirnow);
398static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
399static void ex_loadview(exarg_T *eap);
400static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000401static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402#else
403# define ex_loadview ex_ni
404#endif
405#ifndef FEAT_EVAL
406# define ex_compiler ex_ni
407#endif
408#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100409static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410#else
411# define ex_viminfo ex_ni
412#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100413static void ex_behave(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100415static void ex_filetype(exarg_T *eap);
416static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417#else
418# define ex_filetype ex_ni
419# define ex_setfiletype ex_ni
420#endif
421#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000422# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423# define ex_diffpatch ex_ni
424# define ex_diffgetput ex_ni
425# define ex_diffsplit ex_ni
426# define ex_diffthis ex_ni
427# define ex_diffupdate ex_ni
428#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100429static void ex_digraphs(exarg_T *eap);
430static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
432# define ex_options ex_ni
433#endif
434#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100435static void ex_nohlsearch(exarg_T *eap);
436static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437#else
438# define ex_nohlsearch ex_ni
439# define ex_match ex_ni
440#endif
441#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100442static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443#else
444# define ex_X ex_ni
445#endif
446#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100447static void ex_fold(exarg_T *eap);
448static void ex_foldopen(exarg_T *eap);
449static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450#else
451# define ex_fold ex_ni
452# define ex_foldopen ex_ni
453# define ex_folddo ex_ni
454#endif
455#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
456 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
457# define ex_language ex_ni
458#endif
459#ifndef FEAT_SIGNS
460# define ex_sign ex_ni
461#endif
462#ifndef FEAT_SUN_WORKSHOP
463# define ex_wsverb ex_ni
464#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000465#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200466# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000467# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200468# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
471#ifndef FEAT_EVAL
472# define ex_debug ex_ni
473# define ex_breakadd ex_ni
474# define ex_debuggreedy ex_ni
475# define ex_breakdel ex_ni
476# define ex_breaklist ex_ni
477#endif
478
479#ifndef FEAT_CMDHIST
480# define ex_history ex_ni
481#endif
482#ifndef FEAT_JUMPLIST
483# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200484# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485# define ex_changes ex_ni
486#endif
487
Bram Moolenaar05159a02005-02-26 23:04:13 +0000488#ifndef FEAT_PROFILE
489# define ex_profile ex_ni
490#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200491#ifndef FEAT_TERMINAL
492# define ex_terminal ex_ni
493#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000494
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495/*
496 * Declare cmdnames[].
497 */
498#define DO_DECLARE_EXCMD
499#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200500#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100501
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502static char_u dollar_command[2] = {'$', 0};
503
504
505#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000506/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507typedef struct
508{
509 char_u *line; /* command line */
510 linenr_T lnum; /* sourcing_lnum of the line */
511} wcmd_T;
512
513/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000514 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000516 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000518struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519{
520 garray_T *lines_gap; /* growarray with line info */
521 int current_line; /* last read line from growarray */
522 int repeating; /* TRUE when looping a second time */
523 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100524 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 void *cookie;
526};
527
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100528static char_u *get_loop_line(int c, void *cookie, int indent);
529static int store_loop_line(garray_T *gap, char_u *line);
530static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000531
532/* Struct to save a few things while debugging. Used in do_cmdline() only. */
533struct dbg_stuff
534{
535 int trylevel;
536 int force_abort;
537 except_T *caught_stack;
538 char_u *vv_exception;
539 char_u *vv_throwpoint;
540 int did_emsg;
541 int got_int;
542 int did_throw;
543 int need_rethrow;
544 int check_cstack;
545 except_T *current_exception;
546};
547
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100548static void save_dbg_stuff(struct dbg_stuff *dsp);
549static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000550
551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100552save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000553{
554 dsp->trylevel = trylevel; trylevel = 0;
555 dsp->force_abort = force_abort; force_abort = FALSE;
556 dsp->caught_stack = caught_stack; caught_stack = NULL;
557 dsp->vv_exception = v_exception(NULL);
558 dsp->vv_throwpoint = v_throwpoint(NULL);
559
560 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
561 dsp->did_emsg = did_emsg; did_emsg = FALSE;
562 dsp->got_int = got_int; got_int = FALSE;
563 dsp->did_throw = did_throw; did_throw = FALSE;
564 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
565 dsp->check_cstack = check_cstack; check_cstack = FALSE;
566 dsp->current_exception = current_exception; current_exception = NULL;
567}
568
569 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100570restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000571{
572 suppress_errthrow = FALSE;
573 trylevel = dsp->trylevel;
574 force_abort = dsp->force_abort;
575 caught_stack = dsp->caught_stack;
576 (void)v_exception(dsp->vv_exception);
577 (void)v_throwpoint(dsp->vv_throwpoint);
578 did_emsg = dsp->did_emsg;
579 got_int = dsp->got_int;
580 did_throw = dsp->did_throw;
581 need_rethrow = dsp->need_rethrow;
582 check_cstack = dsp->check_cstack;
583 current_exception = dsp->current_exception;
584}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585#endif
586
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587/*
588 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
589 * command is given.
590 */
591 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100592do_exmode(
593 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594{
595 int save_msg_scroll;
596 int prev_msg_row;
597 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100598 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000599
600 if (improved)
601 exmode_active = EXMODE_VIM;
602 else
603 exmode_active = EXMODE_NORMAL;
604 State = NORMAL;
605
606 /* When using ":global /pat/ visual" and then "Q" we return to continue
607 * the :global command. */
608 if (global_busy)
609 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
611 save_msg_scroll = msg_scroll;
612 ++RedrawingDisabled; /* don't redisplay the window */
613 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#ifdef FEAT_GUI
615 /* Ignore scrollbar and mouse events in Ex mode */
616 ++hold_gui_events;
617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618
619 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
620 while (exmode_active)
621 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000622 /* Check for a ":normal" command and no more characters left. */
623 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
624 {
625 exmode_active = FALSE;
626 break;
627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 msg_scroll = TRUE;
629 need_wait_return = FALSE;
630 ex_pressedreturn = FALSE;
631 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100632 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 prev_msg_row = msg_row;
634 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 if (improved)
636 {
637 cmdline_row = msg_row;
638 do_cmdline(NULL, getexline, NULL, 0);
639 }
640 else
641 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
642 lines_left = Rows - 1;
643
Bram Moolenaardf177f62005-02-22 08:39:57 +0000644 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100645 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000647 if (curbuf->b_ml.ml_flags & ML_EMPTY)
648 EMSG(_(e_emptybuf));
649 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000651 if (ex_pressedreturn)
652 {
653 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000654 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000655 msg_row = prev_msg_row;
656 if (prev_msg_row == Rows - 1)
657 msg_row--;
658 }
659 msg_col = 0;
660 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
661 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000664 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
665 {
666 if (curbuf->b_ml.ml_flags & ML_EMPTY)
667 EMSG(_(e_emptybuf));
668 else
669 EMSG(_("E501: At end-of-file"));
670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 }
672
673#ifdef FEAT_GUI
674 --hold_gui_events;
675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 --RedrawingDisabled;
677 --no_wait_return;
678 update_screen(CLEAR);
679 need_wait_return = FALSE;
680 msg_scroll = save_msg_scroll;
681}
682
683/*
684 * Execute a simple command line. Used for translated commands like "*".
685 */
686 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100687do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
689 return do_cmdline(cmd, NULL, NULL,
690 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
691}
692
693/*
694 * do_cmdline(): execute one Ex command line
695 *
696 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100697 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 * 2. Split up in parts separated with '|'.
699 *
700 * This function can be called recursively!
701 *
702 * flags:
703 * DOCMD_VERBOSE - The command will be included in the error message.
704 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100705 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 * DOCMD_KEYTYPED - Don't reset KeyTyped.
707 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
708 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
709 *
710 * return FAIL if cmdline could not be executed, OK otherwise
711 */
712 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100713do_cmdline(
714 char_u *cmdline,
715 char_u *(*fgetline)(int, void *, int),
716 void *cookie, /* argument for fgetline() */
717 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718{
719 char_u *next_cmdline; /* next cmd to execute */
720 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100721 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 static int recursive = 0; /* recursive depth */
723 int msg_didout_before_start = 0;
724 int count = 0; /* line number count */
725 int did_inc = FALSE; /* incremented RedrawingDisabled */
726 int retval = OK;
727#ifdef FEAT_EVAL
728 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000729 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 int current_line = 0; /* active line in lines_ga */
731 char_u *fname = NULL; /* function or script name */
732 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
733 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000734 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 int initial_trylevel;
736 struct msglist **saved_msg_list = NULL;
737 struct msglist *private_msg_list;
738
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100739 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100740 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000742 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000744 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100746# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747# define cmd_cookie cookie
748#endif
749 static int call_depth = 0; /* recursiveness */
750
751#ifdef FEAT_EVAL
752 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
753 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000754 * This ensures that the do_errthrow() call in do_one_cmd() does not
755 * combine the messages stored by an earlier invocation of do_one_cmd()
756 * with the command name of the later one. This would happen when
757 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 saved_msg_list = msg_list;
759 msg_list = &private_msg_list;
760 private_msg_list = NULL;
761#endif
762
763 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100764 * here. The value of 200 allows nested function calls, ":source", etc.
765 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100766 if (call_depth >= 200
767#ifdef FEAT_EVAL
768 && call_depth >= p_mfd
769#endif
770 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 {
772 EMSG(_("E169: Command too recursive"));
773#ifdef FEAT_EVAL
774 /* When converting to an exception, we do not include the command name
775 * since this is not an error of the specific command. */
776 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
777 msg_list = saved_msg_list;
778#endif
779 return FAIL;
780 }
781 ++call_depth;
782
783#ifdef FEAT_EVAL
784 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000785 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 cstack.cs_trylevel = 0;
787 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000788 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
790
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100791 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
793 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100794 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000795 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 ++ex_nesting_level;
797
798 /* Get the function or script name and the address where the next breakpoint
799 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 {
802 fname = func_name(real_cookie);
803 breakpoint = func_breakpoint(real_cookie);
804 dbg_tick = func_dbg_tick(real_cookie);
805 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100806 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 {
808 fname = sourcing_name;
809 breakpoint = source_breakpoint(real_cookie);
810 dbg_tick = source_dbg_tick(real_cookie);
811 }
812
813 /*
814 * Initialize "force_abort" and "suppress_errthrow" at the top level.
815 */
816 if (!recursive)
817 {
818 force_abort = FALSE;
819 suppress_errthrow = FALSE;
820 }
821
822 /*
823 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000824 * exception handling (used when debugging). Otherwise clear it to avoid
825 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000827 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000828 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000829 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200830 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831
832 initial_trylevel = trylevel;
833
834 /*
835 * "did_throw" will be set to TRUE when an exception is being thrown.
836 */
837 did_throw = FALSE;
838#endif
839 /*
840 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000841 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 * If force_abort is set, we cancel everything.
843 */
844 did_emsg = FALSE;
845
846 /*
847 * KeyTyped is only set when calling vgetc(). Reset it here when not
848 * calling vgetc() (sourced command lines).
849 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100850 if (!(flags & DOCMD_KEYTYPED)
851 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 KeyTyped = FALSE;
853
854 /*
855 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000856 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 * - for multiple commands on one line, separated with '|'
858 * - when repeating until there are no more lines (for ":source")
859 */
860 next_cmdline = cmdline;
861 do
862 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000863#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100864 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000865#endif
866
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000867 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 if (next_cmdline == NULL
869#ifdef FEAT_EVAL
870 && !force_abort
871 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000872 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873#endif
874 )
875 did_emsg = FALSE;
876
877 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000878 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100879 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 * 3. If a line is given: Make a copy, so we can mess with it.
881 */
882
883#ifdef FEAT_EVAL
884 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000885 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 {
887 /* Each '|' separated command is stored separately in lines_ga, to
888 * be able to jump to it. Don't use next_cmdline now. */
889 vim_free(cmdline_copy);
890 cmdline_copy = NULL;
891
892 /* Check if a function has returned or, unless it has an unclosed
893 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000894 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000897 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000898 func_line_end(real_cookie);
899# endif
900 if (func_has_ended(real_cookie))
901 {
902 retval = FAIL;
903 break;
904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000906#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000907 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100908 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000909 script_line_end();
910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
912 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100913 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 {
915 retval = FAIL;
916 break;
917 }
918
919 /* If breakpoints have been added/deleted need to check for it. */
920 if (breakpoint != NULL && dbg_tick != NULL
921 && *dbg_tick != debug_tick)
922 {
923 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100924 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 fname, sourcing_lnum);
926 *dbg_tick = debug_tick;
927 }
928
929 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
930 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
931
932 /* Did we encounter a breakpoint? */
933 if (breakpoint != NULL && *breakpoint != 0
934 && *breakpoint <= sourcing_lnum)
935 {
936 dbg_breakpoint(fname, sourcing_lnum);
937 /* Find next breakpoint. */
938 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100939 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 fname, sourcing_lnum);
941 *dbg_tick = debug_tick;
942 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000943# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000944 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000945 {
946 if (getline_is_func)
947 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100948 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000949 script_line_start();
950 }
951# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 }
953
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000954 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000956 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100957 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 * below, so that it stores lines in or reads them from
959 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000960 * while/for loop. */
961 cmd_getline = get_loop_line;
962 cmd_cookie = (void *)&cmd_loop_cookie;
963 cmd_loop_cookie.lines_gap = &lines_ga;
964 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100965 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000966 cmd_loop_cookie.cookie = cookie;
967 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 }
969 else
970 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100971 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 cmd_cookie = cookie;
973 }
974#endif
975
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100976 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 if (next_cmdline == NULL)
978 {
979 /*
980 * Need to set msg_didout for the first line after an ":if",
981 * otherwise the ":if" will be overwritten.
982 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100983 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100985 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986#ifdef FEAT_EVAL
987 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
988#else
989 0
990#endif
991 )) == NULL)
992 {
993 /* Don't call wait_return for aborted command line. The NULL
994 * returned for the end of a sourced file or executed function
995 * doesn't do this. */
996 if (KeyTyped && !(flags & DOCMD_REPEAT))
997 need_wait_return = FALSE;
998 retval = FAIL;
999 break;
1000 }
1001 used_getline = TRUE;
1002
1003 /*
1004 * Keep the first typed line. Clear it when more lines are typed.
1005 */
1006 if (flags & DOCMD_KEEPLINE)
1007 {
1008 vim_free(repeat_cmdline);
1009 if (count == 0)
1010 repeat_cmdline = vim_strsave(next_cmdline);
1011 else
1012 repeat_cmdline = NULL;
1013 }
1014 }
1015
1016 /* 3. Make a copy of the command so we can mess with it. */
1017 else if (cmdline_copy == NULL)
1018 {
1019 next_cmdline = vim_strsave(next_cmdline);
1020 if (next_cmdline == NULL)
1021 {
1022 EMSG(_(e_outofmem));
1023 retval = FAIL;
1024 break;
1025 }
1026 }
1027 cmdline_copy = next_cmdline;
1028
1029#ifdef FEAT_EVAL
1030 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001031 * Save the current line when inside a ":while" or ":for", and when
1032 * the command looks like a ":while" or ":for", because we may need it
1033 * later. When there is a '|' and another command, it is stored
1034 * separately, because we need to be able to jump back to it from an
1035 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001037 if (current_line == lines_ga.ga_len
1038 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001040 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 {
1042 retval = FAIL;
1043 break;
1044 }
1045 }
1046 did_endif = FALSE;
1047#endif
1048
1049 if (count++ == 0)
1050 {
1051 /*
1052 * All output from the commands is put below each other, without
1053 * waiting for a return. Don't do this when executing commands
1054 * from a script or when being called recursive (e.g. for ":e
1055 * +command file").
1056 */
1057 if (!(flags & DOCMD_NOWAIT) && !recursive)
1058 {
1059 msg_didout_before_start = msg_didout;
1060 msg_didany = FALSE; /* no output yet */
1061 msg_start();
1062 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001063 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 ++RedrawingDisabled;
1065 did_inc = TRUE;
1066 }
1067 }
1068
1069 if (p_verbose >= 15 && sourcing_name != NULL)
1070 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001072 verbose_enter_scroll();
1073
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 smsg((char_u *)_("line %ld: %s"),
1075 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001076 if (msg_silent == 0)
1077 msg_puts((char_u *)"\n"); /* don't overwrite this */
1078
1079 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 --no_wait_return;
1081 }
1082
1083 /*
1084 * 2. Execute one '|' separated command.
1085 * do_one_cmd() will return NULL if there is no trailing '|'.
1086 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1087 */
1088 ++recursive;
1089 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1090#ifdef FEAT_EVAL
1091 &cstack,
1092#endif
1093 cmd_getline, cmd_cookie);
1094 --recursive;
1095
1096#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 if (cmd_cookie == (void *)&cmd_loop_cookie)
1098 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001100 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101#endif
1102
1103 if (next_cmdline == NULL)
1104 {
1105 vim_free(cmdline_copy);
1106 cmdline_copy = NULL;
1107#ifdef FEAT_CMDHIST
1108 /*
1109 * If the command was typed, remember it for the ':' register.
1110 * Do this AFTER executing the command to make :@: work.
1111 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001112 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 && new_last_cmdline != NULL)
1114 {
1115 vim_free(last_cmdline);
1116 last_cmdline = new_last_cmdline;
1117 new_last_cmdline = NULL;
1118 }
1119#endif
1120 }
1121 else
1122 {
1123 /* need to copy the command after the '|' to cmdline_copy, for the
1124 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001125 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 next_cmdline = cmdline_copy;
1127 }
1128
1129
1130#ifdef FEAT_EVAL
1131 /* reset did_emsg for a function that is not aborted by an error */
1132 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001133 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 && !func_has_abort(real_cookie))
1135 did_emsg = FALSE;
1136
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 {
1139 ++current_line;
1140
1141 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001142 * An ":endwhile", ":endfor" and ":continue" is handled here.
1143 * If we were executing commands, jump back to the ":while" or
1144 * ":for".
1145 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001147 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001149 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001151 /* Jump back to the matching ":while" or ":for". Be careful
1152 * not to use a cs_line[] from an entry that isn't a ":while"
1153 * or ":for": It would make "current_line" invalid and can
1154 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (!did_emsg && !got_int && !did_throw
1156 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001157 && (cstack.cs_flags[cstack.cs_idx]
1158 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 && cstack.cs_line[cstack.cs_idx] >= 0
1160 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1161 {
1162 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001163 /* remember we jumped there */
1164 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 line_breakcheck(); /* check if CTRL-C typed */
1166
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001167 /* Check for the next breakpoint at or after the ":while"
1168 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 if (breakpoint != NULL)
1170 {
1171 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001172 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 fname,
1174 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1175 *dbg_tick = debug_tick;
1176 }
1177 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001178 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001182 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1183 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 }
1185 }
1186
1187 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001188 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001190 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001192 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1194 }
1195 }
1196
1197 /*
1198 * When not inside any ":while" loop, clear remembered lines.
1199 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 {
1202 if (lines_ga.ga_len > 0)
1203 {
1204 sourcing_lnum =
1205 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1206 free_cmdlines(&lines_ga);
1207 }
1208 current_line = 0;
1209 }
1210
1211 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001212 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1213 * being restored at the ":endtry". Reset them here and set the
1214 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1215 * This includes the case where a missing ":endif", ":endwhile" or
1216 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001218 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001220 cstack.cs_lflags &= ~CSL_HAD_FINA;
1221 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1222 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 did_throw ? (void *)current_exception : NULL);
1224 did_emsg = got_int = did_throw = FALSE;
1225 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1226 }
1227
1228 /* Update global "trylevel" for recursive calls to do_cmdline() from
1229 * within this loop. */
1230 trylevel = initial_trylevel + cstack.cs_trylevel;
1231
1232 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001233 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 * files) is aborted because of an error, an interrupt, or an uncaught
1235 * exception, cancel everything. If it is left normally, reset
1236 * force_abort to get the non-EH compatible abortion behavior for
1237 * the rest of the script.
1238 */
1239 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1240 force_abort = FALSE;
1241
1242 /* Convert an interrupt to an exception if appropriate. */
1243 (void)do_intthrow(&cstack);
1244#endif /* FEAT_EVAL */
1245
1246 }
1247 /*
1248 * Continue executing command lines when:
1249 * - no CTRL-C typed, no aborting error, no exception thrown or try
1250 * conditionals need to be checked for executing finally clauses or
1251 * catching an interrupt exception
1252 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001253 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 * looping for ":source" command or function call.
1255 */
1256 while (!((got_int
1257#ifdef FEAT_EVAL
1258 || (did_emsg && force_abort) || did_throw
1259#endif
1260 )
1261#ifdef FEAT_EVAL
1262 && cstack.cs_trylevel == 0
1263#endif
1264 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001265 && !(did_emsg
1266#ifdef FEAT_EVAL
1267 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001268 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001269 * the :endtry to be missed. */
1270 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1271#endif
1272 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001273 && (getline_equal(fgetline, cookie, getexmodeline)
1274 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 && (next_cmdline != NULL
1276#ifdef FEAT_EVAL
1277 || cstack.cs_idx >= 0
1278#endif
1279 || (flags & DOCMD_REPEAT)));
1280
1281 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001282 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283#ifdef FEAT_EVAL
1284 free_cmdlines(&lines_ga);
1285 ga_clear(&lines_ga);
1286
1287 if (cstack.cs_idx >= 0)
1288 {
1289 /*
1290 * If a sourced file or executed function ran to its end, report the
1291 * unclosed conditional.
1292 */
1293 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001294 && ((getline_equal(fgetline, cookie, getsourceline)
1295 && !source_finished(fgetline, cookie))
1296 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 && !func_has_ended(real_cookie))))
1298 {
1299 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1300 EMSG(_(e_endtry));
1301 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1302 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001303 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1304 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 else
1306 EMSG(_(e_endif));
1307 }
1308
1309 /*
1310 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1311 * ":endtry" in a sourced file or executed function. If the try
1312 * conditional is in its finally clause, ignore anything pending.
1313 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001314 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 */
1316 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001317 {
1318 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1319
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001320 if (idx >= 0)
1321 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001322 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1323 &cstack.cs_looplevel);
1324 }
1325 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 trylevel = initial_trylevel;
1327 }
1328
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001329 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1330 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001332 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 ? (char_u *)"endfunction" : (char_u *)NULL);
1334
1335 if (trylevel == 0)
1336 {
1337 /*
1338 * When an exception is being thrown out of the outermost try
1339 * conditional, discard the uncaught exception, disable the conversion
1340 * of interrupts or errors to exceptions, and ensure that no more
1341 * commands are executed.
1342 */
1343 if (did_throw)
1344 {
1345 void *p = NULL;
1346 char_u *saved_sourcing_name;
1347 int saved_sourcing_lnum;
1348 struct msglist *messages = NULL, *next;
1349
1350 /*
1351 * If the uncaught exception is a user exception, report it as an
1352 * error. If it is an error exception, display the saved error
1353 * message now. For an interrupt exception, do nothing; the
1354 * interrupt message is given elsewhere.
1355 */
1356 switch (current_exception->type)
1357 {
1358 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001359 vim_snprintf((char *)IObuff, IOSIZE,
1360 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 current_exception->value);
1362 p = vim_strsave(IObuff);
1363 break;
1364 case ET_ERROR:
1365 messages = current_exception->messages;
1366 current_exception->messages = NULL;
1367 break;
1368 case ET_INTERRUPT:
1369 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 }
1371
1372 saved_sourcing_name = sourcing_name;
1373 saved_sourcing_lnum = sourcing_lnum;
1374 sourcing_name = current_exception->throw_name;
1375 sourcing_lnum = current_exception->throw_lnum;
1376 current_exception->throw_name = NULL;
1377
1378 discard_current_exception(); /* uses IObuff if 'verbose' */
1379 suppress_errthrow = TRUE;
1380 force_abort = TRUE;
1381
1382 if (messages != NULL)
1383 {
1384 do
1385 {
1386 next = messages->next;
1387 emsg(messages->msg);
1388 vim_free(messages->msg);
1389 vim_free(messages);
1390 messages = next;
1391 }
1392 while (messages != NULL);
1393 }
1394 else if (p != NULL)
1395 {
1396 emsg(p);
1397 vim_free(p);
1398 }
1399 vim_free(sourcing_name);
1400 sourcing_name = saved_sourcing_name;
1401 sourcing_lnum = saved_sourcing_lnum;
1402 }
1403
1404 /*
1405 * On an interrupt or an aborting error not converted to an exception,
1406 * disable the conversion of errors to exceptions. (Interrupts are not
1407 * converted any more, here.) This enables also the interrupt message
1408 * when force_abort is set and did_emsg unset in case of an interrupt
1409 * from a finally clause after an error.
1410 */
1411 else if (got_int || (did_emsg && force_abort))
1412 suppress_errthrow = TRUE;
1413 }
1414
1415 /*
1416 * The current cstack will be freed when do_cmdline() returns. An uncaught
1417 * exception will have to be rethrown in the previous cstack. If a function
1418 * has just returned or a script file was just finished and the previous
1419 * cstack belongs to the same function or, respectively, script file, it
1420 * will have to be checked for finally clauses to be executed due to the
1421 * ":return" or ":finish". This is done in do_one_cmd().
1422 */
1423 if (did_throw)
1424 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001425 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001427 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 && ex_nesting_level > func_level(real_cookie) + 1))
1429 {
1430 if (!did_throw)
1431 check_cstack = TRUE;
1432 }
1433 else
1434 {
1435 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001436 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 --ex_nesting_level;
1438 /*
1439 * Go to debug mode when returning from a function in which we are
1440 * single-stepping.
1441 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001442 if ((getline_equal(fgetline, cookie, getsourceline)
1443 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001445 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 ? (char_u *)_("End of sourced file")
1447 : (char_u *)_("End of function"));
1448 }
1449
1450 /*
1451 * Restore the exception environment (done after returning from the
1452 * debugger).
1453 */
1454 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001455 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
1457 msg_list = saved_msg_list;
1458#endif /* FEAT_EVAL */
1459
1460 /*
1461 * If there was too much output to fit on the command line, ask the user to
1462 * hit return before redrawing the screen. With the ":global" command we do
1463 * this only once after the command is finished.
1464 */
1465 if (did_inc)
1466 {
1467 --RedrawingDisabled;
1468 --no_wait_return;
1469 msg_scroll = FALSE;
1470
1471 /*
1472 * When just finished an ":if"-":else" which was typed, no need to
1473 * wait for hit-return. Also for an error situation.
1474 */
1475 if (retval == FAIL
1476#ifdef FEAT_EVAL
1477 || (did_endif && KeyTyped && !did_emsg)
1478#endif
1479 )
1480 {
1481 need_wait_return = FALSE;
1482 msg_didany = FALSE; /* don't wait when restarting edit */
1483 }
1484 else if (need_wait_return)
1485 {
1486 /*
1487 * The msg_start() above clears msg_didout. The wait_return we do
1488 * here should not overwrite the command that may be shown before
1489 * doing that.
1490 */
1491 msg_didout |= msg_didout_before_start;
1492 wait_return(FALSE);
1493 }
1494 }
1495
Bram Moolenaar2a942252012-11-28 23:03:07 +01001496#ifdef FEAT_EVAL
1497 did_endif = FALSE; /* in case do_cmdline used recursively */
1498#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 /*
1500 * Reset if_level, in case a sourced script file contains more ":if" than
1501 * ":endif" (could be ":if x | foo | endif").
1502 */
1503 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001504#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001505
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 --call_depth;
1507 return retval;
1508}
1509
1510#ifdef FEAT_EVAL
1511/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001512 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 */
1514 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001515get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001517 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 wcmd_T *wp;
1519 char_u *line;
1520
1521 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1522 {
1523 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001524 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001526 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 if (cp->getline == NULL)
1528 line = getcmdline(c, 0L, indent);
1529 else
1530 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001531 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 ++cp->current_line;
1533
1534 return line;
1535 }
1536
1537 KeyTyped = FALSE;
1538 ++cp->current_line;
1539 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1540 sourcing_lnum = wp->lnum;
1541 return vim_strsave(wp->line);
1542}
1543
1544/*
1545 * Store a line in "gap" so that a ":while" loop can execute it again.
1546 */
1547 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001548store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
1550 if (ga_grow(gap, 1) == FAIL)
1551 return FAIL;
1552 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1553 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1554 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 return OK;
1556}
1557
1558/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001559 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 */
1561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001562free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
1564 while (gap->ga_len > 0)
1565 {
1566 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1567 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 }
1569}
1570#endif
1571
1572/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001573 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1574 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001577getline_equal(
1578 char_u *(*fgetline)(int, void *, int),
1579 void *cookie UNUSED, /* argument for fgetline() */
1580 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
1582#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001583 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001584 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
Bram Moolenaar89d40322006-08-29 15:30:07 +00001586 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001587 * function that's originally used to obtain the lines. This may be
1588 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001589 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001590 cp = (struct loop_cookie *)cookie;
1591 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 {
1593 gp = cp->getline;
1594 cp = cp->cookie;
1595 }
1596 return gp == func;
1597#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001598 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599#endif
1600}
1601
1602#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1603/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001604 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 * getline function. Otherwise return "cookie".
1606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001608getline_cookie(
1609 char_u *(*fgetline)(int, void *, int) UNUSED,
1610 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611{
1612# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001613 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001614 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
Bram Moolenaar89d40322006-08-29 15:30:07 +00001616 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001617 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001619 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001620 cp = (struct loop_cookie *)cookie;
1621 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
1623 gp = cp->getline;
1624 cp = cp->cookie;
1625 }
1626 return cp;
1627# else
1628 return cookie;
1629# endif
1630}
1631#endif
1632
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001633
1634/*
1635 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1636 * ":bwipeout", etc.
1637 * Returns the buffer number.
1638 */
1639 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001640compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001641{
1642 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001643 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001644 int count = offset;
1645
1646 buf = firstbuf;
1647 while (buf->b_next != NULL && buf->b_fnum < lnum)
1648 buf = buf->b_next;
1649 while (count != 0)
1650 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001651 count += (offset < 0) ? 1 : -1;
1652 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1653 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001654 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001655 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001656 if (addr_type == ADDR_LOADED_BUFFERS)
1657 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001658 while (buf->b_ml.ml_mfp == NULL)
1659 {
1660 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1661 if (nextbuf == NULL)
1662 break;
1663 buf = nextbuf;
1664 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001665 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001666 /* we might have gone too far, last buffer is not loadedd */
1667 if (addr_type == ADDR_LOADED_BUFFERS)
1668 while (buf->b_ml.ml_mfp == NULL)
1669 {
1670 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1671 if (nextbuf == NULL)
1672 break;
1673 buf = nextbuf;
1674 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001675 return buf->b_fnum;
1676}
1677
Bram Moolenaarf240e182014-11-27 18:33:02 +01001678#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001679static int current_win_nr(win_T *win);
1680static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001681
1682 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001683current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001684{
1685 win_T *wp;
1686 int nr = 0;
1687
Bram Moolenaar29323592016-07-24 22:04:11 +02001688 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001689 {
1690 ++nr;
1691 if (wp == win)
1692 break;
1693 }
1694 return nr;
1695}
1696
1697 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001698current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001699{
1700 tabpage_T *tp;
1701 int nr = 0;
1702
Bram Moolenaar29323592016-07-24 22:04:11 +02001703 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001704 {
1705 ++nr;
1706 if (tp == tab)
1707 break;
1708 }
1709 return nr;
1710}
1711
1712# define CURRENT_WIN_NR current_win_nr(curwin)
1713# define LAST_WIN_NR current_win_nr(NULL)
1714# define CURRENT_TAB_NR current_tab_nr(curtab)
1715# define LAST_TAB_NR current_tab_nr(NULL)
1716#else
1717# define CURRENT_WIN_NR 1
1718# define LAST_WIN_NR 1
1719# define CURRENT_TAB_NR 1
1720# define LAST_TAB_NR 1
1721#endif
1722
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001723
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724/*
1725 * Execute one Ex command.
1726 *
1727 * If 'sourcing' is TRUE, the command will be included in the error message.
1728 *
1729 * 1. skip comment lines and leading space
1730 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001731 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001732 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001733 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001734 * 6. parse arguments
1735 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001737 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 *
1739 * This function may be called recursively!
1740 */
1741#if (_MSC_VER == 1200)
1742/*
Bram Moolenaared203462004-06-16 11:19:22 +00001743 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001745 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746#endif
1747 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001748do_one_cmd(
1749 char_u **cmdlinep,
1750 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001752 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001754 char_u *(*fgetline)(int, void *, int),
1755 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
1757 char_u *p;
1758 linenr_T lnum;
1759 long n;
1760 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001761 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 exarg_T ea; /* Ex command arguments */
1763 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001764 int save_msg_scroll = msg_scroll;
1765 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001767#ifdef HAVE_SANDBOX
1768 int did_sandbox = FALSE;
1769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 cmdmod_T save_cmdmod;
1771 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001772 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001773 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774
1775 vim_memset(&ea, 0, sizeof(ea));
1776 ea.line1 = 1;
1777 ea.line2 = 1;
1778#ifdef FEAT_EVAL
1779 ++ex_nesting_level;
1780#endif
1781
Bram Moolenaar21691f82012-12-05 19:13:18 +01001782 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 if (quitmore
1784#ifdef FEAT_EVAL
1785 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001786 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001787#endif
1788#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001789 /* avoid that an autocommand, e.g. QuitPre, does this */
1790 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791#endif
1792 )
1793 --quitmore;
1794
1795 /*
1796 * Reset browse, confirm, etc.. They are restored when returning, for
1797 * recursive calls.
1798 */
1799 save_cmdmod = cmdmod;
1800 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1801
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001802 /* "#!anything" is handled like a comment. */
1803 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1804 goto doend;
1805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 /*
1807 * Repeat until no more command modifiers are found.
1808 */
1809 ea.cmd = *cmdlinep;
1810 for (;;)
1811 {
1812/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001813 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 */
1815 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1816 ++ea.cmd;
1817
1818 /* in ex mode, an empty line works like :+ */
1819 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001820 && (getline_equal(fgetline, cookie, getexmodeline)
1821 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001822 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
1824 ea.cmd = (char_u *)"+";
1825 ex_pressedreturn = TRUE;
1826 }
1827
1828 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001829 if (*ea.cmd == '"')
1830 goto doend;
1831 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001832 {
1833 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836
1837/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001838 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001840 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 switch (*p)
1842 {
1843 /* When adding an entry, also modify cmd_exists(). */
1844 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1845 break;
1846#ifdef FEAT_WINDOWS
1847 cmdmod.split |= WSP_ABOVE;
1848#endif
1849 continue;
1850
1851 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1852 {
1853#ifdef FEAT_WINDOWS
1854 cmdmod.split |= WSP_BELOW;
1855#endif
1856 continue;
1857 }
1858 if (checkforcmd(&ea.cmd, "browse", 3))
1859 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001860#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 cmdmod.browse = TRUE;
1862#endif
1863 continue;
1864 }
1865 if (!checkforcmd(&ea.cmd, "botright", 2))
1866 break;
1867#ifdef FEAT_WINDOWS
1868 cmdmod.split |= WSP_BOT;
1869#endif
1870 continue;
1871
1872 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1873 break;
1874#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1875 cmdmod.confirm = TRUE;
1876#endif
1877 continue;
1878
1879 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1880 {
1881 cmdmod.keepmarks = TRUE;
1882 continue;
1883 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001884 if (checkforcmd(&ea.cmd, "keepalt", 5))
1885 {
1886 cmdmod.keepalt = TRUE;
1887 continue;
1888 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001889 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1890 {
1891 cmdmod.keeppatterns = TRUE;
1892 continue;
1893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1895 break;
1896 cmdmod.keepjumps = TRUE;
1897 continue;
1898
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001899 case 'f': /* only accept ":filter {pat} cmd" */
1900 {
1901 char_u *reg_pat;
1902
1903 if (!checkforcmd(&p, "filter", 4)
1904 || *p == NUL || ends_excmd(*p))
1905 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001906 if (*p == '!')
1907 {
1908 cmdmod.filter_force = TRUE;
1909 p = skipwhite(p + 1);
1910 if (*p == NUL || ends_excmd(*p))
1911 break;
1912 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001913 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1914 if (p == NULL || *p == NUL)
1915 break;
1916 cmdmod.filter_regmatch.regprog =
1917 vim_regcomp(reg_pat, RE_MAGIC);
1918 if (cmdmod.filter_regmatch.regprog == NULL)
1919 break;
1920 ea.cmd = p;
1921 continue;
1922 }
1923
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 /* ":hide" and ":hide | cmd" are not modifiers */
1925 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1926 || *p == NUL || ends_excmd(*p))
1927 break;
1928 ea.cmd = p;
1929 cmdmod.hide = TRUE;
1930 continue;
1931
1932 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1933 {
1934 cmdmod.lockmarks = TRUE;
1935 continue;
1936 }
1937
1938 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1939 break;
1940#ifdef FEAT_WINDOWS
1941 cmdmod.split |= WSP_ABOVE;
1942#endif
1943 continue;
1944
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001945 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001946 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001947#ifdef FEAT_AUTOCMD
1948 if (cmdmod.save_ei == NULL)
1949 {
1950 /* Set 'eventignore' to "all". Restore the
1951 * existing option value later. */
1952 cmdmod.save_ei = vim_strsave(p_ei);
1953 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001954 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001955 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001956#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001957 continue;
1958 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001959 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001960 break;
1961 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001962 continue;
1963
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1965 break;
1966#ifdef FEAT_WINDOWS
1967 cmdmod.split |= WSP_BELOW;
1968#endif
1969 continue;
1970
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001971 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1972 {
1973#ifdef HAVE_SANDBOX
1974 if (!did_sandbox)
1975 ++sandbox;
1976 did_sandbox = TRUE;
1977#endif
1978 continue;
1979 }
1980 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001982 if (save_msg_silent == -1)
1983 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 ++msg_silent;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001985 if (*ea.cmd == '!' && !VIM_ISWHITE(ea.cmd[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 {
1987 /* ":silent!", but not "silent !cmd" */
1988 ea.cmd = skipwhite(ea.cmd + 1);
1989 ++emsg_silent;
1990 ++did_esilent;
1991 }
1992 continue;
1993
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001994 case 't': if (checkforcmd(&p, "tab", 3))
1995 {
1996#ifdef FEAT_WINDOWS
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001997 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01001998 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001999 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002000 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002001 else
2002 {
2003 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2004 {
2005 errormsg = (char_u *)_(e_invrange);
2006 goto doend;
2007 }
2008 cmdmod.tab = tabnr + 1;
2009 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002010 ea.cmd = p;
2011#endif
2012 continue;
2013 }
2014 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 break;
2016#ifdef FEAT_WINDOWS
2017 cmdmod.split |= WSP_TOP;
2018#endif
2019 continue;
2020
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002021 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2022 break;
2023 if (save_msg_silent == -1)
2024 save_msg_silent = msg_silent;
2025 msg_silent = 0;
2026 continue;
2027
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2029 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002030#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 cmdmod.split |= WSP_VERT;
2032#endif
2033 continue;
2034 }
2035 if (!checkforcmd(&p, "verbose", 4))
2036 break;
2037 if (verbose_save < 0)
2038 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002039 if (vim_isdigit(*ea.cmd))
2040 p_verbose = atoi((char *)ea.cmd);
2041 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 p_verbose = 1;
2043 ea.cmd = p;
2044 continue;
2045 }
2046 break;
2047 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002048 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049
2050#ifdef FEAT_EVAL
2051 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2052 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2053#else
2054 ea.skip = (if_level > 0);
2055#endif
2056
2057#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002058# ifdef FEAT_PROFILE
2059 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002060 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002061 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002062 if (getline_equal(fgetline, cookie, get_func_line))
2063 func_line_exec(getline_cookie(fgetline, cookie));
2064 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002065 script_line_exec();
2066 }
2067#endif
2068
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 /* May go to debug mode. If this happens and the ">quit" debug command is
2070 * used, throw an interrupt exception and skip the next command. */
2071 dbg_check_breakpoint(&ea);
2072 if (!ea.skip && got_int)
2073 {
2074 ea.skip = TRUE;
2075 (void)do_intthrow(cstack);
2076 }
2077#endif
2078
2079/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002080 * 3. Skip over the range to find the command. Let "p" point to after it.
2081 *
2082 * We need the command to know what kind of range it uses.
2083 */
2084 cmd = ea.cmd;
2085 ea.cmd = skip_range(ea.cmd, NULL);
2086 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2087 ea.cmd = skipwhite(ea.cmd + 1);
2088 p = find_command(&ea, NULL);
2089
2090/*
2091 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 *
2093 * where 'addr' is:
2094 *
2095 * % (entire file)
2096 * $ [+-NUM]
2097 * 'x [+-NUM] (where x denotes a currently defined mark)
2098 * . [+-NUM]
2099 * [+-NUM]..
2100 * NUM
2101 *
2102 * The ea.cmd pointer is updated to point to the first character following the
2103 * range spec. If an initial address is found, but no second, the upper bound
2104 * is equal to the lower.
2105 */
2106
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002107 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002108 if (!IS_USER_CMDIDX(ea.cmdidx))
2109 {
2110 if (ea.cmdidx != CMD_SIZE)
2111 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2112 else
2113 ea.addr_type = ADDR_LINES;
2114
2115#ifdef FEAT_WINDOWS
2116 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002117 if (ea.cmdidx == CMD_wincmd && p != NULL)
2118 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002119#endif
2120 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002121
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002123 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 for (;;)
2125 {
2126 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002127 switch (ea.addr_type)
2128 {
2129 case ADDR_LINES:
2130 /* default is current line number */
2131 ea.line2 = curwin->w_cursor.lnum;
2132 break;
2133 case ADDR_WINDOWS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002134 ea.line2 = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002135 break;
2136 case ADDR_ARGUMENTS:
2137 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002138 if (ea.line2 > ARGCOUNT)
2139 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002140 break;
2141 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002142 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002143 ea.line2 = curbuf->b_fnum;
2144 break;
2145 case ADDR_TABS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002146 ea.line2 = CURRENT_TAB_NR;
2147 break;
2148 case ADDR_TABS_RELATIVE:
2149 ea.line2 = 1;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002150 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002151#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002152 case ADDR_QUICKFIX:
2153 ea.line2 = qf_get_cur_valid_idx(&ea);
2154 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002155#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002158 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002159 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 if (ea.cmd == NULL) /* error detected */
2161 goto doend;
2162 if (lnum == MAXLNUM)
2163 {
2164 if (*ea.cmd == '%') /* '%' - all lines */
2165 {
2166 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002167 switch (ea.addr_type)
2168 {
2169 case ADDR_LINES:
2170 ea.line1 = 1;
2171 ea.line2 = curbuf->b_ml.ml_line_count;
2172 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002173 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002174 {
2175 buf_T *buf = firstbuf;
2176
2177 while (buf->b_next != NULL
2178 && buf->b_ml.ml_mfp == NULL)
2179 buf = buf->b_next;
2180 ea.line1 = buf->b_fnum;
2181 buf = lastbuf;
2182 while (buf->b_prev != NULL
2183 && buf->b_ml.ml_mfp == NULL)
2184 buf = buf->b_prev;
2185 ea.line2 = buf->b_fnum;
2186 break;
2187 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002188 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002189 ea.line1 = firstbuf->b_fnum;
2190 ea.line2 = lastbuf->b_fnum;
2191 break;
2192 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002193 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002194 if (IS_USER_CMDIDX(ea.cmdidx))
2195 {
2196 ea.line1 = 1;
2197 ea.line2 = ea.addr_type == ADDR_WINDOWS
2198 ? LAST_WIN_NR : LAST_TAB_NR;
2199 }
2200 else
2201 {
2202 /* there is no Vim command which uses '%' and
2203 * ADDR_WINDOWS or ADDR_TABS */
2204 errormsg = (char_u *)_(e_invrange);
2205 goto doend;
2206 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002207 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002208 case ADDR_TABS_RELATIVE:
2209 errormsg = (char_u *)_(e_invrange);
2210 goto doend;
2211 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002212 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002213 if (ARGCOUNT == 0)
2214 ea.line1 = ea.line2 = 0;
2215 else
2216 {
2217 ea.line1 = 1;
2218 ea.line2 = ARGCOUNT;
2219 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002220 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002221#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002222 case ADDR_QUICKFIX:
2223 ea.line1 = 1;
2224 ea.line2 = qf_get_size(&ea);
2225 if (ea.line2 == 0)
2226 ea.line2 = 1;
2227 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002228#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 ++ea.addr_count;
2231 }
2232 /* '*' - visual area */
2233 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2234 {
2235 pos_T *fp;
2236
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002237 if (ea.addr_type != ADDR_LINES)
2238 {
2239 errormsg = (char_u *)_(e_invrange);
2240 goto doend;
2241 }
2242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 ++ea.cmd;
2244 if (!ea.skip)
2245 {
2246 fp = getmark('<', FALSE);
2247 if (check_mark(fp) == FAIL)
2248 goto doend;
2249 ea.line1 = fp->lnum;
2250 fp = getmark('>', FALSE);
2251 if (check_mark(fp) == FAIL)
2252 goto doend;
2253 ea.line2 = fp->lnum;
2254 ++ea.addr_count;
2255 }
2256 }
2257 }
2258 else
2259 ea.line2 = lnum;
2260 ea.addr_count++;
2261
2262 if (*ea.cmd == ';')
2263 {
2264 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002265 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +01002267 /* don't leave the cursor on an illegal line or column */
2268 check_cursor();
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 }
2271 else if (*ea.cmd != ',')
2272 break;
2273 ++ea.cmd;
2274 }
2275
2276 /* One address given: set start and end lines */
2277 if (ea.addr_count == 1)
2278 {
2279 ea.line1 = ea.line2;
2280 /* ... but only implicit: really no address given */
2281 if (lnum == MAXLNUM)
2282 ea.addr_count = 0;
2283 }
2284
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002286 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 */
2288
2289 /*
2290 * Skip ':' and any white space
2291 */
2292 ea.cmd = skipwhite(ea.cmd);
2293 while (*ea.cmd == ':')
2294 ea.cmd = skipwhite(ea.cmd + 1);
2295
2296 /*
2297 * If we got a line, but no command, then go to the line.
2298 * If we find a '|' or '\n' we set ea.nextcmd.
2299 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002300 if (*ea.cmd == NUL || *ea.cmd == '"'
2301 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
2303 /*
2304 * strange vi behaviour:
2305 * ":3" jumps to line 3
2306 * ":3|..." prints line 3
2307 * ":|" prints current line
2308 */
2309 if (ea.skip) /* skip this if inside :if */
2310 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002311 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 {
2313 ea.cmdidx = CMD_print;
2314 ea.argt = RANGE+COUNT+TRLBAR;
2315 if ((errormsg = invalid_range(&ea)) == NULL)
2316 {
2317 correct_range(&ea);
2318 ex_print(&ea);
2319 }
2320 }
2321 else if (ea.addr_count != 0)
2322 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002323 if (ea.line2 > curbuf->b_ml.ml_line_count)
2324 {
2325 /* With '-' in 'cpoptions' a line number past the file is an
2326 * error, otherwise put it at the end of the file. */
2327 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2328 ea.line2 = -1;
2329 else
2330 ea.line2 = curbuf->b_ml.ml_line_count;
2331 }
2332
2333 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002334 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 else
2336 {
2337 if (ea.line2 == 0)
2338 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 else
2340 curwin->w_cursor.lnum = ea.line2;
2341 beginline(BL_SOL | BL_FIX);
2342 }
2343 }
2344 goto doend;
2345 }
2346
Bram Moolenaard5005162014-08-22 23:05:54 +02002347#ifdef FEAT_AUTOCMD
2348 /* If this looks like an undefined user command and there are CmdUndefined
2349 * autocommands defined, trigger the matching autocommands. */
2350 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2351 && ASCII_ISUPPER(*ea.cmd)
2352 && has_cmdundefined())
2353 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002354 int ret;
2355
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002356 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002357 while (ASCII_ISALNUM(*p))
2358 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002359 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002360 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2361 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002362 /* If the autocommands did something and didn't cause an error, try
2363 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002364 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002365 }
2366#endif
2367
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368#ifdef FEAT_USR_CMDS
2369 if (p == NULL)
2370 {
2371 if (!ea.skip)
2372 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2373 goto doend;
2374 }
2375 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002376 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2377 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 {
2379 errormsg = uc_fun_cmd();
2380 goto doend;
2381 }
2382#endif
2383 if (ea.cmdidx == CMD_SIZE)
2384 {
2385 if (!ea.skip)
2386 {
2387 STRCPY(IObuff, _("E492: Not an editor command"));
2388 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002389 {
2390 /* If the modifier was parsed OK the error must be in the
2391 * following command */
2392 if (after_modifier != NULL)
2393 append_command(after_modifier);
2394 else
2395 append_command(*cmdlinep);
2396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002398 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
2400 goto doend;
2401 }
2402
Bram Moolenaar958636c2014-10-21 20:01:58 +02002403 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2404 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002405#ifdef HAVE_EX_SCRIPT_NI
2406 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2407#endif
2408 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409
2410#ifndef FEAT_EVAL
2411 /*
2412 * When the expression evaluation is disabled, recognize the ":if" and
2413 * ":endif" commands and ignore everything in between it.
2414 */
2415 if (ea.cmdidx == CMD_if)
2416 ++if_level;
2417 if (if_level)
2418 {
2419 if (ea.cmdidx == CMD_endif)
2420 --if_level;
2421 goto doend;
2422 }
2423
2424#endif
2425
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002426 /* forced commands */
2427 if (*p == '!' && ea.cmdidx != CMD_substitute
2428 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 {
2430 ++p;
2431 ea.forceit = TRUE;
2432 }
2433 else
2434 ea.forceit = FALSE;
2435
2436/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002437 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002439 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002440 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
2442 if (!ea.skip)
2443 {
2444#ifdef HAVE_SANDBOX
2445 if (sandbox != 0 && !(ea.argt & SBOXOK))
2446 {
2447 /* Command not allowed in sandbox. */
2448 errormsg = (char_u *)_(e_sandbox);
2449 goto doend;
2450 }
2451#endif
2452 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2453 {
2454 /* Command not allowed in non-'modifiable' buffer */
2455 errormsg = (char_u *)_(e_modifiable);
2456 goto doend;
2457 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002458
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002459 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002460 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002462 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002463 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 goto doend;
2465 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002466#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002467 /* Disallow editing another buffer when "curbuf_lock" is set.
2468 * Do allow ":edit" (check for argument later).
2469 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002470 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002471 && ea.cmdidx != CMD_edit
2472 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002473 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002474 && curbuf_locked())
2475 goto doend;
2476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
2478 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2479 {
2480 /* no range allowed */
2481 errormsg = (char_u *)_(e_norange);
2482 goto doend;
2483 }
2484 }
2485
2486 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2487 {
2488 errormsg = (char_u *)_(e_nobang);
2489 goto doend;
2490 }
2491
2492 /*
2493 * Don't complain about the range if it is not used
2494 * (could happen if line_count is accidentally set to 0).
2495 */
2496 if (!ea.skip && !ni)
2497 {
2498 /*
2499 * If the range is backwards, ask for confirmation and, if given, swap
2500 * ea.line1 & ea.line2 so it's forwards again.
2501 * When global command is busy, don't ask, will fail below.
2502 */
2503 if (!global_busy && ea.line1 > ea.line2)
2504 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002505 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002507 if (sourcing || exmode_active)
2508 {
2509 errormsg = (char_u *)_("E493: Backwards range given");
2510 goto doend;
2511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 if (ask_yesno((char_u *)
2513 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002514 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 }
2516 lnum = ea.line1;
2517 ea.line1 = ea.line2;
2518 ea.line2 = lnum;
2519 }
2520 if ((errormsg = invalid_range(&ea)) != NULL)
2521 goto doend;
2522 }
2523
2524 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2525 ea.line2 = 1;
2526
2527 correct_range(&ea);
2528
2529#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002530 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2531 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
2533 /* Put the first line at the start of a closed fold, put the last line
2534 * at the end of a closed fold. */
2535 (void)hasFolding(ea.line1, &ea.line1, NULL);
2536 (void)hasFolding(ea.line2, NULL, &ea.line2);
2537 }
2538#endif
2539
2540#ifdef FEAT_QUICKFIX
2541 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002542 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 * option here, so things like % get expanded.
2544 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002545 p = replace_makeprg(&ea, p, cmdlinep);
2546 if (p == NULL)
2547 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548#endif
2549
2550 /*
2551 * Skip to start of argument.
2552 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2553 */
2554 if (ea.cmdidx == CMD_bang)
2555 ea.arg = p;
2556 else
2557 ea.arg = skipwhite(p);
2558
2559 /*
2560 * Check for "++opt=val" argument.
2561 * Must be first, allow ":w ++enc=utf8 !cmd"
2562 */
2563 if (ea.argt & ARGOPT)
2564 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2565 if (getargopt(&ea) == FAIL && !ni)
2566 {
2567 errormsg = (char_u *)_(e_invarg);
2568 goto doend;
2569 }
2570
2571 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2572 {
2573 if (*ea.arg == '>') /* append */
2574 {
2575 if (*++ea.arg != '>') /* typed wrong */
2576 {
2577 errormsg = (char_u *)_("E494: Use w or w>>");
2578 goto doend;
2579 }
2580 ea.arg = skipwhite(ea.arg + 1);
2581 ea.append = TRUE;
2582 }
2583 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2584 {
2585 ++ea.arg;
2586 ea.usefilter = TRUE;
2587 }
2588 }
2589
2590 if (ea.cmdidx == CMD_read)
2591 {
2592 if (ea.forceit)
2593 {
2594 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2595 ea.forceit = FALSE;
2596 }
2597 else if (*ea.arg == '!') /* :r !filter */
2598 {
2599 ++ea.arg;
2600 ea.usefilter = TRUE;
2601 }
2602 }
2603
2604 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2605 {
2606 ea.amount = 1;
2607 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2608 {
2609 ++ea.arg;
2610 ++ea.amount;
2611 }
2612 ea.arg = skipwhite(ea.arg);
2613 }
2614
2615 /*
2616 * Check for "+command" argument, before checking for next command.
2617 * Don't do this for ":read !cmd" and ":write !cmd".
2618 */
2619 if ((ea.argt & EDITCMD) && !ea.usefilter)
2620 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2621
2622 /*
2623 * Check for '|' to separate commands and '"' to start comments.
2624 * Don't do this for ":read !cmd" and ":write !cmd".
2625 */
2626 if ((ea.argt & TRLBAR) && !ea.usefilter)
2627 separate_nextcmd(&ea);
2628
2629 /*
2630 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002631 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2632 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002634 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002635 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002636 || ea.cmdidx == CMD_global
2637 || ea.cmdidx == CMD_vglobal
2638 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 {
2640 for (p = ea.arg; *p; ++p)
2641 {
2642 /* Remove one backslash before a newline, so that it's possible to
2643 * pass a newline to the shell and also a newline that is preceded
2644 * with a backslash. This makes it impossible to end a shell
2645 * command in a backslash, but that doesn't appear useful.
2646 * Halving the number of backslashes is incompatible with previous
2647 * versions. */
2648 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002649 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 else if (*p == '\n')
2651 {
2652 ea.nextcmd = p + 1;
2653 *p = NUL;
2654 break;
2655 }
2656 }
2657 }
2658
2659 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2660 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002661 buf_T *buf;
2662
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002664 switch (ea.addr_type)
2665 {
2666 case ADDR_LINES:
2667 ea.line2 = curbuf->b_ml.ml_line_count;
2668 break;
2669 case ADDR_LOADED_BUFFERS:
2670 buf = firstbuf;
2671 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2672 buf = buf->b_next;
2673 ea.line1 = buf->b_fnum;
2674 buf = lastbuf;
2675 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2676 buf = buf->b_prev;
2677 ea.line2 = buf->b_fnum;
2678 break;
2679 case ADDR_BUFFERS:
2680 ea.line1 = firstbuf->b_fnum;
2681 ea.line2 = lastbuf->b_fnum;
2682 break;
2683 case ADDR_WINDOWS:
2684 ea.line2 = LAST_WIN_NR;
2685 break;
2686 case ADDR_TABS:
2687 ea.line2 = LAST_TAB_NR;
2688 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002689 case ADDR_TABS_RELATIVE:
2690 ea.line2 = 1;
2691 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002692 case ADDR_ARGUMENTS:
2693 if (ARGCOUNT == 0)
2694 ea.line1 = ea.line2 = 0;
2695 else
2696 ea.line2 = ARGCOUNT;
2697 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002698#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002699 case ADDR_QUICKFIX:
2700 ea.line2 = qf_get_size(&ea);
2701 if (ea.line2 == 0)
2702 ea.line2 = 1;
2703 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002704#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 }
2707
2708 /* accept numbered register only when no count allowed (:put) */
2709 if ( (ea.argt & REGSTR)
2710 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002711 /* Do not allow register = for user commands */
2712 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2714 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002715#ifndef FEAT_CLIPBOARD
2716 /* check these explicitly for a more specific error message */
2717 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002719 errormsg = (char_u *)_(e_invalidreg);
2720 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 }
2722#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002723 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2724 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002725 {
2726 ea.regname = *ea.arg++;
2727#ifdef FEAT_EVAL
2728 /* for '=' register: accept the rest of the line as an expression */
2729 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2730 {
2731 set_expr_line(vim_strsave(ea.arg));
2732 ea.arg += STRLEN(ea.arg);
2733 }
2734#endif
2735 ea.arg = skipwhite(ea.arg);
2736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 }
2738
2739 /*
2740 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2741 * count, it's a buffer name.
2742 */
2743 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2744 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002745 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 {
2747 n = getdigits(&ea.arg);
2748 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002749 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 {
2751 errormsg = (char_u *)_(e_zerocount);
2752 goto doend;
2753 }
2754 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2755 {
2756 ea.line2 = n;
2757 if (ea.addr_count == 0)
2758 ea.addr_count = 1;
2759 }
2760 else
2761 {
2762 ea.line1 = ea.line2;
2763 ea.line2 += n - 1;
2764 ++ea.addr_count;
2765 /*
2766 * Be vi compatible: no error message for out of range.
2767 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002768 if (ea.addr_type == ADDR_LINES
2769 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 ea.line2 = curbuf->b_ml.ml_line_count;
2771 }
2772 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002773
2774 /*
2775 * Check for flags: 'l', 'p' and '#'.
2776 */
2777 if (ea.argt & EXFLAGS)
2778 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002780 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002781 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 {
2783 errormsg = (char_u *)_(e_trailing);
2784 goto doend;
2785 }
2786
2787 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2788 {
2789 errormsg = (char_u *)_(e_argreq);
2790 goto doend;
2791 }
2792
2793#ifdef FEAT_EVAL
2794 /*
2795 * Skip the command when it's not going to be executed.
2796 * The commands like :if, :endif, etc. always need to be executed.
2797 * Also make an exception for commands that handle a trailing command
2798 * themselves.
2799 */
2800 if (ea.skip)
2801 {
2802 switch (ea.cmdidx)
2803 {
2804 /* commands that need evaluation */
2805 case CMD_while:
2806 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002807 case CMD_for:
2808 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 case CMD_if:
2810 case CMD_elseif:
2811 case CMD_else:
2812 case CMD_endif:
2813 case CMD_try:
2814 case CMD_catch:
2815 case CMD_finally:
2816 case CMD_endtry:
2817 case CMD_function:
2818 break;
2819
2820 /* Commands that handle '|' themselves. Check: A command should
2821 * either have the TRLBAR flag, appear in this list or appear in
2822 * the list at ":help :bar". */
2823 case CMD_aboveleft:
2824 case CMD_and:
2825 case CMD_belowright:
2826 case CMD_botright:
2827 case CMD_browse:
2828 case CMD_call:
2829 case CMD_confirm:
2830 case CMD_delfunction:
2831 case CMD_djump:
2832 case CMD_dlist:
2833 case CMD_dsearch:
2834 case CMD_dsplit:
2835 case CMD_echo:
2836 case CMD_echoerr:
2837 case CMD_echomsg:
2838 case CMD_echon:
2839 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002840 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 case CMD_help:
2842 case CMD_hide:
2843 case CMD_ijump:
2844 case CMD_ilist:
2845 case CMD_isearch:
2846 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002847 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 case CMD_keepjumps:
2849 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002850 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 case CMD_leftabove:
2852 case CMD_let:
2853 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002854 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002856 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002857 case CMD_noautocmd:
2858 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 case CMD_perl:
2860 case CMD_psearch:
2861 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002862 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002863 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 case CMD_return:
2865 case CMD_rightbelow:
2866 case CMD_ruby:
2867 case CMD_silent:
2868 case CMD_smagic:
2869 case CMD_snomagic:
2870 case CMD_substitute:
2871 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002872 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 case CMD_tcl:
2874 case CMD_throw:
2875 case CMD_tilde:
2876 case CMD_topleft:
2877 case CMD_unlet:
2878 case CMD_verbose:
2879 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002880 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 break;
2882
2883 default: goto doend;
2884 }
2885 }
2886#endif
2887
2888 if (ea.argt & XFILE)
2889 {
2890 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2891 goto doend;
2892 }
2893
2894#ifdef FEAT_LISTCMDS
2895 /*
2896 * Accept buffer name. Cannot be used at the same time with a buffer
2897 * number. Don't do this for a user command.
2898 */
2899 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002900 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 {
2902 /*
2903 * :bdelete, :bwipeout and :bunload take several arguments, separated
2904 * by spaces: find next space (skipping over escaped characters).
2905 * The others take one argument: ignore trailing spaces.
2906 */
2907 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2908 || ea.cmdidx == CMD_bunload)
2909 p = skiptowhite_esc(ea.arg);
2910 else
2911 {
2912 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002913 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 --p;
2915 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002916 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2917 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 if (ea.line2 < 0) /* failed */
2919 goto doend;
2920 ea.addr_count = 1;
2921 ea.arg = skipwhite(p);
2922 }
2923#endif
2924
2925/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002926 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 *
2928 * The "ea" structure holds the arguments that can be used.
2929 */
2930 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002931 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 ea.cookie = cookie;
2933#ifdef FEAT_EVAL
2934 ea.cstack = cstack;
2935#endif
2936
2937#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002938 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 {
2940 /*
2941 * Execute a user-defined command.
2942 */
2943 do_ucmd(&ea);
2944 }
2945 else
2946#endif
2947 {
2948 /*
2949 * Call the function to execute the command.
2950 */
2951 ea.errmsg = NULL;
2952 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2953 if (ea.errmsg != NULL)
2954 errormsg = (char_u *)_(ea.errmsg);
2955 }
2956
2957#ifdef FEAT_EVAL
2958 /*
2959 * If the command just executed called do_cmdline(), any throw or ":return"
2960 * or ":finish" encountered there must also check the cstack of the still
2961 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2962 * exception, or reanimate a returned function or finished script file and
2963 * return or finish it again.
2964 */
2965 if (need_rethrow)
2966 do_throw(cstack);
2967 else if (check_cstack)
2968 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002969 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002971 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 && current_func_returned())
2973 do_return(&ea, TRUE, FALSE, NULL);
2974 }
2975 need_rethrow = check_cstack = FALSE;
2976#endif
2977
2978doend:
2979 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002982 curwin->w_cursor.col = 0;
2983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984
2985 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2986 {
2987 if (sourcing)
2988 {
2989 if (errormsg != IObuff)
2990 {
2991 STRCPY(IObuff, errormsg);
2992 errormsg = IObuff;
2993 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002994 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
2996 emsg(errormsg);
2997 }
2998#ifdef FEAT_EVAL
2999 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02003000 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
3001 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002#endif
3003
3004 if (verbose_save >= 0)
3005 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003006#ifdef FEAT_AUTOCMD
3007 if (cmdmod.save_ei != NULL)
3008 {
3009 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003010 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
3011 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003012 free_string_option(cmdmod.save_ei);
3013 }
3014#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003015 if (cmdmod.filter_regmatch.regprog != NULL)
3016 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017
3018 cmdmod = save_cmdmod;
3019
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003020 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 {
3022 /* messages could be enabled for a serious error, need to check if the
3023 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00003024 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003025 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 emsg_silent -= did_esilent;
3027 if (emsg_silent < 0)
3028 emsg_silent = 0;
3029 /* Restore msg_scroll, it's set by file I/O commands, even when no
3030 * message is actually displayed. */
3031 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003032
3033 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3034 * somewhere in the line. Put it back in the first column. */
3035 if (redirecting())
3036 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 }
3038
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003039#ifdef HAVE_SANDBOX
3040 if (did_sandbox)
3041 --sandbox;
3042#endif
3043
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3045 ea.nextcmd = NULL;
3046
3047#ifdef FEAT_EVAL
3048 --ex_nesting_level;
3049#endif
3050
3051 return ea.nextcmd;
3052}
3053#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003054 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055#endif
3056
3057/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003058 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 * If there is a match advance "pp" to the argument and return TRUE.
3060 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003061 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003062checkforcmd(
3063 char_u **pp, /* start of command */
3064 char *cmd, /* name of command */
3065 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
3067 int i;
3068
3069 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003070 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 break;
3072 if (i >= len && !isalpha((*pp)[i]))
3073 {
3074 *pp = skipwhite(*pp + i);
3075 return TRUE;
3076 }
3077 return FALSE;
3078}
3079
3080/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003081 * Append "cmd" to the error message in IObuff.
3082 * Takes care of limiting the length and handling 0xa0, which would be
3083 * invisible otherwise.
3084 */
3085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003086append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003087{
3088 char_u *s = cmd;
3089 char_u *d;
3090
3091 STRCAT(IObuff, ": ");
3092 d = IObuff + STRLEN(IObuff);
3093 while (*s != NUL && d - IObuff < IOSIZE - 7)
3094 {
3095 if (
3096#ifdef FEAT_MBYTE
3097 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3098#endif
3099 *s == 0xa0)
3100 {
3101 s +=
3102#ifdef FEAT_MBYTE
3103 enc_utf8 ? 2 :
3104#endif
3105 1;
3106 STRCPY(d, "<a0>");
3107 d += 4;
3108 }
3109 else
3110 MB_COPY_CHAR(s, d);
3111 }
3112 *d = NUL;
3113}
3114
3115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003117 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003119 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 * Returns NULL for an ambiguous user command.
3121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003123find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124{
3125 int len;
3126 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003127 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128
3129 /*
3130 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003131 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 * - the 'k' command can directly be followed by any character.
3133 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003134 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003136 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 */
3138 p = eap->cmd;
3139 if (*p == 'k')
3140 {
3141 eap->cmdidx = CMD_k;
3142 ++p;
3143 }
3144 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003145 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3146 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 || p[1] == 'g'
3148 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3149 || p[1] == 'I'
3150 || (p[1] == 'r' && p[2] != 'e')))
3151 {
3152 eap->cmdidx = CMD_substitute;
3153 ++p;
3154 }
3155 else
3156 {
3157 while (ASCII_ISALPHA(*p))
3158 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003159 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003160 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003161 while (ASCII_ISALNUM(*p))
3162 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003163
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 /* check for non-alpha command */
3165 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3166 ++p;
3167 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003168 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3169 {
3170 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3171 * :delete with the 'l' flag. Same for 'p'. */
3172 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003173 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003174 break;
3175 if (i == len - 1)
3176 {
3177 --len;
3178 if (p[-1] == 'l')
3179 eap->flags |= EXFLAG_LIST;
3180 else
3181 eap->flags |= EXFLAG_PRINT;
3182 }
3183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003185 if (ASCII_ISLOWER(eap->cmd[0]))
3186 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003187 int c1 = eap->cmd[0];
3188 int c2 = eap->cmd[1];
3189
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003190 if (command_count != (int)CMD_SIZE)
3191 {
3192 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3193 getout(1);
3194 }
3195
3196 /* Use a precomputed index for fast look-up in cmdnames[]
3197 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003198 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3199 if (ASCII_ISLOWER(c2))
3200 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003203 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
3205 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3206 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3207 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3208 (size_t)len) == 0)
3209 {
3210#ifdef FEAT_EVAL
3211 if (full != NULL
3212 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3213 *full = TRUE;
3214#endif
3215 break;
3216 }
3217
3218#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003219 /* Look for a user defined command as a last resort. Let ":Print" be
3220 * overruled by a user defined command. */
3221 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3222 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003224 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 while (ASCII_ISALNUM(*p))
3226 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003227 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 }
3229#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003230 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 eap->cmdidx = CMD_SIZE;
3232 }
3233
3234 return p;
3235}
3236
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003237#ifdef FEAT_USR_CMDS
3238/*
3239 * Search for a user command that matches "eap->cmd".
3240 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3241 * Return a pointer to just after the command.
3242 * Return NULL if there is no matching command.
3243 */
3244 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003245find_ucmd(
3246 exarg_T *eap,
3247 char_u *p, /* end of the command (possibly including count) */
3248 int *full, /* set to TRUE for a full match */
3249 expand_T *xp, /* used for completion, NULL otherwise */
3250 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003251{
3252 int len = (int)(p - eap->cmd);
3253 int j, k, matchlen = 0;
3254 ucmd_T *uc;
3255 int found = FALSE;
3256 int possible = FALSE;
3257 char_u *cp, *np; /* Point into typed cmd and test name */
3258 garray_T *gap;
3259 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3260 only full match global is accepted. */
3261
3262 /*
3263 * Look for buffer-local user commands first, then global ones.
3264 */
3265 gap = &curbuf->b_ucmds;
3266 for (;;)
3267 {
3268 for (j = 0; j < gap->ga_len; ++j)
3269 {
3270 uc = USER_CMD_GA(gap, j);
3271 cp = eap->cmd;
3272 np = uc->uc_name;
3273 k = 0;
3274 while (k < len && *np != NUL && *cp++ == *np++)
3275 k++;
3276 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3277 {
3278 /* If finding a second match, the command is ambiguous. But
3279 * not if a buffer-local command wasn't a full match and a
3280 * global command is a full match. */
3281 if (k == len && found && *np != NUL)
3282 {
3283 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003284 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003285 amb_local = TRUE;
3286 }
3287
3288 if (!found || (k == len && *np == NUL))
3289 {
3290 /* If we matched up to a digit, then there could
3291 * be another command including the digit that we
3292 * should use instead.
3293 */
3294 if (k == len)
3295 found = TRUE;
3296 else
3297 possible = TRUE;
3298
3299 if (gap == &ucmds)
3300 eap->cmdidx = CMD_USER;
3301 else
3302 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003303 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003304 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003305 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003306
3307# ifdef FEAT_CMDL_COMPL
3308 if (compl != NULL)
3309 *compl = uc->uc_compl;
3310# ifdef FEAT_EVAL
3311 if (xp != NULL)
3312 {
3313 xp->xp_arg = uc->uc_compl_arg;
3314 xp->xp_scriptID = uc->uc_scriptID;
3315 }
3316# endif
3317# endif
3318 /* Do not search for further abbreviations
3319 * if this is an exact match. */
3320 matchlen = k;
3321 if (k == len && *np == NUL)
3322 {
3323 if (full != NULL)
3324 *full = TRUE;
3325 amb_local = FALSE;
3326 break;
3327 }
3328 }
3329 }
3330 }
3331
3332 /* Stop if we found a full match or searched all. */
3333 if (j < gap->ga_len || gap == &ucmds)
3334 break;
3335 gap = &ucmds;
3336 }
3337
3338 /* Only found ambiguous matches. */
3339 if (amb_local)
3340 {
3341 if (xp != NULL)
3342 xp->xp_context = EXPAND_UNSUCCESSFUL;
3343 return NULL;
3344 }
3345
3346 /* The match we found may be followed immediately by a number. Move "p"
3347 * back to point to it. */
3348 if (found || possible)
3349 return p + (matchlen - len);
3350 return p;
3351}
3352#endif
3353
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003355static struct cmdmod
3356{
3357 char *name;
3358 int minlen;
3359 int has_count; /* :123verbose :3tab */
3360} cmdmods[] = {
3361 {"aboveleft", 3, FALSE},
3362 {"belowright", 3, FALSE},
3363 {"botright", 2, FALSE},
3364 {"browse", 3, FALSE},
3365 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003366 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003367 {"hide", 3, FALSE},
3368 {"keepalt", 5, FALSE},
3369 {"keepjumps", 5, FALSE},
3370 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003371 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003372 {"leftabove", 5, FALSE},
3373 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003374 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003375 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003376 {"rightbelow", 6, FALSE},
3377 {"sandbox", 3, FALSE},
3378 {"silent", 3, FALSE},
3379 {"tab", 3, TRUE},
3380 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003381 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003382 {"verbose", 4, TRUE},
3383 {"vertical", 4, FALSE},
3384};
3385
3386/*
3387 * Return length of a command modifier (including optional count).
3388 * Return zero when it's not a modifier.
3389 */
3390 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003391modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003392{
3393 int i, j;
3394 char_u *p = cmd;
3395
3396 if (VIM_ISDIGIT(*cmd))
3397 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003398 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003399 {
3400 for (j = 0; p[j] != NUL; ++j)
3401 if (p[j] != cmdmods[i].name[j])
3402 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003403 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003404 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003405 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003406 }
3407 return 0;
3408}
3409
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410/*
3411 * Return > 0 if an Ex command "name" exists.
3412 * Return 2 if there is an exact match.
3413 * Return 3 if there is an ambiguous match.
3414 */
3415 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003416cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417{
3418 exarg_T ea;
3419 int full = FALSE;
3420 int i;
3421 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003422 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423
3424 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003425 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 {
3427 for (j = 0; name[j] != NUL; ++j)
3428 if (name[j] != cmdmods[i].name[j])
3429 break;
3430 if (name[j] == NUL && j >= cmdmods[i].minlen)
3431 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3432 }
3433
Bram Moolenaara9587612006-05-04 21:47:50 +00003434 /* Check built-in commands and user defined commands.
3435 * For ":2match" and ":3match" we need to skip the number. */
3436 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003438 p = find_command(&ea, &full);
3439 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003441 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3442 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003443 if (*skipwhite(p) != NUL)
3444 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3446}
3447#endif
3448
3449/*
3450 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3451 * we don't need/want deleted. Maybe this could be done better if we didn't
3452 * repeat all this stuff. The only problem is that they may not stay
3453 * perfectly compatible with each other, but then the command line syntax
3454 * probably won't change that much -- webb.
3455 */
3456 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003457set_one_cmd_context(
3458 expand_T *xp,
3459 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460{
3461 char_u *p;
3462 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003463 int len = 0;
3464 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3466 int compl = EXPAND_NOTHING;
3467#endif
3468#ifdef FEAT_CMDL_COMPL
3469 int delim;
3470#endif
3471 int forceit = FALSE;
3472 int usefilter = FALSE; /* filter instead of file name */
3473
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003474 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 xp->xp_pattern = buff;
3476 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003477 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478
3479/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003480 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 */
3482 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3483 ;
3484 xp->xp_pattern = cmd;
3485
3486 if (*cmd == NUL)
3487 return NULL;
3488 if (*cmd == '"') /* ignore comment lines */
3489 {
3490 xp->xp_context = EXPAND_NOTHING;
3491 return NULL;
3492 }
3493
3494/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003495 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 */
3497 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 xp->xp_pattern = cmd;
3499 if (*cmd == NUL)
3500 return NULL;
3501 if (*cmd == '"')
3502 {
3503 xp->xp_context = EXPAND_NOTHING;
3504 return NULL;
3505 }
3506
3507 if (*cmd == '|' || *cmd == '\n')
3508 return cmd + 1; /* There's another command */
3509
3510 /*
3511 * Isolate the command and search for it in the command table.
3512 * Exceptions:
3513 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003514 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3516 */
3517 if (*cmd == 'k' && cmd[1] != 'e')
3518 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003519 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 p = cmd + 1;
3521 }
3522 else
3523 {
3524 p = cmd;
3525 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3526 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003527 /* a user command may contain digits */
3528 if (ASCII_ISUPPER(cmd[0]))
3529 while (ASCII_ISALNUM(*p) || *p == '*')
3530 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003531 /* for python 3.x: ":py3*" commands completion */
3532 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003533 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003534 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003535 while (ASCII_ISALPHA(*p) || *p == '*')
3536 ++p;
3537 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003538 /* check for non-alpha command */
3539 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3540 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003541 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003543 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 {
3545 xp->xp_context = EXPAND_UNSUCCESSFUL;
3546 return NULL;
3547 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003548 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003549 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3550 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3551 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 break;
3553
3554#ifdef FEAT_USR_CMDS
3555 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3557 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558#endif
3559 }
3560
3561 /*
3562 * If the cursor is touching the command, and it ends in an alpha-numeric
3563 * character, complete the command name.
3564 */
3565 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3566 return NULL;
3567
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003568 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 {
3570 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3571 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003572 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 p = cmd + 1;
3574 }
3575#ifdef FEAT_USR_CMDS
3576 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3577 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003578 ea.cmd = cmd;
3579 p = find_ucmd(&ea, p, NULL, xp,
3580# if defined(FEAT_CMDL_COMPL)
3581 &compl
3582# else
3583 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003585 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003586 if (p == NULL)
3587 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 }
3589#endif
3590 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003591 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 {
3593 /* Not still touching the command and it was an illegal one */
3594 xp->xp_context = EXPAND_UNSUCCESSFUL;
3595 return NULL;
3596 }
3597
3598 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3599
3600 if (*p == '!') /* forced commands */
3601 {
3602 forceit = TRUE;
3603 ++p;
3604 }
3605
3606/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003607 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003609 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003610 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611
3612 arg = skipwhite(p);
3613
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003614 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 {
3616 if (*arg == '>') /* append */
3617 {
3618 if (*++arg == '>')
3619 ++arg;
3620 arg = skipwhite(arg);
3621 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003622 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
3624 ++arg;
3625 usefilter = TRUE;
3626 }
3627 }
3628
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003629 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 {
3631 usefilter = forceit; /* :r! filter if forced */
3632 if (*arg == '!') /* :r !filter */
3633 {
3634 ++arg;
3635 usefilter = TRUE;
3636 }
3637 }
3638
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003639 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 {
3641 while (*arg == *cmd) /* allow any number of '>' or '<' */
3642 ++arg;
3643 arg = skipwhite(arg);
3644 }
3645
3646 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003647 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 {
3649 /* Check if we're in the +command */
3650 p = arg + 1;
3651 arg = skip_cmd_arg(arg, FALSE);
3652
3653 /* Still touching the command after '+'? */
3654 if (*arg == NUL)
3655 return p;
3656
3657 /* Skip space(s) after +command to get to the real argument */
3658 arg = skipwhite(arg);
3659 }
3660
3661 /*
3662 * Check for '|' to separate commands and '"' to start comments.
3663 * Don't do this for ":read !cmd" and ":write !cmd".
3664 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003665 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 {
3667 p = arg;
3668 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003669 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 p += 2;
3671 while (*p)
3672 {
3673 if (*p == Ctrl_V)
3674 {
3675 if (p[1] != NUL)
3676 ++p;
3677 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003678 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 || *p == '|' || *p == '\n')
3680 {
3681 if (*(p - 1) != '\\')
3682 {
3683 if (*p == '|' || *p == '\n')
3684 return p + 1;
3685 return NULL; /* It's a comment */
3686 }
3687 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003688 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
3690 }
3691
3692 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003693 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 vim_strchr((char_u *)"|\"", *arg) == NULL)
3695 return NULL;
3696
3697 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003698 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003700 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003701 while (*p && p < buff + len)
3702 {
3703 if (*p == ' ' || *p == TAB)
3704 {
3705 /* argument starts after a space */
3706 xp->xp_pattern = ++p;
3707 }
3708 else
3709 {
3710 if (*p == '\\' && *(p + 1) != NUL)
3711 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003712 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003713 }
3714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003716 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003718 int c;
3719 int in_quote = FALSE;
3720 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721
3722 /*
3723 * Allow spaces within back-quotes to count as part of the argument
3724 * being expanded.
3725 */
3726 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003727 p = xp->xp_pattern;
3728 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003730#ifdef FEAT_MBYTE
3731 if (has_mbyte)
3732 c = mb_ptr2char(p);
3733 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003735 c = *p;
3736 if (c == '\\' && p[1] != NUL)
3737 ++p;
3738 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 {
3740 if (!in_quote)
3741 {
3742 xp->xp_pattern = p;
3743 bow = p + 1;
3744 }
3745 in_quote = !in_quote;
3746 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003747 /* An argument can contain just about everything, except
3748 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003749 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003750#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003751 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003752#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003753 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003754 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003755 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003756 while (*p != NUL)
3757 {
3758#ifdef FEAT_MBYTE
3759 if (has_mbyte)
3760 c = mb_ptr2char(p);
3761 else
3762#endif
3763 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003764 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003765 break;
3766#ifdef FEAT_MBYTE
3767 if (has_mbyte)
3768 len = (*mb_ptr2len)(p);
3769 else
3770#endif
3771 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003772 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003773 }
3774 if (in_quote)
3775 bow = p;
3776 else
3777 xp->xp_pattern = p;
3778 p -= len;
3779 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003780 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 }
3782
3783 /*
3784 * If we are still inside the quotes, and we passed a space, just
3785 * expand from there.
3786 */
3787 if (bow != NULL && in_quote)
3788 xp->xp_pattern = bow;
3789 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003790
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003791 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003792 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003793 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003794#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003795 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003796#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003797 /* When still after the command name expand executables. */
3798 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003799 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801
3802 /* Check for environment variable */
3803 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003804#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 || *xp->xp_pattern == '%'
3806#endif
3807 )
3808 {
3809 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3810 if (!vim_isIDc(*p))
3811 break;
3812 if (*p == NUL)
3813 {
3814 xp->xp_context = EXPAND_ENV_VARS;
3815 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003816#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3817 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003818 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003819 compl = EXPAND_ENV_VARS;
3820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 }
3822 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003823#if defined(FEAT_CMDL_COMPL)
3824 /* Check for user names */
3825 if (*xp->xp_pattern == '~')
3826 {
3827 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3828 ;
3829 /* Complete ~user only if it partially matches a user name.
3830 * A full match ~user<Tab> will be replaced by user's home
3831 * directory i.e. something like ~user<Tab> -> /home/user/ */
3832 if (*p == NUL && p > xp->xp_pattern + 1
3833 && match_user(xp->xp_pattern + 1) == 1)
3834 {
3835 xp->xp_context = EXPAND_USER;
3836 ++xp->xp_pattern;
3837 }
3838 }
3839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
3841
3842/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003843 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003845 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003847 case CMD_find:
3848 case CMD_sfind:
3849 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003850 if (xp->xp_context == EXPAND_FILES)
3851 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003852 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 case CMD_cd:
3854 case CMD_chdir:
3855 case CMD_lcd:
3856 case CMD_lchdir:
3857 if (xp->xp_context == EXPAND_FILES)
3858 xp->xp_context = EXPAND_DIRECTORIES;
3859 break;
3860 case CMD_help:
3861 xp->xp_context = EXPAND_HELP;
3862 xp->xp_pattern = arg;
3863 break;
3864
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003865 /* Command modifiers: return the argument.
3866 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003868 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 case CMD_belowright:
3870 case CMD_botright:
3871 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003872 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003873 case CMD_cdo:
3874 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003876 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 case CMD_folddoclosed:
3878 case CMD_folddoopen:
3879 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003880 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 case CMD_keepjumps:
3882 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003883 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003884 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003886 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003888 case CMD_noautocmd:
3889 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003891 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003893 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003894 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 case CMD_topleft:
3896 case CMD_verbose:
3897 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003898 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 return arg;
3900
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003901 case CMD_filter:
3902 if (*arg != NUL)
3903 arg = skip_vimgrep_pat(arg, NULL, NULL);
3904 if (arg == NULL || *arg == NUL)
3905 {
3906 xp->xp_context = EXPAND_NOTHING;
3907 return NULL;
3908 }
3909 return skipwhite(arg);
3910
Bram Moolenaar4f688582007-07-24 12:34:30 +00003911#ifdef FEAT_CMDL_COMPL
3912# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 case CMD_match:
3914 if (*arg == NUL || !ends_excmd(*arg))
3915 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003916 /* also complete "None" */
3917 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 arg = skipwhite(skiptowhite(arg));
3919 if (*arg != NUL)
3920 {
3921 xp->xp_context = EXPAND_NOTHING;
3922 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3923 }
3924 }
3925 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003926# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928/*
3929 * All completion for the +cmdline_compl feature goes here.
3930 */
3931
3932# ifdef FEAT_USR_CMDS
3933 case CMD_command:
3934 /* Check for attributes */
3935 while (*arg == '-')
3936 {
3937 arg++; /* Skip "-" */
3938 p = skiptowhite(arg);
3939 if (*p == NUL)
3940 {
3941 /* Cursor is still in the attribute */
3942 p = vim_strchr(arg, '=');
3943 if (p == NULL)
3944 {
3945 /* No "=", so complete attribute names */
3946 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3947 xp->xp_pattern = arg;
3948 return NULL;
3949 }
3950
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003951 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 * their arguments as well.
3953 */
3954 if (STRNICMP(arg, "complete", p - arg) == 0)
3955 {
3956 xp->xp_context = EXPAND_USER_COMPLETE;
3957 xp->xp_pattern = p + 1;
3958 return NULL;
3959 }
3960 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3961 {
3962 xp->xp_context = EXPAND_USER_NARGS;
3963 xp->xp_pattern = p + 1;
3964 return NULL;
3965 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003966 else if (STRNICMP(arg, "addr", p - arg) == 0)
3967 {
3968 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3969 xp->xp_pattern = p + 1;
3970 return NULL;
3971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 return NULL;
3973 }
3974 arg = skipwhite(p);
3975 }
3976
3977 /* After the attributes comes the new command name */
3978 p = skiptowhite(arg);
3979 if (*p == NUL)
3980 {
3981 xp->xp_context = EXPAND_USER_COMMANDS;
3982 xp->xp_pattern = arg;
3983 break;
3984 }
3985
3986 /* And finally comes a normal command */
3987 return skipwhite(p);
3988
3989 case CMD_delcommand:
3990 xp->xp_context = EXPAND_USER_COMMANDS;
3991 xp->xp_pattern = arg;
3992 break;
3993# endif
3994
3995 case CMD_global:
3996 case CMD_vglobal:
3997 delim = *arg; /* get the delimiter */
3998 if (delim)
3999 ++arg; /* skip delimiter if there is one */
4000
4001 while (arg[0] != NUL && arg[0] != delim)
4002 {
4003 if (arg[0] == '\\' && arg[1] != NUL)
4004 ++arg;
4005 ++arg;
4006 }
4007 if (arg[0] != NUL)
4008 return arg + 1;
4009 break;
4010 case CMD_and:
4011 case CMD_substitute:
4012 delim = *arg;
4013 if (delim)
4014 {
4015 /* skip "from" part */
4016 ++arg;
4017 arg = skip_regexp(arg, delim, p_magic, NULL);
4018 }
4019 /* skip "to" part */
4020 while (arg[0] != NUL && arg[0] != delim)
4021 {
4022 if (arg[0] == '\\' && arg[1] != NUL)
4023 ++arg;
4024 ++arg;
4025 }
4026 if (arg[0] != NUL) /* skip delimiter */
4027 ++arg;
4028 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4029 ++arg;
4030 if (arg[0] != NUL)
4031 return arg;
4032 break;
4033 case CMD_isearch:
4034 case CMD_dsearch:
4035 case CMD_ilist:
4036 case CMD_dlist:
4037 case CMD_ijump:
4038 case CMD_psearch:
4039 case CMD_djump:
4040 case CMD_isplit:
4041 case CMD_dsplit:
4042 arg = skipwhite(skipdigits(arg)); /* skip count */
4043 if (*arg == '/') /* Match regexp, not just whole words */
4044 {
4045 for (++arg; *arg && *arg != '/'; arg++)
4046 if (*arg == '\\' && arg[1] != NUL)
4047 arg++;
4048 if (*arg)
4049 {
4050 arg = skipwhite(arg + 1);
4051
4052 /* Check for trailing illegal characters */
4053 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4054 xp->xp_context = EXPAND_NOTHING;
4055 else
4056 return arg;
4057 }
4058 }
4059 break;
4060#ifdef FEAT_AUTOCMD
4061 case CMD_autocmd:
4062 return set_context_in_autocmd(xp, arg, FALSE);
4063
4064 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004065 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 return set_context_in_autocmd(xp, arg, TRUE);
4067#endif
4068 case CMD_set:
4069 set_context_in_set_cmd(xp, arg, 0);
4070 break;
4071 case CMD_setglobal:
4072 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4073 break;
4074 case CMD_setlocal:
4075 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4076 break;
4077 case CMD_tag:
4078 case CMD_stag:
4079 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004080 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 case CMD_tselect:
4082 case CMD_stselect:
4083 case CMD_ptselect:
4084 case CMD_tjump:
4085 case CMD_stjump:
4086 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004087 if (*p_wop != NUL)
4088 xp->xp_context = EXPAND_TAGS_LISTFILES;
4089 else
4090 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 xp->xp_pattern = arg;
4092 break;
4093 case CMD_augroup:
4094 xp->xp_context = EXPAND_AUGROUP;
4095 xp->xp_pattern = arg;
4096 break;
4097#ifdef FEAT_SYN_HL
4098 case CMD_syntax:
4099 set_context_in_syntax_cmd(xp, arg);
4100 break;
4101#endif
4102#ifdef FEAT_EVAL
4103 case CMD_let:
4104 case CMD_if:
4105 case CMD_elseif:
4106 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004107 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 case CMD_echo:
4109 case CMD_echon:
4110 case CMD_execute:
4111 case CMD_echomsg:
4112 case CMD_echoerr:
4113 case CMD_call:
4114 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004115 case CMD_cexpr:
4116 case CMD_caddexpr:
4117 case CMD_cgetexpr:
4118 case CMD_lexpr:
4119 case CMD_laddexpr:
4120 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004121 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 break;
4123
4124 case CMD_unlet:
4125 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4126 arg = xp->xp_pattern + 1;
4127 xp->xp_context = EXPAND_USER_VARS;
4128 xp->xp_pattern = arg;
4129 break;
4130
4131 case CMD_function:
4132 case CMD_delfunction:
4133 xp->xp_context = EXPAND_USER_FUNC;
4134 xp->xp_pattern = arg;
4135 break;
4136
4137 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004138 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 break;
4140#endif
4141 case CMD_highlight:
4142 set_context_in_highlight_cmd(xp, arg);
4143 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004144#ifdef FEAT_CSCOPE
4145 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004146 case CMD_lcscope:
4147 case CMD_scscope:
4148 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004149 break;
4150#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004151#ifdef FEAT_SIGNS
4152 case CMD_sign:
4153 set_context_in_sign_cmd(xp, arg);
4154 break;
4155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156#ifdef FEAT_LISTCMDS
4157 case CMD_bdelete:
4158 case CMD_bwipeout:
4159 case CMD_bunload:
4160 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4161 arg = xp->xp_pattern + 1;
4162 /*FALLTHROUGH*/
4163 case CMD_buffer:
4164 case CMD_sbuffer:
4165 case CMD_checktime:
4166 xp->xp_context = EXPAND_BUFFERS;
4167 xp->xp_pattern = arg;
4168 break;
4169#endif
4170#ifdef FEAT_USR_CMDS
4171 case CMD_USER:
4172 case CMD_USER_BUF:
4173 if (compl != EXPAND_NOTHING)
4174 {
4175 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004176 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 {
4178# ifdef FEAT_MENU
4179 if (compl == EXPAND_MENUS)
4180 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4181# endif
4182 if (compl == EXPAND_COMMANDS)
4183 return arg;
4184 if (compl == EXPAND_MAPPINGS)
4185 return set_context_in_map_cmd(xp, (char_u *)"map",
4186 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004187 /* Find start of last argument. */
4188 p = arg;
4189 while (*p)
4190 {
4191 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004192 /* argument starts after a space */
4193 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004194 else if (*p == '\\' && *(p + 1) != NUL)
4195 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004196 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 xp->xp_pattern = arg;
4199 }
4200 xp->xp_context = compl;
4201 }
4202 break;
4203#endif
4204 case CMD_map: case CMD_noremap:
4205 case CMD_nmap: case CMD_nnoremap:
4206 case CMD_vmap: case CMD_vnoremap:
4207 case CMD_omap: case CMD_onoremap:
4208 case CMD_imap: case CMD_inoremap:
4209 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004210 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004211 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004212 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004213 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004215 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 case CMD_unmap:
4217 case CMD_nunmap:
4218 case CMD_vunmap:
4219 case CMD_ounmap:
4220 case CMD_iunmap:
4221 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004222 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004223 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004224 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004225 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004227 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004228 case CMD_mapclear:
4229 case CMD_nmapclear:
4230 case CMD_vmapclear:
4231 case CMD_omapclear:
4232 case CMD_imapclear:
4233 case CMD_cmapclear:
4234 case CMD_lmapclear:
4235 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004236 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004237 case CMD_xmapclear:
4238 xp->xp_context = EXPAND_MAPCLEAR;
4239 xp->xp_pattern = arg;
4240 break;
4241
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 case CMD_abbreviate: case CMD_noreabbrev:
4243 case CMD_cabbrev: case CMD_cnoreabbrev:
4244 case CMD_iabbrev: case CMD_inoreabbrev:
4245 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004246 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 case CMD_unabbreviate:
4248 case CMD_cunabbrev:
4249 case CMD_iunabbrev:
4250 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004251 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252#ifdef FEAT_MENU
4253 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4254 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4255 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4256 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4257 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4258 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4259 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4260 case CMD_tmenu: case CMD_tunmenu:
4261 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4262 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4263#endif
4264
4265 case CMD_colorscheme:
4266 xp->xp_context = EXPAND_COLORS;
4267 xp->xp_pattern = arg;
4268 break;
4269
4270 case CMD_compiler:
4271 xp->xp_context = EXPAND_COMPILER;
4272 xp->xp_pattern = arg;
4273 break;
4274
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004275 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004276 xp->xp_context = EXPAND_OWNSYNTAX;
4277 xp->xp_pattern = arg;
4278 break;
4279
4280 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004281 xp->xp_context = EXPAND_FILETYPE;
4282 xp->xp_pattern = arg;
4283 break;
4284
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004285 case CMD_packadd:
4286 xp->xp_context = EXPAND_PACKADD;
4287 xp->xp_pattern = arg;
4288 break;
4289
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4291 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4292 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004293 p = skiptowhite(arg);
4294 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 {
4296 xp->xp_context = EXPAND_LANGUAGE;
4297 xp->xp_pattern = arg;
4298 }
4299 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004300 {
4301 if ( STRNCMP(arg, "messages", p - arg) == 0
4302 || STRNCMP(arg, "ctype", p - arg) == 0
4303 || STRNCMP(arg, "time", p - arg) == 0)
4304 {
4305 xp->xp_context = EXPAND_LOCALES;
4306 xp->xp_pattern = skipwhite(p);
4307 }
4308 else
4309 xp->xp_context = EXPAND_NOTHING;
4310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 break;
4312#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004313#if defined(FEAT_PROFILE)
4314 case CMD_profile:
4315 set_context_in_profile_cmd(xp, arg);
4316 break;
4317#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004318 case CMD_behave:
4319 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004320 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004321 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004323 case CMD_messages:
4324 xp->xp_context = EXPAND_MESSAGES;
4325 xp->xp_pattern = arg;
4326 break;
4327
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004328#if defined(FEAT_CMDHIST)
4329 case CMD_history:
4330 xp->xp_context = EXPAND_HISTORY;
4331 xp->xp_pattern = arg;
4332 break;
4333#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004334#if defined(FEAT_PROFILE)
4335 case CMD_syntime:
4336 xp->xp_context = EXPAND_SYNTIME;
4337 xp->xp_pattern = arg;
4338 break;
4339#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004340
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341#endif /* FEAT_CMDL_COMPL */
4342
4343 default:
4344 break;
4345 }
4346 return NULL;
4347}
4348
4349/*
4350 * skip a range specifier of the form: addr [,addr] [;addr] ..
4351 *
4352 * Backslashed delimiters after / or ? will be skipped, and commands will
4353 * not be expanded between /'s and ?'s or after "'".
4354 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004355 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 * Returns the "cmd" pointer advanced to beyond the range.
4357 */
4358 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004359skip_range(
4360 char_u *cmd,
4361 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004363 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004365 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004367 if (*cmd == '\\')
4368 {
4369 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4370 ++cmd;
4371 else
4372 break;
4373 }
4374 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 {
4376 if (*++cmd == NUL && ctx != NULL)
4377 *ctx = EXPAND_NOTHING;
4378 }
4379 else if (*cmd == '/' || *cmd == '?')
4380 {
4381 delim = *cmd++;
4382 while (*cmd != NUL && *cmd != delim)
4383 if (*cmd++ == '\\' && *cmd != NUL)
4384 ++cmd;
4385 if (*cmd == NUL && ctx != NULL)
4386 *ctx = EXPAND_NOTHING;
4387 }
4388 if (*cmd != NUL)
4389 ++cmd;
4390 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004391
4392 /* Skip ":" and white space. */
4393 while (*cmd == ':')
4394 cmd = skipwhite(cmd + 1);
4395
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 return cmd;
4397}
4398
4399/*
4400 * get a single EX address
4401 *
4402 * Set ptr to the next character after the part that was interpreted.
4403 * Set ptr to NULL when an error is encountered.
4404 *
4405 * Return MAXLNUM when no Ex address was found.
4406 */
4407 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004408get_address(
4409 exarg_T *eap UNUSED,
4410 char_u **ptr,
4411 int addr_type, /* flag: one of ADDR_LINES, ... */
4412 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004413 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004414 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415{
4416 int c;
4417 int i;
4418 long n;
4419 char_u *cmd;
4420 pos_T pos;
4421 pos_T *fp;
4422 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004423 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424
4425 cmd = skipwhite(*ptr);
4426 lnum = MAXLNUM;
4427 do
4428 {
4429 switch (*cmd)
4430 {
4431 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004432 ++cmd;
4433 switch (addr_type)
4434 {
4435 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 lnum = curwin->w_cursor.lnum;
4437 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004438 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004439 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004440 break;
4441 case ADDR_ARGUMENTS:
4442 lnum = curwin->w_arg_idx + 1;
4443 break;
4444 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004445 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004446 lnum = curbuf->b_fnum;
4447 break;
4448 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004449 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004450 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004451 case ADDR_TABS_RELATIVE:
4452 EMSG(_(e_invrange));
4453 cmd = NULL;
4454 goto error;
4455 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004456#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004457 case ADDR_QUICKFIX:
4458 lnum = qf_get_cur_valid_idx(eap);
4459 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004460#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004461 }
4462 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
4464 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004465 ++cmd;
4466 switch (addr_type)
4467 {
4468 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 lnum = curbuf->b_ml.ml_line_count;
4470 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004471 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004472 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004473 break;
4474 case ADDR_ARGUMENTS:
4475 lnum = ARGCOUNT;
4476 break;
4477 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004478 buf = lastbuf;
4479 while (buf->b_ml.ml_mfp == NULL)
4480 {
4481 if (buf->b_prev == NULL)
4482 break;
4483 buf = buf->b_prev;
4484 }
4485 lnum = buf->b_fnum;
4486 break;
4487 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004488 lnum = lastbuf->b_fnum;
4489 break;
4490 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004491 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004492 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004493 case ADDR_TABS_RELATIVE:
4494 EMSG(_(e_invrange));
4495 cmd = NULL;
4496 goto error;
4497 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004498#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004499 case ADDR_QUICKFIX:
4500 lnum = qf_get_size(eap);
4501 if (lnum == 0)
4502 lnum = 1;
4503 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004504#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004505 }
4506 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507
4508 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004509 if (*++cmd == NUL)
4510 {
4511 cmd = NULL;
4512 goto error;
4513 }
4514 if (addr_type != ADDR_LINES)
4515 {
4516 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004517 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004518 goto error;
4519 }
4520 if (skip)
4521 ++cmd;
4522 else
4523 {
4524 /* Only accept a mark in another file when it is
4525 * used by itself: ":'M". */
4526 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4527 ++cmd;
4528 if (fp == (pos_T *)-1)
4529 /* Jumped to another file. */
4530 lnum = curwin->w_cursor.lnum;
4531 else
4532 {
4533 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 {
4535 cmd = NULL;
4536 goto error;
4537 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004538 lnum = fp->lnum;
4539 }
4540 }
4541 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542
4543 case '/':
4544 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004545 c = *cmd++;
4546 if (addr_type != ADDR_LINES)
4547 {
4548 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004549 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004550 goto error;
4551 }
4552 if (skip) /* skip "/pat/" */
4553 {
4554 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4555 if (*cmd == c)
4556 ++cmd;
4557 }
4558 else
4559 {
4560 pos = curwin->w_cursor; /* save curwin->w_cursor */
4561 /*
4562 * When '/' or '?' follows another address, start
4563 * from there.
4564 */
4565 if (lnum != MAXLNUM)
4566 curwin->w_cursor.lnum = lnum;
4567 /*
4568 * Start a forward search at the end of the line.
4569 * Start a backward search at the start of the line.
4570 * This makes sure we never match in the current
4571 * line, and can match anywhere in the
4572 * next/previous line.
4573 */
4574 if (c == '/')
4575 curwin->w_cursor.col = MAXCOL;
4576 else
4577 curwin->w_cursor.col = 0;
4578 searchcmdlen = 0;
4579 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004580 SEARCH_HIS | SEARCH_MSG, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004581 {
4582 curwin->w_cursor = pos;
4583 cmd = NULL;
4584 goto error;
4585 }
4586 lnum = curwin->w_cursor.lnum;
4587 curwin->w_cursor = pos;
4588 /* adjust command string pointer */
4589 cmd += searchcmdlen;
4590 }
4591 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
4593 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004594 ++cmd;
4595 if (addr_type != ADDR_LINES)
4596 {
4597 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004598 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004599 goto error;
4600 }
4601 if (*cmd == '&')
4602 i = RE_SUBST;
4603 else if (*cmd == '?' || *cmd == '/')
4604 i = RE_SEARCH;
4605 else
4606 {
4607 EMSG(_(e_backslash));
4608 cmd = NULL;
4609 goto error;
4610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004612 if (!skip)
4613 {
4614 /*
4615 * When search follows another address, start from
4616 * there.
4617 */
4618 if (lnum != MAXLNUM)
4619 pos.lnum = lnum;
4620 else
4621 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004623 /*
4624 * Start the search just like for the above
4625 * do_search().
4626 */
4627 if (*cmd != '?')
4628 pos.col = MAXCOL;
4629 else
4630 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004631#ifdef FEAT_VIRTUALEDIT
4632 pos.coladd = 0;
4633#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004634 if (searchit(curwin, curbuf, &pos,
4635 *cmd == '?' ? BACKWARD : FORWARD,
4636 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004637 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004638 lnum = pos.lnum;
4639 else
4640 {
4641 cmd = NULL;
4642 goto error;
4643 }
4644 }
4645 ++cmd;
4646 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647
4648 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004649 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4650 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
4652
4653 for (;;)
4654 {
4655 cmd = skipwhite(cmd);
4656 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4657 break;
4658
4659 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004660 {
4661 switch (addr_type)
4662 {
4663 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004664 /* "+1" is same as ".+1" */
4665 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004666 break;
4667 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004668 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004669 break;
4670 case ADDR_ARGUMENTS:
4671 lnum = curwin->w_arg_idx + 1;
4672 break;
4673 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004674 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004675 lnum = curbuf->b_fnum;
4676 break;
4677 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004678 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004679 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004680 case ADDR_TABS_RELATIVE:
4681 lnum = 1;
4682 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004683#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004684 case ADDR_QUICKFIX:
4685 lnum = qf_get_cur_valid_idx(eap);
4686 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004687#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004688 }
4689 }
4690
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 if (VIM_ISDIGIT(*cmd))
4692 i = '+'; /* "number" is same as "+number" */
4693 else
4694 i = *cmd++;
4695 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4696 n = 1;
4697 else
4698 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004699
4700 if (addr_type == ADDR_TABS_RELATIVE)
4701 {
4702 EMSG(_(e_invrange));
4703 cmd = NULL;
4704 goto error;
4705 }
4706 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004707 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004708 lnum = compute_buffer_local_count(
4709 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004711 {
4712#ifdef FEAT_FOLDING
4713 /* Relative line addressing, need to adjust for folded lines
4714 * now, but only do it after the first address. */
4715 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4716 && address_count >= 2)
4717 (void)hasFolding(lnum, NULL, &lnum);
4718#endif
4719 if (i == '-')
4720 lnum -= n;
4721 else
4722 lnum += n;
4723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 }
4725 } while (*cmd == '/' || *cmd == '?');
4726
4727error:
4728 *ptr = cmd;
4729 return lnum;
4730}
4731
4732/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004733 * Get flags from an Ex command argument.
4734 */
4735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004736get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004737{
4738 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4739 {
4740 if (*eap->arg == 'l')
4741 eap->flags |= EXFLAG_LIST;
4742 else if (*eap->arg == 'p')
4743 eap->flags |= EXFLAG_PRINT;
4744 else
4745 eap->flags |= EXFLAG_NR;
4746 eap->arg = skipwhite(eap->arg + 1);
4747 }
4748}
4749
4750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 * Function called for command which is Not Implemented. NI!
4752 */
4753 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004754ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755{
4756 if (!eap->skip)
4757 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4758}
4759
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004760#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761/*
4762 * Function called for script command which is Not Implemented. NI!
4763 * Skips over ":perl <<EOF" constructs.
4764 */
4765 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004766ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767{
4768 if (!eap->skip)
4769 ex_ni(eap);
4770 else
4771 vim_free(script_get(eap, eap->arg));
4772}
4773#endif
4774
4775/*
4776 * Check range in Ex command for validity.
4777 * Return NULL when valid, error message when invalid.
4778 */
4779 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004780invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004782 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 if ( eap->line1 < 0
4784 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004785 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004787
4788 if (eap->argt & RANGE)
4789 {
4790 switch(eap->addr_type)
4791 {
4792 case ADDR_LINES:
4793 if (!(eap->argt & NOTADR)
4794 && eap->line2 > curbuf->b_ml.ml_line_count
4795#ifdef FEAT_DIFF
4796 + (eap->cmdidx == CMD_diffget)
4797#endif
4798 )
4799 return (char_u *)_(e_invrange);
4800 break;
4801 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004802 /* add 1 if ARGCOUNT is 0 */
4803 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004804 return (char_u *)_(e_invrange);
4805 break;
4806 case ADDR_BUFFERS:
4807 if (eap->line1 < firstbuf->b_fnum
4808 || eap->line2 > lastbuf->b_fnum)
4809 return (char_u *)_(e_invrange);
4810 break;
4811 case ADDR_LOADED_BUFFERS:
4812 buf = firstbuf;
4813 while (buf->b_ml.ml_mfp == NULL)
4814 {
4815 if (buf->b_next == NULL)
4816 return (char_u *)_(e_invrange);
4817 buf = buf->b_next;
4818 }
4819 if (eap->line1 < buf->b_fnum)
4820 return (char_u *)_(e_invrange);
4821 buf = lastbuf;
4822 while (buf->b_ml.ml_mfp == NULL)
4823 {
4824 if (buf->b_prev == NULL)
4825 return (char_u *)_(e_invrange);
4826 buf = buf->b_prev;
4827 }
4828 if (eap->line2 > buf->b_fnum)
4829 return (char_u *)_(e_invrange);
4830 break;
4831 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004832 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004833 return (char_u *)_(e_invrange);
4834 break;
4835 case ADDR_TABS:
4836 if (eap->line2 > LAST_TAB_NR)
4837 return (char_u *)_(e_invrange);
4838 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004839 case ADDR_TABS_RELATIVE:
4840 /* Do nothing */
4841 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004842#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004843 case ADDR_QUICKFIX:
4844 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4845 return (char_u *)_(e_invrange);
4846 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004847#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004848 }
4849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 return NULL;
4851}
4852
4853/*
4854 * Correct the range for zero line number, if required.
4855 */
4856 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004857correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858{
4859 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4860 {
4861 if (eap->line1 == 0)
4862 eap->line1 = 1;
4863 if (eap->line2 == 0)
4864 eap->line2 = 1;
4865 }
4866}
4867
Bram Moolenaar748bf032005-02-02 23:04:36 +00004868#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004869static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004870
4871/*
4872 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4873 * pattern. Otherwise return eap->arg.
4874 */
4875 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004876skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004877{
4878 char_u *p = eap->arg;
4879
Bram Moolenaara37420f2006-02-04 22:37:47 +00004880 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4881 || eap->cmdidx == CMD_vimgrepadd
4882 || eap->cmdidx == CMD_lvimgrepadd
4883 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004884 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004885 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004886 if (p == NULL)
4887 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004888 }
4889 return p;
4890}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004891
4892/*
4893 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4894 * in the command line, so that things like % get expanded.
4895 */
4896 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004897replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004898{
4899 char_u *new_cmdline;
4900 char_u *program;
4901 char_u *pos;
4902 char_u *ptr;
4903 int len;
4904 int i;
4905
4906 /*
4907 * Don't do it when ":vimgrep" is used for ":grep".
4908 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004909 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4910 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4911 || eap->cmdidx == CMD_grepadd
4912 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004913 && !grep_internal(eap->cmdidx))
4914 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004915 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4916 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004917 {
4918 if (*curbuf->b_p_gp == NUL)
4919 program = p_gp;
4920 else
4921 program = curbuf->b_p_gp;
4922 }
4923 else
4924 {
4925 if (*curbuf->b_p_mp == NUL)
4926 program = p_mp;
4927 else
4928 program = curbuf->b_p_mp;
4929 }
4930
4931 p = skipwhite(p);
4932
4933 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4934 {
4935 /* replace $* by given arguments */
4936 i = 1;
4937 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4938 ++i;
4939 len = (int)STRLEN(p);
4940 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4941 if (new_cmdline == NULL)
4942 return NULL; /* out of memory */
4943 ptr = new_cmdline;
4944 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4945 {
4946 i = (int)(pos - program);
4947 STRNCPY(ptr, program, i);
4948 STRCPY(ptr += i, p);
4949 ptr += len;
4950 program = pos + 2;
4951 }
4952 STRCPY(ptr, program);
4953 }
4954 else
4955 {
4956 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4957 if (new_cmdline == NULL)
4958 return NULL; /* out of memory */
4959 STRCPY(new_cmdline, program);
4960 STRCAT(new_cmdline, " ");
4961 STRCAT(new_cmdline, p);
4962 }
4963 msg_make(p);
4964
4965 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4966 vim_free(*cmdlinep);
4967 *cmdlinep = new_cmdline;
4968 p = new_cmdline;
4969 }
4970 return p;
4971}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004972#endif
4973
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974/*
4975 * Expand file name in Ex command argument.
4976 * Return FAIL for failure, OK otherwise.
4977 */
4978 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004979expand_filename(
4980 exarg_T *eap,
4981 char_u **cmdlinep,
4982 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983{
4984 int has_wildcards; /* need to expand wildcards */
4985 char_u *repl;
4986 int srclen;
4987 char_u *p;
4988 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004989 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990
Bram Moolenaar748bf032005-02-02 23:04:36 +00004991#ifdef FEAT_QUICKFIX
4992 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4993 p = skip_grep_pat(eap);
4994#else
4995 p = eap->arg;
4996#endif
4997
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 /*
4999 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5000 * the file name contains a wildcard it should not cause expanding.
5001 * (it will be expanded anyway if there is a wildcard before replacing).
5002 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005003 has_wildcards = mch_has_wildcard(p);
5004 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005006#ifdef FEAT_EVAL
5007 /* Skip over `=expr`, wildcards in it are not expanded. */
5008 if (p[0] == '`' && p[1] == '=')
5009 {
5010 p += 2;
5011 (void)skip_expr(&p);
5012 if (*p == '`')
5013 ++p;
5014 continue;
5015 }
5016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 /*
5018 * Quick check if this cannot be the start of a special string.
5019 * Also removes backslash before '%', '#' and '<'.
5020 */
5021 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5022 {
5023 ++p;
5024 continue;
5025 }
5026
5027 /*
5028 * Try to find a match at this position.
5029 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005030 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5031 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 if (*errormsgp != NULL) /* error detected */
5033 return FAIL;
5034 if (repl == NULL) /* no match found */
5035 {
5036 p += srclen;
5037 continue;
5038 }
5039
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005040 /* Wildcards won't be expanded below, the replacement is taken
5041 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5042 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5043 {
5044 char_u *l = repl;
5045
5046 repl = expand_env_save(repl);
5047 vim_free(l);
5048 }
5049
Bram Moolenaar63b92542007-03-27 14:57:09 +00005050 /* Need to escape white space et al. with a backslash.
5051 * Don't do this for:
5052 * - replacement that already has been escaped: "##"
5053 * - shell commands (may have to use quotes instead).
5054 * - non-unix systems when there is a single argument (spaces don't
5055 * separate arguments then).
5056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005058 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 && eap->cmdidx != CMD_grep
5061 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005062 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005063 && eap->cmdidx != CMD_lgrep
5064 && eap->cmdidx != CMD_lgrepadd
5065 && eap->cmdidx != CMD_lmake
5066 && eap->cmdidx != CMD_make
5067 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068#ifndef UNIX
5069 && !(eap->argt & NOSPC)
5070#endif
5071 )
5072 {
5073 char_u *l;
5074#ifdef BACKSLASH_IN_FILENAME
5075 /* Don't escape a backslash here, because rem_backslash() doesn't
5076 * remove it later. */
5077 static char_u *nobslash = (char_u *)" \t\"|";
5078# define ESCAPE_CHARS nobslash
5079#else
5080# define ESCAPE_CHARS escape_chars
5081#endif
5082
5083 for (l = repl; *l; ++l)
5084 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5085 {
5086 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5087 if (l != NULL)
5088 {
5089 vim_free(repl);
5090 repl = l;
5091 }
5092 break;
5093 }
5094 }
5095
5096 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005097 if ((eap->usefilter || eap->cmdidx == CMD_bang
5098 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005099 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
5101 char_u *l;
5102
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005103 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 if (l != NULL)
5105 {
5106 vim_free(repl);
5107 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
5109 }
5110
5111 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5112 vim_free(repl);
5113 if (p == NULL)
5114 return FAIL;
5115 }
5116
5117 /*
5118 * One file argument: Expand wildcards.
5119 * Don't do this with ":r !command" or ":w !command".
5120 */
5121 if ((eap->argt & NOSPC) && !eap->usefilter)
5122 {
5123 /*
5124 * May do this twice:
5125 * 1. Replace environment variables.
5126 * 2. Replace any other wildcards, remove backslashes.
5127 */
5128 for (n = 1; n <= 2; ++n)
5129 {
5130 if (n == 2)
5131 {
5132#ifdef UNIX
5133 /*
5134 * Only for Unix we check for more than one file name.
5135 * For other systems spaces are considered to be part
5136 * of the file name.
5137 * Only check here if there is no wildcard, otherwise
5138 * ExpandOne() will check for errors. This allows
5139 * ":e `ls ve*.c`" on Unix.
5140 */
5141 if (!has_wildcards)
5142 for (p = eap->arg; *p; ++p)
5143 {
5144 /* skip escaped characters */
5145 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5146 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005147 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
5149 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5150 return FAIL;
5151 }
5152 }
5153#endif
5154
5155 /*
5156 * Halve the number of backslashes (this is Vi compatible).
5157 * For Unix and OS/2, when wildcards are expanded, this is
5158 * done by ExpandOne() below.
5159 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005160#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 if (!has_wildcards)
5162#endif
5163 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165
5166 if (has_wildcards)
5167 {
5168 if (n == 1)
5169 {
5170 /*
5171 * First loop: May expand environment variables. This
5172 * can be done much faster with expand_env() than with
5173 * something else (e.g., calling a shell).
5174 * After expanding environment variables, check again
5175 * if there are still wildcards present.
5176 */
5177 if (vim_strchr(eap->arg, '$') != NULL
5178 || vim_strchr(eap->arg, '~') != NULL)
5179 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005180 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005181 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 has_wildcards = mch_has_wildcard(NameBuff);
5183 p = NameBuff;
5184 }
5185 else
5186 p = NULL;
5187 }
5188 else /* n == 2 */
5189 {
5190 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005191 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192
5193 ExpandInit(&xpc);
5194 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005195 if (p_wic)
5196 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005198 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 if (p == NULL)
5200 return FAIL;
5201 }
5202 if (p != NULL)
5203 {
5204 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5205 p, cmdlinep);
5206 if (n == 2) /* p came from ExpandOne() */
5207 vim_free(p);
5208 }
5209 }
5210 }
5211 }
5212 return OK;
5213}
5214
5215/*
5216 * Replace part of the command line, keeping eap->cmd, eap->arg and
5217 * eap->nextcmd correct.
5218 * "src" points to the part that is to be replaced, of length "srclen".
5219 * "repl" is the replacement string.
5220 * Returns a pointer to the character after the replaced string.
5221 * Returns NULL for failure.
5222 */
5223 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005224repl_cmdline(
5225 exarg_T *eap,
5226 char_u *src,
5227 int srclen,
5228 char_u *repl,
5229 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230{
5231 int len;
5232 int i;
5233 char_u *new_cmdline;
5234
5235 /*
5236 * The new command line is build in new_cmdline[].
5237 * First allocate it.
5238 * Careful: a "+cmd" argument may have been NUL terminated.
5239 */
5240 len = (int)STRLEN(repl);
5241 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005242 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5244 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5245 return NULL; /* out of memory! */
5246
5247 /*
5248 * Copy the stuff before the expanded part.
5249 * Copy the expanded stuff.
5250 * Copy what came after the expanded part.
5251 * Copy the next commands, if there are any.
5252 */
5253 i = (int)(src - *cmdlinep); /* length of part before match */
5254 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005255
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 mch_memmove(new_cmdline + i, repl, (size_t)len);
5257 i += len; /* remember the end of the string */
5258 STRCPY(new_cmdline + i, src + srclen);
5259 src = new_cmdline + i; /* remember where to continue */
5260
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005261 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 {
5263 i = (int)STRLEN(new_cmdline) + 1;
5264 STRCPY(new_cmdline + i, eap->nextcmd);
5265 eap->nextcmd = new_cmdline + i;
5266 }
5267 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5268 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5269 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5270 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5271 vim_free(*cmdlinep);
5272 *cmdlinep = new_cmdline;
5273
5274 return src;
5275}
5276
5277/*
5278 * Check for '|' to separate commands and '"' to start comments.
5279 */
5280 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005281separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282{
5283 char_u *p;
5284
Bram Moolenaar86b68352004-12-27 21:59:20 +00005285#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005286 p = skip_grep_pat(eap);
5287#else
5288 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005289#endif
5290
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005291 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {
5293 if (*p == Ctrl_V)
5294 {
5295 if (eap->argt & (USECTRLV | XFILE))
5296 ++p; /* skip CTRL-V and next char */
5297 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005298 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005299 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 if (*p == NUL) /* stop at NUL after CTRL-V */
5301 break;
5302 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005303
5304#ifdef FEAT_EVAL
5305 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005306 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005307 {
5308 p += 2;
5309 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005310 }
5311#endif
5312
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 /* Check for '"': start of comment or '|': next command */
5314 /* :@" and :*" do not start a comment!
5315 * :redir @" doesn't either. */
5316 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5317 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5318 || p != eap->arg)
5319 && (eap->cmdidx != CMD_redir
5320 || p != eap->arg + 1 || p[-1] != '@'))
5321 || *p == '|' || *p == '\n')
5322 {
5323 /*
5324 * We remove the '\' before the '|', unless USECTRLV is used
5325 * AND 'b' is present in 'cpoptions'.
5326 */
5327 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5328 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5329 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005330 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 --p;
5332 }
5333 else
5334 {
5335 eap->nextcmd = check_nextcmd(p);
5336 *p = NUL;
5337 break;
5338 }
5339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005341
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5343 del_trailing_spaces(eap->arg);
5344}
5345
5346/*
5347 * get + command from ex argument
5348 */
5349 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005350getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351{
5352 char_u *arg = *argp;
5353 char_u *command = NULL;
5354
5355 if (*arg == '+') /* +[command] */
5356 {
5357 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005358 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 command = dollar_command;
5360 else
5361 {
5362 command = arg;
5363 arg = skip_cmd_arg(command, TRUE);
5364 if (*arg != NUL)
5365 *arg++ = NUL; /* terminate command with NUL */
5366 }
5367
5368 arg = skipwhite(arg); /* skip over spaces */
5369 *argp = arg;
5370 }
5371 return command;
5372}
5373
5374/*
5375 * Find end of "+command" argument. Skip over "\ " and "\\".
5376 */
5377 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005378skip_cmd_arg(
5379 char_u *p,
5380 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381{
5382 while (*p && !vim_isspace(*p))
5383 {
5384 if (*p == '\\' && p[1] != NUL)
5385 {
5386 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005387 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 else
5389 ++p;
5390 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005391 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 }
5393 return p;
5394}
5395
5396/*
5397 * Get "++opt=arg" argument.
5398 * Return FAIL or OK.
5399 */
5400 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005401getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402{
5403 char_u *arg = eap->arg + 2;
5404 int *pp = NULL;
5405#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005406 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 char_u *p;
5408#endif
5409
5410 /* ":edit ++[no]bin[ary] file" */
5411 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5412 {
5413 if (*arg == 'n')
5414 {
5415 arg += 2;
5416 eap->force_bin = FORCE_NOBIN;
5417 }
5418 else
5419 eap->force_bin = FORCE_BIN;
5420 if (!checkforcmd(&arg, "binary", 3))
5421 return FAIL;
5422 eap->arg = skipwhite(arg);
5423 return OK;
5424 }
5425
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005426 /* ":read ++edit file" */
5427 if (STRNCMP(arg, "edit", 4) == 0)
5428 {
5429 eap->read_edit = TRUE;
5430 eap->arg = skipwhite(arg + 4);
5431 return OK;
5432 }
5433
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 if (STRNCMP(arg, "ff", 2) == 0)
5435 {
5436 arg += 2;
5437 pp = &eap->force_ff;
5438 }
5439 else if (STRNCMP(arg, "fileformat", 10) == 0)
5440 {
5441 arg += 10;
5442 pp = &eap->force_ff;
5443 }
5444#ifdef FEAT_MBYTE
5445 else if (STRNCMP(arg, "enc", 3) == 0)
5446 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005447 if (STRNCMP(arg, "encoding", 8) == 0)
5448 arg += 8;
5449 else
5450 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 pp = &eap->force_enc;
5452 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005453 else if (STRNCMP(arg, "bad", 3) == 0)
5454 {
5455 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005456 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458#endif
5459
5460 if (pp == NULL || *arg != '=')
5461 return FAIL;
5462
5463 ++arg;
5464 *pp = (int)(arg - eap->cmd);
5465 arg = skip_cmd_arg(arg, FALSE);
5466 eap->arg = skipwhite(arg);
5467 *arg = NUL;
5468
5469#ifdef FEAT_MBYTE
5470 if (pp == &eap->force_ff)
5471 {
5472#endif
5473 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5474 return FAIL;
5475#ifdef FEAT_MBYTE
5476 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005477 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 {
5479 /* Make 'fileencoding' lower case. */
5480 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5481 *p = TOLOWER_ASC(*p);
5482 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005483 else
5484 {
5485 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5486 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005487 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005488 if (STRICMP(p, "keep") == 0)
5489 eap->bad_char = BAD_KEEP;
5490 else if (STRICMP(p, "drop") == 0)
5491 eap->bad_char = BAD_DROP;
5492 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5493 eap->bad_char = *p;
5494 else
5495 return FAIL;
5496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497#endif
5498
5499 return OK;
5500}
5501
5502/*
5503 * ":abbreviate" and friends.
5504 */
5505 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005506ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507{
5508 do_exmap(eap, TRUE); /* almost the same as mapping */
5509}
5510
5511/*
5512 * ":map" and friends.
5513 */
5514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005515ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516{
5517 /*
5518 * If we are sourcing .exrc or .vimrc in current directory we
5519 * print the mappings for security reasons.
5520 */
5521 if (secure)
5522 {
5523 secure = 2;
5524 msg_outtrans(eap->cmd);
5525 msg_putchar('\n');
5526 }
5527 do_exmap(eap, FALSE);
5528}
5529
5530/*
5531 * ":unmap" and friends.
5532 */
5533 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005534ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535{
5536 do_exmap(eap, FALSE);
5537}
5538
5539/*
5540 * ":mapclear" and friends.
5541 */
5542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005543ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544{
5545 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5546}
5547
5548/*
5549 * ":abclear" and friends.
5550 */
5551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005552ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553{
5554 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5555}
5556
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005557#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005559ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560{
5561 /*
5562 * Disallow auto commands from .exrc and .vimrc in current
5563 * directory for security reasons.
5564 */
5565 if (secure)
5566 {
5567 secure = 2;
5568 eap->errmsg = e_curdir;
5569 }
5570 else if (eap->cmdidx == CMD_autocmd)
5571 do_autocmd(eap->arg, eap->forceit);
5572 else
5573 do_augroup(eap->arg, eap->forceit);
5574}
5575
5576/*
5577 * ":doautocmd": Apply the automatic commands to the current buffer.
5578 */
5579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005580ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005582 char_u *arg = eap->arg;
5583 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005584 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005585
Bram Moolenaar1610d052016-06-09 22:53:01 +02005586 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5587 /* Only when there is no <nomodeline>. */
5588 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005589 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590}
5591#endif
5592
5593#ifdef FEAT_LISTCMDS
5594/*
5595 * :[N]bunload[!] [N] [bufname] unload buffer
5596 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5597 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5598 */
5599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005600ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601{
5602 eap->errmsg = do_bufdel(
5603 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5604 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5605 : DOBUF_UNLOAD, eap->arg,
5606 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5607}
5608
5609/*
5610 * :[N]buffer [N] to buffer N
5611 * :[N]sbuffer [N] to buffer N
5612 */
5613 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005614ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615{
5616 if (*eap->arg)
5617 eap->errmsg = e_trailing;
5618 else
5619 {
5620 if (eap->addr_count == 0) /* default is current buffer */
5621 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5622 else
5623 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005624 if (eap->do_ecmd_cmd != NULL)
5625 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 }
5627}
5628
5629/*
5630 * :[N]bmodified [N] to next mod. buffer
5631 * :[N]sbmodified [N] to next mod. buffer
5632 */
5633 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005634ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635{
5636 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005637 if (eap->do_ecmd_cmd != NULL)
5638 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639}
5640
5641/*
5642 * :[N]bnext [N] to next buffer
5643 * :[N]sbnext [N] split and to next buffer
5644 */
5645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005646ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647{
5648 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005649 if (eap->do_ecmd_cmd != NULL)
5650 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651}
5652
5653/*
5654 * :[N]bNext [N] to previous buffer
5655 * :[N]bprevious [N] to previous buffer
5656 * :[N]sbNext [N] split and to previous buffer
5657 * :[N]sbprevious [N] split and to previous buffer
5658 */
5659 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005660ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661{
5662 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005663 if (eap->do_ecmd_cmd != NULL)
5664 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665}
5666
5667/*
5668 * :brewind to first buffer
5669 * :bfirst to first buffer
5670 * :sbrewind split and to first buffer
5671 * :sbfirst split and to first buffer
5672 */
5673 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005674ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675{
5676 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005677 if (eap->do_ecmd_cmd != NULL)
5678 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679}
5680
5681/*
5682 * :blast to last buffer
5683 * :sblast split and to last buffer
5684 */
5685 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005686ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687{
5688 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005689 if (eap->do_ecmd_cmd != NULL)
5690 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691}
5692#endif
5693
5694 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005695ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696{
5697 return (c == NUL || c == '|' || c == '"' || c == '\n');
5698}
5699
5700#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5701 || defined(PROTO)
5702/*
5703 * Return the next command, after the first '|' or '\n'.
5704 * Return NULL if not found.
5705 */
5706 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005707find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708{
5709 while (*p != '|' && *p != '\n')
5710 {
5711 if (*p == NUL)
5712 return NULL;
5713 ++p;
5714 }
5715 return (p + 1);
5716}
5717#endif
5718
5719/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005720 * Check if *p is a separator between Ex commands, skipping over white space.
5721 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 */
5723 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005724check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005726 char_u *s = skipwhite(p);
5727
5728 if (*s == '|' || *s == '\n')
5729 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 else
5731 return NULL;
5732}
5733
5734/*
5735 * - if there are more files to edit
5736 * - and this is the last window
5737 * - and forceit not used
5738 * - and not repeated twice on a row
5739 * return FAIL and give error message if 'message' TRUE
5740 * return OK otherwise
5741 */
5742 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005743check_more(
5744 int message, /* when FALSE check only, no messages */
5745 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746{
5747 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5748
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005749 if (!forceit && only_one_window()
5750 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 {
5752 if (message)
5753 {
5754#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5755 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5756 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005757 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758
5759 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005760 vim_strncpy(buff,
5761 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005762 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005764 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 _("%d more files to edit. Quit anyway?"), n);
5766 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5767 return OK;
5768 return FAIL;
5769 }
5770#endif
5771 if (n == 1)
5772 EMSG(_("E173: 1 more file to edit"));
5773 else
5774 EMSGN(_("E173: %ld more files to edit"), n);
5775 quitmore = 2; /* next try to quit is allowed */
5776 }
5777 return FAIL;
5778 }
5779 return OK;
5780}
5781
5782#ifdef FEAT_CMDL_COMPL
5783/*
5784 * Function given to ExpandGeneric() to obtain the list of command names.
5785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005787get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788{
5789 if (idx >= (int)CMD_SIZE)
5790# ifdef FEAT_USR_CMDS
5791 return get_user_command_name(idx);
5792# else
5793 return NULL;
5794# endif
5795 return cmdnames[idx].cmd_name;
5796}
5797#endif
5798
5799#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005800static 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);
5801static void uc_list(char_u *name, size_t name_len);
5802static 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);
5803static char_u *uc_split_args(char_u *arg, size_t *lenp);
5804static 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 +00005805
5806 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005807uc_add_command(
5808 char_u *name,
5809 size_t name_len,
5810 char_u *rep,
5811 long argt,
5812 long def,
5813 int flags,
5814 int compl,
5815 char_u *compl_arg,
5816 int addr_type,
5817 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818{
5819 ucmd_T *cmd = NULL;
5820 char_u *p;
5821 int i;
5822 int cmp = 1;
5823 char_u *rep_buf = NULL;
5824 garray_T *gap;
5825
Bram Moolenaar9c102382006-05-03 21:26:49 +00005826 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 if (rep_buf == NULL)
5828 {
5829 /* Can't replace termcodes - try using the string as is */
5830 rep_buf = vim_strsave(rep);
5831
5832 /* Give up if out of memory */
5833 if (rep_buf == NULL)
5834 return FAIL;
5835 }
5836
5837 /* get address of growarray: global or in curbuf */
5838 if (flags & UC_BUFFER)
5839 {
5840 gap = &curbuf->b_ucmds;
5841 if (gap->ga_itemsize == 0)
5842 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5843 }
5844 else
5845 gap = &ucmds;
5846
5847 /* Search for the command in the already defined commands. */
5848 for (i = 0; i < gap->ga_len; ++i)
5849 {
5850 size_t len;
5851
5852 cmd = USER_CMD_GA(gap, i);
5853 len = STRLEN(cmd->uc_name);
5854 cmp = STRNCMP(name, cmd->uc_name, name_len);
5855 if (cmp == 0)
5856 {
5857 if (name_len < len)
5858 cmp = -1;
5859 else if (name_len > len)
5860 cmp = 1;
5861 }
5862
5863 if (cmp == 0)
5864 {
5865 if (!force)
5866 {
5867 EMSG(_("E174: Command already exists: add ! to replace it"));
5868 goto fail;
5869 }
5870
5871 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005872 cmd->uc_rep = NULL;
5873#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5874 vim_free(cmd->uc_compl_arg);
5875 cmd->uc_compl_arg = NULL;
5876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 break;
5878 }
5879
5880 /* Stop as soon as we pass the name to add */
5881 if (cmp < 0)
5882 break;
5883 }
5884
5885 /* Extend the array unless we're replacing an existing command */
5886 if (cmp != 0)
5887 {
5888 if (ga_grow(gap, 1) != OK)
5889 goto fail;
5890 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5891 goto fail;
5892
5893 cmd = USER_CMD_GA(gap, i);
5894 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5895
5896 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897
5898 cmd->uc_name = p;
5899 }
5900
5901 cmd->uc_rep = rep_buf;
5902 cmd->uc_argt = argt;
5903 cmd->uc_def = def;
5904 cmd->uc_compl = compl;
5905#ifdef FEAT_EVAL
5906 cmd->uc_scriptID = current_SID;
5907# ifdef FEAT_CMDL_COMPL
5908 cmd->uc_compl_arg = compl_arg;
5909# endif
5910#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005911 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912
5913 return OK;
5914
5915fail:
5916 vim_free(rep_buf);
5917#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5918 vim_free(compl_arg);
5919#endif
5920 return FAIL;
5921}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005924#if defined(FEAT_USR_CMDS)
5925static struct
5926{
5927 int expand;
5928 char *name;
5929} addr_type_complete[] =
5930{
5931 {ADDR_ARGUMENTS, "arguments"},
5932 {ADDR_LINES, "lines"},
5933 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5934 {ADDR_TABS, "tabs"},
5935 {ADDR_BUFFERS, "buffers"},
5936 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005937 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005938 {-1, NULL}
5939};
5940#endif
5941
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005942#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943/*
5944 * List of names for completion for ":command" with the EXPAND_ flag.
5945 * Must be alphabetical for completion.
5946 */
5947static struct
5948{
5949 int expand;
5950 char *name;
5951} command_complete[] =
5952{
5953 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005954 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005956 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005958 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005959#if defined(FEAT_CSCOPE)
5960 {EXPAND_CSCOPE, "cscope"},
5961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5963 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005964 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965#endif
5966 {EXPAND_DIRECTORIES, "dir"},
5967 {EXPAND_ENV_VARS, "environment"},
5968 {EXPAND_EVENTS, "event"},
5969 {EXPAND_EXPRESSION, "expression"},
5970 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005971 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005972 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 {EXPAND_FUNCTIONS, "function"},
5974 {EXPAND_HELP, "help"},
5975 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005976#if defined(FEAT_CMDHIST)
5977 {EXPAND_HISTORY, "history"},
5978#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005979#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005980 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005981 {EXPAND_LOCALES, "locale"},
5982#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005983 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 {EXPAND_MAPPINGS, "mapping"},
5985 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005986 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005987 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005988#if defined(FEAT_PROFILE)
5989 {EXPAND_SYNTIME, "syntime"},
5990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005992 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005993 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005994#if defined(FEAT_SIGNS)
5995 {EXPAND_SIGN, "sign"},
5996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 {EXPAND_TAGS, "tag"},
5998 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005999 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 {EXPAND_USER_VARS, "var"},
6001 {0, NULL}
6002};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006005#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006007uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008{
6009 int i, j;
6010 int found = FALSE;
6011 ucmd_T *cmd;
6012 int len;
6013 long a;
6014 garray_T *gap;
6015
6016 gap = &curbuf->b_ucmds;
6017 for (;;)
6018 {
6019 for (i = 0; i < gap->ga_len; ++i)
6020 {
6021 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006022 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023
Bram Moolenaard29459b2016-08-26 22:29:11 +02006024 /* Skip commands which don't match the requested prefix and
6025 * commands filtered out. */
6026 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6027 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 continue;
6029
6030 /* Put out the title first time */
6031 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006032 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 found = TRUE;
6034 msg_putchar('\n');
6035 if (got_int)
6036 break;
6037
6038 /* Special cases */
6039 msg_putchar(a & BANG ? '!' : ' ');
6040 msg_putchar(a & REGSTR ? '"' : ' ');
6041 msg_putchar(gap != &ucmds ? 'b' : ' ');
6042 msg_putchar(' ');
6043
Bram Moolenaar8820b482017-03-16 17:23:31 +01006044 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 len = (int)STRLEN(cmd->uc_name) + 4;
6046
6047 do {
6048 msg_putchar(' ');
6049 ++len;
6050 } while (len < 16);
6051
6052 len = 0;
6053
6054 /* Arguments */
6055 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6056 {
6057 case 0: IObuff[len++] = '0'; break;
6058 case (EXTRA): IObuff[len++] = '*'; break;
6059 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6060 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6061 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6062 }
6063
6064 do {
6065 IObuff[len++] = ' ';
6066 } while (len < 5);
6067
6068 /* Range */
6069 if (a & (RANGE|COUNT))
6070 {
6071 if (a & COUNT)
6072 {
6073 /* -count=N */
6074 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6075 len += (int)STRLEN(IObuff + len);
6076 }
6077 else if (a & DFLALL)
6078 IObuff[len++] = '%';
6079 else if (cmd->uc_def >= 0)
6080 {
6081 /* -range=N */
6082 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6083 len += (int)STRLEN(IObuff + len);
6084 }
6085 else
6086 IObuff[len++] = '.';
6087 }
6088
6089 do {
6090 IObuff[len++] = ' ';
6091 } while (len < 11);
6092
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006093 /* Address Type */
6094 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6095 if (addr_type_complete[j].expand != ADDR_LINES
6096 && addr_type_complete[j].expand == cmd->uc_addr_type)
6097 {
6098 STRCPY(IObuff + len, addr_type_complete[j].name);
6099 len += (int)STRLEN(IObuff + len);
6100 break;
6101 }
6102
6103 do {
6104 IObuff[len++] = ' ';
6105 } while (len < 21);
6106
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 /* Completion */
6108 for (j = 0; command_complete[j].expand != 0; ++j)
6109 if (command_complete[j].expand == cmd->uc_compl)
6110 {
6111 STRCPY(IObuff + len, command_complete[j].name);
6112 len += (int)STRLEN(IObuff + len);
6113 break;
6114 }
6115
6116 do {
6117 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006118 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119
6120 IObuff[len] = '\0';
6121 msg_outtrans(IObuff);
6122
6123 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006124#ifdef FEAT_EVAL
6125 if (p_verbose > 0)
6126 last_set_msg(cmd->uc_scriptID);
6127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 out_flush();
6129 ui_breakcheck();
6130 if (got_int)
6131 break;
6132 }
6133 if (gap == &ucmds || i < gap->ga_len)
6134 break;
6135 gap = &ucmds;
6136 }
6137
6138 if (!found)
6139 MSG(_("No user-defined commands found"));
6140}
6141
6142 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006143uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144{
6145 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6146 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6147 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6148 0xb9, 0x7f, 0};
6149 int i;
6150
6151 for (i = 0; fcmd[i]; ++i)
6152 IObuff[i] = fcmd[i] - 0x40;
6153 IObuff[i] = 0;
6154 return IObuff;
6155}
6156
6157 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006158uc_scan_attr(
6159 char_u *attr,
6160 size_t len,
6161 long *argt,
6162 long *def,
6163 int *flags,
6164 int *compl,
6165 char_u **compl_arg,
6166 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167{
6168 char_u *p;
6169
6170 if (len == 0)
6171 {
6172 EMSG(_("E175: No attribute specified"));
6173 return FAIL;
6174 }
6175
6176 /* First, try the simple attributes (no arguments) */
6177 if (STRNICMP(attr, "bang", len) == 0)
6178 *argt |= BANG;
6179 else if (STRNICMP(attr, "buffer", len) == 0)
6180 *flags |= UC_BUFFER;
6181 else if (STRNICMP(attr, "register", len) == 0)
6182 *argt |= REGSTR;
6183 else if (STRNICMP(attr, "bar", len) == 0)
6184 *argt |= TRLBAR;
6185 else
6186 {
6187 int i;
6188 char_u *val = NULL;
6189 size_t vallen = 0;
6190 size_t attrlen = len;
6191
6192 /* Look for the attribute name - which is the part before any '=' */
6193 for (i = 0; i < (int)len; ++i)
6194 {
6195 if (attr[i] == '=')
6196 {
6197 val = &attr[i + 1];
6198 vallen = len - i - 1;
6199 attrlen = i;
6200 break;
6201 }
6202 }
6203
6204 if (STRNICMP(attr, "nargs", attrlen) == 0)
6205 {
6206 if (vallen == 1)
6207 {
6208 if (*val == '0')
6209 /* Do nothing - this is the default */;
6210 else if (*val == '1')
6211 *argt |= (EXTRA | NOSPC | NEEDARG);
6212 else if (*val == '*')
6213 *argt |= EXTRA;
6214 else if (*val == '?')
6215 *argt |= (EXTRA | NOSPC);
6216 else if (*val == '+')
6217 *argt |= (EXTRA | NEEDARG);
6218 else
6219 goto wrong_nargs;
6220 }
6221 else
6222 {
6223wrong_nargs:
6224 EMSG(_("E176: Invalid number of arguments"));
6225 return FAIL;
6226 }
6227 }
6228 else if (STRNICMP(attr, "range", attrlen) == 0)
6229 {
6230 *argt |= RANGE;
6231 if (vallen == 1 && *val == '%')
6232 *argt |= DFLALL;
6233 else if (val != NULL)
6234 {
6235 p = val;
6236 if (*def >= 0)
6237 {
6238two_count:
6239 EMSG(_("E177: Count cannot be specified twice"));
6240 return FAIL;
6241 }
6242
6243 *def = getdigits(&p);
6244 *argt |= (ZEROR | NOTADR);
6245
6246 if (p != val + vallen || vallen == 0)
6247 {
6248invalid_count:
6249 EMSG(_("E178: Invalid default value for count"));
6250 return FAIL;
6251 }
6252 }
6253 }
6254 else if (STRNICMP(attr, "count", attrlen) == 0)
6255 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006256 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257
6258 if (val != NULL)
6259 {
6260 p = val;
6261 if (*def >= 0)
6262 goto two_count;
6263
6264 *def = getdigits(&p);
6265
6266 if (p != val + vallen)
6267 goto invalid_count;
6268 }
6269
6270 if (*def < 0)
6271 *def = 0;
6272 }
6273 else if (STRNICMP(attr, "complete", attrlen) == 0)
6274 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 if (val == NULL)
6276 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006277 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 return FAIL;
6279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006281 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6282 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006285 else if (STRNICMP(attr, "addr", attrlen) == 0)
6286 {
6287 *argt |= RANGE;
6288 if (val == NULL)
6289 {
6290 EMSG(_("E179: argument required for -addr"));
6291 return FAIL;
6292 }
6293 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6294 == FAIL)
6295 return FAIL;
6296 if (addr_type_arg != ADDR_LINES)
6297 *argt |= (ZEROR | NOTADR) ;
6298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 else
6300 {
6301 char_u ch = attr[len];
6302 attr[len] = '\0';
6303 EMSG2(_("E181: Invalid attribute: %s"), attr);
6304 attr[len] = ch;
6305 return FAIL;
6306 }
6307 }
6308
6309 return OK;
6310}
6311
Bram Moolenaara850a712009-01-28 14:42:59 +00006312/*
6313 * ":command ..."
6314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006316ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317{
6318 char_u *name;
6319 char_u *end;
6320 char_u *p;
6321 long argt = 0;
6322 long def = -1;
6323 int flags = 0;
6324 int compl = EXPAND_NOTHING;
6325 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006326 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006328 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329
6330 p = eap->arg;
6331
6332 /* Check for attributes */
6333 while (*p == '-')
6334 {
6335 ++p;
6336 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006337 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6338 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 == FAIL)
6340 return;
6341 p = skipwhite(end);
6342 }
6343
6344 /* Get the name (if any) and skip to the following argument */
6345 name = p;
6346 if (ASCII_ISALPHA(*p))
6347 while (ASCII_ISALNUM(*p))
6348 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006349 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 {
6351 EMSG(_("E182: Invalid command name"));
6352 return;
6353 }
6354 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006355 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356
6357 /* If there is nothing after the name, and no attributes were specified,
6358 * we are listing commands
6359 */
6360 p = skipwhite(end);
6361 if (!has_attr && ends_excmd(*p))
6362 {
6363 uc_list(name, end - name);
6364 }
6365 else if (!ASCII_ISUPPER(*name))
6366 {
6367 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6368 return;
6369 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006370 else if ((name_len == 1 && *name == 'X')
6371 || (name_len <= 4
6372 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6373 {
6374 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6375 return;
6376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 else
6378 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006379 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380}
6381
6382/*
6383 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 * Clear all user commands, global and for current buffer.
6385 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006386 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006387ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
6389 uc_clear(&ucmds);
6390 uc_clear(&curbuf->b_ucmds);
6391}
6392
6393/*
6394 * Clear all user commands for "gap".
6395 */
6396 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006397uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398{
6399 int i;
6400 ucmd_T *cmd;
6401
6402 for (i = 0; i < gap->ga_len; ++i)
6403 {
6404 cmd = USER_CMD_GA(gap, i);
6405 vim_free(cmd->uc_name);
6406 vim_free(cmd->uc_rep);
6407# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6408 vim_free(cmd->uc_compl_arg);
6409# endif
6410 }
6411 ga_clear(gap);
6412}
6413
6414 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006415ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416{
6417 int i = 0;
6418 ucmd_T *cmd = NULL;
6419 int cmp = -1;
6420 garray_T *gap;
6421
6422 gap = &curbuf->b_ucmds;
6423 for (;;)
6424 {
6425 for (i = 0; i < gap->ga_len; ++i)
6426 {
6427 cmd = USER_CMD_GA(gap, i);
6428 cmp = STRCMP(eap->arg, cmd->uc_name);
6429 if (cmp <= 0)
6430 break;
6431 }
6432 if (gap == &ucmds || cmp == 0)
6433 break;
6434 gap = &ucmds;
6435 }
6436
6437 if (cmp != 0)
6438 {
6439 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6440 return;
6441 }
6442
6443 vim_free(cmd->uc_name);
6444 vim_free(cmd->uc_rep);
6445# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6446 vim_free(cmd->uc_compl_arg);
6447# endif
6448
6449 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450
6451 if (i < gap->ga_len)
6452 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6453}
6454
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006455/*
6456 * split and quote args for <f-args>
6457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006459uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460{
6461 char_u *buf;
6462 char_u *p;
6463 char_u *q;
6464 int len;
6465
6466 /* Precalculate length */
6467 p = arg;
6468 len = 2; /* Initial and final quotes */
6469
6470 while (*p)
6471 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006472 if (p[0] == '\\' && p[1] == '\\')
6473 {
6474 len += 2;
6475 p += 2;
6476 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006477 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 {
6479 len += 1;
6480 p += 2;
6481 }
6482 else if (*p == '\\' || *p == '"')
6483 {
6484 len += 2;
6485 p += 1;
6486 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006487 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 {
6489 p = skipwhite(p);
6490 if (*p == NUL)
6491 break;
6492 len += 3; /* "," */
6493 }
6494 else
6495 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006496#ifdef FEAT_MBYTE
6497 int charlen = (*mb_ptr2len)(p);
6498 len += charlen;
6499 p += charlen;
6500#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 ++len;
6502 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 }
6505 }
6506
6507 buf = alloc(len + 1);
6508 if (buf == NULL)
6509 {
6510 *lenp = 0;
6511 return buf;
6512 }
6513
6514 p = arg;
6515 q = buf;
6516 *q++ = '"';
6517 while (*p)
6518 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006519 if (p[0] == '\\' && p[1] == '\\')
6520 {
6521 *q++ = '\\';
6522 *q++ = '\\';
6523 p += 2;
6524 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006525 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 {
6527 *q++ = p[1];
6528 p += 2;
6529 }
6530 else if (*p == '\\' || *p == '"')
6531 {
6532 *q++ = '\\';
6533 *q++ = *p++;
6534 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006535 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 {
6537 p = skipwhite(p);
6538 if (*p == NUL)
6539 break;
6540 *q++ = '"';
6541 *q++ = ',';
6542 *q++ = '"';
6543 }
6544 else
6545 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006546 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 }
6548 }
6549 *q++ = '"';
6550 *q = 0;
6551
6552 *lenp = len;
6553 return buf;
6554}
6555
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006556 static size_t
6557add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6558{
6559 size_t result;
6560
6561 result = STRLEN(mod_str);
6562 if (*multi_mods)
6563 result += 1;
6564 if (buf != NULL)
6565 {
6566 if (*multi_mods)
6567 STRCAT(buf, " ");
6568 STRCAT(buf, mod_str);
6569 }
6570
6571 *multi_mods = 1;
6572
6573 return result;
6574}
6575
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576/*
6577 * Check for a <> code in a user command.
6578 * "code" points to the '<'. "len" the length of the <> (inclusive).
6579 * "buf" is where the result is to be added.
6580 * "split_buf" points to a buffer used for splitting, caller should free it.
6581 * "split_len" is the length of what "split_buf" contains.
6582 * Returns the length of the replacement, which has been added to "buf".
6583 * Returns -1 if there was no match, and only the "<" has been copied.
6584 */
6585 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006586uc_check_code(
6587 char_u *code,
6588 size_t len,
6589 char_u *buf,
6590 ucmd_T *cmd, /* the user command we're expanding */
6591 exarg_T *eap, /* ex arguments */
6592 char_u **split_buf,
6593 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594{
6595 size_t result = 0;
6596 char_u *p = code + 1;
6597 size_t l = len - 2;
6598 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006599 enum {
6600 ct_ARGS,
6601 ct_BANG,
6602 ct_COUNT,
6603 ct_LINE1,
6604 ct_LINE2,
6605 ct_RANGE,
6606 ct_MODS,
6607 ct_REGISTER,
6608 ct_LT,
6609 ct_NONE
6610 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611
6612 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6613 {
6614 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6615 p += 2;
6616 l -= 2;
6617 }
6618
Bram Moolenaar371d5402006-03-20 21:47:49 +00006619 ++l;
6620 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006622 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006624 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006626 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006628 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006630 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006632 else if (STRNICMP(p, "range>", l) == 0)
6633 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006634 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006636 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006638 else if (STRNICMP(p, "mods>", l) == 0)
6639 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640
6641 switch (type)
6642 {
6643 case ct_ARGS:
6644 /* Simple case first */
6645 if (*eap->arg == NUL)
6646 {
6647 if (quote == 1)
6648 {
6649 result = 2;
6650 if (buf != NULL)
6651 STRCPY(buf, "''");
6652 }
6653 else
6654 result = 0;
6655 break;
6656 }
6657
6658 /* When specified there is a single argument don't split it.
6659 * Works for ":Cmd %" when % is "a b c". */
6660 if ((eap->argt & NOSPC) && quote == 2)
6661 quote = 1;
6662
6663 switch (quote)
6664 {
6665 case 0: /* No quoting, no splitting */
6666 result = STRLEN(eap->arg);
6667 if (buf != NULL)
6668 STRCPY(buf, eap->arg);
6669 break;
6670 case 1: /* Quote, but don't split */
6671 result = STRLEN(eap->arg) + 2;
6672 for (p = eap->arg; *p; ++p)
6673 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006674#ifdef FEAT_MBYTE
6675 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6676 /* DBCS can contain \ in a trail byte, skip the
6677 * double-byte character. */
6678 ++p;
6679 else
6680#endif
6681 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 ++result;
6683 }
6684
6685 if (buf != NULL)
6686 {
6687 *buf++ = '"';
6688 for (p = eap->arg; *p; ++p)
6689 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006690#ifdef FEAT_MBYTE
6691 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6692 /* DBCS can contain \ in a trail byte, copy the
6693 * double-byte character to avoid escaping. */
6694 *buf++ = *p++;
6695 else
6696#endif
6697 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 *buf++ = '\\';
6699 *buf++ = *p;
6700 }
6701 *buf = '"';
6702 }
6703
6704 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006705 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 /* This is hard, so only do it once, and cache the result */
6707 if (*split_buf == NULL)
6708 *split_buf = uc_split_args(eap->arg, split_len);
6709
6710 result = *split_len;
6711 if (buf != NULL && result != 0)
6712 STRCPY(buf, *split_buf);
6713
6714 break;
6715 }
6716 break;
6717
6718 case ct_BANG:
6719 result = eap->forceit ? 1 : 0;
6720 if (quote)
6721 result += 2;
6722 if (buf != NULL)
6723 {
6724 if (quote)
6725 *buf++ = '"';
6726 if (eap->forceit)
6727 *buf++ = '!';
6728 if (quote)
6729 *buf = '"';
6730 }
6731 break;
6732
6733 case ct_LINE1:
6734 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006735 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 case ct_COUNT:
6737 {
6738 char num_buf[20];
6739 long num = (type == ct_LINE1) ? eap->line1 :
6740 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006741 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6743 size_t num_len;
6744
6745 sprintf(num_buf, "%ld", num);
6746 num_len = STRLEN(num_buf);
6747 result = num_len;
6748
6749 if (quote)
6750 result += 2;
6751
6752 if (buf != NULL)
6753 {
6754 if (quote)
6755 *buf++ = '"';
6756 STRCPY(buf, num_buf);
6757 buf += num_len;
6758 if (quote)
6759 *buf = '"';
6760 }
6761
6762 break;
6763 }
6764
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006765 case ct_MODS:
6766 {
6767 int multi_mods = 0;
6768 typedef struct {
6769 int *varp;
6770 char *name;
6771 } mod_entry_T;
6772 static mod_entry_T mod_entries[] = {
6773#ifdef FEAT_BROWSE_CMD
6774 {&cmdmod.browse, "browse"},
6775#endif
6776#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6777 {&cmdmod.confirm, "confirm"},
6778#endif
6779 {&cmdmod.hide, "hide"},
6780 {&cmdmod.keepalt, "keepalt"},
6781 {&cmdmod.keepjumps, "keepjumps"},
6782 {&cmdmod.keepmarks, "keepmarks"},
6783 {&cmdmod.keeppatterns, "keeppatterns"},
6784 {&cmdmod.lockmarks, "lockmarks"},
6785 {&cmdmod.noswapfile, "noswapfile"},
6786 {NULL, NULL}
6787 };
6788 int i;
6789
6790 result = quote ? 2 : 0;
6791 if (buf != NULL)
6792 {
6793 if (quote)
6794 *buf++ = '"';
6795 *buf = '\0';
6796 }
6797
6798#ifdef FEAT_WINDOWS
6799 /* :aboveleft and :leftabove */
6800 if (cmdmod.split & WSP_ABOVE)
6801 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6802 /* :belowright and :rightbelow */
6803 if (cmdmod.split & WSP_BELOW)
6804 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6805 /* :botright */
6806 if (cmdmod.split & WSP_BOT)
6807 result += add_cmd_modifier(buf, "botright", &multi_mods);
6808#endif
6809
6810 /* the modifiers that are simple flags */
6811 for (i = 0; mod_entries[i].varp != NULL; ++i)
6812 if (*mod_entries[i].varp)
6813 result += add_cmd_modifier(buf, mod_entries[i].name,
6814 &multi_mods);
6815
6816 /* TODO: How to support :noautocmd? */
6817#ifdef HAVE_SANDBOX
6818 /* TODO: How to support :sandbox?*/
6819#endif
6820 /* :silent */
6821 if (msg_silent > 0)
6822 result += add_cmd_modifier(buf,
6823 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6824#ifdef FEAT_WINDOWS
6825 /* :tab */
6826 if (cmdmod.tab > 0)
6827 result += add_cmd_modifier(buf, "tab", &multi_mods);
6828 /* :topleft */
6829 if (cmdmod.split & WSP_TOP)
6830 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6831#endif
6832 /* TODO: How to support :unsilent?*/
6833 /* :verbose */
6834 if (p_verbose > 0)
6835 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6836#ifdef FEAT_WINDOWS
6837 /* :vertical */
6838 if (cmdmod.split & WSP_VERT)
6839 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6840#endif
6841 if (quote && buf != NULL)
6842 {
6843 buf += result - 2;
6844 *buf = '"';
6845 }
6846 break;
6847 }
6848
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 case ct_REGISTER:
6850 result = eap->regname ? 1 : 0;
6851 if (quote)
6852 result += 2;
6853 if (buf != NULL)
6854 {
6855 if (quote)
6856 *buf++ = '\'';
6857 if (eap->regname)
6858 *buf++ = eap->regname;
6859 if (quote)
6860 *buf = '\'';
6861 }
6862 break;
6863
6864 case ct_LT:
6865 result = 1;
6866 if (buf != NULL)
6867 *buf = '<';
6868 break;
6869
6870 default:
6871 /* Not recognized: just copy the '<' and return -1. */
6872 result = (size_t)-1;
6873 if (buf != NULL)
6874 *buf = '<';
6875 break;
6876 }
6877
6878 return result;
6879}
6880
6881 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006882do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883{
6884 char_u *buf;
6885 char_u *p;
6886 char_u *q;
6887
6888 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006889 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006890 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 size_t len, totlen;
6892
6893 size_t split_len = 0;
6894 char_u *split_buf = NULL;
6895 ucmd_T *cmd;
6896#ifdef FEAT_EVAL
6897 scid_T save_current_SID = current_SID;
6898#endif
6899
6900 if (eap->cmdidx == CMD_USER)
6901 cmd = USER_CMD(eap->useridx);
6902 else
6903 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6904
6905 /*
6906 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006907 * First round: "buf" is NULL, compute length, allocate "buf".
6908 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 */
6910 buf = NULL;
6911 for (;;)
6912 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006913 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006914 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006916
6917 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006919 start = vim_strchr(p, '<');
6920 if (start != NULL)
6921 end = vim_strchr(start + 1, '>');
6922 if (buf != NULL)
6923 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006924 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6925 ;
6926 if (*ksp == K_SPECIAL
6927 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006928 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6929# ifdef FEAT_GUI
6930 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6931# endif
6932 ))
6933 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006934 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006935 * KS_SPECIAL KE_FILLER, like for mappings, but
6936 * do_cmdline() doesn't handle that, so convert it back.
6937 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6938 len = ksp - p;
6939 if (len > 0)
6940 {
6941 mch_memmove(q, p, len);
6942 q += len;
6943 }
6944 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6945 p = ksp + 3;
6946 continue;
6947 }
6948 }
6949
6950 /* break if there no <item> is found */
6951 if (start == NULL || end == NULL)
6952 break;
6953
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 /* Include the '>' */
6955 ++end;
6956
6957 /* Take everything up to the '<' */
6958 len = start - p;
6959 if (buf == NULL)
6960 totlen += len;
6961 else
6962 {
6963 mch_memmove(q, p, len);
6964 q += len;
6965 }
6966
6967 len = uc_check_code(start, end - start, q, cmd, eap,
6968 &split_buf, &split_len);
6969 if (len == (size_t)-1)
6970 {
6971 /* no match, continue after '<' */
6972 p = start + 1;
6973 len = 1;
6974 }
6975 else
6976 p = end;
6977 if (buf == NULL)
6978 totlen += len;
6979 else
6980 q += len;
6981 }
6982 if (buf != NULL) /* second time here, finished */
6983 {
6984 STRCPY(q, p);
6985 break;
6986 }
6987
6988 totlen += STRLEN(p); /* Add on the trailing characters */
6989 buf = alloc((unsigned)(totlen + 1));
6990 if (buf == NULL)
6991 {
6992 vim_free(split_buf);
6993 return;
6994 }
6995 }
6996
6997#ifdef FEAT_EVAL
6998 current_SID = cmd->uc_scriptID;
6999#endif
7000 (void)do_cmdline(buf, eap->getline, eap->cookie,
7001 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
7002#ifdef FEAT_EVAL
7003 current_SID = save_current_SID;
7004#endif
7005 vim_free(buf);
7006 vim_free(split_buf);
7007}
7008
7009# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7010 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007011get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012{
7013 return get_user_commands(NULL, idx - (int)CMD_SIZE);
7014}
7015
7016/*
7017 * Function given to ExpandGeneric() to obtain the list of user command names.
7018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007020get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021{
7022 if (idx < curbuf->b_ucmds.ga_len)
7023 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
7024 idx -= curbuf->b_ucmds.ga_len;
7025 if (idx < ucmds.ga_len)
7026 return USER_CMD(idx)->uc_name;
7027 return NULL;
7028}
7029
7030/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007031 * Function given to ExpandGeneric() to obtain the list of user address type names.
7032 */
7033 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007034get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007035{
7036 return (char_u *)addr_type_complete[idx].name;
7037}
7038
7039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 * Function given to ExpandGeneric() to obtain the list of user command
7041 * attributes.
7042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007044get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045{
7046 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007047 {"addr", "bang", "bar", "buffer", "complete",
7048 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007050 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 return NULL;
7052 return (char_u *)user_cmd_flags[idx];
7053}
7054
7055/*
7056 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007059get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060{
7061 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7062
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007063 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 return NULL;
7065 return (char_u *)user_cmd_nargs[idx];
7066}
7067
7068/*
7069 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007072get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073{
7074 return (char_u *)command_complete[idx].name;
7075}
7076# endif /* FEAT_CMDL_COMPL */
7077
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007078/*
7079 * Parse address type argument
7080 */
7081 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007082parse_addr_type_arg(
7083 char_u *value,
7084 int vallen,
7085 long *argt,
7086 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007087{
7088 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007089
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007090 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7091 {
7092 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7093 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7094 if (a && b)
7095 {
7096 *addr_type_arg = addr_type_complete[i].expand;
7097 break;
7098 }
7099 }
7100
7101 if (addr_type_complete[i].expand == -1)
7102 {
7103 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007104
Bram Moolenaar1c465442017-03-12 20:10:05 +01007105 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007106 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007107 err[i] = NUL;
7108 EMSG2(_("E180: Invalid address type value: %s"), err);
7109 return FAIL;
7110 }
7111
7112 if (*addr_type_arg != ADDR_LINES)
7113 *argt |= NOTADR;
7114
7115 return OK;
7116}
7117
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118#endif /* FEAT_USR_CMDS */
7119
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007120#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7121/*
7122 * Parse a completion argument "value[vallen]".
7123 * The detected completion goes in "*complp", argument type in "*argt".
7124 * When there is an argument, for function and user defined completion, it's
7125 * copied to allocated memory and stored in "*compl_arg".
7126 * Returns FAIL if something is wrong.
7127 */
7128 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007129parse_compl_arg(
7130 char_u *value,
7131 int vallen,
7132 int *complp,
7133 long *argt,
7134 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007135{
7136 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007137# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007138 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007139# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007140 int i;
7141 int valend = vallen;
7142
7143 /* Look for any argument part - which is the part after any ',' */
7144 for (i = 0; i < vallen; ++i)
7145 {
7146 if (value[i] == ',')
7147 {
7148 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007149# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007150 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007151# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007152 valend = i;
7153 break;
7154 }
7155 }
7156
7157 for (i = 0; command_complete[i].expand != 0; ++i)
7158 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007159 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007160 && STRNCMP(value, command_complete[i].name, valend) == 0)
7161 {
7162 *complp = command_complete[i].expand;
7163 if (command_complete[i].expand == EXPAND_BUFFERS)
7164 *argt |= BUFNAME;
7165 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7166 || command_complete[i].expand == EXPAND_FILES)
7167 *argt |= XFILE;
7168 break;
7169 }
7170 }
7171
7172 if (command_complete[i].expand == 0)
7173 {
7174 EMSG2(_("E180: Invalid complete value: %s"), value);
7175 return FAIL;
7176 }
7177
7178# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7179 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7180 && arg != NULL)
7181# else
7182 if (arg != NULL)
7183# endif
7184 {
7185 EMSG(_("E468: Completion argument only allowed for custom completion"));
7186 return FAIL;
7187 }
7188
7189# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7190 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7191 && arg == NULL)
7192 {
7193 EMSG(_("E467: Custom completion requires a function argument"));
7194 return FAIL;
7195 }
7196
7197 if (arg != NULL)
7198 *compl_arg = vim_strnsave(arg, (int)arglen);
7199# endif
7200 return OK;
7201}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007202
7203 int
7204cmdcomplete_str_to_type(char_u *complete_str)
7205{
7206 int i;
7207
7208 for (i = 0; command_complete[i].expand != 0; ++i)
7209 if (STRCMP(complete_str, command_complete[i].name) == 0)
7210 return command_complete[i].expand;
7211
7212 return EXPAND_NOTHING;
7213}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007214#endif
7215
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007217ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218{
Bram Moolenaare6850792010-05-14 15:28:44 +02007219 if (*eap->arg == NUL)
7220 {
7221#ifdef FEAT_EVAL
7222 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7223 char_u *p = NULL;
7224
7225 if (expr != NULL)
7226 {
7227 ++emsg_off;
7228 p = eval_to_string(expr, NULL, FALSE);
7229 --emsg_off;
7230 vim_free(expr);
7231 }
7232 if (p != NULL)
7233 {
7234 MSG(p);
7235 vim_free(p);
7236 }
7237 else
7238 MSG("default");
7239#else
7240 MSG(_("unknown"));
7241#endif
7242 }
7243 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007244 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245}
7246
7247 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007248ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249{
7250 if (*eap->arg == NUL && eap->cmd[2] == '!')
7251 MSG(_("Greetings, Vim user!"));
7252 do_highlight(eap->arg, eap->forceit, FALSE);
7253}
7254
7255
7256/*
7257 * Call this function if we thought we were going to exit, but we won't
7258 * (because of an error). May need to restore the terminal mode.
7259 */
7260 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007261not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262{
7263 exiting = FALSE;
7264 settmode(TMODE_RAW);
7265}
7266
7267/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007268 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 */
7270 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007271ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007273#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007274 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007275#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007276
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277#ifdef FEAT_CMDWIN
7278 if (cmdwin_type != 0)
7279 {
7280 cmdwin_result = Ctrl_C;
7281 return;
7282 }
7283#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007284 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007285 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007286 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007287 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007288 return;
7289 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007290#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007291 if (eap->addr_count > 0)
7292 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007293 int wnr = eap->line2;
7294
7295 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7296 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007297 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007298 }
7299 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007300#endif
7301#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007302 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007303#endif
7304
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007305#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007306 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007307 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007308 * being closed (can only happen in autocommands). */
Bram Moolenaar0ea50702017-07-08 14:44:50 +02007309 if (curbuf_locked()
7310# ifdef FEAT_WINDOWS
7311 || !win_valid(wp)
7312# endif
7313 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007314 return;
7315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316
7317#ifdef FEAT_NETBEANS_INTG
7318 netbeansForcedQuit = eap->forceit;
7319#endif
7320
7321 /*
7322 * If there are more files or windows we won't exit.
7323 */
7324 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7325 exiting = TRUE;
Bram Moolenaareb44a682017-08-03 22:44:55 +02007326 if ((!buf_hide(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007327 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7328 | (eap->forceit ? CCGD_FORCEIT : 0)
7329 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007331 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 {
7333 not_exiting();
7334 }
7335 else
7336 {
7337#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007338 /* quit last window
7339 * Note: only_one_window() returns true, even so a help window is
7340 * still open. In that case only quit, if no address has been
7341 * specified. Example:
7342 * :h|wincmd w|1q - don't quit
7343 * :h|wincmd w|q - quit
7344 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007345 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346#endif
7347 getout(0);
7348#ifdef FEAT_WINDOWS
7349# ifdef FEAT_GUI
7350 need_mouse_correct = TRUE;
7351# endif
7352 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007353 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354#endif
7355 }
7356}
7357
7358/*
7359 * ":cquit".
7360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007362ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363{
7364 getout(1); /* this does not always pass on the exit code to the Manx
7365 compiler. why? */
7366}
7367
7368/*
7369 * ":qall": try to quit all windows
7370 */
7371 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007372ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373{
7374# ifdef FEAT_CMDWIN
7375 if (cmdwin_type != 0)
7376 {
7377 if (eap->forceit)
7378 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7379 else
7380 cmdwin_result = K_XF2;
7381 return;
7382 }
7383# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007384
7385 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007386 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007387 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007388 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007389 return;
7390 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007391#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007392 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7393 /* Refuse to quit when locked or when the buffer in the last window is
7394 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007395 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007396 return;
7397#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007398
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007400 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 getout(0);
7402 not_exiting();
7403}
7404
Bram Moolenaard9967712006-03-11 21:18:15 +00007405#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406/*
7407 * ":close": close current window, unless it is the last one
7408 */
7409 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007410ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007412 win_T *win;
7413 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414# ifdef FEAT_CMDWIN
7415 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007416 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 else
7418# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007419 if (!text_locked()
7420#ifdef FEAT_AUTOCMD
7421 && !curbuf_locked()
7422#endif
7423 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007424 {
7425 if (eap->addr_count == 0)
7426 ex_win_close(eap->forceit, curwin, NULL);
7427 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007428 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007429 {
7430 winnr++;
7431 if (winnr == eap->line2)
7432 break;
7433 }
7434 if (win == NULL)
7435 win = lastwin;
7436 ex_win_close(eap->forceit, win, NULL);
7437 }
7438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439}
7440
Bram Moolenaard9967712006-03-11 21:18:15 +00007441# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007442/*
7443 * ":pclose": Close any preview window.
7444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007446ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007447{
7448 win_T *win;
7449
Bram Moolenaar29323592016-07-24 22:04:11 +02007450 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007451 if (win->w_p_pvw)
7452 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007453 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007454 break;
7455 }
7456}
Bram Moolenaard9967712006-03-11 21:18:15 +00007457# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007458
Bram Moolenaarf740b292006-02-16 22:11:02 +00007459/*
7460 * Close window "win" and take care of handling closing the last window for a
7461 * modified buffer.
7462 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007463 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007464ex_win_close(
7465 int forceit,
7466 win_T *win,
7467 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468{
7469 int need_hide;
7470 buf_T *buf = win->w_buffer;
7471
7472 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007473 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007475# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 if ((p_confirm || cmdmod.confirm) && p_write)
7477 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007478 bufref_T bufref;
7479
7480 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007482 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 return;
7484 need_hide = FALSE;
7485 }
7486 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007487# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007489 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 return;
7491 }
7492 }
7493
Bram Moolenaard9967712006-03-11 21:18:15 +00007494# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007496# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007497
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007499 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007500 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007501 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007502 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503}
7504
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007506 * Handle the argument for a tabpage related ex command.
7507 * Returns a tabpage number.
7508 * When an error is encountered then eap->errmsg is set.
7509 */
7510 static int
7511get_tabpage_arg(exarg_T *eap)
7512{
7513 int tab_number;
7514 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7515
7516 if (eap->arg && *eap->arg != NUL)
7517 {
7518 char_u *p = eap->arg;
7519 char_u *p_save;
7520 int relative = 0; /* argument +N/-N means: go to N places to the
7521 * right/left relative to the current position. */
7522
7523 if (*p == '-')
7524 {
7525 relative = -1;
7526 p++;
7527 }
7528 else if (*p == '+')
7529 {
7530 relative = 1;
7531 p++;
7532 }
7533
7534 p_save = p;
7535 tab_number = getdigits(&p);
7536
7537 if (relative == 0)
7538 {
7539 if (STRCMP(p, "$") == 0)
7540 tab_number = LAST_TAB_NR;
7541 else if (p == p_save || *p_save == '-' || *p != NUL
7542 || tab_number > LAST_TAB_NR)
7543 {
7544 /* No numbers as argument. */
7545 eap->errmsg = e_invarg;
7546 goto theend;
7547 }
7548 }
7549 else
7550 {
7551 if (*p_save == NUL)
7552 tab_number = 1;
7553 else if (p == p_save || *p_save == '-' || *p != NUL
7554 || tab_number == 0)
7555 {
7556 /* No numbers as argument. */
7557 eap->errmsg = e_invarg;
7558 goto theend;
7559 }
7560 tab_number = tab_number * relative + tabpage_index(curtab);
7561 if (!unaccept_arg0 && relative == -1)
7562 --tab_number;
7563 }
7564 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7565 eap->errmsg = e_invarg;
7566 }
7567 else if (eap->addr_count > 0)
7568 {
7569 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007570 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007571 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007572 tab_number = 0;
7573 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007574 else
7575 {
7576 tab_number = eap->line2;
7577 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7578 {
7579 --tab_number;
7580 if (tab_number < unaccept_arg0)
7581 eap->errmsg = e_invarg;
7582 }
7583 }
7584 }
7585 else
7586 {
7587 switch (eap->cmdidx)
7588 {
7589 case CMD_tabnext:
7590 tab_number = tabpage_index(curtab) + 1;
7591 if (tab_number > LAST_TAB_NR)
7592 tab_number = 1;
7593 break;
7594 case CMD_tabmove:
7595 tab_number = LAST_TAB_NR;
7596 break;
7597 default:
7598 tab_number = tabpage_index(curtab);
7599 }
7600 }
7601
7602theend:
7603 return tab_number;
7604}
7605
7606/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007607 * ":tabclose": close current tab page, unless it is the last one.
7608 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 */
7610 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007611ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007613 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007614 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007615
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007616# ifdef FEAT_CMDWIN
7617 if (cmdwin_type != 0)
7618 cmdwin_result = K_IGNORE;
7619 else
7620# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007621 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007622 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007623 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007625 tab_number = get_tabpage_arg(eap);
7626 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007627 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007628 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007629 if (tp == NULL)
7630 {
7631 beep_flush();
7632 return;
7633 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007634 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007635 {
7636 tabpage_close_other(tp, eap->forceit);
7637 return;
7638 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007639 else if (!text_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007640#ifdef FEAT_AUTOCMD
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007641 && !curbuf_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007642#endif
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007643 )
7644 tabpage_close(eap->forceit);
7645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007647}
7648
7649/*
7650 * ":tabonly": close all tab pages except the current one
7651 */
7652 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007653ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007654{
7655 tabpage_T *tp;
7656 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007657 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007658
7659# ifdef FEAT_CMDWIN
7660 if (cmdwin_type != 0)
7661 cmdwin_result = K_IGNORE;
7662 else
7663# endif
7664 if (first_tabpage->tp_next == NULL)
7665 MSG(_("Already only one tab page"));
7666 else
7667 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007668 tab_number = get_tabpage_arg(eap);
7669 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007670 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007671 goto_tabpage(tab_number);
7672 /* Repeat this up to a 1000 times, because autocommands may
7673 * mess up the lists. */
7674 for (done = 0; done < 1000; ++done)
7675 {
7676 FOR_ALL_TABPAGES(tp)
7677 if (tp->tp_topframe != topframe)
7678 {
7679 tabpage_close_other(tp, eap->forceit);
7680 /* if we failed to close it quit */
7681 if (valid_tabpage(tp))
7682 done = 1000;
7683 /* start over, "tp" is now invalid */
7684 break;
7685 }
7686 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007687 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007688 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007689 }
7690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692
7693/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007694 * Close the current tab page.
7695 */
7696 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007697tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007698{
7699 /* First close all the windows but the current one. If that worked then
7700 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007701 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007702 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007703 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007704 ex_win_close(forceit, curwin, NULL);
7705# ifdef FEAT_GUI
7706 need_mouse_correct = TRUE;
7707# endif
7708}
7709
7710/*
7711 * Close tab page "tp", which is not the current tab page.
7712 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007713 * Also takes care of the tab pages line disappearing when closing the
7714 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007715 */
7716 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007717tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007718{
7719 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007720 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007721 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007722
7723 /* Limit to 1000 windows, autocommands may add a window while we close
7724 * one. OK, so I'm paranoid... */
7725 while (++done < 1000)
7726 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007727 wp = tp->tp_firstwin;
7728 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007729
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007730 /* Autocommands may delete the tab page under our fingers and we may
7731 * fail to close a window with a modified buffer. */
7732 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007733 break;
7734 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007735
Bram Moolenaar29323592016-07-24 22:04:11 +02007736#ifdef FEAT_AUTOCMD
7737 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7738#endif
7739
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007740 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007741 if (h != tabline_height())
7742 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007743}
7744
7745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 * ":only".
7747 */
7748 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007749ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007751 win_T *wp;
7752 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753# ifdef FEAT_GUI
7754 need_mouse_correct = TRUE;
7755# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007756 if (eap->addr_count > 0)
7757 {
7758 wnr = eap->line2;
7759 for (wp = firstwin; --wnr > 0; )
7760 {
7761 if (wp->w_next == NULL)
7762 break;
7763 else
7764 wp = wp->w_next;
7765 }
7766 win_goto(wp);
7767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 close_others(TRUE, eap->forceit);
7769}
7770
7771/*
7772 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007773 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007775 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007776ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777{
7778 if (eap->addr_count == 0)
7779 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007780 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781}
7782#endif /* FEAT_WINDOWS */
7783
7784 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007785ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007787 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788#ifdef FEAT_WINDOWS
Bram Moolenaar2256c992016-11-15 21:17:07 +01007789 if (!eap->skip)
7790 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791# ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007792 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793# endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007794 if (eap->addr_count == 0)
7795 win_close(curwin, FALSE); /* don't free buffer */
7796 else
7797 {
7798 int winnr = 0;
7799 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007800
Bram Moolenaar2256c992016-11-15 21:17:07 +01007801 FOR_ALL_WINDOWS(win)
7802 {
7803 winnr++;
7804 if (winnr == eap->line2)
7805 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007806 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007807 if (win == NULL)
7808 win = lastwin;
7809 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813}
7814
7815/*
7816 * ":stop" and ":suspend": Suspend Vim.
7817 */
7818 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007819ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820{
7821 /*
7822 * Disallow suspending for "rvim".
7823 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007824 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 {
7826 if (!eap->forceit)
7827 autowrite_all();
7828 windgoto((int)Rows - 1, 0);
7829 out_char('\n');
7830 out_flush();
7831 stoptermcap();
7832 out_flush(); /* needed for SUN to restore xterm buffer */
7833#ifdef FEAT_TITLE
7834 mch_restore_title(3); /* restore window titles */
7835#endif
7836 ui_suspend(); /* call machine specific function */
7837#ifdef FEAT_TITLE
7838 maketitle();
7839 resettitle(); /* force updating the title */
7840#endif
7841 starttermcap();
7842 scroll_start(); /* scroll screen before redrawing */
7843 redraw_later_clear();
7844 shell_resized(); /* may have resized window */
7845 }
7846}
7847
7848/*
7849 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7850 */
7851 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007852ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853{
7854#ifdef FEAT_CMDWIN
7855 if (cmdwin_type != 0)
7856 {
7857 cmdwin_result = Ctrl_C;
7858 return;
7859 }
7860#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007861 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007862 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007863 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007864 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007865 return;
7866 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007867#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007868 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7869 /* Refuse to quit when locked or when the buffer in the last window is
7870 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007871 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007872 return;
7873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874
7875 /*
7876 * if more files or windows we won't exit
7877 */
7878 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7879 exiting = TRUE;
7880 if ( ((eap->cmdidx == CMD_wq
7881 || curbufIsChanged())
7882 && do_write(eap) == FAIL)
7883 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007884 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 {
7886 not_exiting();
7887 }
7888 else
7889 {
7890#ifdef FEAT_WINDOWS
7891 if (only_one_window()) /* quit last window, exit Vim */
7892#endif
7893 getout(0);
7894#ifdef FEAT_WINDOWS
7895# ifdef FEAT_GUI
7896 need_mouse_correct = TRUE;
7897# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007898 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007899 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900#endif
7901 }
7902}
7903
7904/*
7905 * ":print", ":list", ":number".
7906 */
7907 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007908ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007910 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7911 EMSG(_(e_emptybuf));
7912 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007914 for ( ;!got_int; ui_breakcheck())
7915 {
7916 print_line(eap->line1,
7917 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7918 || (eap->flags & EXFLAG_NR)),
7919 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7920 if (++eap->line1 > eap->line2)
7921 break;
7922 out_flush(); /* show one line at a time */
7923 }
7924 setpcmark();
7925 /* put cursor at last line */
7926 curwin->w_cursor.lnum = eap->line2;
7927 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 }
7929
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931}
7932
7933#ifdef FEAT_BYTEOFF
7934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007935ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936{
7937 goto_byte(eap->line2);
7938}
7939#endif
7940
7941/*
7942 * ":shell".
7943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007945ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946{
7947 do_shell(NULL, 0);
7948}
7949
7950#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7951 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007952 || defined(FEAT_GUI_MSWIN) \
7953 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 || defined(PROTO)
7955
7956/*
7957 * Handle a file drop. The code is here because a drop is *nearly* like an
7958 * :args command, but not quite (we have a list of exact filenames, so we
7959 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7960 * code is very similar to :args and hence needs access to a lot of the static
7961 * functions in this file.
7962 *
7963 * The list should be allocated using alloc(), as should each item in the
7964 * list. This function takes over responsibility for freeing the list.
7965 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007966 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007967 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 */
7969 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007970handle_drop(
7971 int filec, /* the number of files dropped */
7972 char_u **filev, /* the list of files dropped */
7973 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974{
7975 exarg_T ea;
7976 int save_msg_scroll = msg_scroll;
7977
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007978 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007979 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007981#ifdef FEAT_AUTOCMD
7982 if (curbuf_locked())
7983 return;
7984#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007985 /* When the screen is being updated we should not change buffers and
7986 * windows structures, it may cause freed memory to be used. */
7987 if (updating_screen)
7988 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989
7990 /* Check whether the current buffer is changed. If so, we will need
7991 * to split the current window or data could be lost.
7992 * We don't need to check if the 'hidden' option is set, as in this
7993 * case the buffer won't be lost.
7994 */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007995 if (!buf_hide(curbuf) && !split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 {
7997 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007998 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 --emsg_off;
8000 }
8001 if (split)
8002 {
8003# ifdef FEAT_WINDOWS
8004 if (win_split(0, 0) == FAIL)
8005 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008006 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007
8008 /* When splitting the window, create a new alist. Otherwise the
8009 * existing one is overwritten. */
8010 alist_unlink(curwin->w_alist);
8011 alist_new();
8012# else
8013 return; /* can't split, always fail */
8014# endif
8015 }
8016
8017 /*
8018 * Set up the new argument list.
8019 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00008020 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021
8022 /*
8023 * Move to the first file.
8024 */
8025 /* Fake up a minimal "next" command for do_argfile() */
8026 vim_memset(&ea, 0, sizeof(ea));
8027 ea.cmd = (char_u *)"next";
8028 do_argfile(&ea, 0);
8029
8030 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
8031 * mode that is not needed here. */
8032 need_start_insertmode = FALSE;
8033
8034 /* Restore msg_scroll, otherwise a following command may cause scrolling
8035 * unexpectedly. The screen will be redrawn by the caller, thus
8036 * msg_scroll being set by displaying a message is irrelevant. */
8037 msg_scroll = save_msg_scroll;
8038}
8039#endif
8040
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041/*
8042 * Clear an argument list: free all file names and reset it to zero entries.
8043 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008044 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008045alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046{
8047 while (--al->al_ga.ga_len >= 0)
8048 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8049 ga_clear(&al->al_ga);
8050}
8051
8052/*
8053 * Init an argument list.
8054 */
8055 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008056alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057{
8058 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8059}
8060
8061#if defined(FEAT_WINDOWS) || defined(PROTO)
8062
8063/*
8064 * Remove a reference from an argument list.
8065 * Ignored when the argument list is the global one.
8066 * If the argument list is no longer used by any window, free it.
8067 */
8068 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008069alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070{
8071 if (al != &global_alist && --al->al_refcount <= 0)
8072 {
8073 alist_clear(al);
8074 vim_free(al);
8075 }
8076}
8077
8078# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
8079/*
8080 * Create a new argument list and use it for the current window.
8081 */
8082 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008083alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084{
8085 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8086 if (curwin->w_alist == NULL)
8087 {
8088 curwin->w_alist = &global_alist;
8089 ++global_alist.al_refcount;
8090 }
8091 else
8092 {
8093 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008094 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 alist_init(curwin->w_alist);
8096 }
8097}
8098# endif
8099#endif
8100
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008101#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102/*
8103 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008104 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8105 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 */
8107 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008108alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109{
8110 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008111 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 char_u **new_arg_files;
8113 int new_arg_file_count;
8114 char_u *save_p_su = p_su;
8115 int i;
8116
8117 /* Don't use 'suffixes' here. This should work like the shell did the
8118 * expansion. Also, the vimrc file isn't read yet, thus the user
8119 * can't set the options. */
8120 p_su = empty_option;
8121 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8122 if (old_arg_files != NULL)
8123 {
8124 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008125 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8126 old_arg_count = GARGCOUNT;
8127 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008129 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 && new_arg_file_count > 0)
8131 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008132 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8133 TRUE, fnum_list, fnum_len);
8134 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 }
8137 p_su = save_p_su;
8138}
8139#endif
8140
8141/*
8142 * Set the argument list for the current window.
8143 * Takes over the allocated files[] and the allocated fnames in it.
8144 */
8145 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008146alist_set(
8147 alist_T *al,
8148 int count,
8149 char_u **files,
8150 int use_curbuf,
8151 int *fnum_list,
8152 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153{
8154 int i;
8155
8156 alist_clear(al);
8157 if (ga_grow(&al->al_ga, count) == OK)
8158 {
8159 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008160 {
8161 if (got_int)
8162 {
8163 /* When adding many buffers this can take a long time. Allow
8164 * interrupting here. */
8165 while (i < count)
8166 vim_free(files[i++]);
8167 break;
8168 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008169
8170 /* May set buffer name of a buffer previously used for the
8171 * argument list, so that it's re-used by alist_add. */
8172 if (fnum_list != NULL && i < fnum_len)
8173 buf_set_name(fnum_list[i], files[i]);
8174
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008176 ui_breakcheck();
8177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 vim_free(files);
8179 }
8180 else
8181 FreeWild(count, files);
8182#ifdef FEAT_WINDOWS
8183 if (al == &global_alist)
8184#endif
8185 arg_had_last = FALSE;
8186}
8187
8188/*
8189 * Add file "fname" to argument list "al".
8190 * "fname" must have been allocated and "al" must have been checked for room.
8191 */
8192 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008193alist_add(
8194 alist_T *al,
8195 char_u *fname,
8196 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197{
8198 if (fname == NULL) /* don't add NULL file names */
8199 return;
8200#ifdef BACKSLASH_IN_FILENAME
8201 slash_adjust(fname);
8202#endif
8203 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8204 if (set_fnum > 0)
8205 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8206 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8207 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208}
8209
8210#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8211/*
8212 * Adjust slashes in file names. Called after 'shellslash' was set.
8213 */
8214 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008215alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216{
8217 int i;
8218# ifdef FEAT_WINDOWS
8219 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008220 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221# endif
8222
8223 for (i = 0; i < GARGCOUNT; ++i)
8224 if (GARGLIST[i].ae_fname != NULL)
8225 slash_adjust(GARGLIST[i].ae_fname);
8226# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00008227 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 if (wp->w_alist != &global_alist)
8229 for (i = 0; i < WARGCOUNT(wp); ++i)
8230 if (WARGLIST(wp)[i].ae_fname != NULL)
8231 slash_adjust(WARGLIST(wp)[i].ae_fname);
8232# endif
8233}
8234#endif
8235
8236/*
8237 * ":preserve".
8238 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008240ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008242 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 ml_preserve(curbuf, TRUE);
8244}
8245
8246/*
8247 * ":recover".
8248 */
8249 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008250ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251{
8252 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8253 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008254 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8255 | CCGD_MULTWIN
8256 | (eap->forceit ? CCGD_FORCEIT : 0)
8257 | CCGD_EXCMD)
8258
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 && (*eap->arg == NUL
8260 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8261 ml_recover();
8262 recoverymode = FALSE;
8263}
8264
8265/*
8266 * Command modifier used in a wrong way.
8267 */
8268 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008269ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270{
8271 eap->errmsg = e_invcmd;
8272}
8273
8274#ifdef FEAT_WINDOWS
8275/*
8276 * :sview [+command] file split window with new file, read-only
8277 * :split [[+command] file] split window with current or new file
8278 * :vsplit [[+command] file] split window vertically with current or new file
8279 * :new [[+command] file] split window with no or new file
8280 * :vnew [[+command] file] split vertically window with no or new file
8281 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008282 *
8283 * :tabedit open new Tab page with empty window
8284 * :tabedit [+command] file open new Tab page and edit "file"
8285 * :tabnew [[+command] file] just like :tabedit
8286 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 */
8288 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008289ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008291 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008292# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008294# endif
8295# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008297# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008299# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008301# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008303# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008305 * quickfix windows. But it's OK when doing ":tab split". */
8306 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {
8308 if (eap->cmdidx == CMD_split)
8309 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 if (eap->cmdidx == CMD_vsplit)
8311 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008315# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008316 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {
8318 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8319 FNAME_MESS, TRUE, curbuf->b_ffname);
8320 if (fname == NULL)
8321 goto theend;
8322 eap->arg = fname;
8323 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008324# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008326# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008328# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 && eap->cmdidx != CMD_new)
8332 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008333# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008334 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008335# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008336 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008337# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008338 au_has_group((char_u *)"FileExplorer"))
8339 {
8340 /* No browsing supported but we do have the file explorer:
8341 * Edit the directory. */
8342 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8343 eap->arg = (char_u *)".";
8344 }
8345 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008346# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008347 {
8348 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008350 if (fname == NULL)
8351 goto theend;
8352 eap->arg = fname;
8353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 }
8355 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008356# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008358 /*
8359 * Either open new tab page or split the window.
8360 */
8361 if (eap->cmdidx == CMD_tabedit
8362 || eap->cmdidx == CMD_tabfind
8363 || eap->cmdidx == CMD_tabnew)
8364 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008365 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8366 : eap->addr_count == 0 ? 0
8367 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008368 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008369 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008370
8371 /* set the alternate buffer for the window we came from */
8372 if (curwin != old_curwin
8373 && win_valid(old_curwin)
8374 && old_curwin->w_buffer != curbuf
8375 && !cmdmod.keepalt)
8376 old_curwin->w_alt_fnum = curbuf->b_fnum;
8377 }
8378 }
8379 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8381 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008382# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 /* Reset 'scrollbind' when editing another file, but keep it when
8384 * doing ":split" without arguments. */
8385 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008386# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008388# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008390 {
8391 RESET_BINDING(curwin);
8392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 else
8394 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008395# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 do_exedit(eap, old_curwin);
8397 }
8398
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008399# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008403# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404theend:
8405 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008408
8409/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008410 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008411 */
8412 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008413tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008414{
8415 exarg_T ea;
8416
8417 vim_memset(&ea, 0, sizeof(ea));
8418 ea.cmdidx = CMD_tabnew;
8419 ea.cmd = (char_u *)"tabn";
8420 ea.arg = (char_u *)"";
8421 ex_splitview(&ea);
8422}
8423
8424/*
8425 * :tabnext command
8426 */
8427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008428ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008429{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008430 int tab_number;
8431
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008432 switch (eap->cmdidx)
8433 {
8434 case CMD_tabfirst:
8435 case CMD_tabrewind:
8436 goto_tabpage(1);
8437 break;
8438 case CMD_tablast:
8439 goto_tabpage(9999);
8440 break;
8441 case CMD_tabprevious:
8442 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008443 if (eap->arg && *eap->arg != NUL)
8444 {
8445 char_u *p = eap->arg;
8446 char_u *p_save = p;
8447
8448 tab_number = getdigits(&p);
8449 if (p == p_save || *p_save == '-' || *p != NUL
8450 || tab_number == 0)
8451 {
8452 /* No numbers as argument. */
8453 eap->errmsg = e_invarg;
8454 return;
8455 }
8456 }
8457 else
8458 {
8459 if (eap->addr_count == 0)
8460 tab_number = 1;
8461 else
8462 {
8463 tab_number = eap->line2;
8464 if (tab_number < 1)
8465 {
8466 eap->errmsg = e_invrange;
8467 return;
8468 }
8469 }
8470 }
8471 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008472 break;
8473 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008474 tab_number = get_tabpage_arg(eap);
8475 if (eap->errmsg == NULL)
8476 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008477 break;
8478 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008479}
8480
8481/*
8482 * :tabmove command
8483 */
8484 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008485ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008486{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008487 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008488
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008489 tab_number = get_tabpage_arg(eap);
8490 if (eap->errmsg == NULL)
8491 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008492}
8493
8494/*
8495 * :tabs command: List tabs and their contents.
8496 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008498ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008499{
8500 tabpage_T *tp;
8501 win_T *wp;
8502 int tabcount = 1;
8503
8504 msg_start();
8505 msg_scroll = TRUE;
8506 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8507 {
8508 msg_putchar('\n');
8509 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008510 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008511 out_flush(); /* output one line at a time */
8512 ui_breakcheck();
8513
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008514 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008515 wp = firstwin;
8516 else
8517 wp = tp->tp_firstwin;
8518 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8519 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008520 msg_putchar('\n');
8521 msg_putchar(wp == curwin ? '>' : ' ');
8522 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008523 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8524 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008525 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008526 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008527 else
8528 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8529 IObuff, IOSIZE, TRUE);
8530 msg_outtrans(IObuff);
8531 out_flush(); /* output one line at a time */
8532 ui_breakcheck();
8533 }
8534 }
8535}
8536
8537#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538
8539/*
8540 * ":mode": Set screen mode.
8541 * If no argument given, just get the screen size and redraw.
8542 */
8543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008544ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545{
8546 if (*eap->arg == NUL)
8547 shell_resized();
8548 else
8549 mch_screenmode(eap->arg);
8550}
8551
8552#ifdef FEAT_WINDOWS
8553/*
8554 * ":resize".
8555 * set, increment or decrement current window height
8556 */
8557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008558ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559{
8560 int n;
8561 win_T *wp = curwin;
8562
8563 if (eap->addr_count > 0)
8564 {
8565 n = eap->line2;
8566 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8567 ;
8568 }
8569
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008570# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 if (cmdmod.split & WSP_VERT)
8575 {
8576 if (*eap->arg == '-' || *eap->arg == '+')
8577 n += W_WIDTH(curwin);
8578 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8579 n = 9999;
8580 win_setwidth_win((int)n, wp);
8581 }
8582 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 {
8584 if (*eap->arg == '-' || *eap->arg == '+')
8585 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008586 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 n = 9999;
8588 win_setheight_win((int)n, wp);
8589 }
8590}
8591#endif
8592
8593/*
8594 * ":find [+command] <file>" command.
8595 */
8596 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008597ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598{
8599#ifdef FEAT_SEARCHPATH
8600 char_u *fname;
8601 int count;
8602
8603 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8604 TRUE, curbuf->b_ffname);
8605 if (eap->addr_count > 0)
8606 {
8607 /* Repeat finding the file "count" times. This matters when it
8608 * appears several times in the path. */
8609 count = eap->line2;
8610 while (fname != NULL && --count > 0)
8611 {
8612 vim_free(fname);
8613 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8614 FALSE, curbuf->b_ffname);
8615 }
8616 }
8617
8618 if (fname != NULL)
8619 {
8620 eap->arg = fname;
8621#endif
8622 do_exedit(eap, NULL);
8623#ifdef FEAT_SEARCHPATH
8624 vim_free(fname);
8625 }
8626#endif
8627}
8628
8629/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008630 * ":open" simulation: for now just work like ":visual".
8631 */
8632 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008633ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008634{
8635 regmatch_T regmatch;
8636 char_u *p;
8637
8638 curwin->w_cursor.lnum = eap->line2;
8639 beginline(BL_SOL | BL_FIX);
8640 if (*eap->arg == '/')
8641 {
8642 /* ":open /pattern/": put cursor in column found with pattern */
8643 ++eap->arg;
8644 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8645 *p = NUL;
8646 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8647 if (regmatch.regprog != NULL)
8648 {
8649 regmatch.rm_ic = p_ic;
8650 p = ml_get_curline();
8651 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008652 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008653 else
8654 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008655 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008656 }
8657 /* Move to the NUL, ignore any other arguments. */
8658 eap->arg += STRLEN(eap->arg);
8659 }
8660 check_cursor();
8661
8662 eap->cmdidx = CMD_visual;
8663 do_exedit(eap, NULL);
8664}
8665
8666/*
8667 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 */
8669 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008670ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671{
8672 do_exedit(eap, NULL);
8673}
8674
8675/*
8676 * ":edit <file>" command and alikes.
8677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008679do_exedit(
8680 exarg_T *eap,
8681 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682{
8683 int n;
8684#ifdef FEAT_WINDOWS
8685 int need_hide;
8686#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008687 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688
8689 /*
8690 * ":vi" command ends Ex mode.
8691 */
8692 if (exmode_active && (eap->cmdidx == CMD_visual
8693 || eap->cmdidx == CMD_view))
8694 {
8695 exmode_active = FALSE;
8696 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008697 {
8698 /* Special case: ":global/pat/visual\NLvi-commands" */
8699 if (global_busy)
8700 {
8701 int rd = RedrawingDisabled;
8702 int nwr = no_wait_return;
8703 int ms = msg_scroll;
8704#ifdef FEAT_GUI
8705 int he = hold_gui_events;
8706#endif
8707
8708 if (eap->nextcmd != NULL)
8709 {
8710 stuffReadbuff(eap->nextcmd);
8711 eap->nextcmd = NULL;
8712 }
8713
8714 if (exmode_was != EXMODE_VIM)
8715 settmode(TMODE_RAW);
8716 RedrawingDisabled = 0;
8717 no_wait_return = 0;
8718 need_wait_return = FALSE;
8719 msg_scroll = 0;
8720#ifdef FEAT_GUI
8721 hold_gui_events = 0;
8722#endif
8723 must_redraw = CLEAR;
8724
8725 main_loop(FALSE, TRUE);
8726
8727 RedrawingDisabled = rd;
8728 no_wait_return = nwr;
8729 msg_scroll = ms;
8730#ifdef FEAT_GUI
8731 hold_gui_events = he;
8732#endif
8733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 }
8737
8738 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008739 || eap->cmdidx == CMD_tabnew
8740 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008741#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 || eap->cmdidx == CMD_vnew
8743#endif
8744 ) && *eap->arg == NUL)
8745 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008746 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 setpcmark();
8748 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008749 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8750 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 }
8752 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008753#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 && eap->cmdidx != CMD_vsplit
8755#endif
8756 )
8757 || *eap->arg != NUL
8758#ifdef FEAT_BROWSE
8759 || cmdmod.browse
8760#endif
8761 )
8762 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008763#ifdef FEAT_AUTOCMD
8764 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8765 * can bring us here, others are stopped earlier. */
8766 if (*eap->arg != NUL && curbuf_locked())
8767 return;
8768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 n = readonlymode;
8770 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8771 readonlymode = TRUE;
8772 else if (eap->cmdidx == CMD_enew)
8773 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8774 empty buffer */
8775 setpcmark();
8776 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8777 NULL, eap,
8778 /* ":edit" goes to first line if Vi compatible */
8779 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8780 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8781 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008782 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008784 /* after a split we can use an existing buffer */
8785 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786#ifdef FEAT_LISTCMDS
8787 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8788#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008789 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 {
8791 /* Editing the file failed. If the window was split, close it. */
8792#ifdef FEAT_WINDOWS
8793 if (old_curwin != NULL)
8794 {
8795 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008796 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008798# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8799 cleanup_T cs;
8800
8801 /* Reset the error/interrupt/exception state here so that
8802 * aborting() returns FALSE when closing a window. */
8803 enter_cleanup(&cs);
8804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805# ifdef FEAT_GUI
8806 need_mouse_correct = TRUE;
8807# endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008808 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008809
8810# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8811 /* Restore the error/interrupt/exception state if not
8812 * discarded by a new aborting error, interrupt, or
8813 * uncaught exception. */
8814 leave_cleanup(&cs);
8815# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 }
8817 }
8818#endif
8819 }
8820 else if (readonlymode && curbuf->b_nwindows == 1)
8821 {
8822 /* When editing an already visited buffer, 'readonly' won't be set
8823 * but the previous value is kept. With ":view" and ":sview" we
8824 * want the file to be readonly, except when another window is
8825 * editing the same buffer. */
8826 curbuf->b_p_ro = TRUE;
8827 }
8828 readonlymode = n;
8829 }
8830 else
8831 {
8832 if (eap->do_ecmd_cmd != NULL)
8833 do_cmdline_cmd(eap->do_ecmd_cmd);
8834#ifdef FEAT_TITLE
8835 n = curwin->w_arg_idx_invalid;
8836#endif
8837 check_arg_idx(curwin);
8838#ifdef FEAT_TITLE
8839 if (n != curwin->w_arg_idx_invalid)
8840 maketitle();
8841#endif
8842 }
8843
8844#ifdef FEAT_WINDOWS
8845 /*
8846 * if ":split file" worked, set alternate file name in old window to new
8847 * file
8848 */
8849 if (old_curwin != NULL
8850 && *eap->arg != NUL
8851 && curwin != old_curwin
8852 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008853 && old_curwin->w_buffer != curbuf
8854 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 old_curwin->w_alt_fnum = curbuf->b_fnum;
8856#endif
8857
8858 ex_no_reprint = TRUE;
8859}
8860
8861#ifndef FEAT_GUI
8862/*
8863 * ":gui" and ":gvim" when there is no GUI.
8864 */
8865 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008866ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867{
8868 eap->errmsg = e_nogvim;
8869}
8870#endif
8871
8872#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8873 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008874ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875{
8876 gui_make_tearoff(eap->arg);
8877}
8878#endif
8879
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008880#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008882ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008884 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885}
8886#endif
8887
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008889ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890{
8891 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8892 MSG(_("No swap file"));
8893 else
8894 msg(curbuf->b_ml.ml_mfp->mf_fname);
8895}
8896
8897/*
8898 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8899 * offset.
8900 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008903ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904{
8905#ifdef FEAT_SCROLLBIND
8906 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008907 win_T *save_curwin = curwin;
8908 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 long topline;
8910 long y;
8911 linenr_T old_linenr = curwin->w_cursor.lnum;
8912
8913 setpcmark();
8914
8915 /*
8916 * determine max topline
8917 */
8918 if (curwin->w_p_scb)
8919 {
8920 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008921 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 {
8923 if (wp->w_p_scb && wp->w_buffer)
8924 {
8925 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8926 if (topline > y)
8927 topline = y;
8928 }
8929 }
8930 if (topline < 1)
8931 topline = 1;
8932 }
8933 else
8934 {
8935 topline = 1;
8936 }
8937
8938
8939 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008940 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008942 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 {
8944 if (curwin->w_p_scb)
8945 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008946 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 y = topline - curwin->w_topline;
8948 if (y > 0)
8949 scrollup(y, TRUE);
8950 else
8951 scrolldown(-y, TRUE);
8952 curwin->w_scbind_pos = topline;
8953 redraw_later(VALID);
8954 cursor_correct();
8955#ifdef FEAT_WINDOWS
8956 curwin->w_redr_status = TRUE;
8957#endif
8958 }
8959 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008960 curwin = save_curwin;
8961 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 if (curwin->w_p_scb)
8963 {
8964 did_syncbind = TRUE;
8965 checkpcmark();
8966 if (old_linenr != curwin->w_cursor.lnum)
8967 {
8968 char_u ctrl_o[2];
8969
8970 ctrl_o[0] = Ctrl_O;
8971 ctrl_o[1] = 0;
8972 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8973 }
8974 }
8975#endif
8976}
8977
8978
8979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008980ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008982 int i;
8983 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8984 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985
8986 if (eap->usefilter) /* :r!cmd */
8987 do_bang(1, eap, FALSE, FALSE, TRUE);
8988 else
8989 {
8990 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8991 return;
8992
8993#ifdef FEAT_BROWSE
8994 if (cmdmod.browse)
8995 {
8996 char_u *browseFile;
8997
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008998 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 NULL, NULL, NULL, curbuf);
9000 if (browseFile != NULL)
9001 {
9002 i = readfile(browseFile, NULL,
9003 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9004 vim_free(browseFile);
9005 }
9006 else
9007 i = OK;
9008 }
9009 else
9010#endif
9011 if (*eap->arg == NUL)
9012 {
9013 if (check_fname() == FAIL) /* check for no file name */
9014 return;
9015 i = readfile(curbuf->b_ffname, curbuf->b_fname,
9016 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9017 }
9018 else
9019 {
9020 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
9021 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
9022 i = readfile(eap->arg, NULL,
9023 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9024
9025 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01009026 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 {
9028#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
9029 if (!aborting())
9030#endif
9031 EMSG2(_(e_notopen), eap->arg);
9032 }
9033 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009034 {
9035 if (empty && exmode_active)
9036 {
9037 /* Delete the empty line that remains. Historically ex does
9038 * this but vi doesn't. */
9039 if (eap->line2 == 0)
9040 lnum = curbuf->b_ml.ml_line_count;
9041 else
9042 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009043 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009044 {
9045 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009046 if (curwin->w_cursor.lnum > 1
9047 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009048 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009049 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009050 }
9051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 }
9055}
9056
Bram Moolenaarea408852005-06-25 22:49:46 +00009057static char_u *prev_dir = NULL;
9058
9059#if defined(EXITFREE) || defined(PROTO)
9060 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009061free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009062{
9063 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00009064 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00009065
9066 vim_free(globaldir);
9067 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00009068}
9069#endif
9070
Bram Moolenaarf4258302013-06-02 18:20:17 +02009071/*
9072 * Deal with the side effects of changing the current directory.
9073 * When "local" is TRUE then this was after an ":lcd" command.
9074 */
9075 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009076post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009077{
9078 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01009079 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02009080 if (local)
9081 {
9082 /* If still in global directory, need to remember current
9083 * directory as global directory. */
9084 if (globaldir == NULL && prev_dir != NULL)
9085 globaldir = vim_strsave(prev_dir);
9086 /* Remember this local directory for the window. */
9087 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9088 curwin->w_localdir = vim_strsave(NameBuff);
9089 }
9090 else
9091 {
9092 /* We are now in the global directory, no need to remember its
9093 * name. */
9094 vim_free(globaldir);
9095 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02009096 }
9097
9098 shorten_fnames(TRUE);
9099}
9100
Bram Moolenaarea408852005-06-25 22:49:46 +00009101
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102/*
9103 * ":cd", ":lcd", ":chdir" and ":lchdir".
9104 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009105 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009106ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 char_u *new_dir;
9109 char_u *tofree;
9110
9111 new_dir = eap->arg;
9112#if !defined(UNIX) && !defined(VMS)
9113 /* for non-UNIX ":cd" means: print current directory */
9114 if (*new_dir == NUL)
9115 ex_pwd(NULL);
9116 else
9117#endif
9118 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009119#ifdef FEAT_AUTOCMD
9120 if (allbuf_locked())
9121 return;
9122#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009123 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9124 && !eap->forceit)
9125 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009126 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009127 return;
9128 }
9129
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 /* ":cd -": Change to previous directory */
9131 if (STRCMP(new_dir, "-") == 0)
9132 {
9133 if (prev_dir == NULL)
9134 {
9135 EMSG(_("E186: No previous directory"));
9136 return;
9137 }
9138 new_dir = prev_dir;
9139 }
9140
9141 /* Save current directory for next ":cd -" */
9142 tofree = prev_dir;
9143 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9144 prev_dir = vim_strsave(NameBuff);
9145 else
9146 prev_dir = NULL;
9147
9148#if defined(UNIX) || defined(VMS)
9149 /* for UNIX ":cd" means: go to home directory */
9150 if (*new_dir == NUL)
9151 {
9152 /* use NameBuff for home directory name */
9153# ifdef VMS
9154 char_u *p;
9155
9156 p = mch_getenv((char_u *)"SYS$LOGIN");
9157 if (p == NULL || *p == NUL) /* empty is the same as not set */
9158 NameBuff[0] = NUL;
9159 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009160 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161# else
9162 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9163# endif
9164 new_dir = NameBuff;
9165 }
9166#endif
9167 if (new_dir == NULL || vim_chdir(new_dir))
9168 EMSG(_(e_failed));
9169 else
9170 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02009171 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172
9173 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009174 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 ex_pwd(eap);
9176 }
9177 vim_free(tofree);
9178 }
9179}
9180
9181/*
9182 * ":pwd".
9183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009185ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186{
9187 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9188 {
9189#ifdef BACKSLASH_IN_FILENAME
9190 slash_adjust(NameBuff);
9191#endif
9192 msg(NameBuff);
9193 }
9194 else
9195 EMSG(_("E187: Unknown"));
9196}
9197
9198/*
9199 * ":=".
9200 */
9201 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009202ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203{
9204 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009205 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206}
9207
9208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009209ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009211 int n;
9212 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213
9214 if (cursor_valid())
9215 {
9216 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9217 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01009218 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009220
9221 len = eap->line2;
9222 switch (*eap->arg)
9223 {
9224 case 'm': break;
9225 case NUL: len *= 1000L; break;
9226 default: EMSG2(_(e_invarg2), eap->arg); return;
9227 }
9228 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229}
9230
9231/*
9232 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9233 */
9234 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009235do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236{
9237 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009238 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239
9240 cursor_on();
9241 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009242 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009244 wait_now = msec - done > 1000L ? 1000L : msec - done;
9245#ifdef FEAT_TIMERS
9246 {
9247 long due_time = check_due_timer();
9248
9249 if (due_time > 0 && due_time < wait_now)
9250 wait_now = due_time;
9251 }
9252#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009253#ifdef FEAT_JOB_CHANNEL
9254 if (has_any_channel() && wait_now > 100L)
9255 wait_now = 100L;
9256#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009257 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009258#ifdef FEAT_JOB_CHANNEL
9259 if (has_any_channel())
9260 ui_breakcheck_force(TRUE);
9261 else
9262#endif
9263 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009264#ifdef MESSAGE_QUEUE
9265 /* Process the netbeans and clientserver messages that may have been
9266 * received in the call to ui_breakcheck() when the GUI is in use. This
9267 * may occur when running a test case. */
9268 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 }
9271}
9272
9273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009274do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275{
9276 int mode;
9277 char_u *cmdp;
9278
9279 cmdp = eap->cmd;
9280 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9281
9282 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9283 eap->arg, mode, isabbrev))
9284 {
9285 case 1: EMSG(_(e_invarg));
9286 break;
9287 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9288 break;
9289 }
9290}
9291
9292/*
9293 * ":winsize" command (obsolete).
9294 */
9295 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009296ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297{
9298 int w, h;
9299 char_u *arg = eap->arg;
9300 char_u *p;
9301
9302 w = getdigits(&arg);
9303 arg = skipwhite(arg);
9304 p = arg;
9305 h = getdigits(&arg);
9306 if (*p != NUL && *arg == NUL)
9307 set_shellsize(w, h, TRUE);
9308 else
9309 EMSG(_("E465: :winsize requires two number arguments"));
9310}
9311
9312#ifdef FEAT_WINDOWS
9313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009314ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315{
9316 int xchar = NUL;
9317 char_u *p;
9318
9319 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9320 {
9321 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9322 if (eap->arg[1] == NUL)
9323 {
9324 EMSG(_(e_invarg));
9325 return;
9326 }
9327 xchar = eap->arg[1];
9328 p = eap->arg + 2;
9329 }
9330 else
9331 p = eap->arg + 1;
9332
9333 eap->nextcmd = check_nextcmd(p);
9334 p = skipwhite(p);
9335 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9336 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009337 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 {
9339 /* Pass flags on for ":vertical wincmd ]". */
9340 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009341 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9343 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009344 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 }
9346}
9347#endif
9348
Bram Moolenaar843ee412004-06-30 16:16:41 +00009349#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350/*
9351 * ":winpos".
9352 */
9353 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009354ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355{
9356 int x, y;
9357 char_u *arg = eap->arg;
9358 char_u *p;
9359
9360 if (*arg == NUL)
9361 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009362# if defined(FEAT_GUI) || defined(MSWIN)
9363# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009365# else
9366 if (mch_get_winpos(&x, &y) != FAIL)
9367# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 {
9369 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9370 msg(IObuff);
9371 }
9372 else
9373# endif
9374 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9375 }
9376 else
9377 {
9378 x = getdigits(&arg);
9379 arg = skipwhite(arg);
9380 p = arg;
9381 y = getdigits(&arg);
9382 if (*p == NUL || *arg != NUL)
9383 {
9384 EMSG(_("E466: :winpos requires two number arguments"));
9385 return;
9386 }
9387# ifdef FEAT_GUI
9388 if (gui.in_use)
9389 gui_mch_set_winpos(x, y);
9390 else if (gui.starting)
9391 {
9392 /* Remember the coordinates for when the window is opened. */
9393 gui_win_x = x;
9394 gui_win_y = y;
9395 }
9396# ifdef HAVE_TGETENT
9397 else
9398# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009399# else
9400# ifdef MSWIN
9401 mch_set_winpos(x, y);
9402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403# endif
9404# ifdef HAVE_TGETENT
9405 if (*T_CWP)
9406 term_set_winpos(x, y);
9407# endif
9408 }
9409}
9410#endif
9411
9412/*
9413 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9414 */
9415 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009416ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417{
9418 oparg_T oa;
9419
9420 clear_oparg(&oa);
9421 oa.regname = eap->regname;
9422 oa.start.lnum = eap->line1;
9423 oa.end.lnum = eap->line2;
9424 oa.line_count = eap->line2 - eap->line1 + 1;
9425 oa.motion_type = MLINE;
9426#ifdef FEAT_VIRTUALEDIT
9427 virtual_op = FALSE;
9428#endif
9429 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9430 {
9431 setpcmark();
9432 curwin->w_cursor.lnum = eap->line1;
9433 beginline(BL_SOL | BL_FIX);
9434 }
9435
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009436 if (VIsual_active)
9437 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009438
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 switch (eap->cmdidx)
9440 {
9441 case CMD_delete:
9442 oa.op_type = OP_DELETE;
9443 op_delete(&oa);
9444 break;
9445
9446 case CMD_yank:
9447 oa.op_type = OP_YANK;
9448 (void)op_yank(&oa, FALSE, TRUE);
9449 break;
9450
9451 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009452 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009454 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9455#else
9456 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009458 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 oa.op_type = OP_RSHIFT;
9460 else
9461 oa.op_type = OP_LSHIFT;
9462 op_shift(&oa, FALSE, eap->amount);
9463 break;
9464 }
9465#ifdef FEAT_VIRTUALEDIT
9466 virtual_op = MAYBE;
9467#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009468 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469}
9470
9471/*
9472 * ":put".
9473 */
9474 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009475ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476{
9477 /* ":0put" works like ":1put!". */
9478 if (eap->line2 == 0)
9479 {
9480 eap->line2 = 1;
9481 eap->forceit = TRUE;
9482 }
9483 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009484 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9485 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486}
9487
9488/*
9489 * Handle ":copy" and ":move".
9490 */
9491 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009492ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493{
9494 long n;
9495
Bram Moolenaarded27822017-01-02 14:27:34 +01009496 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 if (eap->arg == NULL) /* error detected */
9498 {
9499 eap->nextcmd = NULL;
9500 return;
9501 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009502 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503
9504 /*
9505 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9506 */
9507 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9508 {
9509 EMSG(_(e_invaddr));
9510 return;
9511 }
9512
9513 if (eap->cmdidx == CMD_move)
9514 {
9515 if (do_move(eap->line1, eap->line2, n) == FAIL)
9516 return;
9517 }
9518 else
9519 ex_copy(eap->line1, eap->line2, n);
9520 u_clearline();
9521 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009522 ex_may_print(eap);
9523}
9524
9525/*
9526 * Print the current line if flags were given to the Ex command.
9527 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009528 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009529ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009530{
9531 if (eap->flags != 0)
9532 {
9533 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9534 (eap->flags & EXFLAG_LIST));
9535 ex_no_reprint = TRUE;
9536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537}
9538
9539/*
9540 * ":smagic" and ":snomagic".
9541 */
9542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009543ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544{
9545 int magic_save = p_magic;
9546
9547 p_magic = (eap->cmdidx == CMD_smagic);
9548 do_sub(eap);
9549 p_magic = magic_save;
9550}
9551
9552/*
9553 * ":join".
9554 */
9555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009556ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557{
9558 curwin->w_cursor.lnum = eap->line1;
9559 if (eap->line1 == eap->line2)
9560 {
9561 if (eap->addr_count >= 2) /* :2,2join does nothing */
9562 return;
9563 if (eap->line2 == curbuf->b_ml.ml_line_count)
9564 {
9565 beep_flush();
9566 return;
9567 }
9568 ++eap->line2;
9569 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009570 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009572 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573}
9574
9575/*
9576 * ":[addr]@r" or ":[addr]*r": execute register
9577 */
9578 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009579ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580{
9581 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009582 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583
9584 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009585 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586
9587#ifdef USE_ON_FLY_SCROLL
9588 dont_scroll = TRUE; /* disallow scrolling here */
9589#endif
9590
9591 /* get the register name. No name means to use the previous one */
9592 c = *eap->arg;
9593 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9594 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009595 /* Put the register in the typeahead buffer with the "silent" flag. */
9596 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9597 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 else
9602 {
9603 int save_efr = exec_from_reg;
9604
9605 exec_from_reg = TRUE;
9606
9607 /*
9608 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009609 * Continue until the stuff buffer is empty and all added characters
9610 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009612 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9614
9615 exec_from_reg = save_efr;
9616 }
9617}
9618
9619/*
9620 * ":!".
9621 */
9622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009623ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624{
9625 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9626}
9627
9628/*
9629 * ":undo".
9630 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009632ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009634 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009635 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009636 else
9637 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638}
9639
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009640#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009642ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009643{
9644 char_u hash[UNDO_HASH_SIZE];
9645
9646 u_compute_hash(hash);
9647 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9648}
9649
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009651ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009652{
9653 char_u hash[UNDO_HASH_SIZE];
9654
9655 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009656 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009657}
9658#endif
9659
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660/*
9661 * ":redo".
9662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009664ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665{
9666 u_redo(1);
9667}
9668
9669/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009670 * ":earlier" and ":later".
9671 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009673ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009674{
9675 long count = 0;
9676 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009677 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009678 char_u *p = eap->arg;
9679
9680 if (*p == NUL)
9681 count = 1;
9682 else if (isdigit(*p))
9683 {
9684 count = getdigits(&p);
9685 switch (*p)
9686 {
9687 case 's': ++p; sec = TRUE; break;
9688 case 'm': ++p; sec = TRUE; count *= 60; break;
9689 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009690 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9691 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009692 }
9693 }
9694
9695 if (*p != NUL)
9696 EMSG2(_(e_invarg2), eap->arg);
9697 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009698 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9699 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009700}
9701
9702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 * ":redir": start/stop redirection.
9704 */
9705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009706ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707{
9708 char *mode;
9709 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009710 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711
Bram Moolenaarba768492016-07-08 15:32:54 +02009712#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009713 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009714 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009715 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009716 return;
9717 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009718#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009719
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 if (STRICMP(eap->arg, "END") == 0)
9721 close_redir();
9722 else
9723 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009724 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009726 ++arg;
9727 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009729 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 mode = "a";
9731 }
9732 else
9733 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009734 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735
9736 close_redir();
9737
9738 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009739 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 if (fname == NULL)
9741 return;
9742#ifdef FEAT_BROWSE
9743 if (cmdmod.browse)
9744 {
9745 char_u *browseFile;
9746
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009747 browseFile = do_browse(BROWSE_SAVE,
9748 (char_u *)_("Save Redirection"),
9749 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 if (browseFile == NULL)
9751 return; /* operation cancelled */
9752 vim_free(fname);
9753 fname = browseFile;
9754 eap->forceit = TRUE; /* since dialog already asked */
9755 }
9756#endif
9757
9758 redir_fd = open_exfile(fname, eap->forceit, mode);
9759 vim_free(fname);
9760 }
9761#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009762 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 {
9764 /* redirect to a register a-z (resp. A-Z for appending) */
9765 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009766 ++arg;
9767 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009769 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009770 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009772 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009774 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009775 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009776 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009777 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009779 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009780 if (*arg == '>')
9781 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009782 /* Make register empty when not using @A-@Z and the
9783 * command is valid. */
9784 if (*arg == NUL && !isupper(redir_reg))
9785 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009787 }
9788 if (*arg != NUL)
9789 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009790 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009791 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009792 }
9793 }
9794 else if (*arg == '=' && arg[1] == '>')
9795 {
9796 int append;
9797
9798 /* redirect to a variable */
9799 close_redir();
9800 arg += 2;
9801
9802 if (*arg == '>')
9803 {
9804 ++arg;
9805 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 }
9807 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009808 append = FALSE;
9809
9810 if (var_redir_start(skipwhite(arg), append) == OK)
9811 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 }
9813#endif
9814
9815 /* TODO: redirect to a buffer */
9816
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009818 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009820
9821 /* Make sure redirection is not off. Can happen for cmdline completion
9822 * that indirectly invokes a command to catch its output. */
9823 if (redir_fd != NULL
9824#ifdef FEAT_EVAL
9825 || redir_reg || redir_vname
9826#endif
9827 )
9828 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829}
9830
9831/*
9832 * ":redraw": force redraw
9833 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009834 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009835ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836{
9837 int r = RedrawingDisabled;
9838 int p = p_lz;
9839
9840 RedrawingDisabled = 0;
9841 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009842 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009844 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845#ifdef FEAT_TITLE
9846 if (need_maketitle)
9847 maketitle();
9848#endif
9849 RedrawingDisabled = r;
9850 p_lz = p;
9851
9852 /* Reset msg_didout, so that a message that's there is overwritten. */
9853 msg_didout = FALSE;
9854 msg_col = 0;
9855
Bram Moolenaar56667a52013-07-17 11:54:28 +02009856 /* No need to wait after an intentional redraw. */
9857 need_wait_return = FALSE;
9858
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 out_flush();
9860}
9861
9862/*
9863 * ":redrawstatus": force redraw of status line(s)
9864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009866ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867{
9868#if defined(FEAT_WINDOWS)
9869 int r = RedrawingDisabled;
9870 int p = p_lz;
9871
9872 RedrawingDisabled = 0;
9873 p_lz = FALSE;
9874 if (eap->forceit)
9875 status_redraw_all();
9876 else
9877 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009878 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 RedrawingDisabled = r;
9880 p_lz = p;
9881 out_flush();
9882#endif
9883}
9884
9885 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009886close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887{
9888 if (redir_fd != NULL)
9889 {
9890 fclose(redir_fd);
9891 redir_fd = NULL;
9892 }
9893#ifdef FEAT_EVAL
9894 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009895 if (redir_vname)
9896 {
9897 var_redir_stop();
9898 redir_vname = 0;
9899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900#endif
9901}
9902
9903#if defined(FEAT_SESSION) && defined(USE_CRNL)
9904# define MKSESSION_NL
9905static int mksession_nl = FALSE; /* use NL only in put_eol() */
9906#endif
9907
9908/*
9909 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9910 */
9911 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009912ex_mkrc(
9913 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914{
9915 FILE *fd;
9916 int failed = FALSE;
9917 char_u *fname;
9918#ifdef FEAT_BROWSE
9919 char_u *browseFile = NULL;
9920#endif
9921#ifdef FEAT_SESSION
9922 int view_session = FALSE;
9923 int using_vdir = FALSE; /* using 'viewdir'? */
9924 char_u *viewFile = NULL;
9925 unsigned *flagp;
9926#endif
9927
9928 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9929 {
9930#ifdef FEAT_SESSION
9931 view_session = TRUE;
9932#else
9933 ex_ni(eap);
9934 return;
9935#endif
9936 }
9937
9938#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009939 /* Use the short file name until ":lcd" is used. We also don't use the
9940 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009941 did_lcd = FALSE;
9942
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9944 if (eap->cmdidx == CMD_mkview
9945 && (*eap->arg == NUL
9946 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9947 {
9948 eap->forceit = TRUE;
9949 fname = get_view_file(*eap->arg);
9950 if (fname == NULL)
9951 return;
9952 viewFile = fname;
9953 using_vdir = TRUE;
9954 }
9955 else
9956#endif
9957 if (*eap->arg != NUL)
9958 fname = eap->arg;
9959 else if (eap->cmdidx == CMD_mkvimrc)
9960 fname = (char_u *)VIMRC_FILE;
9961#ifdef FEAT_SESSION
9962 else if (eap->cmdidx == CMD_mksession)
9963 fname = (char_u *)SESSION_FILE;
9964#endif
9965 else
9966 fname = (char_u *)EXRC_FILE;
9967
9968#ifdef FEAT_BROWSE
9969 if (cmdmod.browse)
9970 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009971 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972# ifdef FEAT_SESSION
9973 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9974 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9975# endif
9976 (char_u *)_("Save Setup"),
9977 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9978 if (browseFile == NULL)
9979 goto theend;
9980 fname = browseFile;
9981 eap->forceit = TRUE; /* since dialog already asked */
9982 }
9983#endif
9984
9985#if defined(FEAT_SESSION) && defined(vim_mkdir)
9986 /* When using 'viewdir' may have to create the directory. */
9987 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009988 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989#endif
9990
9991 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9992 if (fd != NULL)
9993 {
9994#ifdef FEAT_SESSION
9995 if (eap->cmdidx == CMD_mkview)
9996 flagp = &vop_flags;
9997 else
9998 flagp = &ssop_flags;
9999#endif
10000
10001#ifdef MKSESSION_NL
10002 /* "unix" in 'sessionoptions': use NL line separator */
10003 if (view_session && (*flagp & SSOP_UNIX))
10004 mksession_nl = TRUE;
10005#endif
10006
10007 /* Write the version command for :mkvimrc */
10008 if (eap->cmdidx == CMD_mkvimrc)
10009 (void)put_line(fd, "version 6.0");
10010
10011#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010012 if (eap->cmdidx == CMD_mksession)
10013 {
10014 if (put_line(fd, "let SessionLoad = 1") == FAIL)
10015 failed = TRUE;
10016 }
10017
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 if (eap->cmdidx != CMD_mkview)
10019#endif
10020 {
10021 /* Write setting 'compatible' first, because it has side effects.
10022 * For that same reason only do it when needed. */
10023 if (p_cp)
10024 (void)put_line(fd, "if !&cp | set cp | endif");
10025 else
10026 (void)put_line(fd, "if &cp | set nocp | endif");
10027 }
10028
10029#ifdef FEAT_SESSION
10030 if (!view_session
10031 || (eap->cmdidx == CMD_mksession
10032 && (*flagp & SSOP_OPTIONS)))
10033#endif
10034 failed |= (makemap(fd, NULL) == FAIL
10035 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10036
10037#ifdef FEAT_SESSION
10038 if (!failed && view_session)
10039 {
10040 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10041 failed = TRUE;
10042 if (eap->cmdidx == CMD_mksession)
10043 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010044 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045
Bram Moolenaard9462e32011-04-11 21:35:11 +020010046 dirnow = alloc(MAXPATHL);
10047 if (dirnow == NULL)
10048 failed = TRUE;
10049 else
10050 {
10051 /*
10052 * Change to session file's dir.
10053 */
10054 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010056 *dirnow = NUL;
10057 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10058 {
10059 if (vim_chdirfile(fname) == OK)
10060 shorten_fnames(TRUE);
10061 }
10062 else if (*dirnow != NUL
10063 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10064 {
10065 if (mch_chdir((char *)globaldir) == 0)
10066 shorten_fnames(TRUE);
10067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068
Bram Moolenaard9462e32011-04-11 21:35:11 +020010069 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070
Bram Moolenaard9462e32011-04-11 21:35:11 +020010071 /* restore original dir */
10072 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010074 {
10075 if (mch_chdir((char *)dirnow) != 0)
10076 EMSG(_(e_prev_dir));
10077 shorten_fnames(TRUE);
10078 }
10079 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 }
10081 }
10082 else
10083 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010084 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10085 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 }
10087 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10088 == FAIL)
10089 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010090 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10091 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010092 if (eap->cmdidx == CMD_mksession)
10093 {
10094 if (put_line(fd, "unlet SessionLoad") == FAIL)
10095 failed = TRUE;
10096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 }
10098#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010099 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10100 failed = TRUE;
10101
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 failed |= fclose(fd);
10103
10104 if (failed)
10105 EMSG(_(e_write));
10106#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10107 else if (eap->cmdidx == CMD_mksession)
10108 {
10109 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010110 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111
Bram Moolenaard9462e32011-04-11 21:35:11 +020010112 tbuf = alloc(MAXPATHL);
10113 if (tbuf != NULL)
10114 {
10115 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10116 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10117 vim_free(tbuf);
10118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 }
10120#endif
10121#ifdef MKSESSION_NL
10122 mksession_nl = FALSE;
10123#endif
10124 }
10125
10126#ifdef FEAT_BROWSE
10127theend:
10128 vim_free(browseFile);
10129#endif
10130#ifdef FEAT_SESSION
10131 vim_free(viewFile);
10132#endif
10133}
10134
Bram Moolenaardf177f62005-02-22 08:39:57 +000010135#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10136 || defined(PROTO)
10137 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010138vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010139{
10140 if (vim_mkdir(name, prot) != 0)
10141 {
10142 EMSG2(_("E739: Cannot create directory: %s"), name);
10143 return FAIL;
10144 }
10145 return OK;
10146}
10147#endif
10148
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149/*
10150 * Open a file for writing for an Ex command, with some checks.
10151 * Return file descriptor, or NULL on failure.
10152 */
10153 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010154open_exfile(
10155 char_u *fname,
10156 int forceit,
10157 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158{
10159 FILE *fd;
10160
10161#ifdef UNIX
10162 /* with Unix it is possible to open a directory */
10163 if (mch_isdir(fname))
10164 {
10165 EMSG2(_(e_isadir2), fname);
10166 return NULL;
10167 }
10168#endif
10169 if (!forceit && *mode != 'a' && vim_fexists(fname))
10170 {
10171 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10172 return NULL;
10173 }
10174
10175 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10176 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10177
10178 return fd;
10179}
10180
10181/*
10182 * ":mark" and ":k".
10183 */
10184 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010185ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186{
10187 pos_T pos;
10188
10189 if (*eap->arg == NUL) /* No argument? */
10190 EMSG(_(e_argreq));
10191 else if (eap->arg[1] != NUL) /* more than one character? */
10192 EMSG(_(e_trailing));
10193 else
10194 {
10195 pos = curwin->w_cursor; /* save curwin->w_cursor */
10196 curwin->w_cursor.lnum = eap->line2;
10197 beginline(BL_WHITE | BL_FIX);
10198 if (setmark(*eap->arg) == FAIL) /* set mark */
10199 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10200 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10201 }
10202}
10203
10204/*
10205 * Update w_topline, w_leftcol and the cursor position.
10206 */
10207 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010208update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209{
10210 check_cursor(); /* put cursor on valid line */
10211 update_topline();
10212 if (!curwin->w_p_wrap)
10213 validate_cursor();
10214 update_curswant();
10215}
10216
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217/*
10218 * ":normal[!] {commands}": Execute normal mode commands.
10219 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010220 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010221ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 int save_msg_scroll = msg_scroll;
10224 int save_restart_edit = restart_edit;
10225 int save_msg_didout = msg_didout;
10226 int save_State = State;
10227 tasave_T tabuf;
10228 int save_insertmode = p_im;
10229 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010230 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231#ifdef FEAT_MBYTE
10232 char_u *arg = NULL;
10233 int l;
10234 char_u *p;
10235#endif
10236
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010237 if (ex_normal_lock > 0)
10238 {
10239 EMSG(_(e_secure));
10240 return;
10241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 if (ex_normal_busy >= p_mmd)
10243 {
10244 EMSG(_("E192: Recursive use of :normal too deep"));
10245 return;
10246 }
10247 ++ex_normal_busy;
10248
10249 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10250 restart_edit = 0; /* don't go to Insert mode */
10251 p_im = FALSE; /* don't use 'insertmode' */
10252
10253#ifdef FEAT_MBYTE
10254 /*
10255 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10256 * this for the K_SPECIAL leading byte, otherwise special keys will not
10257 * work.
10258 */
10259 if (has_mbyte)
10260 {
10261 int len = 0;
10262
10263 /* Count the number of characters to be escaped. */
10264 for (p = eap->arg; *p != NUL; ++p)
10265 {
10266# ifdef FEAT_GUI
10267 if (*p == CSI) /* leadbyte CSI */
10268 len += 2;
10269# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010270 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10272# ifdef FEAT_GUI
10273 || *p == CSI
10274# endif
10275 )
10276 len += 2;
10277 }
10278 if (len > 0)
10279 {
10280 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10281 if (arg != NULL)
10282 {
10283 len = 0;
10284 for (p = eap->arg; *p != NUL; ++p)
10285 {
10286 arg[len++] = *p;
10287# ifdef FEAT_GUI
10288 if (*p == CSI)
10289 {
10290 arg[len++] = KS_EXTRA;
10291 arg[len++] = (int)KE_CSI;
10292 }
10293# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010294 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 {
10296 arg[len++] = *++p;
10297 if (*p == K_SPECIAL)
10298 {
10299 arg[len++] = KS_SPECIAL;
10300 arg[len++] = KE_FILLER;
10301 }
10302# ifdef FEAT_GUI
10303 else if (*p == CSI)
10304 {
10305 arg[len++] = KS_EXTRA;
10306 arg[len++] = (int)KE_CSI;
10307 }
10308# endif
10309 }
10310 arg[len] = NUL;
10311 }
10312 }
10313 }
10314 }
10315#endif
10316
10317 /*
10318 * Save the current typeahead. This is required to allow using ":normal"
10319 * from an event handler and makes sure we don't hang when the argument
10320 * ends with half a command.
10321 */
10322 save_typeahead(&tabuf);
10323 if (tabuf.typebuf_valid)
10324 {
10325 /*
10326 * Repeat the :normal command for each line in the range. When no
10327 * range given, execute it just once, without positioning the cursor
10328 * first.
10329 */
10330 do
10331 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 if (eap->addr_count != 0)
10333 {
10334 curwin->w_cursor.lnum = eap->line1++;
10335 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010336 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 }
10338
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010339 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340#ifdef FEAT_MBYTE
10341 arg != NULL ? arg :
10342#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010343 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 }
10345 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10346 }
10347
10348 /* Might not return to the main loop when in an event handler. */
10349 update_topline_cursor();
10350
10351 /* Restore the previous typeahead. */
10352 restore_typeahead(&tabuf);
10353
10354 --ex_normal_busy;
10355 msg_scroll = save_msg_scroll;
10356 restart_edit = save_restart_edit;
10357 p_im = save_insertmode;
10358 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010359 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10361
10362 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010363 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010365#ifdef FEAT_MOUSE
10366 setmouse();
10367#endif
10368#ifdef CURSOR_SHAPE
10369 ui_cursor_shape(); /* may show different cursor shape */
10370#endif
10371
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372#ifdef FEAT_MBYTE
10373 vim_free(arg);
10374#endif
10375}
10376
10377/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010378 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 */
10380 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010381ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010383 if (eap->forceit)
10384 {
10385 coladvance((colnr_T)MAXCOL);
10386 curwin->w_curswant = MAXCOL;
10387 curwin->w_set_curswant = FALSE;
10388 }
10389
Bram Moolenaara40c5002005-01-09 21:16:21 +000010390 /* Ignore the command when already in Insert mode. Inserting an
10391 * expression register that invokes a function can do this. */
10392 if (State & INSERT)
10393 return;
10394
Bram Moolenaar64969662005-12-14 21:59:55 +000010395 if (eap->cmdidx == CMD_startinsert)
10396 restart_edit = 'a';
10397 else if (eap->cmdidx == CMD_startreplace)
10398 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010400 restart_edit = 'V';
10401
10402 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010404 if (eap->cmdidx == CMD_startinsert)
10405 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 curwin->w_curswant = 0; /* avoid MAXCOL */
10407 }
10408}
10409
10410/*
10411 * ":stopinsert"
10412 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010414ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415{
10416 restart_edit = 0;
10417 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010418 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010421/*
10422 * Execute normal mode command "cmd".
10423 * "remap" can be REMAP_NONE or REMAP_YES.
10424 */
10425 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010426exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010427{
Bram Moolenaar25281632016-01-21 23:32:32 +010010428 /* Stuff the argument into the typeahead buffer. */
10429 ins_typebuf(cmd, remap, 0, TRUE, silent);
10430 exec_normal(FALSE);
10431}
Bram Moolenaar25281632016-01-21 23:32:32 +010010432
Bram Moolenaar25281632016-01-21 23:32:32 +010010433/*
10434 * Execute normal_cmd() until there is no typeahead left.
10435 */
10436 void
10437exec_normal(int was_typed)
10438{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010439 oparg_T oa;
10440
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010441 clear_oparg(&oa);
10442 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010443 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10444 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010445 {
10446 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010447 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010448 }
10449}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010450
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451#ifdef FEAT_FIND_ID
10452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010453ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454{
10455 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10456 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10457 (linenr_T)1, (linenr_T)MAXLNUM);
10458}
10459
10460#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10461/*
10462 * ":psearch"
10463 */
10464 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010465ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466{
10467 g_do_tagpreview = p_pvh;
10468 ex_findpat(eap);
10469 g_do_tagpreview = 0;
10470}
10471#endif
10472
10473 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010474ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475{
10476 int whole = TRUE;
10477 long n;
10478 char_u *p;
10479 int action;
10480
10481 switch (cmdnames[eap->cmdidx].cmd_name[2])
10482 {
10483 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10484 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10485 action = ACTION_GOTO;
10486 else
10487 action = ACTION_SHOW;
10488 break;
10489 case 'i': /* ":ilist" and ":dlist" */
10490 action = ACTION_SHOW_ALL;
10491 break;
10492 case 'u': /* ":ijump" and ":djump" */
10493 action = ACTION_GOTO;
10494 break;
10495 default: /* ":isplit" and ":dsplit" */
10496 action = ACTION_SPLIT;
10497 break;
10498 }
10499
10500 n = 1;
10501 if (vim_isdigit(*eap->arg)) /* get count */
10502 {
10503 n = getdigits(&eap->arg);
10504 eap->arg = skipwhite(eap->arg);
10505 }
10506 if (*eap->arg == '/') /* Match regexp, not just whole words */
10507 {
10508 whole = FALSE;
10509 ++eap->arg;
10510 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10511 if (*p)
10512 {
10513 *p++ = NUL;
10514 p = skipwhite(p);
10515
10516 /* Check for trailing illegal characters */
10517 if (!ends_excmd(*p))
10518 eap->errmsg = e_trailing;
10519 else
10520 eap->nextcmd = check_nextcmd(p);
10521 }
10522 }
10523 if (!eap->skip)
10524 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10525 whole, !eap->forceit,
10526 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10527 n, action, eap->line1, eap->line2);
10528}
10529#endif
10530
10531#ifdef FEAT_WINDOWS
10532
10533# ifdef FEAT_QUICKFIX
10534/*
10535 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10536 */
10537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010538ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010540 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10542}
10543
10544/*
10545 * ":pedit"
10546 */
10547 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010548ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549{
10550 win_T *curwin_save = curwin;
10551
10552 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010553 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010554 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 do_exedit(eap, NULL);
10556 keep_help_flag = FALSE;
10557 if (curwin != curwin_save && win_valid(curwin_save))
10558 {
10559 /* Return cursor to where we were */
10560 validate_cursor();
10561 redraw_later(VALID);
10562 win_enter(curwin_save, TRUE);
10563 }
10564 g_do_tagpreview = 0;
10565}
10566# endif
10567
10568/*
10569 * ":stag", ":stselect" and ":stjump".
10570 */
10571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010572ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573{
10574 postponed_split = -1;
10575 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010576 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10578 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010579 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580}
10581#endif
10582
10583/*
10584 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10585 */
10586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010587ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588{
10589 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10590}
10591
10592 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010593ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594{
10595 int cmd;
10596
10597 switch (name[1])
10598 {
10599 case 'j': cmd = DT_JUMP; /* ":tjump" */
10600 break;
10601 case 's': cmd = DT_SELECT; /* ":tselect" */
10602 break;
10603 case 'p': cmd = DT_PREV; /* ":tprevious" */
10604 break;
10605 case 'N': cmd = DT_PREV; /* ":tNext" */
10606 break;
10607 case 'n': cmd = DT_NEXT; /* ":tnext" */
10608 break;
10609 case 'o': cmd = DT_POP; /* ":pop" */
10610 break;
10611 case 'f': /* ":tfirst" */
10612 case 'r': cmd = DT_FIRST; /* ":trewind" */
10613 break;
10614 case 'l': cmd = DT_LAST; /* ":tlast" */
10615 break;
10616 default: /* ":tag" */
10617#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010618 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010620 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 return;
10622 }
10623#endif
10624 cmd = DT_TAG;
10625 break;
10626 }
10627
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010628 if (name[0] == 'l')
10629 {
10630#ifndef FEAT_QUICKFIX
10631 ex_ni(eap);
10632 return;
10633#else
10634 cmd = DT_LTAG;
10635#endif
10636 }
10637
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10639 eap->forceit, TRUE);
10640}
10641
10642/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010643 * Check "str" for starting with a special cmdline variable.
10644 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10645 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10646 */
10647 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010648find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010649{
10650 int len;
10651 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010652 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010653 "%",
10654#define SPEC_PERC 0
10655 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010656#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010657 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010658#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010659 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010660#define SPEC_CCWORD (SPEC_CWORD + 1)
10661 "<cexpr>", /* expr under cursor */
10662#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010663 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010664#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010665 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010666#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010667 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010668#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010669#ifdef FEAT_AUTOCMD
10670 "<afile>", /* autocommand file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010671# define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010672 "<abuf>", /* autocommand buffer number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010673# define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010674 "<amatch>", /* autocommand match name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010675# define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010676#endif
10677#ifdef FEAT_CLIENTSERVER
10678 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010679# ifdef FEAT_AUTOCMD
Bram Moolenaar65f08472017-09-10 18:16:20 +020010680# define SPEC_CLIENT (SPEC_AMATCH + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010681# else
Bram Moolenaar65f08472017-09-10 18:16:20 +020010682# define SPEC_CLIENT (SPEC_SLNUM + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010683# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010684#endif
10685 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010686
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010687 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010688 {
10689 len = (int)STRLEN(spec_str[i]);
10690 if (STRNCMP(src, spec_str[i], len) == 0)
10691 {
10692 *usedlen = len;
10693 return i;
10694 }
10695 }
10696 return -1;
10697}
10698
10699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 * Evaluate cmdline variables.
10701 *
10702 * change '%' to curbuf->b_ffname
10703 * '#' to curwin->w_altfile
10704 * '<cword>' to word under the cursor
10705 * '<cWORD>' to WORD under the cursor
10706 * '<cfile>' to path name under the cursor
10707 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010708 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 * '<afile>' to file name for autocommand
10710 * '<abuf>' to buffer number for autocommand
10711 * '<amatch>' to matching name for autocommand
10712 *
10713 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10714 * "" for error without a message) and NULL is returned.
10715 * Returns an allocated string if a valid match was found.
10716 * Returns NULL if no match was found. "usedlen" then still contains the
10717 * number of characters to skip.
10718 */
10719 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010720eval_vars(
10721 char_u *src, /* pointer into commandline */
10722 char_u *srcstart, /* beginning of valid memory for src */
10723 int *usedlen, /* characters after src that are used */
10724 linenr_T *lnump, /* line number for :e command, or NULL */
10725 char_u **errormsg, /* pointer to error message */
10726 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010727 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728{
10729 int i;
10730 char_u *s;
10731 char_u *result;
10732 char_u *resultbuf = NULL;
10733 int resultlen;
10734 buf_T *buf;
10735 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10736 int spec_idx;
10737#ifdef FEAT_MODIFY_FNAME
10738 int skip_mod = FALSE;
10739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741
10742 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010743 if (escaped != NULL)
10744 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745
10746 /*
10747 * Check if there is something to do.
10748 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010749 spec_idx = find_cmdline_var(src, usedlen);
10750 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 {
10752 *usedlen = 1;
10753 return NULL;
10754 }
10755
10756 /*
10757 * Skip when preceded with a backslash "\%" and "\#".
10758 * Note: In "\\%" the % is also not recognized!
10759 */
10760 if (src > srcstart && src[-1] == '\\')
10761 {
10762 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010763 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 return NULL;
10765 }
10766
10767 /*
10768 * word or WORD under cursor
10769 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010770 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10771 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010773 resultlen = find_ident_under_cursor(&result,
10774 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10775 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10776 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 if (resultlen == 0)
10778 {
10779 *errormsg = (char_u *)"";
10780 return NULL;
10781 }
10782 }
10783
10784 /*
10785 * '#': Alternate file name
10786 * '%': Current file name
10787 * File name under the cursor
10788 * File name for autocommand
10789 * and following modifiers
10790 */
10791 else
10792 {
10793 switch (spec_idx)
10794 {
10795 case SPEC_PERC: /* '%': current file */
10796 if (curbuf->b_fname == NULL)
10797 {
10798 result = (char_u *)"";
10799 valid = 0; /* Must have ":p:h" to be valid */
10800 }
10801 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 break;
10804
10805 case SPEC_HASH: /* '#' or "#99": alternate file */
10806 if (src[1] == '#') /* "##": the argument list */
10807 {
10808 result = arg_all();
10809 resultbuf = result;
10810 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010811 if (escaped != NULL)
10812 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813#ifdef FEAT_MODIFY_FNAME
10814 skip_mod = TRUE;
10815#endif
10816 break;
10817 }
10818 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010819 if (*s == '<') /* "#<99" uses v:oldfiles */
10820 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821 i = (int)getdigits(&s);
10822 *usedlen = (int)(s - src); /* length of what we expand */
10823
Bram Moolenaard812df62008-11-09 12:46:09 +000010824 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010826 if (*usedlen < 2)
10827 {
10828 /* Should we give an error message for #<text? */
10829 *usedlen = 1;
10830 return NULL;
10831 }
10832#ifdef FEAT_EVAL
10833 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10834 (long)i);
10835 if (result == NULL)
10836 {
10837 *errormsg = (char_u *)"";
10838 return NULL;
10839 }
10840#else
10841 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 }
10845 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010846 {
10847 buf = buflist_findnr(i);
10848 if (buf == NULL)
10849 {
10850 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10851 return NULL;
10852 }
10853 if (lnump != NULL)
10854 *lnump = ECMD_LAST;
10855 if (buf->b_fname == NULL)
10856 {
10857 result = (char_u *)"";
10858 valid = 0; /* Must have ":p:h" to be valid */
10859 }
10860 else
10861 result = buf->b_fname;
10862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 break;
10864
10865#ifdef FEAT_SEARCHPATH
10866 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010867 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 if (result == NULL)
10869 {
10870 *errormsg = (char_u *)"";
10871 return NULL;
10872 }
10873 resultbuf = result; /* remember allocated string */
10874 break;
10875#endif
10876
10877#ifdef FEAT_AUTOCMD
10878 case SPEC_AFILE: /* file name for autocommand */
10879 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010880 if (result != NULL && !autocmd_fname_full)
10881 {
10882 /* Still need to turn the fname into a full path. It is
10883 * postponed to avoid a delay when <afile> is not used. */
10884 autocmd_fname_full = TRUE;
10885 result = FullName_save(autocmd_fname, FALSE);
10886 vim_free(autocmd_fname);
10887 autocmd_fname = result;
10888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 if (result == NULL)
10890 {
10891 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10892 return NULL;
10893 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010894 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 break;
10896
10897 case SPEC_ABUF: /* buffer number for autocommand */
10898 if (autocmd_bufnr <= 0)
10899 {
10900 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10901 return NULL;
10902 }
10903 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10904 result = strbuf;
10905 break;
10906
10907 case SPEC_AMATCH: /* match name for autocommand */
10908 result = autocmd_match;
10909 if (result == NULL)
10910 {
10911 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10912 return NULL;
10913 }
10914 break;
10915
10916#endif
10917 case SPEC_SFILE: /* file name for ":so" command */
10918 result = sourcing_name;
10919 if (result == NULL)
10920 {
10921 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10922 return NULL;
10923 }
10924 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010925 case SPEC_SLNUM: /* line in file for ":so" command */
10926 if (sourcing_name == NULL || sourcing_lnum == 0)
10927 {
10928 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10929 return NULL;
10930 }
10931 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10932 result = strbuf;
10933 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934#if defined(FEAT_CLIENTSERVER)
10935 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010936 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10937 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 result = strbuf;
10939 break;
10940#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010941 default:
10942 result = (char_u *)""; /* avoid gcc warning */
10943 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 }
10945
10946 resultlen = (int)STRLEN(result); /* length of new string */
10947 if (src[*usedlen] == '<') /* remove the file name extension */
10948 {
10949 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 resultlen = (int)(s - result);
10952 }
10953#ifdef FEAT_MODIFY_FNAME
10954 else if (!skip_mod)
10955 {
10956 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10957 &resultlen);
10958 if (result == NULL)
10959 {
10960 *errormsg = (char_u *)"";
10961 return NULL;
10962 }
10963 }
10964#endif
10965 }
10966
10967 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10968 {
10969 if (valid != VALID_HEAD + VALID_PATH)
10970 /* xgettext:no-c-format */
10971 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10972 else
10973 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10974 result = NULL;
10975 }
10976 else
10977 result = vim_strnsave(result, resultlen);
10978 vim_free(resultbuf);
10979 return result;
10980}
10981
10982/*
10983 * Concatenate all files in the argument list, separated by spaces, and return
10984 * it in one allocated string.
10985 * Spaces and backslashes in the file names are escaped with a backslash.
10986 * Returns NULL when out of memory.
10987 */
10988 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010989arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990{
10991 int len;
10992 int idx;
10993 char_u *retval = NULL;
10994 char_u *p;
10995
10996 /*
10997 * Do this loop two times:
10998 * first time: compute the total length
10999 * second time: concatenate the names
11000 */
11001 for (;;)
11002 {
11003 len = 0;
11004 for (idx = 0; idx < ARGCOUNT; ++idx)
11005 {
11006 p = alist_name(&ARGLIST[idx]);
11007 if (p != NULL)
11008 {
11009 if (len > 0)
11010 {
11011 /* insert a space in between names */
11012 if (retval != NULL)
11013 retval[len] = ' ';
11014 ++len;
11015 }
11016 for ( ; *p != NUL; ++p)
11017 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011018 if (*p == ' '
11019#ifndef BACKSLASH_IN_FILENAME
11020 || *p == '\\'
11021#endif
11022 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 {
11024 /* insert a backslash */
11025 if (retval != NULL)
11026 retval[len] = '\\';
11027 ++len;
11028 }
11029 if (retval != NULL)
11030 retval[len] = *p;
11031 ++len;
11032 }
11033 }
11034 }
11035
11036 /* second time: break here */
11037 if (retval != NULL)
11038 {
11039 retval[len] = NUL;
11040 break;
11041 }
11042
11043 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011044 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 if (retval == NULL)
11046 break;
11047 }
11048
11049 return retval;
11050}
11051
11052#if defined(FEAT_AUTOCMD) || defined(PROTO)
11053/*
11054 * Expand the <sfile> string in "arg".
11055 *
11056 * Returns an allocated string, or NULL for any error.
11057 */
11058 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011059expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060{
11061 char_u *errormsg;
11062 int len;
11063 char_u *result;
11064 char_u *newres;
11065 char_u *repl;
11066 int srclen;
11067 char_u *p;
11068
11069 result = vim_strsave(arg);
11070 if (result == NULL)
11071 return NULL;
11072
11073 for (p = result; *p; )
11074 {
11075 if (STRNCMP(p, "<sfile>", 7) != 0)
11076 ++p;
11077 else
11078 {
11079 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011080 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 if (errormsg != NULL)
11082 {
11083 if (*errormsg)
11084 emsg(errormsg);
11085 vim_free(result);
11086 return NULL;
11087 }
11088 if (repl == NULL) /* no match (cannot happen) */
11089 {
11090 p += srclen;
11091 continue;
11092 }
11093 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11094 newres = alloc(len);
11095 if (newres == NULL)
11096 {
11097 vim_free(repl);
11098 vim_free(result);
11099 return NULL;
11100 }
11101 mch_memmove(newres, result, (size_t)(p - result));
11102 STRCPY(newres + (p - result), repl);
11103 len = (int)STRLEN(newres);
11104 STRCAT(newres, p + srclen);
11105 vim_free(repl);
11106 vim_free(result);
11107 result = newres;
11108 p = newres + len; /* continue after the match */
11109 }
11110 }
11111
11112 return result;
11113}
11114#endif
11115
11116#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011117static int ses_winsizes(FILE *fd, int restore_size,
11118 win_T *tab_firstwin);
11119static int ses_win_rec(FILE *fd, frame_T *fr);
11120static frame_T *ses_skipframe(frame_T *fr);
11121static int ses_do_frame(frame_T *fr);
11122static int ses_do_win(win_T *wp);
11123static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11124static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011125static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126
11127/*
11128 * Write openfile commands for the current buffers to an .exrc file.
11129 * Return FAIL on error, OK otherwise.
11130 */
11131 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011132makeopens(
11133 FILE *fd,
11134 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135{
11136 buf_T *buf;
11137 int only_save_windows = TRUE;
11138 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 int restore_size = TRUE;
11140 win_T *wp;
11141 char_u *sname;
11142 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011143 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011144 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011145 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011146 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011147 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011148 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149
11150 if (ssop_flags & SSOP_BUFFERS)
11151 only_save_windows = FALSE; /* Save ALL buffers */
11152
11153 /*
11154 * Begin by setting the this_session variable, and then other
11155 * sessionable variables.
11156 */
11157#ifdef FEAT_EVAL
11158 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11159 return FAIL;
11160 if (ssop_flags & SSOP_GLOBALS)
11161 if (store_session_globals(fd) == FAIL)
11162 return FAIL;
11163#endif
11164
11165 /*
11166 * Close all windows but one.
11167 */
11168 if (put_line(fd, "silent only") == FAIL)
11169 return FAIL;
11170
11171 /*
11172 * Now a :cd command to the session directory or the current directory
11173 */
11174 if (ssop_flags & SSOP_SESDIR)
11175 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011176 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11177 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 return FAIL;
11179 }
11180 else if (ssop_flags & SSOP_CURDIR)
11181 {
11182 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11183 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011184 || fputs("cd ", fd) < 0
11185 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11186 || put_eol(fd) == FAIL)
11187 {
11188 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191 vim_free(sname);
11192 }
11193
11194 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011195 * If there is an empty, unnamed buffer we will wipe it out later.
11196 * Remember the buffer number.
11197 */
11198 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11199 return FAIL;
11200 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11201 return FAIL;
11202 if (put_line(fd, "endif") == FAIL)
11203 return FAIL;
11204
11205 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206 * Now save the current files, current buffer first.
11207 */
11208 if (put_line(fd, "set shortmess=aoO") == FAIL)
11209 return FAIL;
11210
11211 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011212 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213 {
11214 if (!(only_save_windows && buf->b_nwindows == 0)
11215 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11216 && buf->b_fname != NULL
11217 && buf->b_p_bl)
11218 {
11219 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11220 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011221 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 return FAIL;
11223 }
11224 }
11225
11226 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011227 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11229 return FAIL;
11230
11231 if (ssop_flags & SSOP_RESIZE)
11232 {
11233 /* Note: after the restore we still check it worked!*/
11234 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11235 || put_eol(fd) == FAIL)
11236 return FAIL;
11237 }
11238
11239#ifdef FEAT_GUI
11240 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11241 {
11242 int x, y;
11243
11244 if (gui_mch_get_winpos(&x, &y) == OK)
11245 {
11246 /* Note: after the restore we still check it worked!*/
11247 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11248 return FAIL;
11249 }
11250 }
11251#endif
11252
11253 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011254 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11255 * will be displayed when creating the next tab. That resizes the windows
11256 * in the first tab, which may cause problems. Set 'showtabline' to 2
11257 * temporarily to avoid that.
11258 */
11259 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11260 {
11261 if (put_line(fd, "set stal=2") == FAIL)
11262 return FAIL;
11263 restore_stal = TRUE;
11264 }
11265
11266 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011267 * May repeat putting Windows for each tab, when "tabpages" is in
11268 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011269 * Don't use goto_tabpage(), it may change directory and trigger
11270 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011272 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011273 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011274 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011276 int need_tabnew = FALSE;
11277 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011278
Bram Moolenaar18144c82006-04-12 21:52:12 +000011279 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011281 tabpage_T *tp = find_tabpage(tabnr);
11282
11283 if (tp == NULL)
11284 break; /* done all tab pages */
11285 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011286 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011287 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011288 tab_topframe = topframe;
11289 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011290 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011291 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011292 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011293 tab_topframe = tp->tp_topframe;
11294 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011295 if (tabnr > 1)
11296 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298
Bram Moolenaar18144c82006-04-12 21:52:12 +000011299 /*
11300 * Before creating the window layout, try loading one file. If this
11301 * is aborted we don't end up with a number of useless windows.
11302 * This may have side effects! (e.g., compressed or network file).
11303 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011304 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011305 {
11306 if (ses_do_win(wp)
11307 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011308 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011309#ifdef FEAT_QUICKFIX
11310 && !bt_nofile(wp->w_buffer)
11311#endif
11312 )
11313 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011314 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011315 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11316 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011317 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011318 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011319 if (!wp->w_arg_idx_invalid)
11320 edited_win = wp;
11321 break;
11322 }
11323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011325 /* If no file got edited create an empty tab page. */
11326 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11327 return FAIL;
11328
Bram Moolenaar18144c82006-04-12 21:52:12 +000011329 /*
11330 * Save current window layout.
11331 */
11332 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011334 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011336 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11337 return FAIL;
11338 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11339 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340
Bram Moolenaar18144c82006-04-12 21:52:12 +000011341 /*
11342 * Check if window sizes can be restored (no windows omitted).
11343 * Remember the window number of the current window after restoring.
11344 */
11345 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011346 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011347 {
11348 if (ses_do_win(wp))
11349 ++nr;
11350 else
11351 restore_size = FALSE;
11352 if (curwin == wp)
11353 cnr = nr;
11354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355
Bram Moolenaar18144c82006-04-12 21:52:12 +000011356 /* Go to the first window. */
11357 if (put_line(fd, "wincmd t") == FAIL)
11358 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011359
Bram Moolenaar18144c82006-04-12 21:52:12 +000011360 /*
11361 * If more than one window, see if sizes can be restored.
11362 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11363 * resized when moving between windows.
11364 * Do this before restoring the view, so that the topline and the
11365 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011366 * winminheight and winminwidth need to be set to avoid an error if the
11367 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011368 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011369 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011370 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011371 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011372 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373
Bram Moolenaar18144c82006-04-12 21:52:12 +000011374 /*
11375 * Restore the view of the window (options, file, cursor, etc.).
11376 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011377 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011378 {
11379 if (!ses_do_win(wp))
11380 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011381 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11382 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011383 return FAIL;
11384 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11385 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011386 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011387 }
11388
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011389 /* The argument index in the first tab page is zero, need to set it in
11390 * each window. For further tab pages it's the window where we do
11391 * "tabedit". */
11392 cur_arg_idx = next_arg_idx;
11393
Bram Moolenaar18144c82006-04-12 21:52:12 +000011394 /*
11395 * Restore cursor to the current window if it's not the first one.
11396 */
11397 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11398 || put_eol(fd) == FAIL))
11399 return FAIL;
11400
11401 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011402 * Restore window sizes again after jumping around in windows, because
11403 * the current window has a minimum size while others may not.
11404 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011405 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011406 return FAIL;
11407
Bram Moolenaar18144c82006-04-12 21:52:12 +000011408 /* Don't continue in another tab page when doing only the current one
11409 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011410 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011411 break;
11412 }
11413
11414 if (ssop_flags & SSOP_TABPAGES)
11415 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011416 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11417 || put_eol(fd) == FAIL)
11418 return FAIL;
11419 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011420 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11421 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011422
Bram Moolenaar9c102382006-05-03 21:26:49 +000011423 /*
11424 * Wipe out an empty unnamed buffer we started in.
11425 */
11426 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11427 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011428 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011429 return FAIL;
11430 if (put_line(fd, "endif") == FAIL)
11431 return FAIL;
11432 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11433 return FAIL;
11434
11435 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11436 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11437 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11438 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011439 /* Re-apply 'winminheight' and 'winminwidth'. */
11440 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11441 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11442 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443
11444 /*
11445 * Lastly, execute the x.vim file if it exists.
11446 */
11447 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11448 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011449 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450 || put_line(fd, "endif") == FAIL)
11451 return FAIL;
11452
11453 return OK;
11454}
11455
11456 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011457ses_winsizes(
11458 FILE *fd,
11459 int restore_size,
11460 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461{
11462 int n = 0;
11463 win_T *wp;
11464
11465 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11466 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011467 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468 {
11469 if (!ses_do_win(wp))
11470 continue;
11471 ++n;
11472
11473 /* restore height when not full height */
11474 if (wp->w_height + wp->w_status_height < topframe->fr_height
11475 && (fprintf(fd,
11476 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11477 n, (long)wp->w_height, Rows / 2, Rows) < 0
11478 || put_eol(fd) == FAIL))
11479 return FAIL;
11480
11481 /* restore width when not full width */
11482 if (wp->w_width < Columns && (fprintf(fd,
11483 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11484 n, (long)wp->w_width, Columns / 2, Columns) < 0
11485 || put_eol(fd) == FAIL))
11486 return FAIL;
11487 }
11488 }
11489 else
11490 {
11491 /* Just equalise window sizes */
11492 if (put_line(fd, "wincmd =") == FAIL)
11493 return FAIL;
11494 }
11495 return OK;
11496}
11497
11498/*
11499 * Write commands to "fd" to recursively create windows for frame "fr",
11500 * horizontally and vertically split.
11501 * After the commands the last window in the frame is the current window.
11502 * Returns FAIL when writing the commands to "fd" fails.
11503 */
11504 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011505ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506{
11507 frame_T *frc;
11508 int count = 0;
11509
11510 if (fr->fr_layout != FR_LEAF)
11511 {
11512 /* Find first frame that's not skipped and then create a window for
11513 * each following one (first frame is already there). */
11514 frc = ses_skipframe(fr->fr_child);
11515 if (frc != NULL)
11516 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11517 {
11518 /* Make window as big as possible so that we have lots of room
11519 * to split. */
11520 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11521 || put_line(fd, fr->fr_layout == FR_COL
11522 ? "split" : "vsplit") == FAIL)
11523 return FAIL;
11524 ++count;
11525 }
11526
11527 /* Go back to the first window. */
11528 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11529 ? "%dwincmd k" : "%dwincmd h", count) < 0
11530 || put_eol(fd) == FAIL))
11531 return FAIL;
11532
11533 /* Recursively create frames/windows in each window of this column or
11534 * row. */
11535 frc = ses_skipframe(fr->fr_child);
11536 while (frc != NULL)
11537 {
11538 ses_win_rec(fd, frc);
11539 frc = ses_skipframe(frc->fr_next);
11540 /* Go to next window. */
11541 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11542 return FAIL;
11543 }
11544 }
11545 return OK;
11546}
11547
11548/*
11549 * Skip frames that don't contain windows we want to save in the Session.
11550 * Returns NULL when there none.
11551 */
11552 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011553ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554{
11555 frame_T *frc;
11556
11557 for (frc = fr; frc != NULL; frc = frc->fr_next)
11558 if (ses_do_frame(frc))
11559 break;
11560 return frc;
11561}
11562
11563/*
11564 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11565 * the Session.
11566 */
11567 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011568ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569{
11570 frame_T *frc;
11571
11572 if (fr->fr_layout == FR_LEAF)
11573 return ses_do_win(fr->fr_win);
11574 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11575 if (ses_do_frame(frc))
11576 return TRUE;
11577 return FALSE;
11578}
11579
11580/*
11581 * Return non-zero if window "wp" is to be stored in the Session.
11582 */
11583 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011584ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585{
11586 if (wp->w_buffer->b_fname == NULL
11587#ifdef FEAT_QUICKFIX
11588 /* When 'buftype' is "nofile" can't restore the window contents. */
11589 || bt_nofile(wp->w_buffer)
11590#endif
11591 )
11592 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011593 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594 return (ssop_flags & SSOP_HELP);
11595 return TRUE;
11596}
11597
11598/*
11599 * Write commands to "fd" to restore the view of a window.
11600 * Caller must make sure 'scrolloff' is zero.
11601 */
11602 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011603put_view(
11604 FILE *fd,
11605 win_T *wp,
11606 int add_edit, /* add ":edit" command to view */
11607 unsigned *flagp, /* vop_flags or ssop_flags */
11608 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011609 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610{
11611 win_T *save_curwin;
11612 int f;
11613 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011614 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615
11616 /* Always restore cursor position for ":mksession". For ":mkview" only
11617 * when 'viewoptions' contains "cursor". */
11618 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11619
11620 /*
11621 * Local argument list.
11622 */
11623 if (wp->w_alist == &global_alist)
11624 {
11625 if (put_line(fd, "argglobal") == FAIL)
11626 return FAIL;
11627 }
11628 else
11629 {
11630 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11631 flagp == &vop_flags
11632 || !(*flagp & SSOP_CURDIR)
11633 || wp->w_localdir != NULL, flagp) == FAIL)
11634 return FAIL;
11635 }
11636
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011637 /* Only when part of a session: restore the argument index. Some
11638 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011639 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011640 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011642 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643 || put_eol(fd) == FAIL)
11644 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011645 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646 }
11647
11648 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011649 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650 {
11651 /*
11652 * Load the file.
11653 */
11654 if (wp->w_buffer->b_ffname != NULL
11655#ifdef FEAT_QUICKFIX
11656 && !bt_nofile(wp->w_buffer)
11657#endif
11658 )
11659 {
11660 /*
11661 * Editing a file in this buffer: use ":edit file".
11662 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011663 *
11664 * Note, if a buffer for that file already exists, use :badd to
11665 * edit that buffer, to not lose folding information (:edit resets
11666 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011668 if (fputs("if bufexists('", fd) < 0
11669 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11670 || fputs("') | buffer ", fd) < 0
11671 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11672 || fputs(" | else | edit ", fd) < 0
11673 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11674 || fputs(" | endif", fd) < 0
11675 ||
11676 put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 return FAIL;
11678 }
11679 else
11680 {
11681 /* No file in this buffer, just make it empty. */
11682 if (put_line(fd, "enew") == FAIL)
11683 return FAIL;
11684#ifdef FEAT_QUICKFIX
11685 if (wp->w_buffer->b_ffname != NULL)
11686 {
11687 /* The buffer does have a name, but it's not a file name. */
11688 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011689 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690 return FAIL;
11691 }
11692#endif
11693 do_cursor = FALSE;
11694 }
11695 }
11696
11697 /*
11698 * Local mappings and abbreviations.
11699 */
11700 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11701 && makemap(fd, wp->w_buffer) == FAIL)
11702 return FAIL;
11703
11704 /*
11705 * Local options. Need to go to the window temporarily.
11706 * Store only local values when using ":mkview" and when ":mksession" is
11707 * used and 'sessionoptions' doesn't include "options".
11708 * Some folding options are always stored when "folds" is included,
11709 * otherwise the folds would not be restored correctly.
11710 */
11711 save_curwin = curwin;
11712 curwin = wp;
11713 curbuf = curwin->w_buffer;
11714 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11715 f = makeset(fd, OPT_LOCAL,
11716 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11717#ifdef FEAT_FOLDING
11718 else if (*flagp & SSOP_FOLDS)
11719 f = makefoldset(fd);
11720#endif
11721 else
11722 f = OK;
11723 curwin = save_curwin;
11724 curbuf = curwin->w_buffer;
11725 if (f == FAIL)
11726 return FAIL;
11727
11728#ifdef FEAT_FOLDING
11729 /*
11730 * Save Folds when 'buftype' is empty and for help files.
11731 */
11732 if ((*flagp & SSOP_FOLDS)
11733 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011734 && (*wp->w_buffer->b_p_bt == NUL || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 {
11736 if (put_folds(fd, wp) == FAIL)
11737 return FAIL;
11738 }
11739#endif
11740
11741 /*
11742 * Set the cursor after creating folds, since that moves the cursor.
11743 */
11744 if (do_cursor)
11745 {
11746
11747 /* Restore the cursor line in the file and relatively in the
11748 * window. Don't use "G", it changes the jumplist. */
11749 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11750 (long)wp->w_cursor.lnum,
11751 (long)(wp->w_cursor.lnum - wp->w_topline),
11752 (long)wp->w_height / 2, (long)wp->w_height) < 0
11753 || put_eol(fd) == FAIL
11754 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11755 || put_line(fd, "exe s:l") == FAIL
11756 || put_line(fd, "normal! zt") == FAIL
11757 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11758 || put_eol(fd) == FAIL)
11759 return FAIL;
11760 /* Restore the cursor column and left offset when not wrapping. */
11761 if (wp->w_cursor.col == 0)
11762 {
11763 if (put_line(fd, "normal! 0") == FAIL)
11764 return FAIL;
11765 }
11766 else
11767 {
11768 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11769 {
11770 if (fprintf(fd,
11771 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011772 (long)wp->w_virtcol + 1,
11773 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774 (long)wp->w_width / 2, (long)wp->w_width) < 0
11775 || put_eol(fd) == FAIL
11776 || put_line(fd, "if s:c > 0") == FAIL
11777 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011778 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11779 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780 || put_eol(fd) == FAIL
11781 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011782 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783 || put_eol(fd) == FAIL
11784 || put_line(fd, "endif") == FAIL)
11785 return FAIL;
11786 }
11787 else
11788 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011789 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790 || put_eol(fd) == FAIL)
11791 return FAIL;
11792 }
11793 }
11794 }
11795
11796 /*
11797 * Local directory.
11798 */
11799 if (wp->w_localdir != NULL)
11800 {
11801 if (fputs("lcd ", fd) < 0
11802 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11803 || put_eol(fd) == FAIL)
11804 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011805 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806 }
11807
11808 return OK;
11809}
11810
11811/*
11812 * Write an argument list to the session file.
11813 * Returns FAIL if writing fails.
11814 */
11815 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011816ses_arglist(
11817 FILE *fd,
11818 char *cmd,
11819 garray_T *gap,
11820 int fullname, /* TRUE: use full path name */
11821 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822{
11823 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011824 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 char_u *s;
11826
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011827 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11828 return FAIL;
11829 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830 return FAIL;
11831 for (i = 0; i < gap->ga_len; ++i)
11832 {
11833 /* NULL file names are skipped (only happens when out of memory). */
11834 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11835 if (s != NULL)
11836 {
11837 if (fullname)
11838 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011839 buf = alloc(MAXPATHL);
11840 if (buf != NULL)
11841 {
11842 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11843 s = buf;
11844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011846 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011847 || ses_put_fname(fd, s, flagp) == FAIL
11848 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011849 {
11850 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011852 }
11853 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 }
11855 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011856 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857}
11858
11859/*
11860 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011861 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862 * Returns FAIL if writing fails.
11863 */
11864 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011865ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866{
11867 char_u *name;
11868
11869 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011870 * the session file will be sourced.
11871 * Don't do this for ":mkview", we don't know the current directory.
11872 * Don't do this after ":lcd", we don't keep track of what the current
11873 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874 if (buf->b_sfname != NULL
11875 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011876 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011877#ifdef FEAT_AUTOCHDIR
11878 && !p_acd
11879#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011880 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881 name = buf->b_sfname;
11882 else
11883 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011884 if (ses_put_fname(fd, name, flagp) == FAIL
11885 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011886 return FAIL;
11887 return OK;
11888}
11889
11890/*
11891 * Write a file name to the session file.
11892 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11893 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011894 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895 */
11896 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011897ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898{
11899 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011900 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902
11903 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011904 if (sname == NULL)
11905 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011907 if (*flagp & SSOP_SLASH)
11908 {
11909 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011910 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011911 if (*p == '\\')
11912 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011914
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011915 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011916 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011918 if (p == NULL)
11919 return FAIL;
11920
11921 /* write the result */
11922 if (fputs((char *)p, fd) < 0)
11923 retval = FAIL;
11924
11925 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926 return retval;
11927}
11928
11929/*
11930 * ":loadview [nr]"
11931 */
11932 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011933ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934{
11935 char_u *fname;
11936
11937 fname = get_view_file(*eap->arg);
11938 if (fname != NULL)
11939 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011940 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941 vim_free(fname);
11942 }
11943}
11944
11945/*
11946 * Get the name of the view file for the current buffer.
11947 */
11948 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011949get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950{
11951 int len = 0;
11952 char_u *p, *s;
11953 char_u *retval;
11954 char_u *sname;
11955
11956 if (curbuf->b_ffname == NULL)
11957 {
11958 EMSG(_(e_noname));
11959 return NULL;
11960 }
11961 sname = home_replace_save(NULL, curbuf->b_ffname);
11962 if (sname == NULL)
11963 return NULL;
11964
11965 /*
11966 * We want a file name without separators, because we're not going to make
11967 * a directory.
11968 * "normal" path separator -> "=+"
11969 * "=" -> "=="
11970 * ":" path separator -> "=-"
11971 */
11972 for (p = sname; *p; ++p)
11973 if (*p == '=' || vim_ispathsep(*p))
11974 ++len;
11975 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11976 if (retval != NULL)
11977 {
11978 STRCPY(retval, p_vdir);
11979 add_pathsep(retval);
11980 s = retval + STRLEN(retval);
11981 for (p = sname; *p; ++p)
11982 {
11983 if (*p == '=')
11984 {
11985 *s++ = '=';
11986 *s++ = '=';
11987 }
11988 else if (vim_ispathsep(*p))
11989 {
11990 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011991#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 if (*p == ':')
11993 *s++ = '-';
11994 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011996 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997 }
11998 else
11999 *s++ = *p;
12000 }
12001 *s++ = '=';
12002 *s++ = c;
12003 STRCPY(s, ".vim");
12004 }
12005
12006 vim_free(sname);
12007 return retval;
12008}
12009
12010#endif /* FEAT_SESSION */
12011
12012/*
12013 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12014 * Return FAIL for a write error.
12015 */
12016 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012017put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018{
12019 if (
12020#ifdef USE_CRNL
12021 (
12022# ifdef MKSESSION_NL
12023 !mksession_nl &&
12024# endif
12025 (putc('\r', fd) < 0)) ||
12026#endif
12027 (putc('\n', fd) < 0))
12028 return FAIL;
12029 return OK;
12030}
12031
12032/*
12033 * Write a line to "fd".
12034 * Return FAIL for a write error.
12035 */
12036 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012037put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038{
12039 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12040 return FAIL;
12041 return OK;
12042}
12043
12044#ifdef FEAT_VIMINFO
12045/*
12046 * ":rviminfo" and ":wviminfo".
12047 */
12048 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012049ex_viminfo(
12050 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051{
12052 char_u *save_viminfo;
12053
12054 save_viminfo = p_viminfo;
12055 if (*p_viminfo == NUL)
12056 p_viminfo = (char_u *)"'100";
12057 if (eap->cmdidx == CMD_rviminfo)
12058 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012059 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12060 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061 EMSG(_("E195: Cannot open viminfo file for reading"));
12062 }
12063 else
12064 write_viminfo(eap->arg, eap->forceit);
12065 p_viminfo = save_viminfo;
12066}
12067#endif
12068
12069#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012070/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012071 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012072 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012073 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012075dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077 if (fname == NULL)
12078 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012079 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080}
12081#endif
12082
12083/*
12084 * ":behave {mswin,xterm}"
12085 */
12086 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012087ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088{
12089 if (STRCMP(eap->arg, "mswin") == 0)
12090 {
12091 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12092 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12093 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12094 set_option_value((char_u *)"keymodel", 0L,
12095 (char_u *)"startsel,stopsel", 0);
12096 }
12097 else if (STRCMP(eap->arg, "xterm") == 0)
12098 {
12099 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12100 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12101 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12102 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12103 }
12104 else
12105 EMSG2(_(e_invarg2), eap->arg);
12106}
12107
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012108#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12109/*
12110 * Function given to ExpandGeneric() to obtain the possible arguments of the
12111 * ":behave {mswin,xterm}" command.
12112 */
12113 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012114get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012115{
12116 if (idx == 0)
12117 return (char_u *)"mswin";
12118 if (idx == 1)
12119 return (char_u *)"xterm";
12120 return NULL;
12121}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012122
12123/*
12124 * Function given to ExpandGeneric() to obtain the possible arguments of the
12125 * ":messages {clear}" command.
12126 */
12127 char_u *
12128get_messages_arg(expand_T *xp UNUSED, int idx)
12129{
12130 if (idx == 0)
12131 return (char_u *)"clear";
12132 return NULL;
12133}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012134#endif
12135
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012136 char_u *
12137get_mapclear_arg(expand_T *xp UNUSED, int idx)
12138{
12139 if (idx == 0)
12140 return (char_u *)"<buffer>";
12141 return NULL;
12142}
12143
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144#ifdef FEAT_AUTOCMD
12145static int filetype_detect = FALSE;
12146static int filetype_plugin = FALSE;
12147static int filetype_indent = FALSE;
12148
12149/*
12150 * ":filetype [plugin] [indent] {on,off,detect}"
12151 * on: Load the filetype.vim file to install autocommands for file types.
12152 * off: Load the ftoff.vim file to remove all autocommands for file types.
12153 * plugin on: load filetype.vim and ftplugin.vim
12154 * plugin off: load ftplugof.vim
12155 * indent on: load filetype.vim and indent.vim
12156 * indent off: load indoff.vim
12157 */
12158 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012159ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160{
12161 char_u *arg = eap->arg;
12162 int plugin = FALSE;
12163 int indent = FALSE;
12164
12165 if (*eap->arg == NUL)
12166 {
12167 /* Print current status. */
12168 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12169 filetype_detect ? "ON" : "OFF",
12170 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12171 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12172 return;
12173 }
12174
12175 /* Accept "plugin" and "indent" in any order. */
12176 for (;;)
12177 {
12178 if (STRNCMP(arg, "plugin", 6) == 0)
12179 {
12180 plugin = TRUE;
12181 arg = skipwhite(arg + 6);
12182 continue;
12183 }
12184 if (STRNCMP(arg, "indent", 6) == 0)
12185 {
12186 indent = TRUE;
12187 arg = skipwhite(arg + 6);
12188 continue;
12189 }
12190 break;
12191 }
12192 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12193 {
12194 if (*arg == 'o' || !filetype_detect)
12195 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012196 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197 filetype_detect = TRUE;
12198 if (plugin)
12199 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012200 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201 filetype_plugin = TRUE;
12202 }
12203 if (indent)
12204 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012205 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206 filetype_indent = TRUE;
12207 }
12208 }
12209 if (*arg == 'd')
12210 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012211 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012212 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213 }
12214 }
12215 else if (STRCMP(arg, "off") == 0)
12216 {
12217 if (plugin || indent)
12218 {
12219 if (plugin)
12220 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012221 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222 filetype_plugin = FALSE;
12223 }
12224 if (indent)
12225 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012226 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227 filetype_indent = FALSE;
12228 }
12229 }
12230 else
12231 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012232 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233 filetype_detect = FALSE;
12234 }
12235 }
12236 else
12237 EMSG2(_(e_invarg2), arg);
12238}
12239
12240/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012241 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242 */
12243 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012244ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245{
12246 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012247 {
12248 char_u *arg = eap->arg;
12249
12250 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12251 arg += 9;
12252
12253 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12254 if (arg != eap->arg)
12255 did_filetype = FALSE;
12256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257}
12258#endif
12259
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012261ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262{
12263#ifdef FEAT_DIGRAPHS
12264 if (*eap->arg != NUL)
12265 putdigraph(eap->arg);
12266 else
12267 listdigraphs();
12268#else
12269 EMSG(_("E196: No digraphs in this version"));
12270#endif
12271}
12272
12273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012274ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275{
12276 int flags = 0;
12277
12278 if (eap->cmdidx == CMD_setlocal)
12279 flags = OPT_LOCAL;
12280 else if (eap->cmdidx == CMD_setglobal)
12281 flags = OPT_GLOBAL;
12282#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12283 if (cmdmod.browse && flags == 0)
12284 ex_options(eap);
12285 else
12286#endif
12287 (void)do_set(eap->arg, flags);
12288}
12289
12290#ifdef FEAT_SEARCH_EXTRA
12291/*
12292 * ":nohlsearch"
12293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012295ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012297 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012298 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299}
12300
12301/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012302 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303 * Sets nextcmd to the start of the next command, if any. Also called when
12304 * skipping commands to find the next command.
12305 */
12306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012307ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308{
12309 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012310 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311 char_u *end;
12312 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012313 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012314
12315 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012316 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012317 else
12318 {
12319 EMSG(e_invcmd);
12320 return;
12321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322
12323 /* First clear any old pattern. */
12324 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012325 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326
12327 if (ends_excmd(*eap->arg))
12328 end = eap->arg;
12329 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012330 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 end = eap->arg + 4;
12332 else
12333 {
12334 p = skiptowhite(eap->arg);
12335 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012336 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337 p = skipwhite(p);
12338 if (*p == NUL)
12339 {
12340 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012341 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342 EMSG2(_(e_invarg2), eap->arg);
12343 return;
12344 }
12345 end = skip_regexp(p + 1, *p, TRUE, NULL);
12346 if (!eap->skip)
12347 {
12348 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12349 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012350 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351 eap->errmsg = e_trailing;
12352 return;
12353 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012354 if (*end != *p)
12355 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012356 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012357 EMSG2(_(e_invarg2), p);
12358 return;
12359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360
12361 c = *end;
12362 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012363 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012364 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012365 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366 }
12367 }
12368 eap->nextcmd = find_nextcmd(end);
12369}
12370#endif
12371
12372#ifdef FEAT_CRYPT
12373/*
12374 * ":X": Get crypt key
12375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012377ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012379 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012380 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381}
12382#endif
12383
12384#ifdef FEAT_FOLDING
12385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012386ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387{
12388 if (foldManualAllowed(TRUE))
12389 foldCreate(eap->line1, eap->line2);
12390}
12391
12392 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012393ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394{
12395 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12396 eap->forceit, FALSE);
12397}
12398
12399 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012400ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401{
12402 linenr_T lnum;
12403
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012404#ifdef FEAT_CLIPBOARD
12405 start_global_changes();
12406#endif
12407
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408 /* First set the marks for all lines closed/open. */
12409 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12410 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12411 ml_setmarked(lnum);
12412
12413 /* Execute the command on the marked lines. */
12414 global_exe(eap->arg);
12415 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012416#ifdef FEAT_CLIPBOARD
12417 end_global_changes();
12418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419}
12420#endif