blob: dd5e0a8c40dbadc5ac10adb179ed4bd058129578 [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
491
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492/*
493 * Declare cmdnames[].
494 */
495#define DO_DECLARE_EXCMD
496#include "ex_cmds.h"
497
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100498/* Beginning of automatically generated code by create_cmdidxs.pl
499 *
500 * Table giving the index of the first command in cmdnames[] to lookup
501 * based on the first letter of a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100503static const unsigned short cmdidxs1[26] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504{
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100505 /* a */ 0,
506 /* b */ 19,
507 /* c */ 42,
508 /* d */ 103,
509 /* e */ 125,
510 /* f */ 145,
511 /* g */ 161,
512 /* h */ 167,
513 /* i */ 176,
514 /* j */ 194,
515 /* k */ 196,
516 /* l */ 201,
517 /* m */ 259,
518 /* n */ 277,
519 /* o */ 297,
520 /* p */ 309,
521 /* q */ 348,
522 /* r */ 351,
523 /* s */ 370,
524 /* t */ 437,
525 /* u */ 472,
526 /* v */ 483,
527 /* w */ 501,
528 /* x */ 516,
529 /* y */ 525,
530 /* z */ 526
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531};
532
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100533/*
534 * Table giving the index of the first command in cmdnames[] to lookup
535 * based on the first 2 letters of a command.
536 * Values in cmdidxs2[c1][c2] are relative to cmdidxs1[c1] so that they
537 * fit in a byte.
538 */
539static const unsigned char cmdidxs2[26][26] =
540{ /* a b c d e f g h i j k l m n o p q r s t u v w x y z */
541 /* a */ { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 6, 0, 0, 0, 7, 15, 0, 16, 0, 0, 0, 0, 0, },
542 /* b */ { 0, 0, 0, 4, 5, 7, 0, 0, 0, 0, 0, 8, 9, 10, 11, 12, 0, 13, 0, 0, 0, 0, 22, 0, 0, 0, },
543 /* c */ { 0, 10, 12, 14, 16, 18, 21, 0, 0, 0, 0, 29, 33, 36, 42, 51, 53, 54, 55, 0, 57, 0, 60, 0, 0, 0, },
544 /* d */ { 0, 0, 0, 0, 0, 0, 0, 0, 6, 15, 0, 16, 0, 0, 17, 0, 0, 19, 20, 0, 0, 0, 0, 0, 0, 0, },
545 /* e */ { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, },
546 /* f */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, },
547 /* g */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 4, 5, 0, 0, 0, 0, },
548 /* h */ { 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
549 /* i */ { 0, 0, 0, 0, 0, 3, 0, 0, 0, 4, 0, 5, 6, 0, 0, 0, 0, 0, 13, 0, 15, 0, 0, 0, 0, 0, },
550 /* j */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, },
551 /* k */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
552 /* l */ { 0, 9, 11, 15, 16, 20, 23, 28, 0, 0, 0, 30, 33, 36, 40, 46, 0, 48, 57, 49, 50, 54, 56, 0, 0, 0, },
553 /* m */ { 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
554 /* n */ { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 8, 10, 0, 0, 0, 0, 0, 17, 0, 0, 0, 0, 0, },
555 /* o */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 5, 0, 0, 0, 0, 0, 0, 9, 0, 11, 0, 0, 0, },
556 /* p */ { 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9, 0, 0, 16, 17, 26, 0, 27, 0, 28, 0, },
557 /* q */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
558 /* r */ { 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 18, 0, 0, 0, 0, },
559 /* s */ { 0, 6, 15, 0, 18, 22, 0, 24, 25, 0, 0, 28, 30, 34, 38, 40, 0, 48, 0, 49, 0, 61, 62, 0, 63, 0, },
560 /* t */ { 0, 0, 19, 0, 22, 23, 0, 24, 0, 25, 0, 26, 27, 28, 29, 30, 0, 31, 33, 0, 34, 0, 0, 0, 0, 0, },
561 /* u */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
562 /* v */ { 0, 0, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 9, 12, 0, 0, 0, 0, 15, 0, 16, 0, 0, 0, 0, 0, },
563 /* w */ { 0, 0, 0, 0, 0, 0, 0, 3, 4, 0, 0, 0, 0, 8, 0, 9, 10, 0, 12, 0, 13, 14, 0, 0, 0, 0, },
564 /* x */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 5, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, },
565 /* y */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
566 /* z */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }
567};
568
Bram Moolenaar980128c2017-03-26 21:46:28 +0200569static const int command_count = 539;
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100570
571/* End of automatically generated code by create_cmdidxs.pl */
572
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573static char_u dollar_command[2] = {'$', 0};
574
575
576#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000577/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578typedef struct
579{
580 char_u *line; /* command line */
581 linenr_T lnum; /* sourcing_lnum of the line */
582} wcmd_T;
583
584/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000585 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000587 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000589struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590{
591 garray_T *lines_gap; /* growarray with line info */
592 int current_line; /* last read line from growarray */
593 int repeating; /* TRUE when looping a second time */
594 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100595 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 void *cookie;
597};
598
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100599static char_u *get_loop_line(int c, void *cookie, int indent);
600static int store_loop_line(garray_T *gap, char_u *line);
601static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000602
603/* Struct to save a few things while debugging. Used in do_cmdline() only. */
604struct dbg_stuff
605{
606 int trylevel;
607 int force_abort;
608 except_T *caught_stack;
609 char_u *vv_exception;
610 char_u *vv_throwpoint;
611 int did_emsg;
612 int got_int;
613 int did_throw;
614 int need_rethrow;
615 int check_cstack;
616 except_T *current_exception;
617};
618
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100619static void save_dbg_stuff(struct dbg_stuff *dsp);
620static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000621
622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100623save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000624{
625 dsp->trylevel = trylevel; trylevel = 0;
626 dsp->force_abort = force_abort; force_abort = FALSE;
627 dsp->caught_stack = caught_stack; caught_stack = NULL;
628 dsp->vv_exception = v_exception(NULL);
629 dsp->vv_throwpoint = v_throwpoint(NULL);
630
631 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
632 dsp->did_emsg = did_emsg; did_emsg = FALSE;
633 dsp->got_int = got_int; got_int = FALSE;
634 dsp->did_throw = did_throw; did_throw = FALSE;
635 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
636 dsp->check_cstack = check_cstack; check_cstack = FALSE;
637 dsp->current_exception = current_exception; current_exception = NULL;
638}
639
640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100641restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000642{
643 suppress_errthrow = FALSE;
644 trylevel = dsp->trylevel;
645 force_abort = dsp->force_abort;
646 caught_stack = dsp->caught_stack;
647 (void)v_exception(dsp->vv_exception);
648 (void)v_throwpoint(dsp->vv_throwpoint);
649 did_emsg = dsp->did_emsg;
650 got_int = dsp->got_int;
651 did_throw = dsp->did_throw;
652 need_rethrow = dsp->need_rethrow;
653 check_cstack = dsp->check_cstack;
654 current_exception = dsp->current_exception;
655}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656#endif
657
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658/*
659 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
660 * command is given.
661 */
662 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100663do_exmode(
664 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665{
666 int save_msg_scroll;
667 int prev_msg_row;
668 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100669 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000670
671 if (improved)
672 exmode_active = EXMODE_VIM;
673 else
674 exmode_active = EXMODE_NORMAL;
675 State = NORMAL;
676
677 /* When using ":global /pat/ visual" and then "Q" we return to continue
678 * the :global command. */
679 if (global_busy)
680 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
682 save_msg_scroll = msg_scroll;
683 ++RedrawingDisabled; /* don't redisplay the window */
684 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685#ifdef FEAT_GUI
686 /* Ignore scrollbar and mouse events in Ex mode */
687 ++hold_gui_events;
688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689
690 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
691 while (exmode_active)
692 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000693 /* Check for a ":normal" command and no more characters left. */
694 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
695 {
696 exmode_active = FALSE;
697 break;
698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 msg_scroll = TRUE;
700 need_wait_return = FALSE;
701 ex_pressedreturn = FALSE;
702 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100703 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 prev_msg_row = msg_row;
705 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 if (improved)
707 {
708 cmdline_row = msg_row;
709 do_cmdline(NULL, getexline, NULL, 0);
710 }
711 else
712 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
713 lines_left = Rows - 1;
714
Bram Moolenaardf177f62005-02-22 08:39:57 +0000715 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100716 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000718 if (curbuf->b_ml.ml_flags & ML_EMPTY)
719 EMSG(_(e_emptybuf));
720 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000722 if (ex_pressedreturn)
723 {
724 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000725 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000726 msg_row = prev_msg_row;
727 if (prev_msg_row == Rows - 1)
728 msg_row--;
729 }
730 msg_col = 0;
731 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
732 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000735 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
736 {
737 if (curbuf->b_ml.ml_flags & ML_EMPTY)
738 EMSG(_(e_emptybuf));
739 else
740 EMSG(_("E501: At end-of-file"));
741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 }
743
744#ifdef FEAT_GUI
745 --hold_gui_events;
746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 --RedrawingDisabled;
748 --no_wait_return;
749 update_screen(CLEAR);
750 need_wait_return = FALSE;
751 msg_scroll = save_msg_scroll;
752}
753
754/*
755 * Execute a simple command line. Used for translated commands like "*".
756 */
757 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100758do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759{
760 return do_cmdline(cmd, NULL, NULL,
761 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
762}
763
764/*
765 * do_cmdline(): execute one Ex command line
766 *
767 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100768 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 * 2. Split up in parts separated with '|'.
770 *
771 * This function can be called recursively!
772 *
773 * flags:
774 * DOCMD_VERBOSE - The command will be included in the error message.
775 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100776 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 * DOCMD_KEYTYPED - Don't reset KeyTyped.
778 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
779 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
780 *
781 * return FAIL if cmdline could not be executed, OK otherwise
782 */
783 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100784do_cmdline(
785 char_u *cmdline,
786 char_u *(*fgetline)(int, void *, int),
787 void *cookie, /* argument for fgetline() */
788 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789{
790 char_u *next_cmdline; /* next cmd to execute */
791 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100792 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 static int recursive = 0; /* recursive depth */
794 int msg_didout_before_start = 0;
795 int count = 0; /* line number count */
796 int did_inc = FALSE; /* incremented RedrawingDisabled */
797 int retval = OK;
798#ifdef FEAT_EVAL
799 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000800 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 int current_line = 0; /* active line in lines_ga */
802 char_u *fname = NULL; /* function or script name */
803 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
804 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000805 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 int initial_trylevel;
807 struct msglist **saved_msg_list = NULL;
808 struct msglist *private_msg_list;
809
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100810 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100811 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000813 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100817# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818# define cmd_cookie cookie
819#endif
820 static int call_depth = 0; /* recursiveness */
821
822#ifdef FEAT_EVAL
823 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
824 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000825 * This ensures that the do_errthrow() call in do_one_cmd() does not
826 * combine the messages stored by an earlier invocation of do_one_cmd()
827 * with the command name of the later one. This would happen when
828 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 saved_msg_list = msg_list;
830 msg_list = &private_msg_list;
831 private_msg_list = NULL;
832#endif
833
834 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100835 * here. The value of 200 allows nested function calls, ":source", etc.
836 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100837 if (call_depth >= 200
838#ifdef FEAT_EVAL
839 && call_depth >= p_mfd
840#endif
841 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 {
843 EMSG(_("E169: Command too recursive"));
844#ifdef FEAT_EVAL
845 /* When converting to an exception, we do not include the command name
846 * since this is not an error of the specific command. */
847 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
848 msg_list = saved_msg_list;
849#endif
850 return FAIL;
851 }
852 ++call_depth;
853
854#ifdef FEAT_EVAL
855 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000856 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 cstack.cs_trylevel = 0;
858 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000859 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
861
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100862 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863
864 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100865 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000866 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 ++ex_nesting_level;
868
869 /* Get the function or script name and the address where the next breakpoint
870 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000871 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 {
873 fname = func_name(real_cookie);
874 breakpoint = func_breakpoint(real_cookie);
875 dbg_tick = func_dbg_tick(real_cookie);
876 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100877 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 {
879 fname = sourcing_name;
880 breakpoint = source_breakpoint(real_cookie);
881 dbg_tick = source_dbg_tick(real_cookie);
882 }
883
884 /*
885 * Initialize "force_abort" and "suppress_errthrow" at the top level.
886 */
887 if (!recursive)
888 {
889 force_abort = FALSE;
890 suppress_errthrow = FALSE;
891 }
892
893 /*
894 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000895 * exception handling (used when debugging). Otherwise clear it to avoid
896 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000898 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000899 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000900 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200901 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902
903 initial_trylevel = trylevel;
904
905 /*
906 * "did_throw" will be set to TRUE when an exception is being thrown.
907 */
908 did_throw = FALSE;
909#endif
910 /*
911 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000912 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 * If force_abort is set, we cancel everything.
914 */
915 did_emsg = FALSE;
916
917 /*
918 * KeyTyped is only set when calling vgetc(). Reset it here when not
919 * calling vgetc() (sourced command lines).
920 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100921 if (!(flags & DOCMD_KEYTYPED)
922 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 KeyTyped = FALSE;
924
925 /*
926 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000927 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 * - for multiple commands on one line, separated with '|'
929 * - when repeating until there are no more lines (for ":source")
930 */
931 next_cmdline = cmdline;
932 do
933 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000934#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100935 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000936#endif
937
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000938 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 if (next_cmdline == NULL
940#ifdef FEAT_EVAL
941 && !force_abort
942 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000943 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944#endif
945 )
946 did_emsg = FALSE;
947
948 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000949 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100950 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 * 3. If a line is given: Make a copy, so we can mess with it.
952 */
953
954#ifdef FEAT_EVAL
955 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000956 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 {
958 /* Each '|' separated command is stored separately in lines_ga, to
959 * be able to jump to it. Don't use next_cmdline now. */
960 vim_free(cmdline_copy);
961 cmdline_copy = NULL;
962
963 /* Check if a function has returned or, unless it has an unclosed
964 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000965 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000967# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000968 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000969 func_line_end(real_cookie);
970# endif
971 if (func_has_ended(real_cookie))
972 {
973 retval = FAIL;
974 break;
975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000977#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000978 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100979 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000980 script_line_end();
981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
983 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100984 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 {
986 retval = FAIL;
987 break;
988 }
989
990 /* If breakpoints have been added/deleted need to check for it. */
991 if (breakpoint != NULL && dbg_tick != NULL
992 && *dbg_tick != debug_tick)
993 {
994 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100995 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 fname, sourcing_lnum);
997 *dbg_tick = debug_tick;
998 }
999
1000 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
1001 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
1002
1003 /* Did we encounter a breakpoint? */
1004 if (breakpoint != NULL && *breakpoint != 0
1005 && *breakpoint <= sourcing_lnum)
1006 {
1007 dbg_breakpoint(fname, sourcing_lnum);
1008 /* Find next breakpoint. */
1009 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001010 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 fname, sourcing_lnum);
1012 *dbg_tick = debug_tick;
1013 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00001014# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +00001015 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001016 {
1017 if (getline_is_func)
1018 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001019 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001020 script_line_start();
1021 }
1022# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 }
1024
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001025 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001027 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001028 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 * below, so that it stores lines in or reads them from
1030 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001031 * while/for loop. */
1032 cmd_getline = get_loop_line;
1033 cmd_cookie = (void *)&cmd_loop_cookie;
1034 cmd_loop_cookie.lines_gap = &lines_ga;
1035 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001036 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001037 cmd_loop_cookie.cookie = cookie;
1038 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 }
1040 else
1041 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001042 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 cmd_cookie = cookie;
1044 }
1045#endif
1046
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001047 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 if (next_cmdline == NULL)
1049 {
1050 /*
1051 * Need to set msg_didout for the first line after an ":if",
1052 * otherwise the ":if" will be overwritten.
1053 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001054 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001056 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057#ifdef FEAT_EVAL
1058 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1059#else
1060 0
1061#endif
1062 )) == NULL)
1063 {
1064 /* Don't call wait_return for aborted command line. The NULL
1065 * returned for the end of a sourced file or executed function
1066 * doesn't do this. */
1067 if (KeyTyped && !(flags & DOCMD_REPEAT))
1068 need_wait_return = FALSE;
1069 retval = FAIL;
1070 break;
1071 }
1072 used_getline = TRUE;
1073
1074 /*
1075 * Keep the first typed line. Clear it when more lines are typed.
1076 */
1077 if (flags & DOCMD_KEEPLINE)
1078 {
1079 vim_free(repeat_cmdline);
1080 if (count == 0)
1081 repeat_cmdline = vim_strsave(next_cmdline);
1082 else
1083 repeat_cmdline = NULL;
1084 }
1085 }
1086
1087 /* 3. Make a copy of the command so we can mess with it. */
1088 else if (cmdline_copy == NULL)
1089 {
1090 next_cmdline = vim_strsave(next_cmdline);
1091 if (next_cmdline == NULL)
1092 {
1093 EMSG(_(e_outofmem));
1094 retval = FAIL;
1095 break;
1096 }
1097 }
1098 cmdline_copy = next_cmdline;
1099
1100#ifdef FEAT_EVAL
1101 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001102 * Save the current line when inside a ":while" or ":for", and when
1103 * the command looks like a ":while" or ":for", because we may need it
1104 * later. When there is a '|' and another command, it is stored
1105 * separately, because we need to be able to jump back to it from an
1106 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001108 if (current_line == lines_ga.ga_len
1109 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001111 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 {
1113 retval = FAIL;
1114 break;
1115 }
1116 }
1117 did_endif = FALSE;
1118#endif
1119
1120 if (count++ == 0)
1121 {
1122 /*
1123 * All output from the commands is put below each other, without
1124 * waiting for a return. Don't do this when executing commands
1125 * from a script or when being called recursive (e.g. for ":e
1126 * +command file").
1127 */
1128 if (!(flags & DOCMD_NOWAIT) && !recursive)
1129 {
1130 msg_didout_before_start = msg_didout;
1131 msg_didany = FALSE; /* no output yet */
1132 msg_start();
1133 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001134 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 ++RedrawingDisabled;
1136 did_inc = TRUE;
1137 }
1138 }
1139
1140 if (p_verbose >= 15 && sourcing_name != NULL)
1141 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001143 verbose_enter_scroll();
1144
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 smsg((char_u *)_("line %ld: %s"),
1146 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001147 if (msg_silent == 0)
1148 msg_puts((char_u *)"\n"); /* don't overwrite this */
1149
1150 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 --no_wait_return;
1152 }
1153
1154 /*
1155 * 2. Execute one '|' separated command.
1156 * do_one_cmd() will return NULL if there is no trailing '|'.
1157 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1158 */
1159 ++recursive;
1160 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1161#ifdef FEAT_EVAL
1162 &cstack,
1163#endif
1164 cmd_getline, cmd_cookie);
1165 --recursive;
1166
1167#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 if (cmd_cookie == (void *)&cmd_loop_cookie)
1169 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001171 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172#endif
1173
1174 if (next_cmdline == NULL)
1175 {
1176 vim_free(cmdline_copy);
1177 cmdline_copy = NULL;
1178#ifdef FEAT_CMDHIST
1179 /*
1180 * If the command was typed, remember it for the ':' register.
1181 * Do this AFTER executing the command to make :@: work.
1182 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001183 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 && new_last_cmdline != NULL)
1185 {
1186 vim_free(last_cmdline);
1187 last_cmdline = new_last_cmdline;
1188 new_last_cmdline = NULL;
1189 }
1190#endif
1191 }
1192 else
1193 {
1194 /* need to copy the command after the '|' to cmdline_copy, for the
1195 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001196 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 next_cmdline = cmdline_copy;
1198 }
1199
1200
1201#ifdef FEAT_EVAL
1202 /* reset did_emsg for a function that is not aborted by an error */
1203 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001204 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 && !func_has_abort(real_cookie))
1206 did_emsg = FALSE;
1207
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001208 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 {
1210 ++current_line;
1211
1212 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001213 * An ":endwhile", ":endfor" and ":continue" is handled here.
1214 * If we were executing commands, jump back to the ":while" or
1215 * ":for".
1216 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001218 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001220 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001222 /* Jump back to the matching ":while" or ":for". Be careful
1223 * not to use a cs_line[] from an entry that isn't a ":while"
1224 * or ":for": It would make "current_line" invalid and can
1225 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (!did_emsg && !got_int && !did_throw
1227 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001228 && (cstack.cs_flags[cstack.cs_idx]
1229 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 && cstack.cs_line[cstack.cs_idx] >= 0
1231 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1232 {
1233 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001234 /* remember we jumped there */
1235 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 line_breakcheck(); /* check if CTRL-C typed */
1237
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001238 /* Check for the next breakpoint at or after the ":while"
1239 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 if (breakpoint != NULL)
1241 {
1242 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001243 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 fname,
1245 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1246 *dbg_tick = debug_tick;
1247 }
1248 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001249 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001251 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001253 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1254 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 }
1256 }
1257
1258 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001259 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001261 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001263 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1265 }
1266 }
1267
1268 /*
1269 * When not inside any ":while" loop, clear remembered lines.
1270 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001271 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 {
1273 if (lines_ga.ga_len > 0)
1274 {
1275 sourcing_lnum =
1276 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1277 free_cmdlines(&lines_ga);
1278 }
1279 current_line = 0;
1280 }
1281
1282 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001283 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1284 * being restored at the ":endtry". Reset them here and set the
1285 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1286 * This includes the case where a missing ":endif", ":endwhile" or
1287 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001289 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001291 cstack.cs_lflags &= ~CSL_HAD_FINA;
1292 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1293 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 did_throw ? (void *)current_exception : NULL);
1295 did_emsg = got_int = did_throw = FALSE;
1296 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1297 }
1298
1299 /* Update global "trylevel" for recursive calls to do_cmdline() from
1300 * within this loop. */
1301 trylevel = initial_trylevel + cstack.cs_trylevel;
1302
1303 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001304 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 * files) is aborted because of an error, an interrupt, or an uncaught
1306 * exception, cancel everything. If it is left normally, reset
1307 * force_abort to get the non-EH compatible abortion behavior for
1308 * the rest of the script.
1309 */
1310 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1311 force_abort = FALSE;
1312
1313 /* Convert an interrupt to an exception if appropriate. */
1314 (void)do_intthrow(&cstack);
1315#endif /* FEAT_EVAL */
1316
1317 }
1318 /*
1319 * Continue executing command lines when:
1320 * - no CTRL-C typed, no aborting error, no exception thrown or try
1321 * conditionals need to be checked for executing finally clauses or
1322 * catching an interrupt exception
1323 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001324 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 * looping for ":source" command or function call.
1326 */
1327 while (!((got_int
1328#ifdef FEAT_EVAL
1329 || (did_emsg && force_abort) || did_throw
1330#endif
1331 )
1332#ifdef FEAT_EVAL
1333 && cstack.cs_trylevel == 0
1334#endif
1335 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001336 && !(did_emsg
1337#ifdef FEAT_EVAL
1338 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001339 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001340 * the :endtry to be missed. */
1341 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1342#endif
1343 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001344 && (getline_equal(fgetline, cookie, getexmodeline)
1345 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 && (next_cmdline != NULL
1347#ifdef FEAT_EVAL
1348 || cstack.cs_idx >= 0
1349#endif
1350 || (flags & DOCMD_REPEAT)));
1351
1352 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001353 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354#ifdef FEAT_EVAL
1355 free_cmdlines(&lines_ga);
1356 ga_clear(&lines_ga);
1357
1358 if (cstack.cs_idx >= 0)
1359 {
1360 /*
1361 * If a sourced file or executed function ran to its end, report the
1362 * unclosed conditional.
1363 */
1364 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001365 && ((getline_equal(fgetline, cookie, getsourceline)
1366 && !source_finished(fgetline, cookie))
1367 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 && !func_has_ended(real_cookie))))
1369 {
1370 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1371 EMSG(_(e_endtry));
1372 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1373 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001374 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1375 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 else
1377 EMSG(_(e_endif));
1378 }
1379
1380 /*
1381 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1382 * ":endtry" in a sourced file or executed function. If the try
1383 * conditional is in its finally clause, ignore anything pending.
1384 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001385 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 */
1387 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001388 {
1389 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1390
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001391 if (idx >= 0)
1392 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001393 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1394 &cstack.cs_looplevel);
1395 }
1396 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 trylevel = initial_trylevel;
1398 }
1399
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001400 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1401 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001403 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 ? (char_u *)"endfunction" : (char_u *)NULL);
1405
1406 if (trylevel == 0)
1407 {
1408 /*
1409 * When an exception is being thrown out of the outermost try
1410 * conditional, discard the uncaught exception, disable the conversion
1411 * of interrupts or errors to exceptions, and ensure that no more
1412 * commands are executed.
1413 */
1414 if (did_throw)
1415 {
1416 void *p = NULL;
1417 char_u *saved_sourcing_name;
1418 int saved_sourcing_lnum;
1419 struct msglist *messages = NULL, *next;
1420
1421 /*
1422 * If the uncaught exception is a user exception, report it as an
1423 * error. If it is an error exception, display the saved error
1424 * message now. For an interrupt exception, do nothing; the
1425 * interrupt message is given elsewhere.
1426 */
1427 switch (current_exception->type)
1428 {
1429 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001430 vim_snprintf((char *)IObuff, IOSIZE,
1431 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 current_exception->value);
1433 p = vim_strsave(IObuff);
1434 break;
1435 case ET_ERROR:
1436 messages = current_exception->messages;
1437 current_exception->messages = NULL;
1438 break;
1439 case ET_INTERRUPT:
1440 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 }
1442
1443 saved_sourcing_name = sourcing_name;
1444 saved_sourcing_lnum = sourcing_lnum;
1445 sourcing_name = current_exception->throw_name;
1446 sourcing_lnum = current_exception->throw_lnum;
1447 current_exception->throw_name = NULL;
1448
1449 discard_current_exception(); /* uses IObuff if 'verbose' */
1450 suppress_errthrow = TRUE;
1451 force_abort = TRUE;
1452
1453 if (messages != NULL)
1454 {
1455 do
1456 {
1457 next = messages->next;
1458 emsg(messages->msg);
1459 vim_free(messages->msg);
1460 vim_free(messages);
1461 messages = next;
1462 }
1463 while (messages != NULL);
1464 }
1465 else if (p != NULL)
1466 {
1467 emsg(p);
1468 vim_free(p);
1469 }
1470 vim_free(sourcing_name);
1471 sourcing_name = saved_sourcing_name;
1472 sourcing_lnum = saved_sourcing_lnum;
1473 }
1474
1475 /*
1476 * On an interrupt or an aborting error not converted to an exception,
1477 * disable the conversion of errors to exceptions. (Interrupts are not
1478 * converted any more, here.) This enables also the interrupt message
1479 * when force_abort is set and did_emsg unset in case of an interrupt
1480 * from a finally clause after an error.
1481 */
1482 else if (got_int || (did_emsg && force_abort))
1483 suppress_errthrow = TRUE;
1484 }
1485
1486 /*
1487 * The current cstack will be freed when do_cmdline() returns. An uncaught
1488 * exception will have to be rethrown in the previous cstack. If a function
1489 * has just returned or a script file was just finished and the previous
1490 * cstack belongs to the same function or, respectively, script file, it
1491 * will have to be checked for finally clauses to be executed due to the
1492 * ":return" or ":finish". This is done in do_one_cmd().
1493 */
1494 if (did_throw)
1495 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001496 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001498 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 && ex_nesting_level > func_level(real_cookie) + 1))
1500 {
1501 if (!did_throw)
1502 check_cstack = TRUE;
1503 }
1504 else
1505 {
1506 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001507 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 --ex_nesting_level;
1509 /*
1510 * Go to debug mode when returning from a function in which we are
1511 * single-stepping.
1512 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001513 if ((getline_equal(fgetline, cookie, getsourceline)
1514 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001516 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 ? (char_u *)_("End of sourced file")
1518 : (char_u *)_("End of function"));
1519 }
1520
1521 /*
1522 * Restore the exception environment (done after returning from the
1523 * debugger).
1524 */
1525 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001526 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527
1528 msg_list = saved_msg_list;
1529#endif /* FEAT_EVAL */
1530
1531 /*
1532 * If there was too much output to fit on the command line, ask the user to
1533 * hit return before redrawing the screen. With the ":global" command we do
1534 * this only once after the command is finished.
1535 */
1536 if (did_inc)
1537 {
1538 --RedrawingDisabled;
1539 --no_wait_return;
1540 msg_scroll = FALSE;
1541
1542 /*
1543 * When just finished an ":if"-":else" which was typed, no need to
1544 * wait for hit-return. Also for an error situation.
1545 */
1546 if (retval == FAIL
1547#ifdef FEAT_EVAL
1548 || (did_endif && KeyTyped && !did_emsg)
1549#endif
1550 )
1551 {
1552 need_wait_return = FALSE;
1553 msg_didany = FALSE; /* don't wait when restarting edit */
1554 }
1555 else if (need_wait_return)
1556 {
1557 /*
1558 * The msg_start() above clears msg_didout. The wait_return we do
1559 * here should not overwrite the command that may be shown before
1560 * doing that.
1561 */
1562 msg_didout |= msg_didout_before_start;
1563 wait_return(FALSE);
1564 }
1565 }
1566
Bram Moolenaar2a942252012-11-28 23:03:07 +01001567#ifdef FEAT_EVAL
1568 did_endif = FALSE; /* in case do_cmdline used recursively */
1569#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 /*
1571 * Reset if_level, in case a sourced script file contains more ":if" than
1572 * ":endif" (could be ":if x | foo | endif").
1573 */
1574 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001575#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001576
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 --call_depth;
1578 return retval;
1579}
1580
1581#ifdef FEAT_EVAL
1582/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001583 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 */
1585 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001586get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001588 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 wcmd_T *wp;
1590 char_u *line;
1591
1592 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1593 {
1594 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001595 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001597 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (cp->getline == NULL)
1599 line = getcmdline(c, 0L, indent);
1600 else
1601 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001602 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 ++cp->current_line;
1604
1605 return line;
1606 }
1607
1608 KeyTyped = FALSE;
1609 ++cp->current_line;
1610 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1611 sourcing_lnum = wp->lnum;
1612 return vim_strsave(wp->line);
1613}
1614
1615/*
1616 * Store a line in "gap" so that a ":while" loop can execute it again.
1617 */
1618 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001619store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620{
1621 if (ga_grow(gap, 1) == FAIL)
1622 return FAIL;
1623 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1624 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1625 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 return OK;
1627}
1628
1629/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001630 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 */
1632 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001633free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634{
1635 while (gap->ga_len > 0)
1636 {
1637 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1638 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 }
1640}
1641#endif
1642
1643/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001644 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1645 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001648getline_equal(
1649 char_u *(*fgetline)(int, void *, int),
1650 void *cookie UNUSED, /* argument for fgetline() */
1651 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652{
1653#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001654 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001655 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
Bram Moolenaar89d40322006-08-29 15:30:07 +00001657 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001658 * function that's originally used to obtain the lines. This may be
1659 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001660 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001661 cp = (struct loop_cookie *)cookie;
1662 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
1664 gp = cp->getline;
1665 cp = cp->cookie;
1666 }
1667 return gp == func;
1668#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001669 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670#endif
1671}
1672
1673#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1674/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001675 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 * getline function. Otherwise return "cookie".
1677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001679getline_cookie(
1680 char_u *(*fgetline)(int, void *, int) UNUSED,
1681 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682{
1683# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001684 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001685 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
Bram Moolenaar89d40322006-08-29 15:30:07 +00001687 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001688 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001690 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001691 cp = (struct loop_cookie *)cookie;
1692 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 {
1694 gp = cp->getline;
1695 cp = cp->cookie;
1696 }
1697 return cp;
1698# else
1699 return cookie;
1700# endif
1701}
1702#endif
1703
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001704
1705/*
1706 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1707 * ":bwipeout", etc.
1708 * Returns the buffer number.
1709 */
1710 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001711compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001712{
1713 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001714 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001715 int count = offset;
1716
1717 buf = firstbuf;
1718 while (buf->b_next != NULL && buf->b_fnum < lnum)
1719 buf = buf->b_next;
1720 while (count != 0)
1721 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001722 count += (offset < 0) ? 1 : -1;
1723 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1724 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001725 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001726 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001727 if (addr_type == ADDR_LOADED_BUFFERS)
1728 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001729 while (buf->b_ml.ml_mfp == NULL)
1730 {
1731 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1732 if (nextbuf == NULL)
1733 break;
1734 buf = nextbuf;
1735 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001736 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001737 /* we might have gone too far, last buffer is not loadedd */
1738 if (addr_type == ADDR_LOADED_BUFFERS)
1739 while (buf->b_ml.ml_mfp == NULL)
1740 {
1741 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1742 if (nextbuf == NULL)
1743 break;
1744 buf = nextbuf;
1745 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001746 return buf->b_fnum;
1747}
1748
Bram Moolenaarf240e182014-11-27 18:33:02 +01001749#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001750static int current_win_nr(win_T *win);
1751static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001752
1753 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001754current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001755{
1756 win_T *wp;
1757 int nr = 0;
1758
Bram Moolenaar29323592016-07-24 22:04:11 +02001759 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001760 {
1761 ++nr;
1762 if (wp == win)
1763 break;
1764 }
1765 return nr;
1766}
1767
1768 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001769current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001770{
1771 tabpage_T *tp;
1772 int nr = 0;
1773
Bram Moolenaar29323592016-07-24 22:04:11 +02001774 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001775 {
1776 ++nr;
1777 if (tp == tab)
1778 break;
1779 }
1780 return nr;
1781}
1782
1783# define CURRENT_WIN_NR current_win_nr(curwin)
1784# define LAST_WIN_NR current_win_nr(NULL)
1785# define CURRENT_TAB_NR current_tab_nr(curtab)
1786# define LAST_TAB_NR current_tab_nr(NULL)
1787#else
1788# define CURRENT_WIN_NR 1
1789# define LAST_WIN_NR 1
1790# define CURRENT_TAB_NR 1
1791# define LAST_TAB_NR 1
1792#endif
1793
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001794
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795/*
1796 * Execute one Ex command.
1797 *
1798 * If 'sourcing' is TRUE, the command will be included in the error message.
1799 *
1800 * 1. skip comment lines and leading space
1801 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001802 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001803 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001804 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001805 * 6. parse arguments
1806 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001808 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 *
1810 * This function may be called recursively!
1811 */
1812#if (_MSC_VER == 1200)
1813/*
Bram Moolenaared203462004-06-16 11:19:22 +00001814 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001816 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817#endif
1818 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001819do_one_cmd(
1820 char_u **cmdlinep,
1821 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001823 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001825 char_u *(*fgetline)(int, void *, int),
1826 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827{
1828 char_u *p;
1829 linenr_T lnum;
1830 long n;
1831 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001832 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 exarg_T ea; /* Ex command arguments */
1834 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001835 int save_msg_scroll = msg_scroll;
1836 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001838#ifdef HAVE_SANDBOX
1839 int did_sandbox = FALSE;
1840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 cmdmod_T save_cmdmod;
1842 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001843 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001844 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
1846 vim_memset(&ea, 0, sizeof(ea));
1847 ea.line1 = 1;
1848 ea.line2 = 1;
1849#ifdef FEAT_EVAL
1850 ++ex_nesting_level;
1851#endif
1852
Bram Moolenaar21691f82012-12-05 19:13:18 +01001853 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 if (quitmore
1855#ifdef FEAT_EVAL
1856 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001857 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001858#endif
1859#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001860 /* avoid that an autocommand, e.g. QuitPre, does this */
1861 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862#endif
1863 )
1864 --quitmore;
1865
1866 /*
1867 * Reset browse, confirm, etc.. They are restored when returning, for
1868 * recursive calls.
1869 */
1870 save_cmdmod = cmdmod;
1871 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1872
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001873 /* "#!anything" is handled like a comment. */
1874 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1875 goto doend;
1876
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 /*
1878 * Repeat until no more command modifiers are found.
1879 */
1880 ea.cmd = *cmdlinep;
1881 for (;;)
1882 {
1883/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001884 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 */
1886 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1887 ++ea.cmd;
1888
1889 /* in ex mode, an empty line works like :+ */
1890 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001891 && (getline_equal(fgetline, cookie, getexmodeline)
1892 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001893 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 {
1895 ea.cmd = (char_u *)"+";
1896 ex_pressedreturn = TRUE;
1897 }
1898
1899 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001900 if (*ea.cmd == '"')
1901 goto doend;
1902 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001903 {
1904 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907
1908/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001909 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001911 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 switch (*p)
1913 {
1914 /* When adding an entry, also modify cmd_exists(). */
1915 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1916 break;
1917#ifdef FEAT_WINDOWS
1918 cmdmod.split |= WSP_ABOVE;
1919#endif
1920 continue;
1921
1922 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1923 {
1924#ifdef FEAT_WINDOWS
1925 cmdmod.split |= WSP_BELOW;
1926#endif
1927 continue;
1928 }
1929 if (checkforcmd(&ea.cmd, "browse", 3))
1930 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001931#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 cmdmod.browse = TRUE;
1933#endif
1934 continue;
1935 }
1936 if (!checkforcmd(&ea.cmd, "botright", 2))
1937 break;
1938#ifdef FEAT_WINDOWS
1939 cmdmod.split |= WSP_BOT;
1940#endif
1941 continue;
1942
1943 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1944 break;
1945#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1946 cmdmod.confirm = TRUE;
1947#endif
1948 continue;
1949
1950 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1951 {
1952 cmdmod.keepmarks = TRUE;
1953 continue;
1954 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001955 if (checkforcmd(&ea.cmd, "keepalt", 5))
1956 {
1957 cmdmod.keepalt = TRUE;
1958 continue;
1959 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001960 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1961 {
1962 cmdmod.keeppatterns = TRUE;
1963 continue;
1964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1966 break;
1967 cmdmod.keepjumps = TRUE;
1968 continue;
1969
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001970 case 'f': /* only accept ":filter {pat} cmd" */
1971 {
1972 char_u *reg_pat;
1973
1974 if (!checkforcmd(&p, "filter", 4)
1975 || *p == NUL || ends_excmd(*p))
1976 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001977 if (*p == '!')
1978 {
1979 cmdmod.filter_force = TRUE;
1980 p = skipwhite(p + 1);
1981 if (*p == NUL || ends_excmd(*p))
1982 break;
1983 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001984 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1985 if (p == NULL || *p == NUL)
1986 break;
1987 cmdmod.filter_regmatch.regprog =
1988 vim_regcomp(reg_pat, RE_MAGIC);
1989 if (cmdmod.filter_regmatch.regprog == NULL)
1990 break;
1991 ea.cmd = p;
1992 continue;
1993 }
1994
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 /* ":hide" and ":hide | cmd" are not modifiers */
1996 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1997 || *p == NUL || ends_excmd(*p))
1998 break;
1999 ea.cmd = p;
2000 cmdmod.hide = TRUE;
2001 continue;
2002
2003 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
2004 {
2005 cmdmod.lockmarks = TRUE;
2006 continue;
2007 }
2008
2009 if (!checkforcmd(&ea.cmd, "leftabove", 5))
2010 break;
2011#ifdef FEAT_WINDOWS
2012 cmdmod.split |= WSP_ABOVE;
2013#endif
2014 continue;
2015
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002016 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002017 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002018#ifdef FEAT_AUTOCMD
2019 if (cmdmod.save_ei == NULL)
2020 {
2021 /* Set 'eventignore' to "all". Restore the
2022 * existing option value later. */
2023 cmdmod.save_ei = vim_strsave(p_ei);
2024 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002025 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002026 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002027#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002028 continue;
2029 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02002030 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002031 break;
2032 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002033 continue;
2034
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
2036 break;
2037#ifdef FEAT_WINDOWS
2038 cmdmod.split |= WSP_BELOW;
2039#endif
2040 continue;
2041
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002042 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
2043 {
2044#ifdef HAVE_SANDBOX
2045 if (!did_sandbox)
2046 ++sandbox;
2047 did_sandbox = TRUE;
2048#endif
2049 continue;
2050 }
2051 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002053 if (save_msg_silent == -1)
2054 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 ++msg_silent;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002056 if (*ea.cmd == '!' && !VIM_ISWHITE(ea.cmd[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 {
2058 /* ":silent!", but not "silent !cmd" */
2059 ea.cmd = skipwhite(ea.cmd + 1);
2060 ++emsg_silent;
2061 ++did_esilent;
2062 }
2063 continue;
2064
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002065 case 't': if (checkforcmd(&p, "tab", 3))
2066 {
2067#ifdef FEAT_WINDOWS
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002068 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01002069 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002070 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002071 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002072 else
2073 {
2074 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2075 {
2076 errormsg = (char_u *)_(e_invrange);
2077 goto doend;
2078 }
2079 cmdmod.tab = tabnr + 1;
2080 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002081 ea.cmd = p;
2082#endif
2083 continue;
2084 }
2085 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 break;
2087#ifdef FEAT_WINDOWS
2088 cmdmod.split |= WSP_TOP;
2089#endif
2090 continue;
2091
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002092 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2093 break;
2094 if (save_msg_silent == -1)
2095 save_msg_silent = msg_silent;
2096 msg_silent = 0;
2097 continue;
2098
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2100 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002101#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 cmdmod.split |= WSP_VERT;
2103#endif
2104 continue;
2105 }
2106 if (!checkforcmd(&p, "verbose", 4))
2107 break;
2108 if (verbose_save < 0)
2109 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002110 if (vim_isdigit(*ea.cmd))
2111 p_verbose = atoi((char *)ea.cmd);
2112 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 p_verbose = 1;
2114 ea.cmd = p;
2115 continue;
2116 }
2117 break;
2118 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002119 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120
2121#ifdef FEAT_EVAL
2122 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2123 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2124#else
2125 ea.skip = (if_level > 0);
2126#endif
2127
2128#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002129# ifdef FEAT_PROFILE
2130 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002131 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002132 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002133 if (getline_equal(fgetline, cookie, get_func_line))
2134 func_line_exec(getline_cookie(fgetline, cookie));
2135 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002136 script_line_exec();
2137 }
2138#endif
2139
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 /* May go to debug mode. If this happens and the ">quit" debug command is
2141 * used, throw an interrupt exception and skip the next command. */
2142 dbg_check_breakpoint(&ea);
2143 if (!ea.skip && got_int)
2144 {
2145 ea.skip = TRUE;
2146 (void)do_intthrow(cstack);
2147 }
2148#endif
2149
2150/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002151 * 3. Skip over the range to find the command. Let "p" point to after it.
2152 *
2153 * We need the command to know what kind of range it uses.
2154 */
2155 cmd = ea.cmd;
2156 ea.cmd = skip_range(ea.cmd, NULL);
2157 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2158 ea.cmd = skipwhite(ea.cmd + 1);
2159 p = find_command(&ea, NULL);
2160
2161/*
2162 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 *
2164 * where 'addr' is:
2165 *
2166 * % (entire file)
2167 * $ [+-NUM]
2168 * 'x [+-NUM] (where x denotes a currently defined mark)
2169 * . [+-NUM]
2170 * [+-NUM]..
2171 * NUM
2172 *
2173 * The ea.cmd pointer is updated to point to the first character following the
2174 * range spec. If an initial address is found, but no second, the upper bound
2175 * is equal to the lower.
2176 */
2177
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002178 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002179 if (!IS_USER_CMDIDX(ea.cmdidx))
2180 {
2181 if (ea.cmdidx != CMD_SIZE)
2182 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2183 else
2184 ea.addr_type = ADDR_LINES;
2185
2186#ifdef FEAT_WINDOWS
2187 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002188 if (ea.cmdidx == CMD_wincmd && p != NULL)
2189 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002190#endif
2191 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002192
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002194 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 for (;;)
2196 {
2197 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002198 switch (ea.addr_type)
2199 {
2200 case ADDR_LINES:
2201 /* default is current line number */
2202 ea.line2 = curwin->w_cursor.lnum;
2203 break;
2204 case ADDR_WINDOWS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002205 ea.line2 = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002206 break;
2207 case ADDR_ARGUMENTS:
2208 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002209 if (ea.line2 > ARGCOUNT)
2210 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002211 break;
2212 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002213 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002214 ea.line2 = curbuf->b_fnum;
2215 break;
2216 case ADDR_TABS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002217 ea.line2 = CURRENT_TAB_NR;
2218 break;
2219 case ADDR_TABS_RELATIVE:
2220 ea.line2 = 1;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002221 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002222#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002223 case ADDR_QUICKFIX:
2224 ea.line2 = qf_get_cur_valid_idx(&ea);
2225 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002226#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002229 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002230 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 if (ea.cmd == NULL) /* error detected */
2232 goto doend;
2233 if (lnum == MAXLNUM)
2234 {
2235 if (*ea.cmd == '%') /* '%' - all lines */
2236 {
2237 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002238 switch (ea.addr_type)
2239 {
2240 case ADDR_LINES:
2241 ea.line1 = 1;
2242 ea.line2 = curbuf->b_ml.ml_line_count;
2243 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002244 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002245 {
2246 buf_T *buf = firstbuf;
2247
2248 while (buf->b_next != NULL
2249 && buf->b_ml.ml_mfp == NULL)
2250 buf = buf->b_next;
2251 ea.line1 = buf->b_fnum;
2252 buf = lastbuf;
2253 while (buf->b_prev != NULL
2254 && buf->b_ml.ml_mfp == NULL)
2255 buf = buf->b_prev;
2256 ea.line2 = buf->b_fnum;
2257 break;
2258 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002259 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002260 ea.line1 = firstbuf->b_fnum;
2261 ea.line2 = lastbuf->b_fnum;
2262 break;
2263 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002264 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002265 if (IS_USER_CMDIDX(ea.cmdidx))
2266 {
2267 ea.line1 = 1;
2268 ea.line2 = ea.addr_type == ADDR_WINDOWS
2269 ? LAST_WIN_NR : LAST_TAB_NR;
2270 }
2271 else
2272 {
2273 /* there is no Vim command which uses '%' and
2274 * ADDR_WINDOWS or ADDR_TABS */
2275 errormsg = (char_u *)_(e_invrange);
2276 goto doend;
2277 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002278 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002279 case ADDR_TABS_RELATIVE:
2280 errormsg = (char_u *)_(e_invrange);
2281 goto doend;
2282 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002283 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002284 if (ARGCOUNT == 0)
2285 ea.line1 = ea.line2 = 0;
2286 else
2287 {
2288 ea.line1 = 1;
2289 ea.line2 = ARGCOUNT;
2290 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002291 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002292#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002293 case ADDR_QUICKFIX:
2294 ea.line1 = 1;
2295 ea.line2 = qf_get_size(&ea);
2296 if (ea.line2 == 0)
2297 ea.line2 = 1;
2298 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002299#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 ++ea.addr_count;
2302 }
2303 /* '*' - visual area */
2304 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2305 {
2306 pos_T *fp;
2307
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002308 if (ea.addr_type != ADDR_LINES)
2309 {
2310 errormsg = (char_u *)_(e_invrange);
2311 goto doend;
2312 }
2313
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 ++ea.cmd;
2315 if (!ea.skip)
2316 {
2317 fp = getmark('<', FALSE);
2318 if (check_mark(fp) == FAIL)
2319 goto doend;
2320 ea.line1 = fp->lnum;
2321 fp = getmark('>', FALSE);
2322 if (check_mark(fp) == FAIL)
2323 goto doend;
2324 ea.line2 = fp->lnum;
2325 ++ea.addr_count;
2326 }
2327 }
2328 }
2329 else
2330 ea.line2 = lnum;
2331 ea.addr_count++;
2332
2333 if (*ea.cmd == ';')
2334 {
2335 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +01002338 /* don't leave the cursor on an illegal line or column */
2339 check_cursor();
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
2342 else if (*ea.cmd != ',')
2343 break;
2344 ++ea.cmd;
2345 }
2346
2347 /* One address given: set start and end lines */
2348 if (ea.addr_count == 1)
2349 {
2350 ea.line1 = ea.line2;
2351 /* ... but only implicit: really no address given */
2352 if (lnum == MAXLNUM)
2353 ea.addr_count = 0;
2354 }
2355
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002357 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 */
2359
2360 /*
2361 * Skip ':' and any white space
2362 */
2363 ea.cmd = skipwhite(ea.cmd);
2364 while (*ea.cmd == ':')
2365 ea.cmd = skipwhite(ea.cmd + 1);
2366
2367 /*
2368 * If we got a line, but no command, then go to the line.
2369 * If we find a '|' or '\n' we set ea.nextcmd.
2370 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002371 if (*ea.cmd == NUL || *ea.cmd == '"'
2372 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 {
2374 /*
2375 * strange vi behaviour:
2376 * ":3" jumps to line 3
2377 * ":3|..." prints line 3
2378 * ":|" prints current line
2379 */
2380 if (ea.skip) /* skip this if inside :if */
2381 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002382 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
2384 ea.cmdidx = CMD_print;
2385 ea.argt = RANGE+COUNT+TRLBAR;
2386 if ((errormsg = invalid_range(&ea)) == NULL)
2387 {
2388 correct_range(&ea);
2389 ex_print(&ea);
2390 }
2391 }
2392 else if (ea.addr_count != 0)
2393 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002394 if (ea.line2 > curbuf->b_ml.ml_line_count)
2395 {
2396 /* With '-' in 'cpoptions' a line number past the file is an
2397 * error, otherwise put it at the end of the file. */
2398 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2399 ea.line2 = -1;
2400 else
2401 ea.line2 = curbuf->b_ml.ml_line_count;
2402 }
2403
2404 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002405 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 else
2407 {
2408 if (ea.line2 == 0)
2409 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 else
2411 curwin->w_cursor.lnum = ea.line2;
2412 beginline(BL_SOL | BL_FIX);
2413 }
2414 }
2415 goto doend;
2416 }
2417
Bram Moolenaard5005162014-08-22 23:05:54 +02002418#ifdef FEAT_AUTOCMD
2419 /* If this looks like an undefined user command and there are CmdUndefined
2420 * autocommands defined, trigger the matching autocommands. */
2421 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2422 && ASCII_ISUPPER(*ea.cmd)
2423 && has_cmdundefined())
2424 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002425 int ret;
2426
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002427 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002428 while (ASCII_ISALNUM(*p))
2429 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002430 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002431 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2432 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002433 /* If the autocommands did something and didn't cause an error, try
2434 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002435 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002436 }
2437#endif
2438
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439#ifdef FEAT_USR_CMDS
2440 if (p == NULL)
2441 {
2442 if (!ea.skip)
2443 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2444 goto doend;
2445 }
2446 /* Check for wrong commands. */
2447 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2448 {
2449 errormsg = uc_fun_cmd();
2450 goto doend;
2451 }
2452#endif
2453 if (ea.cmdidx == CMD_SIZE)
2454 {
2455 if (!ea.skip)
2456 {
2457 STRCPY(IObuff, _("E492: Not an editor command"));
2458 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002459 {
2460 /* If the modifier was parsed OK the error must be in the
2461 * following command */
2462 if (after_modifier != NULL)
2463 append_command(after_modifier);
2464 else
2465 append_command(*cmdlinep);
2466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002468 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 }
2470 goto doend;
2471 }
2472
Bram Moolenaar958636c2014-10-21 20:01:58 +02002473 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2474 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002475#ifdef HAVE_EX_SCRIPT_NI
2476 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2477#endif
2478 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480#ifndef FEAT_EVAL
2481 /*
2482 * When the expression evaluation is disabled, recognize the ":if" and
2483 * ":endif" commands and ignore everything in between it.
2484 */
2485 if (ea.cmdidx == CMD_if)
2486 ++if_level;
2487 if (if_level)
2488 {
2489 if (ea.cmdidx == CMD_endif)
2490 --if_level;
2491 goto doend;
2492 }
2493
2494#endif
2495
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002496 /* forced commands */
2497 if (*p == '!' && ea.cmdidx != CMD_substitute
2498 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 {
2500 ++p;
2501 ea.forceit = TRUE;
2502 }
2503 else
2504 ea.forceit = FALSE;
2505
2506/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002507 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002509 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002510 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511
2512 if (!ea.skip)
2513 {
2514#ifdef HAVE_SANDBOX
2515 if (sandbox != 0 && !(ea.argt & SBOXOK))
2516 {
2517 /* Command not allowed in sandbox. */
2518 errormsg = (char_u *)_(e_sandbox);
2519 goto doend;
2520 }
2521#endif
2522 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2523 {
2524 /* Command not allowed in non-'modifiable' buffer */
2525 errormsg = (char_u *)_(e_modifiable);
2526 goto doend;
2527 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002528
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002529 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002530 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002532 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002533 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 goto doend;
2535 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002536#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002537 /* Disallow editing another buffer when "curbuf_lock" is set.
2538 * Do allow ":edit" (check for argument later).
2539 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002540 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002541 && ea.cmdidx != CMD_edit
2542 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002543 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002544 && curbuf_locked())
2545 goto doend;
2546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
2548 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2549 {
2550 /* no range allowed */
2551 errormsg = (char_u *)_(e_norange);
2552 goto doend;
2553 }
2554 }
2555
2556 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2557 {
2558 errormsg = (char_u *)_(e_nobang);
2559 goto doend;
2560 }
2561
2562 /*
2563 * Don't complain about the range if it is not used
2564 * (could happen if line_count is accidentally set to 0).
2565 */
2566 if (!ea.skip && !ni)
2567 {
2568 /*
2569 * If the range is backwards, ask for confirmation and, if given, swap
2570 * ea.line1 & ea.line2 so it's forwards again.
2571 * When global command is busy, don't ask, will fail below.
2572 */
2573 if (!global_busy && ea.line1 > ea.line2)
2574 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002575 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002577 if (sourcing || exmode_active)
2578 {
2579 errormsg = (char_u *)_("E493: Backwards range given");
2580 goto doend;
2581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 if (ask_yesno((char_u *)
2583 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002584 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 }
2586 lnum = ea.line1;
2587 ea.line1 = ea.line2;
2588 ea.line2 = lnum;
2589 }
2590 if ((errormsg = invalid_range(&ea)) != NULL)
2591 goto doend;
2592 }
2593
2594 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2595 ea.line2 = 1;
2596
2597 correct_range(&ea);
2598
2599#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002600 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2601 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 {
2603 /* Put the first line at the start of a closed fold, put the last line
2604 * at the end of a closed fold. */
2605 (void)hasFolding(ea.line1, &ea.line1, NULL);
2606 (void)hasFolding(ea.line2, NULL, &ea.line2);
2607 }
2608#endif
2609
2610#ifdef FEAT_QUICKFIX
2611 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002612 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 * option here, so things like % get expanded.
2614 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002615 p = replace_makeprg(&ea, p, cmdlinep);
2616 if (p == NULL)
2617 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618#endif
2619
2620 /*
2621 * Skip to start of argument.
2622 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2623 */
2624 if (ea.cmdidx == CMD_bang)
2625 ea.arg = p;
2626 else
2627 ea.arg = skipwhite(p);
2628
2629 /*
2630 * Check for "++opt=val" argument.
2631 * Must be first, allow ":w ++enc=utf8 !cmd"
2632 */
2633 if (ea.argt & ARGOPT)
2634 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2635 if (getargopt(&ea) == FAIL && !ni)
2636 {
2637 errormsg = (char_u *)_(e_invarg);
2638 goto doend;
2639 }
2640
2641 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2642 {
2643 if (*ea.arg == '>') /* append */
2644 {
2645 if (*++ea.arg != '>') /* typed wrong */
2646 {
2647 errormsg = (char_u *)_("E494: Use w or w>>");
2648 goto doend;
2649 }
2650 ea.arg = skipwhite(ea.arg + 1);
2651 ea.append = TRUE;
2652 }
2653 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2654 {
2655 ++ea.arg;
2656 ea.usefilter = TRUE;
2657 }
2658 }
2659
2660 if (ea.cmdidx == CMD_read)
2661 {
2662 if (ea.forceit)
2663 {
2664 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2665 ea.forceit = FALSE;
2666 }
2667 else if (*ea.arg == '!') /* :r !filter */
2668 {
2669 ++ea.arg;
2670 ea.usefilter = TRUE;
2671 }
2672 }
2673
2674 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2675 {
2676 ea.amount = 1;
2677 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2678 {
2679 ++ea.arg;
2680 ++ea.amount;
2681 }
2682 ea.arg = skipwhite(ea.arg);
2683 }
2684
2685 /*
2686 * Check for "+command" argument, before checking for next command.
2687 * Don't do this for ":read !cmd" and ":write !cmd".
2688 */
2689 if ((ea.argt & EDITCMD) && !ea.usefilter)
2690 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2691
2692 /*
2693 * Check for '|' to separate commands and '"' to start comments.
2694 * Don't do this for ":read !cmd" and ":write !cmd".
2695 */
2696 if ((ea.argt & TRLBAR) && !ea.usefilter)
2697 separate_nextcmd(&ea);
2698
2699 /*
2700 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002701 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2702 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002704 else if (ea.cmdidx == CMD_bang
2705 || ea.cmdidx == CMD_global
2706 || ea.cmdidx == CMD_vglobal
2707 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
2709 for (p = ea.arg; *p; ++p)
2710 {
2711 /* Remove one backslash before a newline, so that it's possible to
2712 * pass a newline to the shell and also a newline that is preceded
2713 * with a backslash. This makes it impossible to end a shell
2714 * command in a backslash, but that doesn't appear useful.
2715 * Halving the number of backslashes is incompatible with previous
2716 * versions. */
2717 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002718 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 else if (*p == '\n')
2720 {
2721 ea.nextcmd = p + 1;
2722 *p = NUL;
2723 break;
2724 }
2725 }
2726 }
2727
2728 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2729 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002730 buf_T *buf;
2731
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002733 switch (ea.addr_type)
2734 {
2735 case ADDR_LINES:
2736 ea.line2 = curbuf->b_ml.ml_line_count;
2737 break;
2738 case ADDR_LOADED_BUFFERS:
2739 buf = firstbuf;
2740 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2741 buf = buf->b_next;
2742 ea.line1 = buf->b_fnum;
2743 buf = lastbuf;
2744 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2745 buf = buf->b_prev;
2746 ea.line2 = buf->b_fnum;
2747 break;
2748 case ADDR_BUFFERS:
2749 ea.line1 = firstbuf->b_fnum;
2750 ea.line2 = lastbuf->b_fnum;
2751 break;
2752 case ADDR_WINDOWS:
2753 ea.line2 = LAST_WIN_NR;
2754 break;
2755 case ADDR_TABS:
2756 ea.line2 = LAST_TAB_NR;
2757 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002758 case ADDR_TABS_RELATIVE:
2759 ea.line2 = 1;
2760 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002761 case ADDR_ARGUMENTS:
2762 if (ARGCOUNT == 0)
2763 ea.line1 = ea.line2 = 0;
2764 else
2765 ea.line2 = ARGCOUNT;
2766 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002767#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002768 case ADDR_QUICKFIX:
2769 ea.line2 = qf_get_size(&ea);
2770 if (ea.line2 == 0)
2771 ea.line2 = 1;
2772 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002773#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 }
2776
2777 /* accept numbered register only when no count allowed (:put) */
2778 if ( (ea.argt & REGSTR)
2779 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002780 /* Do not allow register = for user commands */
2781 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2783 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002784#ifndef FEAT_CLIPBOARD
2785 /* check these explicitly for a more specific error message */
2786 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002788 errormsg = (char_u *)_(e_invalidreg);
2789 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 }
2791#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002792 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2793 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002794 {
2795 ea.regname = *ea.arg++;
2796#ifdef FEAT_EVAL
2797 /* for '=' register: accept the rest of the line as an expression */
2798 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2799 {
2800 set_expr_line(vim_strsave(ea.arg));
2801 ea.arg += STRLEN(ea.arg);
2802 }
2803#endif
2804 ea.arg = skipwhite(ea.arg);
2805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 }
2807
2808 /*
2809 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2810 * count, it's a buffer name.
2811 */
2812 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2813 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002814 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 {
2816 n = getdigits(&ea.arg);
2817 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002818 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 {
2820 errormsg = (char_u *)_(e_zerocount);
2821 goto doend;
2822 }
2823 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2824 {
2825 ea.line2 = n;
2826 if (ea.addr_count == 0)
2827 ea.addr_count = 1;
2828 }
2829 else
2830 {
2831 ea.line1 = ea.line2;
2832 ea.line2 += n - 1;
2833 ++ea.addr_count;
2834 /*
2835 * Be vi compatible: no error message for out of range.
2836 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002837 if (ea.addr_type == ADDR_LINES
2838 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 ea.line2 = curbuf->b_ml.ml_line_count;
2840 }
2841 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002842
2843 /*
2844 * Check for flags: 'l', 'p' and '#'.
2845 */
2846 if (ea.argt & EXFLAGS)
2847 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002849 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002850 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
2852 errormsg = (char_u *)_(e_trailing);
2853 goto doend;
2854 }
2855
2856 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2857 {
2858 errormsg = (char_u *)_(e_argreq);
2859 goto doend;
2860 }
2861
2862#ifdef FEAT_EVAL
2863 /*
2864 * Skip the command when it's not going to be executed.
2865 * The commands like :if, :endif, etc. always need to be executed.
2866 * Also make an exception for commands that handle a trailing command
2867 * themselves.
2868 */
2869 if (ea.skip)
2870 {
2871 switch (ea.cmdidx)
2872 {
2873 /* commands that need evaluation */
2874 case CMD_while:
2875 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002876 case CMD_for:
2877 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 case CMD_if:
2879 case CMD_elseif:
2880 case CMD_else:
2881 case CMD_endif:
2882 case CMD_try:
2883 case CMD_catch:
2884 case CMD_finally:
2885 case CMD_endtry:
2886 case CMD_function:
2887 break;
2888
2889 /* Commands that handle '|' themselves. Check: A command should
2890 * either have the TRLBAR flag, appear in this list or appear in
2891 * the list at ":help :bar". */
2892 case CMD_aboveleft:
2893 case CMD_and:
2894 case CMD_belowright:
2895 case CMD_botright:
2896 case CMD_browse:
2897 case CMD_call:
2898 case CMD_confirm:
2899 case CMD_delfunction:
2900 case CMD_djump:
2901 case CMD_dlist:
2902 case CMD_dsearch:
2903 case CMD_dsplit:
2904 case CMD_echo:
2905 case CMD_echoerr:
2906 case CMD_echomsg:
2907 case CMD_echon:
2908 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002909 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 case CMD_help:
2911 case CMD_hide:
2912 case CMD_ijump:
2913 case CMD_ilist:
2914 case CMD_isearch:
2915 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002916 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 case CMD_keepjumps:
2918 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002919 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 case CMD_leftabove:
2921 case CMD_let:
2922 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002923 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002925 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002926 case CMD_noautocmd:
2927 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 case CMD_perl:
2929 case CMD_psearch:
2930 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002931 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002932 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 case CMD_return:
2934 case CMD_rightbelow:
2935 case CMD_ruby:
2936 case CMD_silent:
2937 case CMD_smagic:
2938 case CMD_snomagic:
2939 case CMD_substitute:
2940 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002941 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 case CMD_tcl:
2943 case CMD_throw:
2944 case CMD_tilde:
2945 case CMD_topleft:
2946 case CMD_unlet:
2947 case CMD_verbose:
2948 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002949 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 break;
2951
2952 default: goto doend;
2953 }
2954 }
2955#endif
2956
2957 if (ea.argt & XFILE)
2958 {
2959 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2960 goto doend;
2961 }
2962
2963#ifdef FEAT_LISTCMDS
2964 /*
2965 * Accept buffer name. Cannot be used at the same time with a buffer
2966 * number. Don't do this for a user command.
2967 */
2968 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002969 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 {
2971 /*
2972 * :bdelete, :bwipeout and :bunload take several arguments, separated
2973 * by spaces: find next space (skipping over escaped characters).
2974 * The others take one argument: ignore trailing spaces.
2975 */
2976 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2977 || ea.cmdidx == CMD_bunload)
2978 p = skiptowhite_esc(ea.arg);
2979 else
2980 {
2981 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002982 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 --p;
2984 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002985 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2986 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 if (ea.line2 < 0) /* failed */
2988 goto doend;
2989 ea.addr_count = 1;
2990 ea.arg = skipwhite(p);
2991 }
2992#endif
2993
2994/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002995 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 *
2997 * The "ea" structure holds the arguments that can be used.
2998 */
2999 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003000 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 ea.cookie = cookie;
3002#ifdef FEAT_EVAL
3003 ea.cstack = cstack;
3004#endif
3005
3006#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02003007 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 {
3009 /*
3010 * Execute a user-defined command.
3011 */
3012 do_ucmd(&ea);
3013 }
3014 else
3015#endif
3016 {
3017 /*
3018 * Call the function to execute the command.
3019 */
3020 ea.errmsg = NULL;
3021 (cmdnames[ea.cmdidx].cmd_func)(&ea);
3022 if (ea.errmsg != NULL)
3023 errormsg = (char_u *)_(ea.errmsg);
3024 }
3025
3026#ifdef FEAT_EVAL
3027 /*
3028 * If the command just executed called do_cmdline(), any throw or ":return"
3029 * or ":finish" encountered there must also check the cstack of the still
3030 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
3031 * exception, or reanimate a returned function or finished script file and
3032 * return or finish it again.
3033 */
3034 if (need_rethrow)
3035 do_throw(cstack);
3036 else if (check_cstack)
3037 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00003038 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003040 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 && current_func_returned())
3042 do_return(&ea, TRUE, FALSE, NULL);
3043 }
3044 need_rethrow = check_cstack = FALSE;
3045#endif
3046
3047doend:
3048 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
3049 curwin->w_cursor.lnum = 1;
3050
3051 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
3052 {
3053 if (sourcing)
3054 {
3055 if (errormsg != IObuff)
3056 {
3057 STRCPY(IObuff, errormsg);
3058 errormsg = IObuff;
3059 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003060 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 }
3062 emsg(errormsg);
3063 }
3064#ifdef FEAT_EVAL
3065 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02003066 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
3067 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068#endif
3069
3070 if (verbose_save >= 0)
3071 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003072#ifdef FEAT_AUTOCMD
3073 if (cmdmod.save_ei != NULL)
3074 {
3075 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003076 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
3077 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003078 free_string_option(cmdmod.save_ei);
3079 }
3080#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003081 if (cmdmod.filter_regmatch.regprog != NULL)
3082 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083
3084 cmdmod = save_cmdmod;
3085
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003086 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 {
3088 /* messages could be enabled for a serious error, need to check if the
3089 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00003090 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003091 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 emsg_silent -= did_esilent;
3093 if (emsg_silent < 0)
3094 emsg_silent = 0;
3095 /* Restore msg_scroll, it's set by file I/O commands, even when no
3096 * message is actually displayed. */
3097 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003098
3099 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3100 * somewhere in the line. Put it back in the first column. */
3101 if (redirecting())
3102 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 }
3104
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003105#ifdef HAVE_SANDBOX
3106 if (did_sandbox)
3107 --sandbox;
3108#endif
3109
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3111 ea.nextcmd = NULL;
3112
3113#ifdef FEAT_EVAL
3114 --ex_nesting_level;
3115#endif
3116
3117 return ea.nextcmd;
3118}
3119#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003120 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121#endif
3122
3123/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003124 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 * If there is a match advance "pp" to the argument and return TRUE.
3126 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003127 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003128checkforcmd(
3129 char_u **pp, /* start of command */
3130 char *cmd, /* name of command */
3131 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132{
3133 int i;
3134
3135 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003136 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 break;
3138 if (i >= len && !isalpha((*pp)[i]))
3139 {
3140 *pp = skipwhite(*pp + i);
3141 return TRUE;
3142 }
3143 return FALSE;
3144}
3145
3146/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003147 * Append "cmd" to the error message in IObuff.
3148 * Takes care of limiting the length and handling 0xa0, which would be
3149 * invisible otherwise.
3150 */
3151 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003152append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003153{
3154 char_u *s = cmd;
3155 char_u *d;
3156
3157 STRCAT(IObuff, ": ");
3158 d = IObuff + STRLEN(IObuff);
3159 while (*s != NUL && d - IObuff < IOSIZE - 7)
3160 {
3161 if (
3162#ifdef FEAT_MBYTE
3163 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3164#endif
3165 *s == 0xa0)
3166 {
3167 s +=
3168#ifdef FEAT_MBYTE
3169 enc_utf8 ? 2 :
3170#endif
3171 1;
3172 STRCPY(d, "<a0>");
3173 d += 4;
3174 }
3175 else
3176 MB_COPY_CHAR(s, d);
3177 }
3178 *d = NUL;
3179}
3180
3181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003183 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003185 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 * Returns NULL for an ambiguous user command.
3187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003189find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190{
3191 int len;
3192 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003193 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
3195 /*
3196 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003197 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 * - the 'k' command can directly be followed by any character.
3199 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003200 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003202 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 */
3204 p = eap->cmd;
3205 if (*p == 'k')
3206 {
3207 eap->cmdidx = CMD_k;
3208 ++p;
3209 }
3210 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003211 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3212 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 || p[1] == 'g'
3214 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3215 || p[1] == 'I'
3216 || (p[1] == 'r' && p[2] != 'e')))
3217 {
3218 eap->cmdidx = CMD_substitute;
3219 ++p;
3220 }
3221 else
3222 {
3223 while (ASCII_ISALPHA(*p))
3224 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003225 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003226 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003227 while (ASCII_ISALNUM(*p))
3228 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003229
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 /* check for non-alpha command */
3231 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3232 ++p;
3233 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003234 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3235 {
3236 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3237 * :delete with the 'l' flag. Same for 'p'. */
3238 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003239 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003240 break;
3241 if (i == len - 1)
3242 {
3243 --len;
3244 if (p[-1] == 'l')
3245 eap->flags |= EXFLAG_LIST;
3246 else
3247 eap->flags |= EXFLAG_PRINT;
3248 }
3249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003251 if (ASCII_ISLOWER(eap->cmd[0]))
3252 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003253 int c1 = eap->cmd[0];
3254 int c2 = eap->cmd[1];
3255
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003256 if (command_count != (int)CMD_SIZE)
3257 {
3258 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3259 getout(1);
3260 }
3261
3262 /* Use a precomputed index for fast look-up in cmdnames[]
3263 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003264 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3265 if (ASCII_ISLOWER(c2))
3266 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003269 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270
3271 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3272 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3273 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3274 (size_t)len) == 0)
3275 {
3276#ifdef FEAT_EVAL
3277 if (full != NULL
3278 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3279 *full = TRUE;
3280#endif
3281 break;
3282 }
3283
3284#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003285 /* Look for a user defined command as a last resort. Let ":Print" be
3286 * overruled by a user defined command. */
3287 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3288 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003290 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 while (ASCII_ISALNUM(*p))
3292 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003293 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 }
3295#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003296 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 eap->cmdidx = CMD_SIZE;
3298 }
3299
3300 return p;
3301}
3302
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003303#ifdef FEAT_USR_CMDS
3304/*
3305 * Search for a user command that matches "eap->cmd".
3306 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3307 * Return a pointer to just after the command.
3308 * Return NULL if there is no matching command.
3309 */
3310 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003311find_ucmd(
3312 exarg_T *eap,
3313 char_u *p, /* end of the command (possibly including count) */
3314 int *full, /* set to TRUE for a full match */
3315 expand_T *xp, /* used for completion, NULL otherwise */
3316 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003317{
3318 int len = (int)(p - eap->cmd);
3319 int j, k, matchlen = 0;
3320 ucmd_T *uc;
3321 int found = FALSE;
3322 int possible = FALSE;
3323 char_u *cp, *np; /* Point into typed cmd and test name */
3324 garray_T *gap;
3325 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3326 only full match global is accepted. */
3327
3328 /*
3329 * Look for buffer-local user commands first, then global ones.
3330 */
3331 gap = &curbuf->b_ucmds;
3332 for (;;)
3333 {
3334 for (j = 0; j < gap->ga_len; ++j)
3335 {
3336 uc = USER_CMD_GA(gap, j);
3337 cp = eap->cmd;
3338 np = uc->uc_name;
3339 k = 0;
3340 while (k < len && *np != NUL && *cp++ == *np++)
3341 k++;
3342 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3343 {
3344 /* If finding a second match, the command is ambiguous. But
3345 * not if a buffer-local command wasn't a full match and a
3346 * global command is a full match. */
3347 if (k == len && found && *np != NUL)
3348 {
3349 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003350 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003351 amb_local = TRUE;
3352 }
3353
3354 if (!found || (k == len && *np == NUL))
3355 {
3356 /* If we matched up to a digit, then there could
3357 * be another command including the digit that we
3358 * should use instead.
3359 */
3360 if (k == len)
3361 found = TRUE;
3362 else
3363 possible = TRUE;
3364
3365 if (gap == &ucmds)
3366 eap->cmdidx = CMD_USER;
3367 else
3368 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003369 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003370 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003371 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003372
3373# ifdef FEAT_CMDL_COMPL
3374 if (compl != NULL)
3375 *compl = uc->uc_compl;
3376# ifdef FEAT_EVAL
3377 if (xp != NULL)
3378 {
3379 xp->xp_arg = uc->uc_compl_arg;
3380 xp->xp_scriptID = uc->uc_scriptID;
3381 }
3382# endif
3383# endif
3384 /* Do not search for further abbreviations
3385 * if this is an exact match. */
3386 matchlen = k;
3387 if (k == len && *np == NUL)
3388 {
3389 if (full != NULL)
3390 *full = TRUE;
3391 amb_local = FALSE;
3392 break;
3393 }
3394 }
3395 }
3396 }
3397
3398 /* Stop if we found a full match or searched all. */
3399 if (j < gap->ga_len || gap == &ucmds)
3400 break;
3401 gap = &ucmds;
3402 }
3403
3404 /* Only found ambiguous matches. */
3405 if (amb_local)
3406 {
3407 if (xp != NULL)
3408 xp->xp_context = EXPAND_UNSUCCESSFUL;
3409 return NULL;
3410 }
3411
3412 /* The match we found may be followed immediately by a number. Move "p"
3413 * back to point to it. */
3414 if (found || possible)
3415 return p + (matchlen - len);
3416 return p;
3417}
3418#endif
3419
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003421static struct cmdmod
3422{
3423 char *name;
3424 int minlen;
3425 int has_count; /* :123verbose :3tab */
3426} cmdmods[] = {
3427 {"aboveleft", 3, FALSE},
3428 {"belowright", 3, FALSE},
3429 {"botright", 2, FALSE},
3430 {"browse", 3, FALSE},
3431 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003432 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003433 {"hide", 3, FALSE},
3434 {"keepalt", 5, FALSE},
3435 {"keepjumps", 5, FALSE},
3436 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003437 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003438 {"leftabove", 5, FALSE},
3439 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003440 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003441 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003442 {"rightbelow", 6, FALSE},
3443 {"sandbox", 3, FALSE},
3444 {"silent", 3, FALSE},
3445 {"tab", 3, TRUE},
3446 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003447 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003448 {"verbose", 4, TRUE},
3449 {"vertical", 4, FALSE},
3450};
3451
3452/*
3453 * Return length of a command modifier (including optional count).
3454 * Return zero when it's not a modifier.
3455 */
3456 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003457modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003458{
3459 int i, j;
3460 char_u *p = cmd;
3461
3462 if (VIM_ISDIGIT(*cmd))
3463 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003464 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003465 {
3466 for (j = 0; p[j] != NUL; ++j)
3467 if (p[j] != cmdmods[i].name[j])
3468 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003469 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003470 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003471 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003472 }
3473 return 0;
3474}
3475
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476/*
3477 * Return > 0 if an Ex command "name" exists.
3478 * Return 2 if there is an exact match.
3479 * Return 3 if there is an ambiguous match.
3480 */
3481 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003482cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
3484 exarg_T ea;
3485 int full = FALSE;
3486 int i;
3487 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003488 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
3490 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003491 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 {
3493 for (j = 0; name[j] != NUL; ++j)
3494 if (name[j] != cmdmods[i].name[j])
3495 break;
3496 if (name[j] == NUL && j >= cmdmods[i].minlen)
3497 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3498 }
3499
Bram Moolenaara9587612006-05-04 21:47:50 +00003500 /* Check built-in commands and user defined commands.
3501 * For ":2match" and ":3match" we need to skip the number. */
3502 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003504 p = find_command(&ea, &full);
3505 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003507 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3508 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003509 if (*skipwhite(p) != NUL)
3510 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3512}
3513#endif
3514
3515/*
3516 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3517 * we don't need/want deleted. Maybe this could be done better if we didn't
3518 * repeat all this stuff. The only problem is that they may not stay
3519 * perfectly compatible with each other, but then the command line syntax
3520 * probably won't change that much -- webb.
3521 */
3522 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003523set_one_cmd_context(
3524 expand_T *xp,
3525 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526{
3527 char_u *p;
3528 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003529 int len = 0;
3530 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3532 int compl = EXPAND_NOTHING;
3533#endif
3534#ifdef FEAT_CMDL_COMPL
3535 int delim;
3536#endif
3537 int forceit = FALSE;
3538 int usefilter = FALSE; /* filter instead of file name */
3539
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003540 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 xp->xp_pattern = buff;
3542 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003543 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544
3545/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003546 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 */
3548 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3549 ;
3550 xp->xp_pattern = cmd;
3551
3552 if (*cmd == NUL)
3553 return NULL;
3554 if (*cmd == '"') /* ignore comment lines */
3555 {
3556 xp->xp_context = EXPAND_NOTHING;
3557 return NULL;
3558 }
3559
3560/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003561 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 */
3563 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 xp->xp_pattern = cmd;
3565 if (*cmd == NUL)
3566 return NULL;
3567 if (*cmd == '"')
3568 {
3569 xp->xp_context = EXPAND_NOTHING;
3570 return NULL;
3571 }
3572
3573 if (*cmd == '|' || *cmd == '\n')
3574 return cmd + 1; /* There's another command */
3575
3576 /*
3577 * Isolate the command and search for it in the command table.
3578 * Exceptions:
3579 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003580 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3582 */
3583 if (*cmd == 'k' && cmd[1] != 'e')
3584 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003585 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 p = cmd + 1;
3587 }
3588 else
3589 {
3590 p = cmd;
3591 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3592 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003593 /* a user command may contain digits */
3594 if (ASCII_ISUPPER(cmd[0]))
3595 while (ASCII_ISALNUM(*p) || *p == '*')
3596 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003597 /* for python 3.x: ":py3*" commands completion */
3598 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003599 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003600 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003601 while (ASCII_ISALPHA(*p) || *p == '*')
3602 ++p;
3603 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003604 /* check for non-alpha command */
3605 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3606 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003607 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003609 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
3611 xp->xp_context = EXPAND_UNSUCCESSFUL;
3612 return NULL;
3613 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003614 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003615 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3616 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3617 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 break;
3619
3620#ifdef FEAT_USR_CMDS
3621 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3623 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624#endif
3625 }
3626
3627 /*
3628 * If the cursor is touching the command, and it ends in an alpha-numeric
3629 * character, complete the command name.
3630 */
3631 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3632 return NULL;
3633
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003634 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 {
3636 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3637 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003638 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 p = cmd + 1;
3640 }
3641#ifdef FEAT_USR_CMDS
3642 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3643 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003644 ea.cmd = cmd;
3645 p = find_ucmd(&ea, p, NULL, xp,
3646# if defined(FEAT_CMDL_COMPL)
3647 &compl
3648# else
3649 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003651 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003652 if (p == NULL)
3653 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 }
3655#endif
3656 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003657 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
3659 /* Not still touching the command and it was an illegal one */
3660 xp->xp_context = EXPAND_UNSUCCESSFUL;
3661 return NULL;
3662 }
3663
3664 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3665
3666 if (*p == '!') /* forced commands */
3667 {
3668 forceit = TRUE;
3669 ++p;
3670 }
3671
3672/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003673 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003675 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003676 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677
3678 arg = skipwhite(p);
3679
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003680 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
3682 if (*arg == '>') /* append */
3683 {
3684 if (*++arg == '>')
3685 ++arg;
3686 arg = skipwhite(arg);
3687 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003688 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 {
3690 ++arg;
3691 usefilter = TRUE;
3692 }
3693 }
3694
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003695 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 {
3697 usefilter = forceit; /* :r! filter if forced */
3698 if (*arg == '!') /* :r !filter */
3699 {
3700 ++arg;
3701 usefilter = TRUE;
3702 }
3703 }
3704
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003705 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 {
3707 while (*arg == *cmd) /* allow any number of '>' or '<' */
3708 ++arg;
3709 arg = skipwhite(arg);
3710 }
3711
3712 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003713 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 {
3715 /* Check if we're in the +command */
3716 p = arg + 1;
3717 arg = skip_cmd_arg(arg, FALSE);
3718
3719 /* Still touching the command after '+'? */
3720 if (*arg == NUL)
3721 return p;
3722
3723 /* Skip space(s) after +command to get to the real argument */
3724 arg = skipwhite(arg);
3725 }
3726
3727 /*
3728 * Check for '|' to separate commands and '"' to start comments.
3729 * Don't do this for ":read !cmd" and ":write !cmd".
3730 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003731 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 {
3733 p = arg;
3734 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003735 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 p += 2;
3737 while (*p)
3738 {
3739 if (*p == Ctrl_V)
3740 {
3741 if (p[1] != NUL)
3742 ++p;
3743 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003744 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 || *p == '|' || *p == '\n')
3746 {
3747 if (*(p - 1) != '\\')
3748 {
3749 if (*p == '|' || *p == '\n')
3750 return p + 1;
3751 return NULL; /* It's a comment */
3752 }
3753 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003754 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
3756 }
3757
3758 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003759 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 vim_strchr((char_u *)"|\"", *arg) == NULL)
3761 return NULL;
3762
3763 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003764 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003766 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003767 while (*p && p < buff + len)
3768 {
3769 if (*p == ' ' || *p == TAB)
3770 {
3771 /* argument starts after a space */
3772 xp->xp_pattern = ++p;
3773 }
3774 else
3775 {
3776 if (*p == '\\' && *(p + 1) != NUL)
3777 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003778 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003779 }
3780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003782 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003784 int c;
3785 int in_quote = FALSE;
3786 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787
3788 /*
3789 * Allow spaces within back-quotes to count as part of the argument
3790 * being expanded.
3791 */
3792 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003793 p = xp->xp_pattern;
3794 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003796#ifdef FEAT_MBYTE
3797 if (has_mbyte)
3798 c = mb_ptr2char(p);
3799 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003801 c = *p;
3802 if (c == '\\' && p[1] != NUL)
3803 ++p;
3804 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 {
3806 if (!in_quote)
3807 {
3808 xp->xp_pattern = p;
3809 bow = p + 1;
3810 }
3811 in_quote = !in_quote;
3812 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003813 /* An argument can contain just about everything, except
3814 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003815 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003816#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003817 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003818#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003819 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003820 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003821 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003822 while (*p != NUL)
3823 {
3824#ifdef FEAT_MBYTE
3825 if (has_mbyte)
3826 c = mb_ptr2char(p);
3827 else
3828#endif
3829 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003830 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003831 break;
3832#ifdef FEAT_MBYTE
3833 if (has_mbyte)
3834 len = (*mb_ptr2len)(p);
3835 else
3836#endif
3837 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003838 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003839 }
3840 if (in_quote)
3841 bow = p;
3842 else
3843 xp->xp_pattern = p;
3844 p -= len;
3845 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003846 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 }
3848
3849 /*
3850 * If we are still inside the quotes, and we passed a space, just
3851 * expand from there.
3852 */
3853 if (bow != NULL && in_quote)
3854 xp->xp_pattern = bow;
3855 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003856
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003857 /* For a shell command more chars need to be escaped. */
3858 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003859 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003860#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003861 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003862#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003863 /* When still after the command name expand executables. */
3864 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003865 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867
3868 /* Check for environment variable */
3869 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003870#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 || *xp->xp_pattern == '%'
3872#endif
3873 )
3874 {
3875 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3876 if (!vim_isIDc(*p))
3877 break;
3878 if (*p == NUL)
3879 {
3880 xp->xp_context = EXPAND_ENV_VARS;
3881 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003882#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3883 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003884 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003885 compl = EXPAND_ENV_VARS;
3886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 }
3888 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003889#if defined(FEAT_CMDL_COMPL)
3890 /* Check for user names */
3891 if (*xp->xp_pattern == '~')
3892 {
3893 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3894 ;
3895 /* Complete ~user only if it partially matches a user name.
3896 * A full match ~user<Tab> will be replaced by user's home
3897 * directory i.e. something like ~user<Tab> -> /home/user/ */
3898 if (*p == NUL && p > xp->xp_pattern + 1
3899 && match_user(xp->xp_pattern + 1) == 1)
3900 {
3901 xp->xp_context = EXPAND_USER;
3902 ++xp->xp_pattern;
3903 }
3904 }
3905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
3907
3908/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003909 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003911 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003913 case CMD_find:
3914 case CMD_sfind:
3915 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003916 if (xp->xp_context == EXPAND_FILES)
3917 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003918 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 case CMD_cd:
3920 case CMD_chdir:
3921 case CMD_lcd:
3922 case CMD_lchdir:
3923 if (xp->xp_context == EXPAND_FILES)
3924 xp->xp_context = EXPAND_DIRECTORIES;
3925 break;
3926 case CMD_help:
3927 xp->xp_context = EXPAND_HELP;
3928 xp->xp_pattern = arg;
3929 break;
3930
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003931 /* Command modifiers: return the argument.
3932 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003934 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 case CMD_belowright:
3936 case CMD_botright:
3937 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003938 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003939 case CMD_cdo:
3940 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003942 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 case CMD_folddoclosed:
3944 case CMD_folddoopen:
3945 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003946 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 case CMD_keepjumps:
3948 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003949 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003950 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003952 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003954 case CMD_noautocmd:
3955 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003957 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003959 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003960 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 case CMD_topleft:
3962 case CMD_verbose:
3963 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003964 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 return arg;
3966
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003967 case CMD_filter:
3968 if (*arg != NUL)
3969 arg = skip_vimgrep_pat(arg, NULL, NULL);
3970 if (arg == NULL || *arg == NUL)
3971 {
3972 xp->xp_context = EXPAND_NOTHING;
3973 return NULL;
3974 }
3975 return skipwhite(arg);
3976
Bram Moolenaar4f688582007-07-24 12:34:30 +00003977#ifdef FEAT_CMDL_COMPL
3978# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 case CMD_match:
3980 if (*arg == NUL || !ends_excmd(*arg))
3981 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003982 /* also complete "None" */
3983 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 arg = skipwhite(skiptowhite(arg));
3985 if (*arg != NUL)
3986 {
3987 xp->xp_context = EXPAND_NOTHING;
3988 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3989 }
3990 }
3991 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003992# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994/*
3995 * All completion for the +cmdline_compl feature goes here.
3996 */
3997
3998# ifdef FEAT_USR_CMDS
3999 case CMD_command:
4000 /* Check for attributes */
4001 while (*arg == '-')
4002 {
4003 arg++; /* Skip "-" */
4004 p = skiptowhite(arg);
4005 if (*p == NUL)
4006 {
4007 /* Cursor is still in the attribute */
4008 p = vim_strchr(arg, '=');
4009 if (p == NULL)
4010 {
4011 /* No "=", so complete attribute names */
4012 xp->xp_context = EXPAND_USER_CMD_FLAGS;
4013 xp->xp_pattern = arg;
4014 return NULL;
4015 }
4016
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004017 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 * their arguments as well.
4019 */
4020 if (STRNICMP(arg, "complete", p - arg) == 0)
4021 {
4022 xp->xp_context = EXPAND_USER_COMPLETE;
4023 xp->xp_pattern = p + 1;
4024 return NULL;
4025 }
4026 else if (STRNICMP(arg, "nargs", p - arg) == 0)
4027 {
4028 xp->xp_context = EXPAND_USER_NARGS;
4029 xp->xp_pattern = p + 1;
4030 return NULL;
4031 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004032 else if (STRNICMP(arg, "addr", p - arg) == 0)
4033 {
4034 xp->xp_context = EXPAND_USER_ADDR_TYPE;
4035 xp->xp_pattern = p + 1;
4036 return NULL;
4037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 return NULL;
4039 }
4040 arg = skipwhite(p);
4041 }
4042
4043 /* After the attributes comes the new command name */
4044 p = skiptowhite(arg);
4045 if (*p == NUL)
4046 {
4047 xp->xp_context = EXPAND_USER_COMMANDS;
4048 xp->xp_pattern = arg;
4049 break;
4050 }
4051
4052 /* And finally comes a normal command */
4053 return skipwhite(p);
4054
4055 case CMD_delcommand:
4056 xp->xp_context = EXPAND_USER_COMMANDS;
4057 xp->xp_pattern = arg;
4058 break;
4059# endif
4060
4061 case CMD_global:
4062 case CMD_vglobal:
4063 delim = *arg; /* get the delimiter */
4064 if (delim)
4065 ++arg; /* skip delimiter if there is one */
4066
4067 while (arg[0] != NUL && arg[0] != delim)
4068 {
4069 if (arg[0] == '\\' && arg[1] != NUL)
4070 ++arg;
4071 ++arg;
4072 }
4073 if (arg[0] != NUL)
4074 return arg + 1;
4075 break;
4076 case CMD_and:
4077 case CMD_substitute:
4078 delim = *arg;
4079 if (delim)
4080 {
4081 /* skip "from" part */
4082 ++arg;
4083 arg = skip_regexp(arg, delim, p_magic, NULL);
4084 }
4085 /* skip "to" part */
4086 while (arg[0] != NUL && arg[0] != delim)
4087 {
4088 if (arg[0] == '\\' && arg[1] != NUL)
4089 ++arg;
4090 ++arg;
4091 }
4092 if (arg[0] != NUL) /* skip delimiter */
4093 ++arg;
4094 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4095 ++arg;
4096 if (arg[0] != NUL)
4097 return arg;
4098 break;
4099 case CMD_isearch:
4100 case CMD_dsearch:
4101 case CMD_ilist:
4102 case CMD_dlist:
4103 case CMD_ijump:
4104 case CMD_psearch:
4105 case CMD_djump:
4106 case CMD_isplit:
4107 case CMD_dsplit:
4108 arg = skipwhite(skipdigits(arg)); /* skip count */
4109 if (*arg == '/') /* Match regexp, not just whole words */
4110 {
4111 for (++arg; *arg && *arg != '/'; arg++)
4112 if (*arg == '\\' && arg[1] != NUL)
4113 arg++;
4114 if (*arg)
4115 {
4116 arg = skipwhite(arg + 1);
4117
4118 /* Check for trailing illegal characters */
4119 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4120 xp->xp_context = EXPAND_NOTHING;
4121 else
4122 return arg;
4123 }
4124 }
4125 break;
4126#ifdef FEAT_AUTOCMD
4127 case CMD_autocmd:
4128 return set_context_in_autocmd(xp, arg, FALSE);
4129
4130 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004131 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 return set_context_in_autocmd(xp, arg, TRUE);
4133#endif
4134 case CMD_set:
4135 set_context_in_set_cmd(xp, arg, 0);
4136 break;
4137 case CMD_setglobal:
4138 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4139 break;
4140 case CMD_setlocal:
4141 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4142 break;
4143 case CMD_tag:
4144 case CMD_stag:
4145 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004146 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 case CMD_tselect:
4148 case CMD_stselect:
4149 case CMD_ptselect:
4150 case CMD_tjump:
4151 case CMD_stjump:
4152 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004153 if (*p_wop != NUL)
4154 xp->xp_context = EXPAND_TAGS_LISTFILES;
4155 else
4156 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 xp->xp_pattern = arg;
4158 break;
4159 case CMD_augroup:
4160 xp->xp_context = EXPAND_AUGROUP;
4161 xp->xp_pattern = arg;
4162 break;
4163#ifdef FEAT_SYN_HL
4164 case CMD_syntax:
4165 set_context_in_syntax_cmd(xp, arg);
4166 break;
4167#endif
4168#ifdef FEAT_EVAL
4169 case CMD_let:
4170 case CMD_if:
4171 case CMD_elseif:
4172 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004173 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 case CMD_echo:
4175 case CMD_echon:
4176 case CMD_execute:
4177 case CMD_echomsg:
4178 case CMD_echoerr:
4179 case CMD_call:
4180 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004181 case CMD_cexpr:
4182 case CMD_caddexpr:
4183 case CMD_cgetexpr:
4184 case CMD_lexpr:
4185 case CMD_laddexpr:
4186 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004187 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 break;
4189
4190 case CMD_unlet:
4191 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4192 arg = xp->xp_pattern + 1;
4193 xp->xp_context = EXPAND_USER_VARS;
4194 xp->xp_pattern = arg;
4195 break;
4196
4197 case CMD_function:
4198 case CMD_delfunction:
4199 xp->xp_context = EXPAND_USER_FUNC;
4200 xp->xp_pattern = arg;
4201 break;
4202
4203 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004204 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 break;
4206#endif
4207 case CMD_highlight:
4208 set_context_in_highlight_cmd(xp, arg);
4209 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004210#ifdef FEAT_CSCOPE
4211 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004212 case CMD_lcscope:
4213 case CMD_scscope:
4214 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004215 break;
4216#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004217#ifdef FEAT_SIGNS
4218 case CMD_sign:
4219 set_context_in_sign_cmd(xp, arg);
4220 break;
4221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222#ifdef FEAT_LISTCMDS
4223 case CMD_bdelete:
4224 case CMD_bwipeout:
4225 case CMD_bunload:
4226 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4227 arg = xp->xp_pattern + 1;
4228 /*FALLTHROUGH*/
4229 case CMD_buffer:
4230 case CMD_sbuffer:
4231 case CMD_checktime:
4232 xp->xp_context = EXPAND_BUFFERS;
4233 xp->xp_pattern = arg;
4234 break;
4235#endif
4236#ifdef FEAT_USR_CMDS
4237 case CMD_USER:
4238 case CMD_USER_BUF:
4239 if (compl != EXPAND_NOTHING)
4240 {
4241 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004242 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 {
4244# ifdef FEAT_MENU
4245 if (compl == EXPAND_MENUS)
4246 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4247# endif
4248 if (compl == EXPAND_COMMANDS)
4249 return arg;
4250 if (compl == EXPAND_MAPPINGS)
4251 return set_context_in_map_cmd(xp, (char_u *)"map",
4252 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004253 /* Find start of last argument. */
4254 p = arg;
4255 while (*p)
4256 {
4257 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004258 /* argument starts after a space */
4259 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004260 else if (*p == '\\' && *(p + 1) != NUL)
4261 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004262 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 xp->xp_pattern = arg;
4265 }
4266 xp->xp_context = compl;
4267 }
4268 break;
4269#endif
4270 case CMD_map: case CMD_noremap:
4271 case CMD_nmap: case CMD_nnoremap:
4272 case CMD_vmap: case CMD_vnoremap:
4273 case CMD_omap: case CMD_onoremap:
4274 case CMD_imap: case CMD_inoremap:
4275 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004276 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004277 case CMD_smap: case CMD_snoremap:
4278 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004280 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 case CMD_unmap:
4282 case CMD_nunmap:
4283 case CMD_vunmap:
4284 case CMD_ounmap:
4285 case CMD_iunmap:
4286 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004287 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004288 case CMD_sunmap:
4289 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004291 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 case CMD_abbreviate: case CMD_noreabbrev:
4293 case CMD_cabbrev: case CMD_cnoreabbrev:
4294 case CMD_iabbrev: case CMD_inoreabbrev:
4295 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004296 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 case CMD_unabbreviate:
4298 case CMD_cunabbrev:
4299 case CMD_iunabbrev:
4300 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004301 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302#ifdef FEAT_MENU
4303 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4304 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4305 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4306 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4307 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4308 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4309 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4310 case CMD_tmenu: case CMD_tunmenu:
4311 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4312 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4313#endif
4314
4315 case CMD_colorscheme:
4316 xp->xp_context = EXPAND_COLORS;
4317 xp->xp_pattern = arg;
4318 break;
4319
4320 case CMD_compiler:
4321 xp->xp_context = EXPAND_COMPILER;
4322 xp->xp_pattern = arg;
4323 break;
4324
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004325 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004326 xp->xp_context = EXPAND_OWNSYNTAX;
4327 xp->xp_pattern = arg;
4328 break;
4329
4330 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004331 xp->xp_context = EXPAND_FILETYPE;
4332 xp->xp_pattern = arg;
4333 break;
4334
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004335 case CMD_packadd:
4336 xp->xp_context = EXPAND_PACKADD;
4337 xp->xp_pattern = arg;
4338 break;
4339
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4341 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4342 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004343 p = skiptowhite(arg);
4344 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 {
4346 xp->xp_context = EXPAND_LANGUAGE;
4347 xp->xp_pattern = arg;
4348 }
4349 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004350 {
4351 if ( STRNCMP(arg, "messages", p - arg) == 0
4352 || STRNCMP(arg, "ctype", p - arg) == 0
4353 || STRNCMP(arg, "time", p - arg) == 0)
4354 {
4355 xp->xp_context = EXPAND_LOCALES;
4356 xp->xp_pattern = skipwhite(p);
4357 }
4358 else
4359 xp->xp_context = EXPAND_NOTHING;
4360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 break;
4362#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004363#if defined(FEAT_PROFILE)
4364 case CMD_profile:
4365 set_context_in_profile_cmd(xp, arg);
4366 break;
4367#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004368 case CMD_behave:
4369 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004370 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004371 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004373 case CMD_messages:
4374 xp->xp_context = EXPAND_MESSAGES;
4375 xp->xp_pattern = arg;
4376 break;
4377
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004378#if defined(FEAT_CMDHIST)
4379 case CMD_history:
4380 xp->xp_context = EXPAND_HISTORY;
4381 xp->xp_pattern = arg;
4382 break;
4383#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004384#if defined(FEAT_PROFILE)
4385 case CMD_syntime:
4386 xp->xp_context = EXPAND_SYNTIME;
4387 xp->xp_pattern = arg;
4388 break;
4389#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004390
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391#endif /* FEAT_CMDL_COMPL */
4392
4393 default:
4394 break;
4395 }
4396 return NULL;
4397}
4398
4399/*
4400 * skip a range specifier of the form: addr [,addr] [;addr] ..
4401 *
4402 * Backslashed delimiters after / or ? will be skipped, and commands will
4403 * not be expanded between /'s and ?'s or after "'".
4404 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004405 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 * Returns the "cmd" pointer advanced to beyond the range.
4407 */
4408 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004409skip_range(
4410 char_u *cmd,
4411 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004413 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004415 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004417 if (*cmd == '\\')
4418 {
4419 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4420 ++cmd;
4421 else
4422 break;
4423 }
4424 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 {
4426 if (*++cmd == NUL && ctx != NULL)
4427 *ctx = EXPAND_NOTHING;
4428 }
4429 else if (*cmd == '/' || *cmd == '?')
4430 {
4431 delim = *cmd++;
4432 while (*cmd != NUL && *cmd != delim)
4433 if (*cmd++ == '\\' && *cmd != NUL)
4434 ++cmd;
4435 if (*cmd == NUL && ctx != NULL)
4436 *ctx = EXPAND_NOTHING;
4437 }
4438 if (*cmd != NUL)
4439 ++cmd;
4440 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004441
4442 /* Skip ":" and white space. */
4443 while (*cmd == ':')
4444 cmd = skipwhite(cmd + 1);
4445
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 return cmd;
4447}
4448
4449/*
4450 * get a single EX address
4451 *
4452 * Set ptr to the next character after the part that was interpreted.
4453 * Set ptr to NULL when an error is encountered.
4454 *
4455 * Return MAXLNUM when no Ex address was found.
4456 */
4457 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004458get_address(
4459 exarg_T *eap UNUSED,
4460 char_u **ptr,
4461 int addr_type, /* flag: one of ADDR_LINES, ... */
4462 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004463 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004464 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465{
4466 int c;
4467 int i;
4468 long n;
4469 char_u *cmd;
4470 pos_T pos;
4471 pos_T *fp;
4472 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004473 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474
4475 cmd = skipwhite(*ptr);
4476 lnum = MAXLNUM;
4477 do
4478 {
4479 switch (*cmd)
4480 {
4481 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004482 ++cmd;
4483 switch (addr_type)
4484 {
4485 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 lnum = curwin->w_cursor.lnum;
4487 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004488 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004489 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004490 break;
4491 case ADDR_ARGUMENTS:
4492 lnum = curwin->w_arg_idx + 1;
4493 break;
4494 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004495 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004496 lnum = curbuf->b_fnum;
4497 break;
4498 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004499 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004500 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004501 case ADDR_TABS_RELATIVE:
4502 EMSG(_(e_invrange));
4503 cmd = NULL;
4504 goto error;
4505 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004506#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004507 case ADDR_QUICKFIX:
4508 lnum = qf_get_cur_valid_idx(eap);
4509 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004510#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004511 }
4512 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513
4514 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004515 ++cmd;
4516 switch (addr_type)
4517 {
4518 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 lnum = curbuf->b_ml.ml_line_count;
4520 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004521 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004522 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004523 break;
4524 case ADDR_ARGUMENTS:
4525 lnum = ARGCOUNT;
4526 break;
4527 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004528 buf = lastbuf;
4529 while (buf->b_ml.ml_mfp == NULL)
4530 {
4531 if (buf->b_prev == NULL)
4532 break;
4533 buf = buf->b_prev;
4534 }
4535 lnum = buf->b_fnum;
4536 break;
4537 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004538 lnum = lastbuf->b_fnum;
4539 break;
4540 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004541 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004542 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004543 case ADDR_TABS_RELATIVE:
4544 EMSG(_(e_invrange));
4545 cmd = NULL;
4546 goto error;
4547 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004548#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004549 case ADDR_QUICKFIX:
4550 lnum = qf_get_size(eap);
4551 if (lnum == 0)
4552 lnum = 1;
4553 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004554#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004555 }
4556 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557
4558 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004559 if (*++cmd == NUL)
4560 {
4561 cmd = NULL;
4562 goto error;
4563 }
4564 if (addr_type != ADDR_LINES)
4565 {
4566 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004567 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004568 goto error;
4569 }
4570 if (skip)
4571 ++cmd;
4572 else
4573 {
4574 /* Only accept a mark in another file when it is
4575 * used by itself: ":'M". */
4576 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4577 ++cmd;
4578 if (fp == (pos_T *)-1)
4579 /* Jumped to another file. */
4580 lnum = curwin->w_cursor.lnum;
4581 else
4582 {
4583 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 {
4585 cmd = NULL;
4586 goto error;
4587 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004588 lnum = fp->lnum;
4589 }
4590 }
4591 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
4593 case '/':
4594 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004595 c = *cmd++;
4596 if (addr_type != ADDR_LINES)
4597 {
4598 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004599 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004600 goto error;
4601 }
4602 if (skip) /* skip "/pat/" */
4603 {
4604 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4605 if (*cmd == c)
4606 ++cmd;
4607 }
4608 else
4609 {
4610 pos = curwin->w_cursor; /* save curwin->w_cursor */
4611 /*
4612 * When '/' or '?' follows another address, start
4613 * from there.
4614 */
4615 if (lnum != MAXLNUM)
4616 curwin->w_cursor.lnum = lnum;
4617 /*
4618 * Start a forward search at the end of the line.
4619 * Start a backward search at the start of the line.
4620 * This makes sure we never match in the current
4621 * line, and can match anywhere in the
4622 * next/previous line.
4623 */
4624 if (c == '/')
4625 curwin->w_cursor.col = MAXCOL;
4626 else
4627 curwin->w_cursor.col = 0;
4628 searchcmdlen = 0;
4629 if (!do_search(NULL, c, cmd, 1L,
4630 SEARCH_HIS | SEARCH_MSG, NULL))
4631 {
4632 curwin->w_cursor = pos;
4633 cmd = NULL;
4634 goto error;
4635 }
4636 lnum = curwin->w_cursor.lnum;
4637 curwin->w_cursor = pos;
4638 /* adjust command string pointer */
4639 cmd += searchcmdlen;
4640 }
4641 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642
4643 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004644 ++cmd;
4645 if (addr_type != ADDR_LINES)
4646 {
4647 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004648 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004649 goto error;
4650 }
4651 if (*cmd == '&')
4652 i = RE_SUBST;
4653 else if (*cmd == '?' || *cmd == '/')
4654 i = RE_SEARCH;
4655 else
4656 {
4657 EMSG(_(e_backslash));
4658 cmd = NULL;
4659 goto error;
4660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004662 if (!skip)
4663 {
4664 /*
4665 * When search follows another address, start from
4666 * there.
4667 */
4668 if (lnum != MAXLNUM)
4669 pos.lnum = lnum;
4670 else
4671 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004673 /*
4674 * Start the search just like for the above
4675 * do_search().
4676 */
4677 if (*cmd != '?')
4678 pos.col = MAXCOL;
4679 else
4680 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004681#ifdef FEAT_VIRTUALEDIT
4682 pos.coladd = 0;
4683#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004684 if (searchit(curwin, curbuf, &pos,
4685 *cmd == '?' ? BACKWARD : FORWARD,
4686 (char_u *)"", 1L, SEARCH_MSG,
4687 i, (linenr_T)0, NULL) != FAIL)
4688 lnum = pos.lnum;
4689 else
4690 {
4691 cmd = NULL;
4692 goto error;
4693 }
4694 }
4695 ++cmd;
4696 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697
4698 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004699 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4700 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 }
4702
4703 for (;;)
4704 {
4705 cmd = skipwhite(cmd);
4706 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4707 break;
4708
4709 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004710 {
4711 switch (addr_type)
4712 {
4713 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004714 /* "+1" is same as ".+1" */
4715 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004716 break;
4717 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004718 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004719 break;
4720 case ADDR_ARGUMENTS:
4721 lnum = curwin->w_arg_idx + 1;
4722 break;
4723 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004724 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004725 lnum = curbuf->b_fnum;
4726 break;
4727 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004728 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004729 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004730 case ADDR_TABS_RELATIVE:
4731 lnum = 1;
4732 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004733#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004734 case ADDR_QUICKFIX:
4735 lnum = qf_get_cur_valid_idx(eap);
4736 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004737#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004738 }
4739 }
4740
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 if (VIM_ISDIGIT(*cmd))
4742 i = '+'; /* "number" is same as "+number" */
4743 else
4744 i = *cmd++;
4745 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4746 n = 1;
4747 else
4748 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004749
4750 if (addr_type == ADDR_TABS_RELATIVE)
4751 {
4752 EMSG(_(e_invrange));
4753 cmd = NULL;
4754 goto error;
4755 }
4756 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004757 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004758 lnum = compute_buffer_local_count(
4759 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004761 {
4762#ifdef FEAT_FOLDING
4763 /* Relative line addressing, need to adjust for folded lines
4764 * now, but only do it after the first address. */
4765 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4766 && address_count >= 2)
4767 (void)hasFolding(lnum, NULL, &lnum);
4768#endif
4769 if (i == '-')
4770 lnum -= n;
4771 else
4772 lnum += n;
4773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 }
4775 } while (*cmd == '/' || *cmd == '?');
4776
4777error:
4778 *ptr = cmd;
4779 return lnum;
4780}
4781
4782/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004783 * Get flags from an Ex command argument.
4784 */
4785 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004786get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004787{
4788 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4789 {
4790 if (*eap->arg == 'l')
4791 eap->flags |= EXFLAG_LIST;
4792 else if (*eap->arg == 'p')
4793 eap->flags |= EXFLAG_PRINT;
4794 else
4795 eap->flags |= EXFLAG_NR;
4796 eap->arg = skipwhite(eap->arg + 1);
4797 }
4798}
4799
4800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 * Function called for command which is Not Implemented. NI!
4802 */
4803 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004804ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805{
4806 if (!eap->skip)
4807 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4808}
4809
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004810#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811/*
4812 * Function called for script command which is Not Implemented. NI!
4813 * Skips over ":perl <<EOF" constructs.
4814 */
4815 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004816ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817{
4818 if (!eap->skip)
4819 ex_ni(eap);
4820 else
4821 vim_free(script_get(eap, eap->arg));
4822}
4823#endif
4824
4825/*
4826 * Check range in Ex command for validity.
4827 * Return NULL when valid, error message when invalid.
4828 */
4829 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004830invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004832 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 if ( eap->line1 < 0
4834 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004835 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004837
4838 if (eap->argt & RANGE)
4839 {
4840 switch(eap->addr_type)
4841 {
4842 case ADDR_LINES:
4843 if (!(eap->argt & NOTADR)
4844 && eap->line2 > curbuf->b_ml.ml_line_count
4845#ifdef FEAT_DIFF
4846 + (eap->cmdidx == CMD_diffget)
4847#endif
4848 )
4849 return (char_u *)_(e_invrange);
4850 break;
4851 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004852 /* add 1 if ARGCOUNT is 0 */
4853 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004854 return (char_u *)_(e_invrange);
4855 break;
4856 case ADDR_BUFFERS:
4857 if (eap->line1 < firstbuf->b_fnum
4858 || eap->line2 > lastbuf->b_fnum)
4859 return (char_u *)_(e_invrange);
4860 break;
4861 case ADDR_LOADED_BUFFERS:
4862 buf = firstbuf;
4863 while (buf->b_ml.ml_mfp == NULL)
4864 {
4865 if (buf->b_next == NULL)
4866 return (char_u *)_(e_invrange);
4867 buf = buf->b_next;
4868 }
4869 if (eap->line1 < buf->b_fnum)
4870 return (char_u *)_(e_invrange);
4871 buf = lastbuf;
4872 while (buf->b_ml.ml_mfp == NULL)
4873 {
4874 if (buf->b_prev == NULL)
4875 return (char_u *)_(e_invrange);
4876 buf = buf->b_prev;
4877 }
4878 if (eap->line2 > buf->b_fnum)
4879 return (char_u *)_(e_invrange);
4880 break;
4881 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004882 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004883 return (char_u *)_(e_invrange);
4884 break;
4885 case ADDR_TABS:
4886 if (eap->line2 > LAST_TAB_NR)
4887 return (char_u *)_(e_invrange);
4888 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004889 case ADDR_TABS_RELATIVE:
4890 /* Do nothing */
4891 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004892#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004893 case ADDR_QUICKFIX:
4894 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4895 return (char_u *)_(e_invrange);
4896 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004897#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004898 }
4899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 return NULL;
4901}
4902
4903/*
4904 * Correct the range for zero line number, if required.
4905 */
4906 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004907correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908{
4909 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4910 {
4911 if (eap->line1 == 0)
4912 eap->line1 = 1;
4913 if (eap->line2 == 0)
4914 eap->line2 = 1;
4915 }
4916}
4917
Bram Moolenaar748bf032005-02-02 23:04:36 +00004918#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004919static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004920
4921/*
4922 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4923 * pattern. Otherwise return eap->arg.
4924 */
4925 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004926skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004927{
4928 char_u *p = eap->arg;
4929
Bram Moolenaara37420f2006-02-04 22:37:47 +00004930 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4931 || eap->cmdidx == CMD_vimgrepadd
4932 || eap->cmdidx == CMD_lvimgrepadd
4933 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004934 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004935 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004936 if (p == NULL)
4937 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004938 }
4939 return p;
4940}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004941
4942/*
4943 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4944 * in the command line, so that things like % get expanded.
4945 */
4946 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004947replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004948{
4949 char_u *new_cmdline;
4950 char_u *program;
4951 char_u *pos;
4952 char_u *ptr;
4953 int len;
4954 int i;
4955
4956 /*
4957 * Don't do it when ":vimgrep" is used for ":grep".
4958 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004959 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4960 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4961 || eap->cmdidx == CMD_grepadd
4962 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004963 && !grep_internal(eap->cmdidx))
4964 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004965 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4966 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004967 {
4968 if (*curbuf->b_p_gp == NUL)
4969 program = p_gp;
4970 else
4971 program = curbuf->b_p_gp;
4972 }
4973 else
4974 {
4975 if (*curbuf->b_p_mp == NUL)
4976 program = p_mp;
4977 else
4978 program = curbuf->b_p_mp;
4979 }
4980
4981 p = skipwhite(p);
4982
4983 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4984 {
4985 /* replace $* by given arguments */
4986 i = 1;
4987 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4988 ++i;
4989 len = (int)STRLEN(p);
4990 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4991 if (new_cmdline == NULL)
4992 return NULL; /* out of memory */
4993 ptr = new_cmdline;
4994 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4995 {
4996 i = (int)(pos - program);
4997 STRNCPY(ptr, program, i);
4998 STRCPY(ptr += i, p);
4999 ptr += len;
5000 program = pos + 2;
5001 }
5002 STRCPY(ptr, program);
5003 }
5004 else
5005 {
5006 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
5007 if (new_cmdline == NULL)
5008 return NULL; /* out of memory */
5009 STRCPY(new_cmdline, program);
5010 STRCAT(new_cmdline, " ");
5011 STRCAT(new_cmdline, p);
5012 }
5013 msg_make(p);
5014
5015 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
5016 vim_free(*cmdlinep);
5017 *cmdlinep = new_cmdline;
5018 p = new_cmdline;
5019 }
5020 return p;
5021}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005022#endif
5023
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024/*
5025 * Expand file name in Ex command argument.
5026 * Return FAIL for failure, OK otherwise.
5027 */
5028 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005029expand_filename(
5030 exarg_T *eap,
5031 char_u **cmdlinep,
5032 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033{
5034 int has_wildcards; /* need to expand wildcards */
5035 char_u *repl;
5036 int srclen;
5037 char_u *p;
5038 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005039 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040
Bram Moolenaar748bf032005-02-02 23:04:36 +00005041#ifdef FEAT_QUICKFIX
5042 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
5043 p = skip_grep_pat(eap);
5044#else
5045 p = eap->arg;
5046#endif
5047
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 /*
5049 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5050 * the file name contains a wildcard it should not cause expanding.
5051 * (it will be expanded anyway if there is a wildcard before replacing).
5052 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005053 has_wildcards = mch_has_wildcard(p);
5054 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005056#ifdef FEAT_EVAL
5057 /* Skip over `=expr`, wildcards in it are not expanded. */
5058 if (p[0] == '`' && p[1] == '=')
5059 {
5060 p += 2;
5061 (void)skip_expr(&p);
5062 if (*p == '`')
5063 ++p;
5064 continue;
5065 }
5066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 /*
5068 * Quick check if this cannot be the start of a special string.
5069 * Also removes backslash before '%', '#' and '<'.
5070 */
5071 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5072 {
5073 ++p;
5074 continue;
5075 }
5076
5077 /*
5078 * Try to find a match at this position.
5079 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005080 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5081 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 if (*errormsgp != NULL) /* error detected */
5083 return FAIL;
5084 if (repl == NULL) /* no match found */
5085 {
5086 p += srclen;
5087 continue;
5088 }
5089
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005090 /* Wildcards won't be expanded below, the replacement is taken
5091 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5092 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5093 {
5094 char_u *l = repl;
5095
5096 repl = expand_env_save(repl);
5097 vim_free(l);
5098 }
5099
Bram Moolenaar63b92542007-03-27 14:57:09 +00005100 /* Need to escape white space et al. with a backslash.
5101 * Don't do this for:
5102 * - replacement that already has been escaped: "##"
5103 * - shell commands (may have to use quotes instead).
5104 * - non-unix systems when there is a single argument (spaces don't
5105 * separate arguments then).
5106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005108 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 && eap->cmdidx != CMD_bang
5110 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00005111 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00005113 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00005115 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116#ifndef UNIX
5117 && !(eap->argt & NOSPC)
5118#endif
5119 )
5120 {
5121 char_u *l;
5122#ifdef BACKSLASH_IN_FILENAME
5123 /* Don't escape a backslash here, because rem_backslash() doesn't
5124 * remove it later. */
5125 static char_u *nobslash = (char_u *)" \t\"|";
5126# define ESCAPE_CHARS nobslash
5127#else
5128# define ESCAPE_CHARS escape_chars
5129#endif
5130
5131 for (l = repl; *l; ++l)
5132 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5133 {
5134 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5135 if (l != NULL)
5136 {
5137 vim_free(repl);
5138 repl = l;
5139 }
5140 break;
5141 }
5142 }
5143
5144 /* For a shell command a '!' must be escaped. */
5145 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005146 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
5148 char_u *l;
5149
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005150 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 if (l != NULL)
5152 {
5153 vim_free(repl);
5154 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 }
5156 }
5157
5158 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5159 vim_free(repl);
5160 if (p == NULL)
5161 return FAIL;
5162 }
5163
5164 /*
5165 * One file argument: Expand wildcards.
5166 * Don't do this with ":r !command" or ":w !command".
5167 */
5168 if ((eap->argt & NOSPC) && !eap->usefilter)
5169 {
5170 /*
5171 * May do this twice:
5172 * 1. Replace environment variables.
5173 * 2. Replace any other wildcards, remove backslashes.
5174 */
5175 for (n = 1; n <= 2; ++n)
5176 {
5177 if (n == 2)
5178 {
5179#ifdef UNIX
5180 /*
5181 * Only for Unix we check for more than one file name.
5182 * For other systems spaces are considered to be part
5183 * of the file name.
5184 * Only check here if there is no wildcard, otherwise
5185 * ExpandOne() will check for errors. This allows
5186 * ":e `ls ve*.c`" on Unix.
5187 */
5188 if (!has_wildcards)
5189 for (p = eap->arg; *p; ++p)
5190 {
5191 /* skip escaped characters */
5192 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5193 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005194 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 {
5196 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5197 return FAIL;
5198 }
5199 }
5200#endif
5201
5202 /*
5203 * Halve the number of backslashes (this is Vi compatible).
5204 * For Unix and OS/2, when wildcards are expanded, this is
5205 * done by ExpandOne() below.
5206 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005207#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 if (!has_wildcards)
5209#endif
5210 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
5212
5213 if (has_wildcards)
5214 {
5215 if (n == 1)
5216 {
5217 /*
5218 * First loop: May expand environment variables. This
5219 * can be done much faster with expand_env() than with
5220 * something else (e.g., calling a shell).
5221 * After expanding environment variables, check again
5222 * if there are still wildcards present.
5223 */
5224 if (vim_strchr(eap->arg, '$') != NULL
5225 || vim_strchr(eap->arg, '~') != NULL)
5226 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005227 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005228 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 has_wildcards = mch_has_wildcard(NameBuff);
5230 p = NameBuff;
5231 }
5232 else
5233 p = NULL;
5234 }
5235 else /* n == 2 */
5236 {
5237 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005238 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
5240 ExpandInit(&xpc);
5241 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005242 if (p_wic)
5243 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005245 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 if (p == NULL)
5247 return FAIL;
5248 }
5249 if (p != NULL)
5250 {
5251 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5252 p, cmdlinep);
5253 if (n == 2) /* p came from ExpandOne() */
5254 vim_free(p);
5255 }
5256 }
5257 }
5258 }
5259 return OK;
5260}
5261
5262/*
5263 * Replace part of the command line, keeping eap->cmd, eap->arg and
5264 * eap->nextcmd correct.
5265 * "src" points to the part that is to be replaced, of length "srclen".
5266 * "repl" is the replacement string.
5267 * Returns a pointer to the character after the replaced string.
5268 * Returns NULL for failure.
5269 */
5270 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005271repl_cmdline(
5272 exarg_T *eap,
5273 char_u *src,
5274 int srclen,
5275 char_u *repl,
5276 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277{
5278 int len;
5279 int i;
5280 char_u *new_cmdline;
5281
5282 /*
5283 * The new command line is build in new_cmdline[].
5284 * First allocate it.
5285 * Careful: a "+cmd" argument may have been NUL terminated.
5286 */
5287 len = (int)STRLEN(repl);
5288 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005289 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5291 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5292 return NULL; /* out of memory! */
5293
5294 /*
5295 * Copy the stuff before the expanded part.
5296 * Copy the expanded stuff.
5297 * Copy what came after the expanded part.
5298 * Copy the next commands, if there are any.
5299 */
5300 i = (int)(src - *cmdlinep); /* length of part before match */
5301 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005302
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 mch_memmove(new_cmdline + i, repl, (size_t)len);
5304 i += len; /* remember the end of the string */
5305 STRCPY(new_cmdline + i, src + srclen);
5306 src = new_cmdline + i; /* remember where to continue */
5307
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005308 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 {
5310 i = (int)STRLEN(new_cmdline) + 1;
5311 STRCPY(new_cmdline + i, eap->nextcmd);
5312 eap->nextcmd = new_cmdline + i;
5313 }
5314 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5315 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5316 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5317 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5318 vim_free(*cmdlinep);
5319 *cmdlinep = new_cmdline;
5320
5321 return src;
5322}
5323
5324/*
5325 * Check for '|' to separate commands and '"' to start comments.
5326 */
5327 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005328separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329{
5330 char_u *p;
5331
Bram Moolenaar86b68352004-12-27 21:59:20 +00005332#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005333 p = skip_grep_pat(eap);
5334#else
5335 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005336#endif
5337
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005338 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 {
5340 if (*p == Ctrl_V)
5341 {
5342 if (eap->argt & (USECTRLV | XFILE))
5343 ++p; /* skip CTRL-V and next char */
5344 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005345 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005346 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 if (*p == NUL) /* stop at NUL after CTRL-V */
5348 break;
5349 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005350
5351#ifdef FEAT_EVAL
5352 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005353 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005354 {
5355 p += 2;
5356 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005357 }
5358#endif
5359
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 /* Check for '"': start of comment or '|': next command */
5361 /* :@" and :*" do not start a comment!
5362 * :redir @" doesn't either. */
5363 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5364 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5365 || p != eap->arg)
5366 && (eap->cmdidx != CMD_redir
5367 || p != eap->arg + 1 || p[-1] != '@'))
5368 || *p == '|' || *p == '\n')
5369 {
5370 /*
5371 * We remove the '\' before the '|', unless USECTRLV is used
5372 * AND 'b' is present in 'cpoptions'.
5373 */
5374 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5375 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5376 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005377 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 --p;
5379 }
5380 else
5381 {
5382 eap->nextcmd = check_nextcmd(p);
5383 *p = NUL;
5384 break;
5385 }
5386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005388
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5390 del_trailing_spaces(eap->arg);
5391}
5392
5393/*
5394 * get + command from ex argument
5395 */
5396 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005397getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398{
5399 char_u *arg = *argp;
5400 char_u *command = NULL;
5401
5402 if (*arg == '+') /* +[command] */
5403 {
5404 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005405 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 command = dollar_command;
5407 else
5408 {
5409 command = arg;
5410 arg = skip_cmd_arg(command, TRUE);
5411 if (*arg != NUL)
5412 *arg++ = NUL; /* terminate command with NUL */
5413 }
5414
5415 arg = skipwhite(arg); /* skip over spaces */
5416 *argp = arg;
5417 }
5418 return command;
5419}
5420
5421/*
5422 * Find end of "+command" argument. Skip over "\ " and "\\".
5423 */
5424 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005425skip_cmd_arg(
5426 char_u *p,
5427 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428{
5429 while (*p && !vim_isspace(*p))
5430 {
5431 if (*p == '\\' && p[1] != NUL)
5432 {
5433 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005434 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 else
5436 ++p;
5437 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005438 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 }
5440 return p;
5441}
5442
5443/*
5444 * Get "++opt=arg" argument.
5445 * Return FAIL or OK.
5446 */
5447 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005448getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449{
5450 char_u *arg = eap->arg + 2;
5451 int *pp = NULL;
5452#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005453 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 char_u *p;
5455#endif
5456
5457 /* ":edit ++[no]bin[ary] file" */
5458 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5459 {
5460 if (*arg == 'n')
5461 {
5462 arg += 2;
5463 eap->force_bin = FORCE_NOBIN;
5464 }
5465 else
5466 eap->force_bin = FORCE_BIN;
5467 if (!checkforcmd(&arg, "binary", 3))
5468 return FAIL;
5469 eap->arg = skipwhite(arg);
5470 return OK;
5471 }
5472
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005473 /* ":read ++edit file" */
5474 if (STRNCMP(arg, "edit", 4) == 0)
5475 {
5476 eap->read_edit = TRUE;
5477 eap->arg = skipwhite(arg + 4);
5478 return OK;
5479 }
5480
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 if (STRNCMP(arg, "ff", 2) == 0)
5482 {
5483 arg += 2;
5484 pp = &eap->force_ff;
5485 }
5486 else if (STRNCMP(arg, "fileformat", 10) == 0)
5487 {
5488 arg += 10;
5489 pp = &eap->force_ff;
5490 }
5491#ifdef FEAT_MBYTE
5492 else if (STRNCMP(arg, "enc", 3) == 0)
5493 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005494 if (STRNCMP(arg, "encoding", 8) == 0)
5495 arg += 8;
5496 else
5497 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 pp = &eap->force_enc;
5499 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005500 else if (STRNCMP(arg, "bad", 3) == 0)
5501 {
5502 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005503 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505#endif
5506
5507 if (pp == NULL || *arg != '=')
5508 return FAIL;
5509
5510 ++arg;
5511 *pp = (int)(arg - eap->cmd);
5512 arg = skip_cmd_arg(arg, FALSE);
5513 eap->arg = skipwhite(arg);
5514 *arg = NUL;
5515
5516#ifdef FEAT_MBYTE
5517 if (pp == &eap->force_ff)
5518 {
5519#endif
5520 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5521 return FAIL;
5522#ifdef FEAT_MBYTE
5523 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005524 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 {
5526 /* Make 'fileencoding' lower case. */
5527 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5528 *p = TOLOWER_ASC(*p);
5529 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005530 else
5531 {
5532 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5533 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005534 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005535 if (STRICMP(p, "keep") == 0)
5536 eap->bad_char = BAD_KEEP;
5537 else if (STRICMP(p, "drop") == 0)
5538 eap->bad_char = BAD_DROP;
5539 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5540 eap->bad_char = *p;
5541 else
5542 return FAIL;
5543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544#endif
5545
5546 return OK;
5547}
5548
5549/*
5550 * ":abbreviate" and friends.
5551 */
5552 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005553ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
5555 do_exmap(eap, TRUE); /* almost the same as mapping */
5556}
5557
5558/*
5559 * ":map" and friends.
5560 */
5561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005562ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563{
5564 /*
5565 * If we are sourcing .exrc or .vimrc in current directory we
5566 * print the mappings for security reasons.
5567 */
5568 if (secure)
5569 {
5570 secure = 2;
5571 msg_outtrans(eap->cmd);
5572 msg_putchar('\n');
5573 }
5574 do_exmap(eap, FALSE);
5575}
5576
5577/*
5578 * ":unmap" and friends.
5579 */
5580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005581ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
5583 do_exmap(eap, FALSE);
5584}
5585
5586/*
5587 * ":mapclear" and friends.
5588 */
5589 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005590ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
5592 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5593}
5594
5595/*
5596 * ":abclear" and friends.
5597 */
5598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005599ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600{
5601 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5602}
5603
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005604#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005606ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607{
5608 /*
5609 * Disallow auto commands from .exrc and .vimrc in current
5610 * directory for security reasons.
5611 */
5612 if (secure)
5613 {
5614 secure = 2;
5615 eap->errmsg = e_curdir;
5616 }
5617 else if (eap->cmdidx == CMD_autocmd)
5618 do_autocmd(eap->arg, eap->forceit);
5619 else
5620 do_augroup(eap->arg, eap->forceit);
5621}
5622
5623/*
5624 * ":doautocmd": Apply the automatic commands to the current buffer.
5625 */
5626 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005627ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005629 char_u *arg = eap->arg;
5630 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005631 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005632
Bram Moolenaar1610d052016-06-09 22:53:01 +02005633 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5634 /* Only when there is no <nomodeline>. */
5635 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005636 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637}
5638#endif
5639
5640#ifdef FEAT_LISTCMDS
5641/*
5642 * :[N]bunload[!] [N] [bufname] unload buffer
5643 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5644 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5645 */
5646 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005647ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648{
5649 eap->errmsg = do_bufdel(
5650 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5651 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5652 : DOBUF_UNLOAD, eap->arg,
5653 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5654}
5655
5656/*
5657 * :[N]buffer [N] to buffer N
5658 * :[N]sbuffer [N] to buffer N
5659 */
5660 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005661ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662{
5663 if (*eap->arg)
5664 eap->errmsg = e_trailing;
5665 else
5666 {
5667 if (eap->addr_count == 0) /* default is current buffer */
5668 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5669 else
5670 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005671 if (eap->do_ecmd_cmd != NULL)
5672 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
5674}
5675
5676/*
5677 * :[N]bmodified [N] to next mod. buffer
5678 * :[N]sbmodified [N] to next mod. buffer
5679 */
5680 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005681ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682{
5683 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005684 if (eap->do_ecmd_cmd != NULL)
5685 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686}
5687
5688/*
5689 * :[N]bnext [N] to next buffer
5690 * :[N]sbnext [N] split and to next buffer
5691 */
5692 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005693ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694{
5695 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005696 if (eap->do_ecmd_cmd != NULL)
5697 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698}
5699
5700/*
5701 * :[N]bNext [N] to previous buffer
5702 * :[N]bprevious [N] to previous buffer
5703 * :[N]sbNext [N] split and to previous buffer
5704 * :[N]sbprevious [N] split and to previous buffer
5705 */
5706 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005707ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708{
5709 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005710 if (eap->do_ecmd_cmd != NULL)
5711 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712}
5713
5714/*
5715 * :brewind to first buffer
5716 * :bfirst to first buffer
5717 * :sbrewind split and to first buffer
5718 * :sbfirst split and to first buffer
5719 */
5720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005721ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
5723 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005724 if (eap->do_ecmd_cmd != NULL)
5725 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726}
5727
5728/*
5729 * :blast to last buffer
5730 * :sblast split and to last buffer
5731 */
5732 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005733ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734{
5735 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005736 if (eap->do_ecmd_cmd != NULL)
5737 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738}
5739#endif
5740
5741 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005742ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743{
5744 return (c == NUL || c == '|' || c == '"' || c == '\n');
5745}
5746
5747#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5748 || defined(PROTO)
5749/*
5750 * Return the next command, after the first '|' or '\n'.
5751 * Return NULL if not found.
5752 */
5753 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005754find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
5756 while (*p != '|' && *p != '\n')
5757 {
5758 if (*p == NUL)
5759 return NULL;
5760 ++p;
5761 }
5762 return (p + 1);
5763}
5764#endif
5765
5766/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005767 * Check if *p is a separator between Ex commands, skipping over white space.
5768 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 */
5770 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005771check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005773 char_u *s = skipwhite(p);
5774
5775 if (*s == '|' || *s == '\n')
5776 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 else
5778 return NULL;
5779}
5780
5781/*
5782 * - if there are more files to edit
5783 * - and this is the last window
5784 * - and forceit not used
5785 * - and not repeated twice on a row
5786 * return FAIL and give error message if 'message' TRUE
5787 * return OK otherwise
5788 */
5789 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005790check_more(
5791 int message, /* when FALSE check only, no messages */
5792 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793{
5794 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5795
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005796 if (!forceit && only_one_window()
5797 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 {
5799 if (message)
5800 {
5801#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5802 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5803 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005804 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805
5806 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005807 vim_strncpy(buff,
5808 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005809 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005811 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 _("%d more files to edit. Quit anyway?"), n);
5813 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5814 return OK;
5815 return FAIL;
5816 }
5817#endif
5818 if (n == 1)
5819 EMSG(_("E173: 1 more file to edit"));
5820 else
5821 EMSGN(_("E173: %ld more files to edit"), n);
5822 quitmore = 2; /* next try to quit is allowed */
5823 }
5824 return FAIL;
5825 }
5826 return OK;
5827}
5828
5829#ifdef FEAT_CMDL_COMPL
5830/*
5831 * Function given to ExpandGeneric() to obtain the list of command names.
5832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005834get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835{
5836 if (idx >= (int)CMD_SIZE)
5837# ifdef FEAT_USR_CMDS
5838 return get_user_command_name(idx);
5839# else
5840 return NULL;
5841# endif
5842 return cmdnames[idx].cmd_name;
5843}
5844#endif
5845
5846#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005847static 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);
5848static void uc_list(char_u *name, size_t name_len);
5849static 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);
5850static char_u *uc_split_args(char_u *arg, size_t *lenp);
5851static 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 +00005852
5853 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005854uc_add_command(
5855 char_u *name,
5856 size_t name_len,
5857 char_u *rep,
5858 long argt,
5859 long def,
5860 int flags,
5861 int compl,
5862 char_u *compl_arg,
5863 int addr_type,
5864 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865{
5866 ucmd_T *cmd = NULL;
5867 char_u *p;
5868 int i;
5869 int cmp = 1;
5870 char_u *rep_buf = NULL;
5871 garray_T *gap;
5872
Bram Moolenaar9c102382006-05-03 21:26:49 +00005873 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 if (rep_buf == NULL)
5875 {
5876 /* Can't replace termcodes - try using the string as is */
5877 rep_buf = vim_strsave(rep);
5878
5879 /* Give up if out of memory */
5880 if (rep_buf == NULL)
5881 return FAIL;
5882 }
5883
5884 /* get address of growarray: global or in curbuf */
5885 if (flags & UC_BUFFER)
5886 {
5887 gap = &curbuf->b_ucmds;
5888 if (gap->ga_itemsize == 0)
5889 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5890 }
5891 else
5892 gap = &ucmds;
5893
5894 /* Search for the command in the already defined commands. */
5895 for (i = 0; i < gap->ga_len; ++i)
5896 {
5897 size_t len;
5898
5899 cmd = USER_CMD_GA(gap, i);
5900 len = STRLEN(cmd->uc_name);
5901 cmp = STRNCMP(name, cmd->uc_name, name_len);
5902 if (cmp == 0)
5903 {
5904 if (name_len < len)
5905 cmp = -1;
5906 else if (name_len > len)
5907 cmp = 1;
5908 }
5909
5910 if (cmp == 0)
5911 {
5912 if (!force)
5913 {
5914 EMSG(_("E174: Command already exists: add ! to replace it"));
5915 goto fail;
5916 }
5917
5918 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005919 cmd->uc_rep = NULL;
5920#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5921 vim_free(cmd->uc_compl_arg);
5922 cmd->uc_compl_arg = NULL;
5923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 break;
5925 }
5926
5927 /* Stop as soon as we pass the name to add */
5928 if (cmp < 0)
5929 break;
5930 }
5931
5932 /* Extend the array unless we're replacing an existing command */
5933 if (cmp != 0)
5934 {
5935 if (ga_grow(gap, 1) != OK)
5936 goto fail;
5937 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5938 goto fail;
5939
5940 cmd = USER_CMD_GA(gap, i);
5941 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5942
5943 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944
5945 cmd->uc_name = p;
5946 }
5947
5948 cmd->uc_rep = rep_buf;
5949 cmd->uc_argt = argt;
5950 cmd->uc_def = def;
5951 cmd->uc_compl = compl;
5952#ifdef FEAT_EVAL
5953 cmd->uc_scriptID = current_SID;
5954# ifdef FEAT_CMDL_COMPL
5955 cmd->uc_compl_arg = compl_arg;
5956# endif
5957#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005958 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959
5960 return OK;
5961
5962fail:
5963 vim_free(rep_buf);
5964#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5965 vim_free(compl_arg);
5966#endif
5967 return FAIL;
5968}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005971#if defined(FEAT_USR_CMDS)
5972static struct
5973{
5974 int expand;
5975 char *name;
5976} addr_type_complete[] =
5977{
5978 {ADDR_ARGUMENTS, "arguments"},
5979 {ADDR_LINES, "lines"},
5980 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5981 {ADDR_TABS, "tabs"},
5982 {ADDR_BUFFERS, "buffers"},
5983 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005984 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005985 {-1, NULL}
5986};
5987#endif
5988
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005989#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990/*
5991 * List of names for completion for ":command" with the EXPAND_ flag.
5992 * Must be alphabetical for completion.
5993 */
5994static struct
5995{
5996 int expand;
5997 char *name;
5998} command_complete[] =
5999{
6000 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02006001 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02006003 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02006005 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00006006#if defined(FEAT_CSCOPE)
6007 {EXPAND_CSCOPE, "cscope"},
6008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6010 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00006011 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012#endif
6013 {EXPAND_DIRECTORIES, "dir"},
6014 {EXPAND_ENV_VARS, "environment"},
6015 {EXPAND_EVENTS, "event"},
6016 {EXPAND_EXPRESSION, "expression"},
6017 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02006018 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02006019 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 {EXPAND_FUNCTIONS, "function"},
6021 {EXPAND_HELP, "help"},
6022 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02006023#if defined(FEAT_CMDHIST)
6024 {EXPAND_HISTORY, "history"},
6025#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02006026#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02006027 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02006028 {EXPAND_LOCALES, "locale"},
6029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 {EXPAND_MAPPINGS, "mapping"},
6031 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02006032 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02006033 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006034#if defined(FEAT_PROFILE)
6035 {EXPAND_SYNTIME, "syntime"},
6036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01006038 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006039 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00006040#if defined(FEAT_SIGNS)
6041 {EXPAND_SIGN, "sign"},
6042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 {EXPAND_TAGS, "tag"},
6044 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02006045 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 {EXPAND_USER_VARS, "var"},
6047 {0, NULL}
6048};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006051#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006053uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
6055 int i, j;
6056 int found = FALSE;
6057 ucmd_T *cmd;
6058 int len;
6059 long a;
6060 garray_T *gap;
6061
6062 gap = &curbuf->b_ucmds;
6063 for (;;)
6064 {
6065 for (i = 0; i < gap->ga_len; ++i)
6066 {
6067 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006068 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069
Bram Moolenaard29459b2016-08-26 22:29:11 +02006070 /* Skip commands which don't match the requested prefix and
6071 * commands filtered out. */
6072 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6073 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 continue;
6075
6076 /* Put out the title first time */
6077 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006078 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 found = TRUE;
6080 msg_putchar('\n');
6081 if (got_int)
6082 break;
6083
6084 /* Special cases */
6085 msg_putchar(a & BANG ? '!' : ' ');
6086 msg_putchar(a & REGSTR ? '"' : ' ');
6087 msg_putchar(gap != &ucmds ? 'b' : ' ');
6088 msg_putchar(' ');
6089
Bram Moolenaar8820b482017-03-16 17:23:31 +01006090 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 len = (int)STRLEN(cmd->uc_name) + 4;
6092
6093 do {
6094 msg_putchar(' ');
6095 ++len;
6096 } while (len < 16);
6097
6098 len = 0;
6099
6100 /* Arguments */
6101 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6102 {
6103 case 0: IObuff[len++] = '0'; break;
6104 case (EXTRA): IObuff[len++] = '*'; break;
6105 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6106 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6107 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6108 }
6109
6110 do {
6111 IObuff[len++] = ' ';
6112 } while (len < 5);
6113
6114 /* Range */
6115 if (a & (RANGE|COUNT))
6116 {
6117 if (a & COUNT)
6118 {
6119 /* -count=N */
6120 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6121 len += (int)STRLEN(IObuff + len);
6122 }
6123 else if (a & DFLALL)
6124 IObuff[len++] = '%';
6125 else if (cmd->uc_def >= 0)
6126 {
6127 /* -range=N */
6128 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6129 len += (int)STRLEN(IObuff + len);
6130 }
6131 else
6132 IObuff[len++] = '.';
6133 }
6134
6135 do {
6136 IObuff[len++] = ' ';
6137 } while (len < 11);
6138
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006139 /* Address Type */
6140 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6141 if (addr_type_complete[j].expand != ADDR_LINES
6142 && addr_type_complete[j].expand == cmd->uc_addr_type)
6143 {
6144 STRCPY(IObuff + len, addr_type_complete[j].name);
6145 len += (int)STRLEN(IObuff + len);
6146 break;
6147 }
6148
6149 do {
6150 IObuff[len++] = ' ';
6151 } while (len < 21);
6152
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 /* Completion */
6154 for (j = 0; command_complete[j].expand != 0; ++j)
6155 if (command_complete[j].expand == cmd->uc_compl)
6156 {
6157 STRCPY(IObuff + len, command_complete[j].name);
6158 len += (int)STRLEN(IObuff + len);
6159 break;
6160 }
6161
6162 do {
6163 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006164 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165
6166 IObuff[len] = '\0';
6167 msg_outtrans(IObuff);
6168
6169 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006170#ifdef FEAT_EVAL
6171 if (p_verbose > 0)
6172 last_set_msg(cmd->uc_scriptID);
6173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 out_flush();
6175 ui_breakcheck();
6176 if (got_int)
6177 break;
6178 }
6179 if (gap == &ucmds || i < gap->ga_len)
6180 break;
6181 gap = &ucmds;
6182 }
6183
6184 if (!found)
6185 MSG(_("No user-defined commands found"));
6186}
6187
6188 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006189uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190{
6191 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6192 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6193 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6194 0xb9, 0x7f, 0};
6195 int i;
6196
6197 for (i = 0; fcmd[i]; ++i)
6198 IObuff[i] = fcmd[i] - 0x40;
6199 IObuff[i] = 0;
6200 return IObuff;
6201}
6202
6203 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006204uc_scan_attr(
6205 char_u *attr,
6206 size_t len,
6207 long *argt,
6208 long *def,
6209 int *flags,
6210 int *compl,
6211 char_u **compl_arg,
6212 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213{
6214 char_u *p;
6215
6216 if (len == 0)
6217 {
6218 EMSG(_("E175: No attribute specified"));
6219 return FAIL;
6220 }
6221
6222 /* First, try the simple attributes (no arguments) */
6223 if (STRNICMP(attr, "bang", len) == 0)
6224 *argt |= BANG;
6225 else if (STRNICMP(attr, "buffer", len) == 0)
6226 *flags |= UC_BUFFER;
6227 else if (STRNICMP(attr, "register", len) == 0)
6228 *argt |= REGSTR;
6229 else if (STRNICMP(attr, "bar", len) == 0)
6230 *argt |= TRLBAR;
6231 else
6232 {
6233 int i;
6234 char_u *val = NULL;
6235 size_t vallen = 0;
6236 size_t attrlen = len;
6237
6238 /* Look for the attribute name - which is the part before any '=' */
6239 for (i = 0; i < (int)len; ++i)
6240 {
6241 if (attr[i] == '=')
6242 {
6243 val = &attr[i + 1];
6244 vallen = len - i - 1;
6245 attrlen = i;
6246 break;
6247 }
6248 }
6249
6250 if (STRNICMP(attr, "nargs", attrlen) == 0)
6251 {
6252 if (vallen == 1)
6253 {
6254 if (*val == '0')
6255 /* Do nothing - this is the default */;
6256 else if (*val == '1')
6257 *argt |= (EXTRA | NOSPC | NEEDARG);
6258 else if (*val == '*')
6259 *argt |= EXTRA;
6260 else if (*val == '?')
6261 *argt |= (EXTRA | NOSPC);
6262 else if (*val == '+')
6263 *argt |= (EXTRA | NEEDARG);
6264 else
6265 goto wrong_nargs;
6266 }
6267 else
6268 {
6269wrong_nargs:
6270 EMSG(_("E176: Invalid number of arguments"));
6271 return FAIL;
6272 }
6273 }
6274 else if (STRNICMP(attr, "range", attrlen) == 0)
6275 {
6276 *argt |= RANGE;
6277 if (vallen == 1 && *val == '%')
6278 *argt |= DFLALL;
6279 else if (val != NULL)
6280 {
6281 p = val;
6282 if (*def >= 0)
6283 {
6284two_count:
6285 EMSG(_("E177: Count cannot be specified twice"));
6286 return FAIL;
6287 }
6288
6289 *def = getdigits(&p);
6290 *argt |= (ZEROR | NOTADR);
6291
6292 if (p != val + vallen || vallen == 0)
6293 {
6294invalid_count:
6295 EMSG(_("E178: Invalid default value for count"));
6296 return FAIL;
6297 }
6298 }
6299 }
6300 else if (STRNICMP(attr, "count", attrlen) == 0)
6301 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006302 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303
6304 if (val != NULL)
6305 {
6306 p = val;
6307 if (*def >= 0)
6308 goto two_count;
6309
6310 *def = getdigits(&p);
6311
6312 if (p != val + vallen)
6313 goto invalid_count;
6314 }
6315
6316 if (*def < 0)
6317 *def = 0;
6318 }
6319 else if (STRNICMP(attr, "complete", attrlen) == 0)
6320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 if (val == NULL)
6322 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006323 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 return FAIL;
6325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006327 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6328 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006331 else if (STRNICMP(attr, "addr", attrlen) == 0)
6332 {
6333 *argt |= RANGE;
6334 if (val == NULL)
6335 {
6336 EMSG(_("E179: argument required for -addr"));
6337 return FAIL;
6338 }
6339 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6340 == FAIL)
6341 return FAIL;
6342 if (addr_type_arg != ADDR_LINES)
6343 *argt |= (ZEROR | NOTADR) ;
6344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 else
6346 {
6347 char_u ch = attr[len];
6348 attr[len] = '\0';
6349 EMSG2(_("E181: Invalid attribute: %s"), attr);
6350 attr[len] = ch;
6351 return FAIL;
6352 }
6353 }
6354
6355 return OK;
6356}
6357
Bram Moolenaara850a712009-01-28 14:42:59 +00006358/*
6359 * ":command ..."
6360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006362ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363{
6364 char_u *name;
6365 char_u *end;
6366 char_u *p;
6367 long argt = 0;
6368 long def = -1;
6369 int flags = 0;
6370 int compl = EXPAND_NOTHING;
6371 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006372 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006374 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375
6376 p = eap->arg;
6377
6378 /* Check for attributes */
6379 while (*p == '-')
6380 {
6381 ++p;
6382 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006383 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 == FAIL)
6385 return;
6386 p = skipwhite(end);
6387 }
6388
6389 /* Get the name (if any) and skip to the following argument */
6390 name = p;
6391 if (ASCII_ISALPHA(*p))
6392 while (ASCII_ISALNUM(*p))
6393 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006394 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 {
6396 EMSG(_("E182: Invalid command name"));
6397 return;
6398 }
6399 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006400 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401
6402 /* If there is nothing after the name, and no attributes were specified,
6403 * we are listing commands
6404 */
6405 p = skipwhite(end);
6406 if (!has_attr && ends_excmd(*p))
6407 {
6408 uc_list(name, end - name);
6409 }
6410 else if (!ASCII_ISUPPER(*name))
6411 {
6412 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6413 return;
6414 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006415 else if ((name_len == 1 && *name == 'X')
6416 || (name_len <= 4
6417 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6418 {
6419 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6420 return;
6421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 else
6423 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006424 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425}
6426
6427/*
6428 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 * Clear all user commands, global and for current buffer.
6430 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006431 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006432ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433{
6434 uc_clear(&ucmds);
6435 uc_clear(&curbuf->b_ucmds);
6436}
6437
6438/*
6439 * Clear all user commands for "gap".
6440 */
6441 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006442uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
6444 int i;
6445 ucmd_T *cmd;
6446
6447 for (i = 0; i < gap->ga_len; ++i)
6448 {
6449 cmd = USER_CMD_GA(gap, i);
6450 vim_free(cmd->uc_name);
6451 vim_free(cmd->uc_rep);
6452# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6453 vim_free(cmd->uc_compl_arg);
6454# endif
6455 }
6456 ga_clear(gap);
6457}
6458
6459 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006460ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461{
6462 int i = 0;
6463 ucmd_T *cmd = NULL;
6464 int cmp = -1;
6465 garray_T *gap;
6466
6467 gap = &curbuf->b_ucmds;
6468 for (;;)
6469 {
6470 for (i = 0; i < gap->ga_len; ++i)
6471 {
6472 cmd = USER_CMD_GA(gap, i);
6473 cmp = STRCMP(eap->arg, cmd->uc_name);
6474 if (cmp <= 0)
6475 break;
6476 }
6477 if (gap == &ucmds || cmp == 0)
6478 break;
6479 gap = &ucmds;
6480 }
6481
6482 if (cmp != 0)
6483 {
6484 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6485 return;
6486 }
6487
6488 vim_free(cmd->uc_name);
6489 vim_free(cmd->uc_rep);
6490# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6491 vim_free(cmd->uc_compl_arg);
6492# endif
6493
6494 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495
6496 if (i < gap->ga_len)
6497 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6498}
6499
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006500/*
6501 * split and quote args for <f-args>
6502 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006504uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505{
6506 char_u *buf;
6507 char_u *p;
6508 char_u *q;
6509 int len;
6510
6511 /* Precalculate length */
6512 p = arg;
6513 len = 2; /* Initial and final quotes */
6514
6515 while (*p)
6516 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006517 if (p[0] == '\\' && p[1] == '\\')
6518 {
6519 len += 2;
6520 p += 2;
6521 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006522 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 {
6524 len += 1;
6525 p += 2;
6526 }
6527 else if (*p == '\\' || *p == '"')
6528 {
6529 len += 2;
6530 p += 1;
6531 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006532 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
6534 p = skipwhite(p);
6535 if (*p == NUL)
6536 break;
6537 len += 3; /* "," */
6538 }
6539 else
6540 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006541#ifdef FEAT_MBYTE
6542 int charlen = (*mb_ptr2len)(p);
6543 len += charlen;
6544 p += charlen;
6545#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 ++len;
6547 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 }
6550 }
6551
6552 buf = alloc(len + 1);
6553 if (buf == NULL)
6554 {
6555 *lenp = 0;
6556 return buf;
6557 }
6558
6559 p = arg;
6560 q = buf;
6561 *q++ = '"';
6562 while (*p)
6563 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006564 if (p[0] == '\\' && p[1] == '\\')
6565 {
6566 *q++ = '\\';
6567 *q++ = '\\';
6568 p += 2;
6569 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006570 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 {
6572 *q++ = p[1];
6573 p += 2;
6574 }
6575 else if (*p == '\\' || *p == '"')
6576 {
6577 *q++ = '\\';
6578 *q++ = *p++;
6579 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006580 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 {
6582 p = skipwhite(p);
6583 if (*p == NUL)
6584 break;
6585 *q++ = '"';
6586 *q++ = ',';
6587 *q++ = '"';
6588 }
6589 else
6590 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006591 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 }
6593 }
6594 *q++ = '"';
6595 *q = 0;
6596
6597 *lenp = len;
6598 return buf;
6599}
6600
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006601 static size_t
6602add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6603{
6604 size_t result;
6605
6606 result = STRLEN(mod_str);
6607 if (*multi_mods)
6608 result += 1;
6609 if (buf != NULL)
6610 {
6611 if (*multi_mods)
6612 STRCAT(buf, " ");
6613 STRCAT(buf, mod_str);
6614 }
6615
6616 *multi_mods = 1;
6617
6618 return result;
6619}
6620
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621/*
6622 * Check for a <> code in a user command.
6623 * "code" points to the '<'. "len" the length of the <> (inclusive).
6624 * "buf" is where the result is to be added.
6625 * "split_buf" points to a buffer used for splitting, caller should free it.
6626 * "split_len" is the length of what "split_buf" contains.
6627 * Returns the length of the replacement, which has been added to "buf".
6628 * Returns -1 if there was no match, and only the "<" has been copied.
6629 */
6630 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006631uc_check_code(
6632 char_u *code,
6633 size_t len,
6634 char_u *buf,
6635 ucmd_T *cmd, /* the user command we're expanding */
6636 exarg_T *eap, /* ex arguments */
6637 char_u **split_buf,
6638 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639{
6640 size_t result = 0;
6641 char_u *p = code + 1;
6642 size_t l = len - 2;
6643 int quote = 0;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006644 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_MODS,
6645 ct_REGISTER, ct_LT, ct_NONE } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646
6647 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6648 {
6649 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6650 p += 2;
6651 l -= 2;
6652 }
6653
Bram Moolenaar371d5402006-03-20 21:47:49 +00006654 ++l;
6655 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006657 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006659 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006661 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006663 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006665 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006667 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006669 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006671 else if (STRNICMP(p, "mods>", l) == 0)
6672 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673
6674 switch (type)
6675 {
6676 case ct_ARGS:
6677 /* Simple case first */
6678 if (*eap->arg == NUL)
6679 {
6680 if (quote == 1)
6681 {
6682 result = 2;
6683 if (buf != NULL)
6684 STRCPY(buf, "''");
6685 }
6686 else
6687 result = 0;
6688 break;
6689 }
6690
6691 /* When specified there is a single argument don't split it.
6692 * Works for ":Cmd %" when % is "a b c". */
6693 if ((eap->argt & NOSPC) && quote == 2)
6694 quote = 1;
6695
6696 switch (quote)
6697 {
6698 case 0: /* No quoting, no splitting */
6699 result = STRLEN(eap->arg);
6700 if (buf != NULL)
6701 STRCPY(buf, eap->arg);
6702 break;
6703 case 1: /* Quote, but don't split */
6704 result = STRLEN(eap->arg) + 2;
6705 for (p = eap->arg; *p; ++p)
6706 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006707#ifdef FEAT_MBYTE
6708 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6709 /* DBCS can contain \ in a trail byte, skip the
6710 * double-byte character. */
6711 ++p;
6712 else
6713#endif
6714 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 ++result;
6716 }
6717
6718 if (buf != NULL)
6719 {
6720 *buf++ = '"';
6721 for (p = eap->arg; *p; ++p)
6722 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006723#ifdef FEAT_MBYTE
6724 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6725 /* DBCS can contain \ in a trail byte, copy the
6726 * double-byte character to avoid escaping. */
6727 *buf++ = *p++;
6728 else
6729#endif
6730 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 *buf++ = '\\';
6732 *buf++ = *p;
6733 }
6734 *buf = '"';
6735 }
6736
6737 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006738 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 /* This is hard, so only do it once, and cache the result */
6740 if (*split_buf == NULL)
6741 *split_buf = uc_split_args(eap->arg, split_len);
6742
6743 result = *split_len;
6744 if (buf != NULL && result != 0)
6745 STRCPY(buf, *split_buf);
6746
6747 break;
6748 }
6749 break;
6750
6751 case ct_BANG:
6752 result = eap->forceit ? 1 : 0;
6753 if (quote)
6754 result += 2;
6755 if (buf != NULL)
6756 {
6757 if (quote)
6758 *buf++ = '"';
6759 if (eap->forceit)
6760 *buf++ = '!';
6761 if (quote)
6762 *buf = '"';
6763 }
6764 break;
6765
6766 case ct_LINE1:
6767 case ct_LINE2:
6768 case ct_COUNT:
6769 {
6770 char num_buf[20];
6771 long num = (type == ct_LINE1) ? eap->line1 :
6772 (type == ct_LINE2) ? eap->line2 :
6773 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6774 size_t num_len;
6775
6776 sprintf(num_buf, "%ld", num);
6777 num_len = STRLEN(num_buf);
6778 result = num_len;
6779
6780 if (quote)
6781 result += 2;
6782
6783 if (buf != NULL)
6784 {
6785 if (quote)
6786 *buf++ = '"';
6787 STRCPY(buf, num_buf);
6788 buf += num_len;
6789 if (quote)
6790 *buf = '"';
6791 }
6792
6793 break;
6794 }
6795
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006796 case ct_MODS:
6797 {
6798 int multi_mods = 0;
6799 typedef struct {
6800 int *varp;
6801 char *name;
6802 } mod_entry_T;
6803 static mod_entry_T mod_entries[] = {
6804#ifdef FEAT_BROWSE_CMD
6805 {&cmdmod.browse, "browse"},
6806#endif
6807#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6808 {&cmdmod.confirm, "confirm"},
6809#endif
6810 {&cmdmod.hide, "hide"},
6811 {&cmdmod.keepalt, "keepalt"},
6812 {&cmdmod.keepjumps, "keepjumps"},
6813 {&cmdmod.keepmarks, "keepmarks"},
6814 {&cmdmod.keeppatterns, "keeppatterns"},
6815 {&cmdmod.lockmarks, "lockmarks"},
6816 {&cmdmod.noswapfile, "noswapfile"},
6817 {NULL, NULL}
6818 };
6819 int i;
6820
6821 result = quote ? 2 : 0;
6822 if (buf != NULL)
6823 {
6824 if (quote)
6825 *buf++ = '"';
6826 *buf = '\0';
6827 }
6828
6829#ifdef FEAT_WINDOWS
6830 /* :aboveleft and :leftabove */
6831 if (cmdmod.split & WSP_ABOVE)
6832 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6833 /* :belowright and :rightbelow */
6834 if (cmdmod.split & WSP_BELOW)
6835 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6836 /* :botright */
6837 if (cmdmod.split & WSP_BOT)
6838 result += add_cmd_modifier(buf, "botright", &multi_mods);
6839#endif
6840
6841 /* the modifiers that are simple flags */
6842 for (i = 0; mod_entries[i].varp != NULL; ++i)
6843 if (*mod_entries[i].varp)
6844 result += add_cmd_modifier(buf, mod_entries[i].name,
6845 &multi_mods);
6846
6847 /* TODO: How to support :noautocmd? */
6848#ifdef HAVE_SANDBOX
6849 /* TODO: How to support :sandbox?*/
6850#endif
6851 /* :silent */
6852 if (msg_silent > 0)
6853 result += add_cmd_modifier(buf,
6854 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6855#ifdef FEAT_WINDOWS
6856 /* :tab */
6857 if (cmdmod.tab > 0)
6858 result += add_cmd_modifier(buf, "tab", &multi_mods);
6859 /* :topleft */
6860 if (cmdmod.split & WSP_TOP)
6861 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6862#endif
6863 /* TODO: How to support :unsilent?*/
6864 /* :verbose */
6865 if (p_verbose > 0)
6866 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6867#ifdef FEAT_WINDOWS
6868 /* :vertical */
6869 if (cmdmod.split & WSP_VERT)
6870 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6871#endif
6872 if (quote && buf != NULL)
6873 {
6874 buf += result - 2;
6875 *buf = '"';
6876 }
6877 break;
6878 }
6879
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 case ct_REGISTER:
6881 result = eap->regname ? 1 : 0;
6882 if (quote)
6883 result += 2;
6884 if (buf != NULL)
6885 {
6886 if (quote)
6887 *buf++ = '\'';
6888 if (eap->regname)
6889 *buf++ = eap->regname;
6890 if (quote)
6891 *buf = '\'';
6892 }
6893 break;
6894
6895 case ct_LT:
6896 result = 1;
6897 if (buf != NULL)
6898 *buf = '<';
6899 break;
6900
6901 default:
6902 /* Not recognized: just copy the '<' and return -1. */
6903 result = (size_t)-1;
6904 if (buf != NULL)
6905 *buf = '<';
6906 break;
6907 }
6908
6909 return result;
6910}
6911
6912 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006913do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914{
6915 char_u *buf;
6916 char_u *p;
6917 char_u *q;
6918
6919 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006920 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006921 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 size_t len, totlen;
6923
6924 size_t split_len = 0;
6925 char_u *split_buf = NULL;
6926 ucmd_T *cmd;
6927#ifdef FEAT_EVAL
6928 scid_T save_current_SID = current_SID;
6929#endif
6930
6931 if (eap->cmdidx == CMD_USER)
6932 cmd = USER_CMD(eap->useridx);
6933 else
6934 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6935
6936 /*
6937 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006938 * First round: "buf" is NULL, compute length, allocate "buf".
6939 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 */
6941 buf = NULL;
6942 for (;;)
6943 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006944 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006945 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006947
6948 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006950 start = vim_strchr(p, '<');
6951 if (start != NULL)
6952 end = vim_strchr(start + 1, '>');
6953 if (buf != NULL)
6954 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006955 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6956 ;
6957 if (*ksp == K_SPECIAL
6958 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006959 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6960# ifdef FEAT_GUI
6961 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6962# endif
6963 ))
6964 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006965 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006966 * KS_SPECIAL KE_FILLER, like for mappings, but
6967 * do_cmdline() doesn't handle that, so convert it back.
6968 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6969 len = ksp - p;
6970 if (len > 0)
6971 {
6972 mch_memmove(q, p, len);
6973 q += len;
6974 }
6975 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6976 p = ksp + 3;
6977 continue;
6978 }
6979 }
6980
6981 /* break if there no <item> is found */
6982 if (start == NULL || end == NULL)
6983 break;
6984
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 /* Include the '>' */
6986 ++end;
6987
6988 /* Take everything up to the '<' */
6989 len = start - p;
6990 if (buf == NULL)
6991 totlen += len;
6992 else
6993 {
6994 mch_memmove(q, p, len);
6995 q += len;
6996 }
6997
6998 len = uc_check_code(start, end - start, q, cmd, eap,
6999 &split_buf, &split_len);
7000 if (len == (size_t)-1)
7001 {
7002 /* no match, continue after '<' */
7003 p = start + 1;
7004 len = 1;
7005 }
7006 else
7007 p = end;
7008 if (buf == NULL)
7009 totlen += len;
7010 else
7011 q += len;
7012 }
7013 if (buf != NULL) /* second time here, finished */
7014 {
7015 STRCPY(q, p);
7016 break;
7017 }
7018
7019 totlen += STRLEN(p); /* Add on the trailing characters */
7020 buf = alloc((unsigned)(totlen + 1));
7021 if (buf == NULL)
7022 {
7023 vim_free(split_buf);
7024 return;
7025 }
7026 }
7027
7028#ifdef FEAT_EVAL
7029 current_SID = cmd->uc_scriptID;
7030#endif
7031 (void)do_cmdline(buf, eap->getline, eap->cookie,
7032 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
7033#ifdef FEAT_EVAL
7034 current_SID = save_current_SID;
7035#endif
7036 vim_free(buf);
7037 vim_free(split_buf);
7038}
7039
7040# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7041 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007042get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043{
7044 return get_user_commands(NULL, idx - (int)CMD_SIZE);
7045}
7046
7047/*
7048 * Function given to ExpandGeneric() to obtain the list of user command names.
7049 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007051get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052{
7053 if (idx < curbuf->b_ucmds.ga_len)
7054 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
7055 idx -= curbuf->b_ucmds.ga_len;
7056 if (idx < ucmds.ga_len)
7057 return USER_CMD(idx)->uc_name;
7058 return NULL;
7059}
7060
7061/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007062 * Function given to ExpandGeneric() to obtain the list of user address type names.
7063 */
7064 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007065get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007066{
7067 return (char_u *)addr_type_complete[idx].name;
7068}
7069
7070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 * Function given to ExpandGeneric() to obtain the list of user command
7072 * attributes.
7073 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007075get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076{
7077 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007078 {"addr", "bang", "bar", "buffer", "complete",
7079 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007081 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 return NULL;
7083 return (char_u *)user_cmd_flags[idx];
7084}
7085
7086/*
7087 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007090get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091{
7092 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7093
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007094 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 return NULL;
7096 return (char_u *)user_cmd_nargs[idx];
7097}
7098
7099/*
7100 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007103get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104{
7105 return (char_u *)command_complete[idx].name;
7106}
7107# endif /* FEAT_CMDL_COMPL */
7108
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007109/*
7110 * Parse address type argument
7111 */
7112 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007113parse_addr_type_arg(
7114 char_u *value,
7115 int vallen,
7116 long *argt,
7117 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007118{
7119 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007120
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007121 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7122 {
7123 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7124 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7125 if (a && b)
7126 {
7127 *addr_type_arg = addr_type_complete[i].expand;
7128 break;
7129 }
7130 }
7131
7132 if (addr_type_complete[i].expand == -1)
7133 {
7134 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007135
Bram Moolenaar1c465442017-03-12 20:10:05 +01007136 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007137 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007138 err[i] = NUL;
7139 EMSG2(_("E180: Invalid address type value: %s"), err);
7140 return FAIL;
7141 }
7142
7143 if (*addr_type_arg != ADDR_LINES)
7144 *argt |= NOTADR;
7145
7146 return OK;
7147}
7148
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149#endif /* FEAT_USR_CMDS */
7150
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007151#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7152/*
7153 * Parse a completion argument "value[vallen]".
7154 * The detected completion goes in "*complp", argument type in "*argt".
7155 * When there is an argument, for function and user defined completion, it's
7156 * copied to allocated memory and stored in "*compl_arg".
7157 * Returns FAIL if something is wrong.
7158 */
7159 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007160parse_compl_arg(
7161 char_u *value,
7162 int vallen,
7163 int *complp,
7164 long *argt,
7165 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007166{
7167 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007168# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007169 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007170# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007171 int i;
7172 int valend = vallen;
7173
7174 /* Look for any argument part - which is the part after any ',' */
7175 for (i = 0; i < vallen; ++i)
7176 {
7177 if (value[i] == ',')
7178 {
7179 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007180# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007181 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007182# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007183 valend = i;
7184 break;
7185 }
7186 }
7187
7188 for (i = 0; command_complete[i].expand != 0; ++i)
7189 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007190 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007191 && STRNCMP(value, command_complete[i].name, valend) == 0)
7192 {
7193 *complp = command_complete[i].expand;
7194 if (command_complete[i].expand == EXPAND_BUFFERS)
7195 *argt |= BUFNAME;
7196 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7197 || command_complete[i].expand == EXPAND_FILES)
7198 *argt |= XFILE;
7199 break;
7200 }
7201 }
7202
7203 if (command_complete[i].expand == 0)
7204 {
7205 EMSG2(_("E180: Invalid complete value: %s"), value);
7206 return FAIL;
7207 }
7208
7209# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7210 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7211 && arg != NULL)
7212# else
7213 if (arg != NULL)
7214# endif
7215 {
7216 EMSG(_("E468: Completion argument only allowed for custom completion"));
7217 return FAIL;
7218 }
7219
7220# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7221 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7222 && arg == NULL)
7223 {
7224 EMSG(_("E467: Custom completion requires a function argument"));
7225 return FAIL;
7226 }
7227
7228 if (arg != NULL)
7229 *compl_arg = vim_strnsave(arg, (int)arglen);
7230# endif
7231 return OK;
7232}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007233
7234 int
7235cmdcomplete_str_to_type(char_u *complete_str)
7236{
7237 int i;
7238
7239 for (i = 0; command_complete[i].expand != 0; ++i)
7240 if (STRCMP(complete_str, command_complete[i].name) == 0)
7241 return command_complete[i].expand;
7242
7243 return EXPAND_NOTHING;
7244}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007245#endif
7246
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007248ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249{
Bram Moolenaare6850792010-05-14 15:28:44 +02007250 if (*eap->arg == NUL)
7251 {
7252#ifdef FEAT_EVAL
7253 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7254 char_u *p = NULL;
7255
7256 if (expr != NULL)
7257 {
7258 ++emsg_off;
7259 p = eval_to_string(expr, NULL, FALSE);
7260 --emsg_off;
7261 vim_free(expr);
7262 }
7263 if (p != NULL)
7264 {
7265 MSG(p);
7266 vim_free(p);
7267 }
7268 else
7269 MSG("default");
7270#else
7271 MSG(_("unknown"));
7272#endif
7273 }
7274 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007275 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276}
7277
7278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007279ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280{
7281 if (*eap->arg == NUL && eap->cmd[2] == '!')
7282 MSG(_("Greetings, Vim user!"));
7283 do_highlight(eap->arg, eap->forceit, FALSE);
7284}
7285
7286
7287/*
7288 * Call this function if we thought we were going to exit, but we won't
7289 * (because of an error). May need to restore the terminal mode.
7290 */
7291 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007292not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293{
7294 exiting = FALSE;
7295 settmode(TMODE_RAW);
7296}
7297
7298/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007299 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 */
7301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007302ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007304#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007305 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007306#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007307
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308#ifdef FEAT_CMDWIN
7309 if (cmdwin_type != 0)
7310 {
7311 cmdwin_result = Ctrl_C;
7312 return;
7313 }
7314#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007315 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007316 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007317 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007318 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007319 return;
7320 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007321#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007322 if (eap->addr_count > 0)
7323 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007324 int wnr = eap->line2;
7325
7326 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7327 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007328 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007329 }
7330 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007331#endif
7332#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007333 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007334#endif
7335
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007336#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007337 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007338 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007339 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007340 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007341 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007342 return;
7343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344
7345#ifdef FEAT_NETBEANS_INTG
7346 netbeansForcedQuit = eap->forceit;
7347#endif
7348
7349 /*
7350 * If there are more files or windows we won't exit.
7351 */
7352 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7353 exiting = TRUE;
7354 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007355 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7356 | (eap->forceit ? CCGD_FORCEIT : 0)
7357 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007359 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 {
7361 not_exiting();
7362 }
7363 else
7364 {
7365#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007366 /* quit last window
7367 * Note: only_one_window() returns true, even so a help window is
7368 * still open. In that case only quit, if no address has been
7369 * specified. Example:
7370 * :h|wincmd w|1q - don't quit
7371 * :h|wincmd w|q - quit
7372 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007373 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374#endif
7375 getout(0);
7376#ifdef FEAT_WINDOWS
7377# ifdef FEAT_GUI
7378 need_mouse_correct = TRUE;
7379# endif
7380 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007381 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382#endif
7383 }
7384}
7385
7386/*
7387 * ":cquit".
7388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007390ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391{
7392 getout(1); /* this does not always pass on the exit code to the Manx
7393 compiler. why? */
7394}
7395
7396/*
7397 * ":qall": try to quit all windows
7398 */
7399 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007400ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401{
7402# ifdef FEAT_CMDWIN
7403 if (cmdwin_type != 0)
7404 {
7405 if (eap->forceit)
7406 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7407 else
7408 cmdwin_result = K_XF2;
7409 return;
7410 }
7411# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007412
7413 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007414 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007415 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007416 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007417 return;
7418 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007419#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007420 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7421 /* Refuse to quit when locked or when the buffer in the last window is
7422 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007423 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007424 return;
7425#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007426
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007428 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 getout(0);
7430 not_exiting();
7431}
7432
Bram Moolenaard9967712006-03-11 21:18:15 +00007433#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434/*
7435 * ":close": close current window, unless it is the last one
7436 */
7437 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007438ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007440 win_T *win;
7441 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442# ifdef FEAT_CMDWIN
7443 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007444 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 else
7446# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007447 if (!text_locked()
7448#ifdef FEAT_AUTOCMD
7449 && !curbuf_locked()
7450#endif
7451 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007452 {
7453 if (eap->addr_count == 0)
7454 ex_win_close(eap->forceit, curwin, NULL);
7455 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007456 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007457 {
7458 winnr++;
7459 if (winnr == eap->line2)
7460 break;
7461 }
7462 if (win == NULL)
7463 win = lastwin;
7464 ex_win_close(eap->forceit, win, NULL);
7465 }
7466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467}
7468
Bram Moolenaard9967712006-03-11 21:18:15 +00007469# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007470/*
7471 * ":pclose": Close any preview window.
7472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007474ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007475{
7476 win_T *win;
7477
Bram Moolenaar29323592016-07-24 22:04:11 +02007478 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007479 if (win->w_p_pvw)
7480 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007481 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007482 break;
7483 }
7484}
Bram Moolenaard9967712006-03-11 21:18:15 +00007485# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007486
Bram Moolenaarf740b292006-02-16 22:11:02 +00007487/*
7488 * Close window "win" and take care of handling closing the last window for a
7489 * modified buffer.
7490 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007491 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007492ex_win_close(
7493 int forceit,
7494 win_T *win,
7495 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496{
7497 int need_hide;
7498 buf_T *buf = win->w_buffer;
7499
7500 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007501 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007503# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 if ((p_confirm || cmdmod.confirm) && p_write)
7505 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007506 bufref_T bufref;
7507
7508 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007510 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 return;
7512 need_hide = FALSE;
7513 }
7514 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007515# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 {
7517 EMSG(_(e_nowrtmsg));
7518 return;
7519 }
7520 }
7521
Bram Moolenaard9967712006-03-11 21:18:15 +00007522# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007524# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007525
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007527 if (tp == NULL)
7528 win_close(win, !need_hide && !P_HID(buf));
7529 else
7530 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531}
7532
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007534 * Handle the argument for a tabpage related ex command.
7535 * Returns a tabpage number.
7536 * When an error is encountered then eap->errmsg is set.
7537 */
7538 static int
7539get_tabpage_arg(exarg_T *eap)
7540{
7541 int tab_number;
7542 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7543
7544 if (eap->arg && *eap->arg != NUL)
7545 {
7546 char_u *p = eap->arg;
7547 char_u *p_save;
7548 int relative = 0; /* argument +N/-N means: go to N places to the
7549 * right/left relative to the current position. */
7550
7551 if (*p == '-')
7552 {
7553 relative = -1;
7554 p++;
7555 }
7556 else if (*p == '+')
7557 {
7558 relative = 1;
7559 p++;
7560 }
7561
7562 p_save = p;
7563 tab_number = getdigits(&p);
7564
7565 if (relative == 0)
7566 {
7567 if (STRCMP(p, "$") == 0)
7568 tab_number = LAST_TAB_NR;
7569 else if (p == p_save || *p_save == '-' || *p != NUL
7570 || tab_number > LAST_TAB_NR)
7571 {
7572 /* No numbers as argument. */
7573 eap->errmsg = e_invarg;
7574 goto theend;
7575 }
7576 }
7577 else
7578 {
7579 if (*p_save == NUL)
7580 tab_number = 1;
7581 else if (p == p_save || *p_save == '-' || *p != NUL
7582 || tab_number == 0)
7583 {
7584 /* No numbers as argument. */
7585 eap->errmsg = e_invarg;
7586 goto theend;
7587 }
7588 tab_number = tab_number * relative + tabpage_index(curtab);
7589 if (!unaccept_arg0 && relative == -1)
7590 --tab_number;
7591 }
7592 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7593 eap->errmsg = e_invarg;
7594 }
7595 else if (eap->addr_count > 0)
7596 {
7597 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007598 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007599 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007600 tab_number = 0;
7601 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007602 else
7603 {
7604 tab_number = eap->line2;
7605 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7606 {
7607 --tab_number;
7608 if (tab_number < unaccept_arg0)
7609 eap->errmsg = e_invarg;
7610 }
7611 }
7612 }
7613 else
7614 {
7615 switch (eap->cmdidx)
7616 {
7617 case CMD_tabnext:
7618 tab_number = tabpage_index(curtab) + 1;
7619 if (tab_number > LAST_TAB_NR)
7620 tab_number = 1;
7621 break;
7622 case CMD_tabmove:
7623 tab_number = LAST_TAB_NR;
7624 break;
7625 default:
7626 tab_number = tabpage_index(curtab);
7627 }
7628 }
7629
7630theend:
7631 return tab_number;
7632}
7633
7634/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007635 * ":tabclose": close current tab page, unless it is the last one.
7636 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 */
7638 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007639ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007641 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007642 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007643
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007644# ifdef FEAT_CMDWIN
7645 if (cmdwin_type != 0)
7646 cmdwin_result = K_IGNORE;
7647 else
7648# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007649 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007650 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007651 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007653 tab_number = get_tabpage_arg(eap);
7654 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007655 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007656 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007657 if (tp == NULL)
7658 {
7659 beep_flush();
7660 return;
7661 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007662 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007663 {
7664 tabpage_close_other(tp, eap->forceit);
7665 return;
7666 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007667 else if (!text_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007668#ifdef FEAT_AUTOCMD
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007669 && !curbuf_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007670#endif
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007671 )
7672 tabpage_close(eap->forceit);
7673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007675}
7676
7677/*
7678 * ":tabonly": close all tab pages except the current one
7679 */
7680 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007681ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007682{
7683 tabpage_T *tp;
7684 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007685 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007686
7687# ifdef FEAT_CMDWIN
7688 if (cmdwin_type != 0)
7689 cmdwin_result = K_IGNORE;
7690 else
7691# endif
7692 if (first_tabpage->tp_next == NULL)
7693 MSG(_("Already only one tab page"));
7694 else
7695 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007696 tab_number = get_tabpage_arg(eap);
7697 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007698 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007699 goto_tabpage(tab_number);
7700 /* Repeat this up to a 1000 times, because autocommands may
7701 * mess up the lists. */
7702 for (done = 0; done < 1000; ++done)
7703 {
7704 FOR_ALL_TABPAGES(tp)
7705 if (tp->tp_topframe != topframe)
7706 {
7707 tabpage_close_other(tp, eap->forceit);
7708 /* if we failed to close it quit */
7709 if (valid_tabpage(tp))
7710 done = 1000;
7711 /* start over, "tp" is now invalid */
7712 break;
7713 }
7714 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007715 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007716 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007717 }
7718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720
7721/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007722 * Close the current tab page.
7723 */
7724 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007725tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007726{
7727 /* First close all the windows but the current one. If that worked then
7728 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007729 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007730 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007731 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007732 ex_win_close(forceit, curwin, NULL);
7733# ifdef FEAT_GUI
7734 need_mouse_correct = TRUE;
7735# endif
7736}
7737
7738/*
7739 * Close tab page "tp", which is not the current tab page.
7740 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007741 * Also takes care of the tab pages line disappearing when closing the
7742 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007743 */
7744 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007745tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007746{
7747 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007748 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007749 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007750
7751 /* Limit to 1000 windows, autocommands may add a window while we close
7752 * one. OK, so I'm paranoid... */
7753 while (++done < 1000)
7754 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007755 wp = tp->tp_firstwin;
7756 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007757
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007758 /* Autocommands may delete the tab page under our fingers and we may
7759 * fail to close a window with a modified buffer. */
7760 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007761 break;
7762 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007763
Bram Moolenaar29323592016-07-24 22:04:11 +02007764#ifdef FEAT_AUTOCMD
7765 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7766#endif
7767
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007768 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007769 if (h != tabline_height())
7770 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007771}
7772
7773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 * ":only".
7775 */
7776 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007777ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007779 win_T *wp;
7780 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781# ifdef FEAT_GUI
7782 need_mouse_correct = TRUE;
7783# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007784 if (eap->addr_count > 0)
7785 {
7786 wnr = eap->line2;
7787 for (wp = firstwin; --wnr > 0; )
7788 {
7789 if (wp->w_next == NULL)
7790 break;
7791 else
7792 wp = wp->w_next;
7793 }
7794 win_goto(wp);
7795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 close_others(TRUE, eap->forceit);
7797}
7798
7799/*
7800 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007801 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007803 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007804ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805{
7806 if (eap->addr_count == 0)
7807 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007808 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809}
7810#endif /* FEAT_WINDOWS */
7811
7812 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007813ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007815 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816#ifdef FEAT_WINDOWS
Bram Moolenaar2256c992016-11-15 21:17:07 +01007817 if (!eap->skip)
7818 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819# ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007820 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821# endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007822 if (eap->addr_count == 0)
7823 win_close(curwin, FALSE); /* don't free buffer */
7824 else
7825 {
7826 int winnr = 0;
7827 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007828
Bram Moolenaar2256c992016-11-15 21:17:07 +01007829 FOR_ALL_WINDOWS(win)
7830 {
7831 winnr++;
7832 if (winnr == eap->line2)
7833 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007834 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007835 if (win == NULL)
7836 win = lastwin;
7837 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841}
7842
7843/*
7844 * ":stop" and ":suspend": Suspend Vim.
7845 */
7846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007847ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848{
7849 /*
7850 * Disallow suspending for "rvim".
7851 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007852 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {
7854 if (!eap->forceit)
7855 autowrite_all();
7856 windgoto((int)Rows - 1, 0);
7857 out_char('\n');
7858 out_flush();
7859 stoptermcap();
7860 out_flush(); /* needed for SUN to restore xterm buffer */
7861#ifdef FEAT_TITLE
7862 mch_restore_title(3); /* restore window titles */
7863#endif
7864 ui_suspend(); /* call machine specific function */
7865#ifdef FEAT_TITLE
7866 maketitle();
7867 resettitle(); /* force updating the title */
7868#endif
7869 starttermcap();
7870 scroll_start(); /* scroll screen before redrawing */
7871 redraw_later_clear();
7872 shell_resized(); /* may have resized window */
7873 }
7874}
7875
7876/*
7877 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7878 */
7879 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007880ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881{
7882#ifdef FEAT_CMDWIN
7883 if (cmdwin_type != 0)
7884 {
7885 cmdwin_result = Ctrl_C;
7886 return;
7887 }
7888#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007889 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007890 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007891 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007892 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007893 return;
7894 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007895#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007896 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7897 /* Refuse to quit when locked or when the buffer in the last window is
7898 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007899 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007900 return;
7901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902
7903 /*
7904 * if more files or windows we won't exit
7905 */
7906 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7907 exiting = TRUE;
7908 if ( ((eap->cmdidx == CMD_wq
7909 || curbufIsChanged())
7910 && do_write(eap) == FAIL)
7911 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007912 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {
7914 not_exiting();
7915 }
7916 else
7917 {
7918#ifdef FEAT_WINDOWS
7919 if (only_one_window()) /* quit last window, exit Vim */
7920#endif
7921 getout(0);
7922#ifdef FEAT_WINDOWS
7923# ifdef FEAT_GUI
7924 need_mouse_correct = TRUE;
7925# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007926 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 win_close(curwin, !P_HID(curwin->w_buffer));
7928#endif
7929 }
7930}
7931
7932/*
7933 * ":print", ":list", ":number".
7934 */
7935 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007936ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007938 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7939 EMSG(_(e_emptybuf));
7940 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007942 for ( ;!got_int; ui_breakcheck())
7943 {
7944 print_line(eap->line1,
7945 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7946 || (eap->flags & EXFLAG_NR)),
7947 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7948 if (++eap->line1 > eap->line2)
7949 break;
7950 out_flush(); /* show one line at a time */
7951 }
7952 setpcmark();
7953 /* put cursor at last line */
7954 curwin->w_cursor.lnum = eap->line2;
7955 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 }
7957
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959}
7960
7961#ifdef FEAT_BYTEOFF
7962 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007963ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964{
7965 goto_byte(eap->line2);
7966}
7967#endif
7968
7969/*
7970 * ":shell".
7971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007973ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974{
7975 do_shell(NULL, 0);
7976}
7977
7978#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7979 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007980 || defined(FEAT_GUI_MSWIN) \
7981 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 || defined(PROTO)
7983
7984/*
7985 * Handle a file drop. The code is here because a drop is *nearly* like an
7986 * :args command, but not quite (we have a list of exact filenames, so we
7987 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7988 * code is very similar to :args and hence needs access to a lot of the static
7989 * functions in this file.
7990 *
7991 * The list should be allocated using alloc(), as should each item in the
7992 * list. This function takes over responsibility for freeing the list.
7993 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007994 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007995 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 */
7997 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007998handle_drop(
7999 int filec, /* the number of files dropped */
8000 char_u **filev, /* the list of files dropped */
8001 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002{
8003 exarg_T ea;
8004 int save_msg_scroll = msg_scroll;
8005
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008006 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008007 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008009#ifdef FEAT_AUTOCMD
8010 if (curbuf_locked())
8011 return;
8012#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008013 /* When the screen is being updated we should not change buffers and
8014 * windows structures, it may cause freed memory to be used. */
8015 if (updating_screen)
8016 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017
8018 /* Check whether the current buffer is changed. If so, we will need
8019 * to split the current window or data could be lost.
8020 * We don't need to check if the 'hidden' option is set, as in this
8021 * case the buffer won't be lost.
8022 */
8023 if (!P_HID(curbuf) && !split)
8024 {
8025 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008026 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 --emsg_off;
8028 }
8029 if (split)
8030 {
8031# ifdef FEAT_WINDOWS
8032 if (win_split(0, 0) == FAIL)
8033 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008034 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035
8036 /* When splitting the window, create a new alist. Otherwise the
8037 * existing one is overwritten. */
8038 alist_unlink(curwin->w_alist);
8039 alist_new();
8040# else
8041 return; /* can't split, always fail */
8042# endif
8043 }
8044
8045 /*
8046 * Set up the new argument list.
8047 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00008048 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049
8050 /*
8051 * Move to the first file.
8052 */
8053 /* Fake up a minimal "next" command for do_argfile() */
8054 vim_memset(&ea, 0, sizeof(ea));
8055 ea.cmd = (char_u *)"next";
8056 do_argfile(&ea, 0);
8057
8058 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
8059 * mode that is not needed here. */
8060 need_start_insertmode = FALSE;
8061
8062 /* Restore msg_scroll, otherwise a following command may cause scrolling
8063 * unexpectedly. The screen will be redrawn by the caller, thus
8064 * msg_scroll being set by displaying a message is irrelevant. */
8065 msg_scroll = save_msg_scroll;
8066}
8067#endif
8068
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069/*
8070 * Clear an argument list: free all file names and reset it to zero entries.
8071 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008072 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008073alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074{
8075 while (--al->al_ga.ga_len >= 0)
8076 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8077 ga_clear(&al->al_ga);
8078}
8079
8080/*
8081 * Init an argument list.
8082 */
8083 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008084alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085{
8086 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8087}
8088
8089#if defined(FEAT_WINDOWS) || defined(PROTO)
8090
8091/*
8092 * Remove a reference from an argument list.
8093 * Ignored when the argument list is the global one.
8094 * If the argument list is no longer used by any window, free it.
8095 */
8096 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008097alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098{
8099 if (al != &global_alist && --al->al_refcount <= 0)
8100 {
8101 alist_clear(al);
8102 vim_free(al);
8103 }
8104}
8105
8106# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
8107/*
8108 * Create a new argument list and use it for the current window.
8109 */
8110 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008111alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112{
8113 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8114 if (curwin->w_alist == NULL)
8115 {
8116 curwin->w_alist = &global_alist;
8117 ++global_alist.al_refcount;
8118 }
8119 else
8120 {
8121 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008122 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 alist_init(curwin->w_alist);
8124 }
8125}
8126# endif
8127#endif
8128
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008129#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130/*
8131 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008132 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8133 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 */
8135 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008136alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137{
8138 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008139 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 char_u **new_arg_files;
8141 int new_arg_file_count;
8142 char_u *save_p_su = p_su;
8143 int i;
8144
8145 /* Don't use 'suffixes' here. This should work like the shell did the
8146 * expansion. Also, the vimrc file isn't read yet, thus the user
8147 * can't set the options. */
8148 p_su = empty_option;
8149 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8150 if (old_arg_files != NULL)
8151 {
8152 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008153 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8154 old_arg_count = GARGCOUNT;
8155 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008157 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 && new_arg_file_count > 0)
8159 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008160 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8161 TRUE, fnum_list, fnum_len);
8162 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 }
8165 p_su = save_p_su;
8166}
8167#endif
8168
8169/*
8170 * Set the argument list for the current window.
8171 * Takes over the allocated files[] and the allocated fnames in it.
8172 */
8173 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008174alist_set(
8175 alist_T *al,
8176 int count,
8177 char_u **files,
8178 int use_curbuf,
8179 int *fnum_list,
8180 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181{
8182 int i;
8183
8184 alist_clear(al);
8185 if (ga_grow(&al->al_ga, count) == OK)
8186 {
8187 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008188 {
8189 if (got_int)
8190 {
8191 /* When adding many buffers this can take a long time. Allow
8192 * interrupting here. */
8193 while (i < count)
8194 vim_free(files[i++]);
8195 break;
8196 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008197
8198 /* May set buffer name of a buffer previously used for the
8199 * argument list, so that it's re-used by alist_add. */
8200 if (fnum_list != NULL && i < fnum_len)
8201 buf_set_name(fnum_list[i], files[i]);
8202
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008204 ui_breakcheck();
8205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 vim_free(files);
8207 }
8208 else
8209 FreeWild(count, files);
8210#ifdef FEAT_WINDOWS
8211 if (al == &global_alist)
8212#endif
8213 arg_had_last = FALSE;
8214}
8215
8216/*
8217 * Add file "fname" to argument list "al".
8218 * "fname" must have been allocated and "al" must have been checked for room.
8219 */
8220 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008221alist_add(
8222 alist_T *al,
8223 char_u *fname,
8224 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225{
8226 if (fname == NULL) /* don't add NULL file names */
8227 return;
8228#ifdef BACKSLASH_IN_FILENAME
8229 slash_adjust(fname);
8230#endif
8231 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8232 if (set_fnum > 0)
8233 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8234 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8235 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236}
8237
8238#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8239/*
8240 * Adjust slashes in file names. Called after 'shellslash' was set.
8241 */
8242 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008243alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244{
8245 int i;
8246# ifdef FEAT_WINDOWS
8247 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008248 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249# endif
8250
8251 for (i = 0; i < GARGCOUNT; ++i)
8252 if (GARGLIST[i].ae_fname != NULL)
8253 slash_adjust(GARGLIST[i].ae_fname);
8254# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00008255 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 if (wp->w_alist != &global_alist)
8257 for (i = 0; i < WARGCOUNT(wp); ++i)
8258 if (WARGLIST(wp)[i].ae_fname != NULL)
8259 slash_adjust(WARGLIST(wp)[i].ae_fname);
8260# endif
8261}
8262#endif
8263
8264/*
8265 * ":preserve".
8266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008268ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008270 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 ml_preserve(curbuf, TRUE);
8272}
8273
8274/*
8275 * ":recover".
8276 */
8277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008278ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279{
8280 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8281 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008282 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8283 | CCGD_MULTWIN
8284 | (eap->forceit ? CCGD_FORCEIT : 0)
8285 | CCGD_EXCMD)
8286
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 && (*eap->arg == NUL
8288 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8289 ml_recover();
8290 recoverymode = FALSE;
8291}
8292
8293/*
8294 * Command modifier used in a wrong way.
8295 */
8296 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008297ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298{
8299 eap->errmsg = e_invcmd;
8300}
8301
8302#ifdef FEAT_WINDOWS
8303/*
8304 * :sview [+command] file split window with new file, read-only
8305 * :split [[+command] file] split window with current or new file
8306 * :vsplit [[+command] file] split window vertically with current or new file
8307 * :new [[+command] file] split window with no or new file
8308 * :vnew [[+command] file] split vertically window with no or new file
8309 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008310 *
8311 * :tabedit open new Tab page with empty window
8312 * :tabedit [+command] file open new Tab page and edit "file"
8313 * :tabnew [[+command] file] just like :tabedit
8314 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 */
8316 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008317ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008319 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008320# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008322# endif
8323# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008325# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008327# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008329# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008331# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008333 * quickfix windows. But it's OK when doing ":tab split". */
8334 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {
8336 if (eap->cmdidx == CMD_split)
8337 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 if (eap->cmdidx == CMD_vsplit)
8339 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008341# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008343# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008344 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 {
8346 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8347 FNAME_MESS, TRUE, curbuf->b_ffname);
8348 if (fname == NULL)
8349 goto theend;
8350 eap->arg = fname;
8351 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008352# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008354# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008356# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 && eap->cmdidx != CMD_new)
8360 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008361# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008362 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008363# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008364 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008365# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008366 au_has_group((char_u *)"FileExplorer"))
8367 {
8368 /* No browsing supported but we do have the file explorer:
8369 * Edit the directory. */
8370 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8371 eap->arg = (char_u *)".";
8372 }
8373 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008374# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008375 {
8376 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008378 if (fname == NULL)
8379 goto theend;
8380 eap->arg = fname;
8381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 }
8383 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008384# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008386 /*
8387 * Either open new tab page or split the window.
8388 */
8389 if (eap->cmdidx == CMD_tabedit
8390 || eap->cmdidx == CMD_tabfind
8391 || eap->cmdidx == CMD_tabnew)
8392 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008393 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8394 : eap->addr_count == 0 ? 0
8395 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008396 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008397 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008398
8399 /* set the alternate buffer for the window we came from */
8400 if (curwin != old_curwin
8401 && win_valid(old_curwin)
8402 && old_curwin->w_buffer != curbuf
8403 && !cmdmod.keepalt)
8404 old_curwin->w_alt_fnum = curbuf->b_fnum;
8405 }
8406 }
8407 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8409 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008410# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 /* Reset 'scrollbind' when editing another file, but keep it when
8412 * doing ":split" without arguments. */
8413 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008414# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008416# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008418 {
8419 RESET_BINDING(curwin);
8420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 else
8422 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 do_exedit(eap, old_curwin);
8425 }
8426
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008427# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008429# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008431# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432theend:
8433 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008434# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008436
8437/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008438 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008439 */
8440 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008441tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008442{
8443 exarg_T ea;
8444
8445 vim_memset(&ea, 0, sizeof(ea));
8446 ea.cmdidx = CMD_tabnew;
8447 ea.cmd = (char_u *)"tabn";
8448 ea.arg = (char_u *)"";
8449 ex_splitview(&ea);
8450}
8451
8452/*
8453 * :tabnext command
8454 */
8455 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008456ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008457{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008458 int tab_number;
8459
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008460 switch (eap->cmdidx)
8461 {
8462 case CMD_tabfirst:
8463 case CMD_tabrewind:
8464 goto_tabpage(1);
8465 break;
8466 case CMD_tablast:
8467 goto_tabpage(9999);
8468 break;
8469 case CMD_tabprevious:
8470 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008471 if (eap->arg && *eap->arg != NUL)
8472 {
8473 char_u *p = eap->arg;
8474 char_u *p_save = p;
8475
8476 tab_number = getdigits(&p);
8477 if (p == p_save || *p_save == '-' || *p != NUL
8478 || tab_number == 0)
8479 {
8480 /* No numbers as argument. */
8481 eap->errmsg = e_invarg;
8482 return;
8483 }
8484 }
8485 else
8486 {
8487 if (eap->addr_count == 0)
8488 tab_number = 1;
8489 else
8490 {
8491 tab_number = eap->line2;
8492 if (tab_number < 1)
8493 {
8494 eap->errmsg = e_invrange;
8495 return;
8496 }
8497 }
8498 }
8499 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008500 break;
8501 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008502 tab_number = get_tabpage_arg(eap);
8503 if (eap->errmsg == NULL)
8504 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008505 break;
8506 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008507}
8508
8509/*
8510 * :tabmove command
8511 */
8512 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008513ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008514{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008515 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008516
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008517 tab_number = get_tabpage_arg(eap);
8518 if (eap->errmsg == NULL)
8519 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008520}
8521
8522/*
8523 * :tabs command: List tabs and their contents.
8524 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008526ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008527{
8528 tabpage_T *tp;
8529 win_T *wp;
8530 int tabcount = 1;
8531
8532 msg_start();
8533 msg_scroll = TRUE;
8534 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8535 {
8536 msg_putchar('\n');
8537 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008538 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008539 out_flush(); /* output one line at a time */
8540 ui_breakcheck();
8541
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008542 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008543 wp = firstwin;
8544 else
8545 wp = tp->tp_firstwin;
8546 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8547 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008548 msg_putchar('\n');
8549 msg_putchar(wp == curwin ? '>' : ' ');
8550 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008551 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8552 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008553 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008554 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008555 else
8556 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8557 IObuff, IOSIZE, TRUE);
8558 msg_outtrans(IObuff);
8559 out_flush(); /* output one line at a time */
8560 ui_breakcheck();
8561 }
8562 }
8563}
8564
8565#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566
8567/*
8568 * ":mode": Set screen mode.
8569 * If no argument given, just get the screen size and redraw.
8570 */
8571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008572ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573{
8574 if (*eap->arg == NUL)
8575 shell_resized();
8576 else
8577 mch_screenmode(eap->arg);
8578}
8579
8580#ifdef FEAT_WINDOWS
8581/*
8582 * ":resize".
8583 * set, increment or decrement current window height
8584 */
8585 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008586ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587{
8588 int n;
8589 win_T *wp = curwin;
8590
8591 if (eap->addr_count > 0)
8592 {
8593 n = eap->line2;
8594 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8595 ;
8596 }
8597
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008598# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008600# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 if (cmdmod.split & WSP_VERT)
8603 {
8604 if (*eap->arg == '-' || *eap->arg == '+')
8605 n += W_WIDTH(curwin);
8606 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8607 n = 9999;
8608 win_setwidth_win((int)n, wp);
8609 }
8610 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 {
8612 if (*eap->arg == '-' || *eap->arg == '+')
8613 n += curwin->w_height;
8614 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8615 n = 9999;
8616 win_setheight_win((int)n, wp);
8617 }
8618}
8619#endif
8620
8621/*
8622 * ":find [+command] <file>" command.
8623 */
8624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008625ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626{
8627#ifdef FEAT_SEARCHPATH
8628 char_u *fname;
8629 int count;
8630
8631 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8632 TRUE, curbuf->b_ffname);
8633 if (eap->addr_count > 0)
8634 {
8635 /* Repeat finding the file "count" times. This matters when it
8636 * appears several times in the path. */
8637 count = eap->line2;
8638 while (fname != NULL && --count > 0)
8639 {
8640 vim_free(fname);
8641 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8642 FALSE, curbuf->b_ffname);
8643 }
8644 }
8645
8646 if (fname != NULL)
8647 {
8648 eap->arg = fname;
8649#endif
8650 do_exedit(eap, NULL);
8651#ifdef FEAT_SEARCHPATH
8652 vim_free(fname);
8653 }
8654#endif
8655}
8656
8657/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008658 * ":open" simulation: for now just work like ":visual".
8659 */
8660 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008661ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008662{
8663 regmatch_T regmatch;
8664 char_u *p;
8665
8666 curwin->w_cursor.lnum = eap->line2;
8667 beginline(BL_SOL | BL_FIX);
8668 if (*eap->arg == '/')
8669 {
8670 /* ":open /pattern/": put cursor in column found with pattern */
8671 ++eap->arg;
8672 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8673 *p = NUL;
8674 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8675 if (regmatch.regprog != NULL)
8676 {
8677 regmatch.rm_ic = p_ic;
8678 p = ml_get_curline();
8679 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008680 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008681 else
8682 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008683 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008684 }
8685 /* Move to the NUL, ignore any other arguments. */
8686 eap->arg += STRLEN(eap->arg);
8687 }
8688 check_cursor();
8689
8690 eap->cmdidx = CMD_visual;
8691 do_exedit(eap, NULL);
8692}
8693
8694/*
8695 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 */
8697 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008698ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699{
8700 do_exedit(eap, NULL);
8701}
8702
8703/*
8704 * ":edit <file>" command and alikes.
8705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008707do_exedit(
8708 exarg_T *eap,
8709 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710{
8711 int n;
8712#ifdef FEAT_WINDOWS
8713 int need_hide;
8714#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008715 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716
8717 /*
8718 * ":vi" command ends Ex mode.
8719 */
8720 if (exmode_active && (eap->cmdidx == CMD_visual
8721 || eap->cmdidx == CMD_view))
8722 {
8723 exmode_active = FALSE;
8724 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008725 {
8726 /* Special case: ":global/pat/visual\NLvi-commands" */
8727 if (global_busy)
8728 {
8729 int rd = RedrawingDisabled;
8730 int nwr = no_wait_return;
8731 int ms = msg_scroll;
8732#ifdef FEAT_GUI
8733 int he = hold_gui_events;
8734#endif
8735
8736 if (eap->nextcmd != NULL)
8737 {
8738 stuffReadbuff(eap->nextcmd);
8739 eap->nextcmd = NULL;
8740 }
8741
8742 if (exmode_was != EXMODE_VIM)
8743 settmode(TMODE_RAW);
8744 RedrawingDisabled = 0;
8745 no_wait_return = 0;
8746 need_wait_return = FALSE;
8747 msg_scroll = 0;
8748#ifdef FEAT_GUI
8749 hold_gui_events = 0;
8750#endif
8751 must_redraw = CLEAR;
8752
8753 main_loop(FALSE, TRUE);
8754
8755 RedrawingDisabled = rd;
8756 no_wait_return = nwr;
8757 msg_scroll = ms;
8758#ifdef FEAT_GUI
8759 hold_gui_events = he;
8760#endif
8761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 }
8765
8766 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008767 || eap->cmdidx == CMD_tabnew
8768 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008769#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 || eap->cmdidx == CMD_vnew
8771#endif
8772 ) && *eap->arg == NUL)
8773 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008774 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 setpcmark();
8776 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008777 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8778 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 }
8780 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008781#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 && eap->cmdidx != CMD_vsplit
8783#endif
8784 )
8785 || *eap->arg != NUL
8786#ifdef FEAT_BROWSE
8787 || cmdmod.browse
8788#endif
8789 )
8790 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008791#ifdef FEAT_AUTOCMD
8792 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8793 * can bring us here, others are stopped earlier. */
8794 if (*eap->arg != NUL && curbuf_locked())
8795 return;
8796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 n = readonlymode;
8798 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8799 readonlymode = TRUE;
8800 else if (eap->cmdidx == CMD_enew)
8801 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8802 empty buffer */
8803 setpcmark();
8804 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8805 NULL, eap,
8806 /* ":edit" goes to first line if Vi compatible */
8807 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8808 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8809 ? ECMD_ONE : eap->do_ecmd_lnum,
8810 (P_HID(curbuf) ? ECMD_HIDE : 0)
8811 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008812 /* after a split we can use an existing buffer */
8813 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814#ifdef FEAT_LISTCMDS
8815 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8816#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008817 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 {
8819 /* Editing the file failed. If the window was split, close it. */
8820#ifdef FEAT_WINDOWS
8821 if (old_curwin != NULL)
8822 {
8823 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8824 if (!need_hide || P_HID(curbuf))
8825 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008826# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8827 cleanup_T cs;
8828
8829 /* Reset the error/interrupt/exception state here so that
8830 * aborting() returns FALSE when closing a window. */
8831 enter_cleanup(&cs);
8832# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833# ifdef FEAT_GUI
8834 need_mouse_correct = TRUE;
8835# endif
8836 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008837
8838# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8839 /* Restore the error/interrupt/exception state if not
8840 * discarded by a new aborting error, interrupt, or
8841 * uncaught exception. */
8842 leave_cleanup(&cs);
8843# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 }
8845 }
8846#endif
8847 }
8848 else if (readonlymode && curbuf->b_nwindows == 1)
8849 {
8850 /* When editing an already visited buffer, 'readonly' won't be set
8851 * but the previous value is kept. With ":view" and ":sview" we
8852 * want the file to be readonly, except when another window is
8853 * editing the same buffer. */
8854 curbuf->b_p_ro = TRUE;
8855 }
8856 readonlymode = n;
8857 }
8858 else
8859 {
8860 if (eap->do_ecmd_cmd != NULL)
8861 do_cmdline_cmd(eap->do_ecmd_cmd);
8862#ifdef FEAT_TITLE
8863 n = curwin->w_arg_idx_invalid;
8864#endif
8865 check_arg_idx(curwin);
8866#ifdef FEAT_TITLE
8867 if (n != curwin->w_arg_idx_invalid)
8868 maketitle();
8869#endif
8870 }
8871
8872#ifdef FEAT_WINDOWS
8873 /*
8874 * if ":split file" worked, set alternate file name in old window to new
8875 * file
8876 */
8877 if (old_curwin != NULL
8878 && *eap->arg != NUL
8879 && curwin != old_curwin
8880 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008881 && old_curwin->w_buffer != curbuf
8882 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 old_curwin->w_alt_fnum = curbuf->b_fnum;
8884#endif
8885
8886 ex_no_reprint = TRUE;
8887}
8888
8889#ifndef FEAT_GUI
8890/*
8891 * ":gui" and ":gvim" when there is no GUI.
8892 */
8893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008894ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895{
8896 eap->errmsg = e_nogvim;
8897}
8898#endif
8899
8900#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8901 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008902ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903{
8904 gui_make_tearoff(eap->arg);
8905}
8906#endif
8907
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008908#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008910ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008912 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913}
8914#endif
8915
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008917ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918{
8919 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8920 MSG(_("No swap file"));
8921 else
8922 msg(curbuf->b_ml.ml_mfp->mf_fname);
8923}
8924
8925/*
8926 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8927 * offset.
8928 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008931ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932{
8933#ifdef FEAT_SCROLLBIND
8934 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008935 win_T *save_curwin = curwin;
8936 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 long topline;
8938 long y;
8939 linenr_T old_linenr = curwin->w_cursor.lnum;
8940
8941 setpcmark();
8942
8943 /*
8944 * determine max topline
8945 */
8946 if (curwin->w_p_scb)
8947 {
8948 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008949 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 {
8951 if (wp->w_p_scb && wp->w_buffer)
8952 {
8953 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8954 if (topline > y)
8955 topline = y;
8956 }
8957 }
8958 if (topline < 1)
8959 topline = 1;
8960 }
8961 else
8962 {
8963 topline = 1;
8964 }
8965
8966
8967 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008968 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008970 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 {
8972 if (curwin->w_p_scb)
8973 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008974 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 y = topline - curwin->w_topline;
8976 if (y > 0)
8977 scrollup(y, TRUE);
8978 else
8979 scrolldown(-y, TRUE);
8980 curwin->w_scbind_pos = topline;
8981 redraw_later(VALID);
8982 cursor_correct();
8983#ifdef FEAT_WINDOWS
8984 curwin->w_redr_status = TRUE;
8985#endif
8986 }
8987 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008988 curwin = save_curwin;
8989 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 if (curwin->w_p_scb)
8991 {
8992 did_syncbind = TRUE;
8993 checkpcmark();
8994 if (old_linenr != curwin->w_cursor.lnum)
8995 {
8996 char_u ctrl_o[2];
8997
8998 ctrl_o[0] = Ctrl_O;
8999 ctrl_o[1] = 0;
9000 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
9001 }
9002 }
9003#endif
9004}
9005
9006
9007 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009008ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009{
Bram Moolenaardf177f62005-02-22 08:39:57 +00009010 int i;
9011 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
9012 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013
9014 if (eap->usefilter) /* :r!cmd */
9015 do_bang(1, eap, FALSE, FALSE, TRUE);
9016 else
9017 {
9018 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
9019 return;
9020
9021#ifdef FEAT_BROWSE
9022 if (cmdmod.browse)
9023 {
9024 char_u *browseFile;
9025
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009026 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 NULL, NULL, NULL, curbuf);
9028 if (browseFile != NULL)
9029 {
9030 i = readfile(browseFile, NULL,
9031 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9032 vim_free(browseFile);
9033 }
9034 else
9035 i = OK;
9036 }
9037 else
9038#endif
9039 if (*eap->arg == NUL)
9040 {
9041 if (check_fname() == FAIL) /* check for no file name */
9042 return;
9043 i = readfile(curbuf->b_ffname, curbuf->b_fname,
9044 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9045 }
9046 else
9047 {
9048 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
9049 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
9050 i = readfile(eap->arg, NULL,
9051 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9052
9053 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01009054 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 {
9056#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
9057 if (!aborting())
9058#endif
9059 EMSG2(_(e_notopen), eap->arg);
9060 }
9061 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009062 {
9063 if (empty && exmode_active)
9064 {
9065 /* Delete the empty line that remains. Historically ex does
9066 * this but vi doesn't. */
9067 if (eap->line2 == 0)
9068 lnum = curbuf->b_ml.ml_line_count;
9069 else
9070 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009071 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009072 {
9073 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009074 if (curwin->w_cursor.lnum > 1
9075 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009076 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009077 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009078 }
9079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 }
9083}
9084
Bram Moolenaarea408852005-06-25 22:49:46 +00009085static char_u *prev_dir = NULL;
9086
9087#if defined(EXITFREE) || defined(PROTO)
9088 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009089free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009090{
9091 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00009092 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00009093
9094 vim_free(globaldir);
9095 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00009096}
9097#endif
9098
Bram Moolenaarf4258302013-06-02 18:20:17 +02009099/*
9100 * Deal with the side effects of changing the current directory.
9101 * When "local" is TRUE then this was after an ":lcd" command.
9102 */
9103 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009104post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009105{
9106 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01009107 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02009108 if (local)
9109 {
9110 /* If still in global directory, need to remember current
9111 * directory as global directory. */
9112 if (globaldir == NULL && prev_dir != NULL)
9113 globaldir = vim_strsave(prev_dir);
9114 /* Remember this local directory for the window. */
9115 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9116 curwin->w_localdir = vim_strsave(NameBuff);
9117 }
9118 else
9119 {
9120 /* We are now in the global directory, no need to remember its
9121 * name. */
9122 vim_free(globaldir);
9123 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02009124 }
9125
9126 shorten_fnames(TRUE);
9127}
9128
Bram Moolenaarea408852005-06-25 22:49:46 +00009129
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130/*
9131 * ":cd", ":lcd", ":chdir" and ":lchdir".
9132 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009133 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009134ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 char_u *new_dir;
9137 char_u *tofree;
9138
9139 new_dir = eap->arg;
9140#if !defined(UNIX) && !defined(VMS)
9141 /* for non-UNIX ":cd" means: print current directory */
9142 if (*new_dir == NUL)
9143 ex_pwd(NULL);
9144 else
9145#endif
9146 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009147#ifdef FEAT_AUTOCMD
9148 if (allbuf_locked())
9149 return;
9150#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009151 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9152 && !eap->forceit)
9153 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009154 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009155 return;
9156 }
9157
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 /* ":cd -": Change to previous directory */
9159 if (STRCMP(new_dir, "-") == 0)
9160 {
9161 if (prev_dir == NULL)
9162 {
9163 EMSG(_("E186: No previous directory"));
9164 return;
9165 }
9166 new_dir = prev_dir;
9167 }
9168
9169 /* Save current directory for next ":cd -" */
9170 tofree = prev_dir;
9171 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9172 prev_dir = vim_strsave(NameBuff);
9173 else
9174 prev_dir = NULL;
9175
9176#if defined(UNIX) || defined(VMS)
9177 /* for UNIX ":cd" means: go to home directory */
9178 if (*new_dir == NUL)
9179 {
9180 /* use NameBuff for home directory name */
9181# ifdef VMS
9182 char_u *p;
9183
9184 p = mch_getenv((char_u *)"SYS$LOGIN");
9185 if (p == NULL || *p == NUL) /* empty is the same as not set */
9186 NameBuff[0] = NUL;
9187 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009188 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189# else
9190 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9191# endif
9192 new_dir = NameBuff;
9193 }
9194#endif
9195 if (new_dir == NULL || vim_chdir(new_dir))
9196 EMSG(_(e_failed));
9197 else
9198 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02009199 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200
9201 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009202 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 ex_pwd(eap);
9204 }
9205 vim_free(tofree);
9206 }
9207}
9208
9209/*
9210 * ":pwd".
9211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009213ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214{
9215 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9216 {
9217#ifdef BACKSLASH_IN_FILENAME
9218 slash_adjust(NameBuff);
9219#endif
9220 msg(NameBuff);
9221 }
9222 else
9223 EMSG(_("E187: Unknown"));
9224}
9225
9226/*
9227 * ":=".
9228 */
9229 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009230ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231{
9232 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009233 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234}
9235
9236 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009237ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009239 int n;
9240 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241
9242 if (cursor_valid())
9243 {
9244 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9245 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01009246 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009248
9249 len = eap->line2;
9250 switch (*eap->arg)
9251 {
9252 case 'm': break;
9253 case NUL: len *= 1000L; break;
9254 default: EMSG2(_(e_invarg2), eap->arg); return;
9255 }
9256 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257}
9258
9259/*
9260 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9261 */
9262 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009263do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264{
9265 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009266 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267
9268 cursor_on();
9269 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009270 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009272 wait_now = msec - done > 1000L ? 1000L : msec - done;
9273#ifdef FEAT_TIMERS
9274 {
9275 long due_time = check_due_timer();
9276
9277 if (due_time > 0 && due_time < wait_now)
9278 wait_now = due_time;
9279 }
9280#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009281#ifdef FEAT_JOB_CHANNEL
9282 if (has_any_channel() && wait_now > 100L)
9283 wait_now = 100L;
9284#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009285 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009286#ifdef FEAT_JOB_CHANNEL
9287 if (has_any_channel())
9288 ui_breakcheck_force(TRUE);
9289 else
9290#endif
9291 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009292#ifdef MESSAGE_QUEUE
9293 /* Process the netbeans and clientserver messages that may have been
9294 * received in the call to ui_breakcheck() when the GUI is in use. This
9295 * may occur when running a test case. */
9296 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 }
9299}
9300
9301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009302do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303{
9304 int mode;
9305 char_u *cmdp;
9306
9307 cmdp = eap->cmd;
9308 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9309
9310 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9311 eap->arg, mode, isabbrev))
9312 {
9313 case 1: EMSG(_(e_invarg));
9314 break;
9315 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9316 break;
9317 }
9318}
9319
9320/*
9321 * ":winsize" command (obsolete).
9322 */
9323 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009324ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325{
9326 int w, h;
9327 char_u *arg = eap->arg;
9328 char_u *p;
9329
9330 w = getdigits(&arg);
9331 arg = skipwhite(arg);
9332 p = arg;
9333 h = getdigits(&arg);
9334 if (*p != NUL && *arg == NUL)
9335 set_shellsize(w, h, TRUE);
9336 else
9337 EMSG(_("E465: :winsize requires two number arguments"));
9338}
9339
9340#ifdef FEAT_WINDOWS
9341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009342ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343{
9344 int xchar = NUL;
9345 char_u *p;
9346
9347 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9348 {
9349 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9350 if (eap->arg[1] == NUL)
9351 {
9352 EMSG(_(e_invarg));
9353 return;
9354 }
9355 xchar = eap->arg[1];
9356 p = eap->arg + 2;
9357 }
9358 else
9359 p = eap->arg + 1;
9360
9361 eap->nextcmd = check_nextcmd(p);
9362 p = skipwhite(p);
9363 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9364 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009365 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 {
9367 /* Pass flags on for ":vertical wincmd ]". */
9368 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009369 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9371 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009372 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 }
9374}
9375#endif
9376
Bram Moolenaar843ee412004-06-30 16:16:41 +00009377#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378/*
9379 * ":winpos".
9380 */
9381 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009382ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383{
9384 int x, y;
9385 char_u *arg = eap->arg;
9386 char_u *p;
9387
9388 if (*arg == NUL)
9389 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009390# if defined(FEAT_GUI) || defined(MSWIN)
9391# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009393# else
9394 if (mch_get_winpos(&x, &y) != FAIL)
9395# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 {
9397 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9398 msg(IObuff);
9399 }
9400 else
9401# endif
9402 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9403 }
9404 else
9405 {
9406 x = getdigits(&arg);
9407 arg = skipwhite(arg);
9408 p = arg;
9409 y = getdigits(&arg);
9410 if (*p == NUL || *arg != NUL)
9411 {
9412 EMSG(_("E466: :winpos requires two number arguments"));
9413 return;
9414 }
9415# ifdef FEAT_GUI
9416 if (gui.in_use)
9417 gui_mch_set_winpos(x, y);
9418 else if (gui.starting)
9419 {
9420 /* Remember the coordinates for when the window is opened. */
9421 gui_win_x = x;
9422 gui_win_y = y;
9423 }
9424# ifdef HAVE_TGETENT
9425 else
9426# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009427# else
9428# ifdef MSWIN
9429 mch_set_winpos(x, y);
9430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431# endif
9432# ifdef HAVE_TGETENT
9433 if (*T_CWP)
9434 term_set_winpos(x, y);
9435# endif
9436 }
9437}
9438#endif
9439
9440/*
9441 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9442 */
9443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009444ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445{
9446 oparg_T oa;
9447
9448 clear_oparg(&oa);
9449 oa.regname = eap->regname;
9450 oa.start.lnum = eap->line1;
9451 oa.end.lnum = eap->line2;
9452 oa.line_count = eap->line2 - eap->line1 + 1;
9453 oa.motion_type = MLINE;
9454#ifdef FEAT_VIRTUALEDIT
9455 virtual_op = FALSE;
9456#endif
9457 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9458 {
9459 setpcmark();
9460 curwin->w_cursor.lnum = eap->line1;
9461 beginline(BL_SOL | BL_FIX);
9462 }
9463
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009464 if (VIsual_active)
9465 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009466
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 switch (eap->cmdidx)
9468 {
9469 case CMD_delete:
9470 oa.op_type = OP_DELETE;
9471 op_delete(&oa);
9472 break;
9473
9474 case CMD_yank:
9475 oa.op_type = OP_YANK;
9476 (void)op_yank(&oa, FALSE, TRUE);
9477 break;
9478
9479 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009480 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009482 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9483#else
9484 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009486 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 oa.op_type = OP_RSHIFT;
9488 else
9489 oa.op_type = OP_LSHIFT;
9490 op_shift(&oa, FALSE, eap->amount);
9491 break;
9492 }
9493#ifdef FEAT_VIRTUALEDIT
9494 virtual_op = MAYBE;
9495#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009496 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497}
9498
9499/*
9500 * ":put".
9501 */
9502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009503ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504{
9505 /* ":0put" works like ":1put!". */
9506 if (eap->line2 == 0)
9507 {
9508 eap->line2 = 1;
9509 eap->forceit = TRUE;
9510 }
9511 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009512 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9513 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514}
9515
9516/*
9517 * Handle ":copy" and ":move".
9518 */
9519 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009520ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521{
9522 long n;
9523
Bram Moolenaarded27822017-01-02 14:27:34 +01009524 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 if (eap->arg == NULL) /* error detected */
9526 {
9527 eap->nextcmd = NULL;
9528 return;
9529 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009530 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531
9532 /*
9533 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9534 */
9535 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9536 {
9537 EMSG(_(e_invaddr));
9538 return;
9539 }
9540
9541 if (eap->cmdidx == CMD_move)
9542 {
9543 if (do_move(eap->line1, eap->line2, n) == FAIL)
9544 return;
9545 }
9546 else
9547 ex_copy(eap->line1, eap->line2, n);
9548 u_clearline();
9549 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009550 ex_may_print(eap);
9551}
9552
9553/*
9554 * Print the current line if flags were given to the Ex command.
9555 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009556 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009557ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009558{
9559 if (eap->flags != 0)
9560 {
9561 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9562 (eap->flags & EXFLAG_LIST));
9563 ex_no_reprint = TRUE;
9564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565}
9566
9567/*
9568 * ":smagic" and ":snomagic".
9569 */
9570 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009571ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572{
9573 int magic_save = p_magic;
9574
9575 p_magic = (eap->cmdidx == CMD_smagic);
9576 do_sub(eap);
9577 p_magic = magic_save;
9578}
9579
9580/*
9581 * ":join".
9582 */
9583 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009584ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585{
9586 curwin->w_cursor.lnum = eap->line1;
9587 if (eap->line1 == eap->line2)
9588 {
9589 if (eap->addr_count >= 2) /* :2,2join does nothing */
9590 return;
9591 if (eap->line2 == curbuf->b_ml.ml_line_count)
9592 {
9593 beep_flush();
9594 return;
9595 }
9596 ++eap->line2;
9597 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009598 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009600 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601}
9602
9603/*
9604 * ":[addr]@r" or ":[addr]*r": execute register
9605 */
9606 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009607ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608{
9609 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009610 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611
9612 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009613 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614
9615#ifdef USE_ON_FLY_SCROLL
9616 dont_scroll = TRUE; /* disallow scrolling here */
9617#endif
9618
9619 /* get the register name. No name means to use the previous one */
9620 c = *eap->arg;
9621 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9622 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009623 /* Put the register in the typeahead buffer with the "silent" flag. */
9624 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9625 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009626 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 else
9630 {
9631 int save_efr = exec_from_reg;
9632
9633 exec_from_reg = TRUE;
9634
9635 /*
9636 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009637 * Continue until the stuff buffer is empty and all added characters
9638 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009640 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9642
9643 exec_from_reg = save_efr;
9644 }
9645}
9646
9647/*
9648 * ":!".
9649 */
9650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009651ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652{
9653 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9654}
9655
9656/*
9657 * ":undo".
9658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009660ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009662 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009663 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009664 else
9665 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666}
9667
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009668#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009669 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009670ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009671{
9672 char_u hash[UNDO_HASH_SIZE];
9673
9674 u_compute_hash(hash);
9675 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9676}
9677
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009678 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009679ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009680{
9681 char_u hash[UNDO_HASH_SIZE];
9682
9683 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009684 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009685}
9686#endif
9687
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688/*
9689 * ":redo".
9690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009692ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693{
9694 u_redo(1);
9695}
9696
9697/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009698 * ":earlier" and ":later".
9699 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009700 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009701ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009702{
9703 long count = 0;
9704 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009705 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009706 char_u *p = eap->arg;
9707
9708 if (*p == NUL)
9709 count = 1;
9710 else if (isdigit(*p))
9711 {
9712 count = getdigits(&p);
9713 switch (*p)
9714 {
9715 case 's': ++p; sec = TRUE; break;
9716 case 'm': ++p; sec = TRUE; count *= 60; break;
9717 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009718 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9719 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009720 }
9721 }
9722
9723 if (*p != NUL)
9724 EMSG2(_(e_invarg2), eap->arg);
9725 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009726 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9727 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009728}
9729
9730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 * ":redir": start/stop redirection.
9732 */
9733 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009734ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735{
9736 char *mode;
9737 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009738 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739
Bram Moolenaarba768492016-07-08 15:32:54 +02009740#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009741 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009742 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009743 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009744 return;
9745 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009746#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009747
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 if (STRICMP(eap->arg, "END") == 0)
9749 close_redir();
9750 else
9751 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009752 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009754 ++arg;
9755 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009757 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 mode = "a";
9759 }
9760 else
9761 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009762 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763
9764 close_redir();
9765
9766 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009767 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 if (fname == NULL)
9769 return;
9770#ifdef FEAT_BROWSE
9771 if (cmdmod.browse)
9772 {
9773 char_u *browseFile;
9774
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009775 browseFile = do_browse(BROWSE_SAVE,
9776 (char_u *)_("Save Redirection"),
9777 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 if (browseFile == NULL)
9779 return; /* operation cancelled */
9780 vim_free(fname);
9781 fname = browseFile;
9782 eap->forceit = TRUE; /* since dialog already asked */
9783 }
9784#endif
9785
9786 redir_fd = open_exfile(fname, eap->forceit, mode);
9787 vim_free(fname);
9788 }
9789#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009790 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 {
9792 /* redirect to a register a-z (resp. A-Z for appending) */
9793 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009794 ++arg;
9795 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009797 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009798 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009800 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009802 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009803 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009804 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009805 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009807 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009808 if (*arg == '>')
9809 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009810 /* Make register empty when not using @A-@Z and the
9811 * command is valid. */
9812 if (*arg == NUL && !isupper(redir_reg))
9813 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009815 }
9816 if (*arg != NUL)
9817 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009818 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009819 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009820 }
9821 }
9822 else if (*arg == '=' && arg[1] == '>')
9823 {
9824 int append;
9825
9826 /* redirect to a variable */
9827 close_redir();
9828 arg += 2;
9829
9830 if (*arg == '>')
9831 {
9832 ++arg;
9833 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 }
9835 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009836 append = FALSE;
9837
9838 if (var_redir_start(skipwhite(arg), append) == OK)
9839 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 }
9841#endif
9842
9843 /* TODO: redirect to a buffer */
9844
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009846 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009848
9849 /* Make sure redirection is not off. Can happen for cmdline completion
9850 * that indirectly invokes a command to catch its output. */
9851 if (redir_fd != NULL
9852#ifdef FEAT_EVAL
9853 || redir_reg || redir_vname
9854#endif
9855 )
9856 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857}
9858
9859/*
9860 * ":redraw": force redraw
9861 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009862 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009863ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864{
9865 int r = RedrawingDisabled;
9866 int p = p_lz;
9867
9868 RedrawingDisabled = 0;
9869 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009870 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009872 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873#ifdef FEAT_TITLE
9874 if (need_maketitle)
9875 maketitle();
9876#endif
9877 RedrawingDisabled = r;
9878 p_lz = p;
9879
9880 /* Reset msg_didout, so that a message that's there is overwritten. */
9881 msg_didout = FALSE;
9882 msg_col = 0;
9883
Bram Moolenaar56667a52013-07-17 11:54:28 +02009884 /* No need to wait after an intentional redraw. */
9885 need_wait_return = FALSE;
9886
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 out_flush();
9888}
9889
9890/*
9891 * ":redrawstatus": force redraw of status line(s)
9892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009894ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895{
9896#if defined(FEAT_WINDOWS)
9897 int r = RedrawingDisabled;
9898 int p = p_lz;
9899
9900 RedrawingDisabled = 0;
9901 p_lz = FALSE;
9902 if (eap->forceit)
9903 status_redraw_all();
9904 else
9905 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009906 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 RedrawingDisabled = r;
9908 p_lz = p;
9909 out_flush();
9910#endif
9911}
9912
9913 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009914close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915{
9916 if (redir_fd != NULL)
9917 {
9918 fclose(redir_fd);
9919 redir_fd = NULL;
9920 }
9921#ifdef FEAT_EVAL
9922 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009923 if (redir_vname)
9924 {
9925 var_redir_stop();
9926 redir_vname = 0;
9927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928#endif
9929}
9930
9931#if defined(FEAT_SESSION) && defined(USE_CRNL)
9932# define MKSESSION_NL
9933static int mksession_nl = FALSE; /* use NL only in put_eol() */
9934#endif
9935
9936/*
9937 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9938 */
9939 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009940ex_mkrc(
9941 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942{
9943 FILE *fd;
9944 int failed = FALSE;
9945 char_u *fname;
9946#ifdef FEAT_BROWSE
9947 char_u *browseFile = NULL;
9948#endif
9949#ifdef FEAT_SESSION
9950 int view_session = FALSE;
9951 int using_vdir = FALSE; /* using 'viewdir'? */
9952 char_u *viewFile = NULL;
9953 unsigned *flagp;
9954#endif
9955
9956 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9957 {
9958#ifdef FEAT_SESSION
9959 view_session = TRUE;
9960#else
9961 ex_ni(eap);
9962 return;
9963#endif
9964 }
9965
9966#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009967 /* Use the short file name until ":lcd" is used. We also don't use the
9968 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009969 did_lcd = FALSE;
9970
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9972 if (eap->cmdidx == CMD_mkview
9973 && (*eap->arg == NUL
9974 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9975 {
9976 eap->forceit = TRUE;
9977 fname = get_view_file(*eap->arg);
9978 if (fname == NULL)
9979 return;
9980 viewFile = fname;
9981 using_vdir = TRUE;
9982 }
9983 else
9984#endif
9985 if (*eap->arg != NUL)
9986 fname = eap->arg;
9987 else if (eap->cmdidx == CMD_mkvimrc)
9988 fname = (char_u *)VIMRC_FILE;
9989#ifdef FEAT_SESSION
9990 else if (eap->cmdidx == CMD_mksession)
9991 fname = (char_u *)SESSION_FILE;
9992#endif
9993 else
9994 fname = (char_u *)EXRC_FILE;
9995
9996#ifdef FEAT_BROWSE
9997 if (cmdmod.browse)
9998 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009999 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000# ifdef FEAT_SESSION
10001 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
10002 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
10003# endif
10004 (char_u *)_("Save Setup"),
10005 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
10006 if (browseFile == NULL)
10007 goto theend;
10008 fname = browseFile;
10009 eap->forceit = TRUE; /* since dialog already asked */
10010 }
10011#endif
10012
10013#if defined(FEAT_SESSION) && defined(vim_mkdir)
10014 /* When using 'viewdir' may have to create the directory. */
10015 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +000010016 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017#endif
10018
10019 fd = open_exfile(fname, eap->forceit, WRITEBIN);
10020 if (fd != NULL)
10021 {
10022#ifdef FEAT_SESSION
10023 if (eap->cmdidx == CMD_mkview)
10024 flagp = &vop_flags;
10025 else
10026 flagp = &ssop_flags;
10027#endif
10028
10029#ifdef MKSESSION_NL
10030 /* "unix" in 'sessionoptions': use NL line separator */
10031 if (view_session && (*flagp & SSOP_UNIX))
10032 mksession_nl = TRUE;
10033#endif
10034
10035 /* Write the version command for :mkvimrc */
10036 if (eap->cmdidx == CMD_mkvimrc)
10037 (void)put_line(fd, "version 6.0");
10038
10039#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010040 if (eap->cmdidx == CMD_mksession)
10041 {
10042 if (put_line(fd, "let SessionLoad = 1") == FAIL)
10043 failed = TRUE;
10044 }
10045
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 if (eap->cmdidx != CMD_mkview)
10047#endif
10048 {
10049 /* Write setting 'compatible' first, because it has side effects.
10050 * For that same reason only do it when needed. */
10051 if (p_cp)
10052 (void)put_line(fd, "if !&cp | set cp | endif");
10053 else
10054 (void)put_line(fd, "if &cp | set nocp | endif");
10055 }
10056
10057#ifdef FEAT_SESSION
10058 if (!view_session
10059 || (eap->cmdidx == CMD_mksession
10060 && (*flagp & SSOP_OPTIONS)))
10061#endif
10062 failed |= (makemap(fd, NULL) == FAIL
10063 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10064
10065#ifdef FEAT_SESSION
10066 if (!failed && view_session)
10067 {
10068 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10069 failed = TRUE;
10070 if (eap->cmdidx == CMD_mksession)
10071 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010072 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073
Bram Moolenaard9462e32011-04-11 21:35:11 +020010074 dirnow = alloc(MAXPATHL);
10075 if (dirnow == NULL)
10076 failed = TRUE;
10077 else
10078 {
10079 /*
10080 * Change to session file's dir.
10081 */
10082 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010084 *dirnow = NUL;
10085 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10086 {
10087 if (vim_chdirfile(fname) == OK)
10088 shorten_fnames(TRUE);
10089 }
10090 else if (*dirnow != NUL
10091 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10092 {
10093 if (mch_chdir((char *)globaldir) == 0)
10094 shorten_fnames(TRUE);
10095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096
Bram Moolenaard9462e32011-04-11 21:35:11 +020010097 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098
Bram Moolenaard9462e32011-04-11 21:35:11 +020010099 /* restore original dir */
10100 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010102 {
10103 if (mch_chdir((char *)dirnow) != 0)
10104 EMSG(_(e_prev_dir));
10105 shorten_fnames(TRUE);
10106 }
10107 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 }
10109 }
10110 else
10111 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010112 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10113 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 }
10115 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10116 == FAIL)
10117 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010118 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10119 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010120 if (eap->cmdidx == CMD_mksession)
10121 {
10122 if (put_line(fd, "unlet SessionLoad") == FAIL)
10123 failed = TRUE;
10124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 }
10126#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010127 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10128 failed = TRUE;
10129
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 failed |= fclose(fd);
10131
10132 if (failed)
10133 EMSG(_(e_write));
10134#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10135 else if (eap->cmdidx == CMD_mksession)
10136 {
10137 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010138 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139
Bram Moolenaard9462e32011-04-11 21:35:11 +020010140 tbuf = alloc(MAXPATHL);
10141 if (tbuf != NULL)
10142 {
10143 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10144 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10145 vim_free(tbuf);
10146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 }
10148#endif
10149#ifdef MKSESSION_NL
10150 mksession_nl = FALSE;
10151#endif
10152 }
10153
10154#ifdef FEAT_BROWSE
10155theend:
10156 vim_free(browseFile);
10157#endif
10158#ifdef FEAT_SESSION
10159 vim_free(viewFile);
10160#endif
10161}
10162
Bram Moolenaardf177f62005-02-22 08:39:57 +000010163#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10164 || defined(PROTO)
10165 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010166vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010167{
10168 if (vim_mkdir(name, prot) != 0)
10169 {
10170 EMSG2(_("E739: Cannot create directory: %s"), name);
10171 return FAIL;
10172 }
10173 return OK;
10174}
10175#endif
10176
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177/*
10178 * Open a file for writing for an Ex command, with some checks.
10179 * Return file descriptor, or NULL on failure.
10180 */
10181 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010182open_exfile(
10183 char_u *fname,
10184 int forceit,
10185 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186{
10187 FILE *fd;
10188
10189#ifdef UNIX
10190 /* with Unix it is possible to open a directory */
10191 if (mch_isdir(fname))
10192 {
10193 EMSG2(_(e_isadir2), fname);
10194 return NULL;
10195 }
10196#endif
10197 if (!forceit && *mode != 'a' && vim_fexists(fname))
10198 {
10199 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10200 return NULL;
10201 }
10202
10203 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10204 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10205
10206 return fd;
10207}
10208
10209/*
10210 * ":mark" and ":k".
10211 */
10212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010213ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214{
10215 pos_T pos;
10216
10217 if (*eap->arg == NUL) /* No argument? */
10218 EMSG(_(e_argreq));
10219 else if (eap->arg[1] != NUL) /* more than one character? */
10220 EMSG(_(e_trailing));
10221 else
10222 {
10223 pos = curwin->w_cursor; /* save curwin->w_cursor */
10224 curwin->w_cursor.lnum = eap->line2;
10225 beginline(BL_WHITE | BL_FIX);
10226 if (setmark(*eap->arg) == FAIL) /* set mark */
10227 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10228 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10229 }
10230}
10231
10232/*
10233 * Update w_topline, w_leftcol and the cursor position.
10234 */
10235 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010236update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237{
10238 check_cursor(); /* put cursor on valid line */
10239 update_topline();
10240 if (!curwin->w_p_wrap)
10241 validate_cursor();
10242 update_curswant();
10243}
10244
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245/*
10246 * ":normal[!] {commands}": Execute normal mode commands.
10247 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010248 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010249ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 int save_msg_scroll = msg_scroll;
10252 int save_restart_edit = restart_edit;
10253 int save_msg_didout = msg_didout;
10254 int save_State = State;
10255 tasave_T tabuf;
10256 int save_insertmode = p_im;
10257 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010258 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259#ifdef FEAT_MBYTE
10260 char_u *arg = NULL;
10261 int l;
10262 char_u *p;
10263#endif
10264
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010265 if (ex_normal_lock > 0)
10266 {
10267 EMSG(_(e_secure));
10268 return;
10269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 if (ex_normal_busy >= p_mmd)
10271 {
10272 EMSG(_("E192: Recursive use of :normal too deep"));
10273 return;
10274 }
10275 ++ex_normal_busy;
10276
10277 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10278 restart_edit = 0; /* don't go to Insert mode */
10279 p_im = FALSE; /* don't use 'insertmode' */
10280
10281#ifdef FEAT_MBYTE
10282 /*
10283 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10284 * this for the K_SPECIAL leading byte, otherwise special keys will not
10285 * work.
10286 */
10287 if (has_mbyte)
10288 {
10289 int len = 0;
10290
10291 /* Count the number of characters to be escaped. */
10292 for (p = eap->arg; *p != NUL; ++p)
10293 {
10294# ifdef FEAT_GUI
10295 if (*p == CSI) /* leadbyte CSI */
10296 len += 2;
10297# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010298 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10300# ifdef FEAT_GUI
10301 || *p == CSI
10302# endif
10303 )
10304 len += 2;
10305 }
10306 if (len > 0)
10307 {
10308 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10309 if (arg != NULL)
10310 {
10311 len = 0;
10312 for (p = eap->arg; *p != NUL; ++p)
10313 {
10314 arg[len++] = *p;
10315# ifdef FEAT_GUI
10316 if (*p == CSI)
10317 {
10318 arg[len++] = KS_EXTRA;
10319 arg[len++] = (int)KE_CSI;
10320 }
10321# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010322 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 {
10324 arg[len++] = *++p;
10325 if (*p == K_SPECIAL)
10326 {
10327 arg[len++] = KS_SPECIAL;
10328 arg[len++] = KE_FILLER;
10329 }
10330# ifdef FEAT_GUI
10331 else if (*p == CSI)
10332 {
10333 arg[len++] = KS_EXTRA;
10334 arg[len++] = (int)KE_CSI;
10335 }
10336# endif
10337 }
10338 arg[len] = NUL;
10339 }
10340 }
10341 }
10342 }
10343#endif
10344
10345 /*
10346 * Save the current typeahead. This is required to allow using ":normal"
10347 * from an event handler and makes sure we don't hang when the argument
10348 * ends with half a command.
10349 */
10350 save_typeahead(&tabuf);
10351 if (tabuf.typebuf_valid)
10352 {
10353 /*
10354 * Repeat the :normal command for each line in the range. When no
10355 * range given, execute it just once, without positioning the cursor
10356 * first.
10357 */
10358 do
10359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 if (eap->addr_count != 0)
10361 {
10362 curwin->w_cursor.lnum = eap->line1++;
10363 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010364 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 }
10366
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010367 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368#ifdef FEAT_MBYTE
10369 arg != NULL ? arg :
10370#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010371 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 }
10373 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10374 }
10375
10376 /* Might not return to the main loop when in an event handler. */
10377 update_topline_cursor();
10378
10379 /* Restore the previous typeahead. */
10380 restore_typeahead(&tabuf);
10381
10382 --ex_normal_busy;
10383 msg_scroll = save_msg_scroll;
10384 restart_edit = save_restart_edit;
10385 p_im = save_insertmode;
10386 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010387 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10389
10390 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010391 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010393#ifdef FEAT_MOUSE
10394 setmouse();
10395#endif
10396#ifdef CURSOR_SHAPE
10397 ui_cursor_shape(); /* may show different cursor shape */
10398#endif
10399
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400#ifdef FEAT_MBYTE
10401 vim_free(arg);
10402#endif
10403}
10404
10405/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010406 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 */
10408 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010409ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010411 if (eap->forceit)
10412 {
10413 coladvance((colnr_T)MAXCOL);
10414 curwin->w_curswant = MAXCOL;
10415 curwin->w_set_curswant = FALSE;
10416 }
10417
Bram Moolenaara40c5002005-01-09 21:16:21 +000010418 /* Ignore the command when already in Insert mode. Inserting an
10419 * expression register that invokes a function can do this. */
10420 if (State & INSERT)
10421 return;
10422
Bram Moolenaar64969662005-12-14 21:59:55 +000010423 if (eap->cmdidx == CMD_startinsert)
10424 restart_edit = 'a';
10425 else if (eap->cmdidx == CMD_startreplace)
10426 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010428 restart_edit = 'V';
10429
10430 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010432 if (eap->cmdidx == CMD_startinsert)
10433 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 curwin->w_curswant = 0; /* avoid MAXCOL */
10435 }
10436}
10437
10438/*
10439 * ":stopinsert"
10440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010442ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443{
10444 restart_edit = 0;
10445 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010446 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010449/*
10450 * Execute normal mode command "cmd".
10451 * "remap" can be REMAP_NONE or REMAP_YES.
10452 */
10453 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010454exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010455{
Bram Moolenaar25281632016-01-21 23:32:32 +010010456 /* Stuff the argument into the typeahead buffer. */
10457 ins_typebuf(cmd, remap, 0, TRUE, silent);
10458 exec_normal(FALSE);
10459}
Bram Moolenaar25281632016-01-21 23:32:32 +010010460
Bram Moolenaar25281632016-01-21 23:32:32 +010010461/*
10462 * Execute normal_cmd() until there is no typeahead left.
10463 */
10464 void
10465exec_normal(int was_typed)
10466{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010467 oparg_T oa;
10468
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010469 clear_oparg(&oa);
10470 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010471 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10472 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010473 {
10474 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010475 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010476 }
10477}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010478
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479#ifdef FEAT_FIND_ID
10480 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010481ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482{
10483 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10484 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10485 (linenr_T)1, (linenr_T)MAXLNUM);
10486}
10487
10488#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10489/*
10490 * ":psearch"
10491 */
10492 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010493ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494{
10495 g_do_tagpreview = p_pvh;
10496 ex_findpat(eap);
10497 g_do_tagpreview = 0;
10498}
10499#endif
10500
10501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010502ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503{
10504 int whole = TRUE;
10505 long n;
10506 char_u *p;
10507 int action;
10508
10509 switch (cmdnames[eap->cmdidx].cmd_name[2])
10510 {
10511 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10512 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10513 action = ACTION_GOTO;
10514 else
10515 action = ACTION_SHOW;
10516 break;
10517 case 'i': /* ":ilist" and ":dlist" */
10518 action = ACTION_SHOW_ALL;
10519 break;
10520 case 'u': /* ":ijump" and ":djump" */
10521 action = ACTION_GOTO;
10522 break;
10523 default: /* ":isplit" and ":dsplit" */
10524 action = ACTION_SPLIT;
10525 break;
10526 }
10527
10528 n = 1;
10529 if (vim_isdigit(*eap->arg)) /* get count */
10530 {
10531 n = getdigits(&eap->arg);
10532 eap->arg = skipwhite(eap->arg);
10533 }
10534 if (*eap->arg == '/') /* Match regexp, not just whole words */
10535 {
10536 whole = FALSE;
10537 ++eap->arg;
10538 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10539 if (*p)
10540 {
10541 *p++ = NUL;
10542 p = skipwhite(p);
10543
10544 /* Check for trailing illegal characters */
10545 if (!ends_excmd(*p))
10546 eap->errmsg = e_trailing;
10547 else
10548 eap->nextcmd = check_nextcmd(p);
10549 }
10550 }
10551 if (!eap->skip)
10552 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10553 whole, !eap->forceit,
10554 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10555 n, action, eap->line1, eap->line2);
10556}
10557#endif
10558
10559#ifdef FEAT_WINDOWS
10560
10561# ifdef FEAT_QUICKFIX
10562/*
10563 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10564 */
10565 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010566ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010568 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10570}
10571
10572/*
10573 * ":pedit"
10574 */
10575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010576ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577{
10578 win_T *curwin_save = curwin;
10579
10580 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010581 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582 keep_help_flag = curwin_save->w_buffer->b_help;
10583 do_exedit(eap, NULL);
10584 keep_help_flag = FALSE;
10585 if (curwin != curwin_save && win_valid(curwin_save))
10586 {
10587 /* Return cursor to where we were */
10588 validate_cursor();
10589 redraw_later(VALID);
10590 win_enter(curwin_save, TRUE);
10591 }
10592 g_do_tagpreview = 0;
10593}
10594# endif
10595
10596/*
10597 * ":stag", ":stselect" and ":stjump".
10598 */
10599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010600ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601{
10602 postponed_split = -1;
10603 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010604 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10606 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010607 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608}
10609#endif
10610
10611/*
10612 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10613 */
10614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010615ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616{
10617 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10618}
10619
10620 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010621ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622{
10623 int cmd;
10624
10625 switch (name[1])
10626 {
10627 case 'j': cmd = DT_JUMP; /* ":tjump" */
10628 break;
10629 case 's': cmd = DT_SELECT; /* ":tselect" */
10630 break;
10631 case 'p': cmd = DT_PREV; /* ":tprevious" */
10632 break;
10633 case 'N': cmd = DT_PREV; /* ":tNext" */
10634 break;
10635 case 'n': cmd = DT_NEXT; /* ":tnext" */
10636 break;
10637 case 'o': cmd = DT_POP; /* ":pop" */
10638 break;
10639 case 'f': /* ":tfirst" */
10640 case 'r': cmd = DT_FIRST; /* ":trewind" */
10641 break;
10642 case 'l': cmd = DT_LAST; /* ":tlast" */
10643 break;
10644 default: /* ":tag" */
10645#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010646 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010648 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 return;
10650 }
10651#endif
10652 cmd = DT_TAG;
10653 break;
10654 }
10655
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010656 if (name[0] == 'l')
10657 {
10658#ifndef FEAT_QUICKFIX
10659 ex_ni(eap);
10660 return;
10661#else
10662 cmd = DT_LTAG;
10663#endif
10664 }
10665
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10667 eap->forceit, TRUE);
10668}
10669
10670/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010671 * Check "str" for starting with a special cmdline variable.
10672 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10673 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10674 */
10675 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010676find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010677{
10678 int len;
10679 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010680 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010681 "%",
10682#define SPEC_PERC 0
10683 "#",
10684#define SPEC_HASH 1
10685 "<cword>", /* cursor word */
10686#define SPEC_CWORD 2
10687 "<cWORD>", /* cursor WORD */
10688#define SPEC_CCWORD 3
10689 "<cfile>", /* cursor path name */
10690#define SPEC_CFILE 4
10691 "<sfile>", /* ":so" file name */
10692#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010693 "<slnum>", /* ":so" file line number */
10694#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010695#ifdef FEAT_AUTOCMD
10696 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010697# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010698 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010699# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010700 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010701# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010702#endif
10703#ifdef FEAT_CLIENTSERVER
10704 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010705# ifdef FEAT_AUTOCMD
10706# define SPEC_CLIENT 10
10707# else
10708# define SPEC_CLIENT 7
10709# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010710#endif
10711 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010712
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010713 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010714 {
10715 len = (int)STRLEN(spec_str[i]);
10716 if (STRNCMP(src, spec_str[i], len) == 0)
10717 {
10718 *usedlen = len;
10719 return i;
10720 }
10721 }
10722 return -1;
10723}
10724
10725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 * Evaluate cmdline variables.
10727 *
10728 * change '%' to curbuf->b_ffname
10729 * '#' to curwin->w_altfile
10730 * '<cword>' to word under the cursor
10731 * '<cWORD>' to WORD under the cursor
10732 * '<cfile>' to path name under the cursor
10733 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010734 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735 * '<afile>' to file name for autocommand
10736 * '<abuf>' to buffer number for autocommand
10737 * '<amatch>' to matching name for autocommand
10738 *
10739 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10740 * "" for error without a message) and NULL is returned.
10741 * Returns an allocated string if a valid match was found.
10742 * Returns NULL if no match was found. "usedlen" then still contains the
10743 * number of characters to skip.
10744 */
10745 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010746eval_vars(
10747 char_u *src, /* pointer into commandline */
10748 char_u *srcstart, /* beginning of valid memory for src */
10749 int *usedlen, /* characters after src that are used */
10750 linenr_T *lnump, /* line number for :e command, or NULL */
10751 char_u **errormsg, /* pointer to error message */
10752 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010753 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754{
10755 int i;
10756 char_u *s;
10757 char_u *result;
10758 char_u *resultbuf = NULL;
10759 int resultlen;
10760 buf_T *buf;
10761 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10762 int spec_idx;
10763#ifdef FEAT_MODIFY_FNAME
10764 int skip_mod = FALSE;
10765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767
10768 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010769 if (escaped != NULL)
10770 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771
10772 /*
10773 * Check if there is something to do.
10774 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010775 spec_idx = find_cmdline_var(src, usedlen);
10776 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 {
10778 *usedlen = 1;
10779 return NULL;
10780 }
10781
10782 /*
10783 * Skip when preceded with a backslash "\%" and "\#".
10784 * Note: In "\\%" the % is also not recognized!
10785 */
10786 if (src > srcstart && src[-1] == '\\')
10787 {
10788 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010789 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 return NULL;
10791 }
10792
10793 /*
10794 * word or WORD under cursor
10795 */
10796 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10797 {
10798 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10799 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10800 if (resultlen == 0)
10801 {
10802 *errormsg = (char_u *)"";
10803 return NULL;
10804 }
10805 }
10806
10807 /*
10808 * '#': Alternate file name
10809 * '%': Current file name
10810 * File name under the cursor
10811 * File name for autocommand
10812 * and following modifiers
10813 */
10814 else
10815 {
10816 switch (spec_idx)
10817 {
10818 case SPEC_PERC: /* '%': current file */
10819 if (curbuf->b_fname == NULL)
10820 {
10821 result = (char_u *)"";
10822 valid = 0; /* Must have ":p:h" to be valid */
10823 }
10824 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 break;
10827
10828 case SPEC_HASH: /* '#' or "#99": alternate file */
10829 if (src[1] == '#') /* "##": the argument list */
10830 {
10831 result = arg_all();
10832 resultbuf = result;
10833 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010834 if (escaped != NULL)
10835 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836#ifdef FEAT_MODIFY_FNAME
10837 skip_mod = TRUE;
10838#endif
10839 break;
10840 }
10841 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010842 if (*s == '<') /* "#<99" uses v:oldfiles */
10843 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 i = (int)getdigits(&s);
10845 *usedlen = (int)(s - src); /* length of what we expand */
10846
Bram Moolenaard812df62008-11-09 12:46:09 +000010847 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010849 if (*usedlen < 2)
10850 {
10851 /* Should we give an error message for #<text? */
10852 *usedlen = 1;
10853 return NULL;
10854 }
10855#ifdef FEAT_EVAL
10856 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10857 (long)i);
10858 if (result == NULL)
10859 {
10860 *errormsg = (char_u *)"";
10861 return NULL;
10862 }
10863#else
10864 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 }
10868 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010869 {
10870 buf = buflist_findnr(i);
10871 if (buf == NULL)
10872 {
10873 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10874 return NULL;
10875 }
10876 if (lnump != NULL)
10877 *lnump = ECMD_LAST;
10878 if (buf->b_fname == NULL)
10879 {
10880 result = (char_u *)"";
10881 valid = 0; /* Must have ":p:h" to be valid */
10882 }
10883 else
10884 result = buf->b_fname;
10885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 break;
10887
10888#ifdef FEAT_SEARCHPATH
10889 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010890 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 if (result == NULL)
10892 {
10893 *errormsg = (char_u *)"";
10894 return NULL;
10895 }
10896 resultbuf = result; /* remember allocated string */
10897 break;
10898#endif
10899
10900#ifdef FEAT_AUTOCMD
10901 case SPEC_AFILE: /* file name for autocommand */
10902 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010903 if (result != NULL && !autocmd_fname_full)
10904 {
10905 /* Still need to turn the fname into a full path. It is
10906 * postponed to avoid a delay when <afile> is not used. */
10907 autocmd_fname_full = TRUE;
10908 result = FullName_save(autocmd_fname, FALSE);
10909 vim_free(autocmd_fname);
10910 autocmd_fname = result;
10911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 if (result == NULL)
10913 {
10914 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10915 return NULL;
10916 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010917 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918 break;
10919
10920 case SPEC_ABUF: /* buffer number for autocommand */
10921 if (autocmd_bufnr <= 0)
10922 {
10923 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10924 return NULL;
10925 }
10926 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10927 result = strbuf;
10928 break;
10929
10930 case SPEC_AMATCH: /* match name for autocommand */
10931 result = autocmd_match;
10932 if (result == NULL)
10933 {
10934 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10935 return NULL;
10936 }
10937 break;
10938
10939#endif
10940 case SPEC_SFILE: /* file name for ":so" command */
10941 result = sourcing_name;
10942 if (result == NULL)
10943 {
10944 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10945 return NULL;
10946 }
10947 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010948 case SPEC_SLNUM: /* line in file for ":so" command */
10949 if (sourcing_name == NULL || sourcing_lnum == 0)
10950 {
10951 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10952 return NULL;
10953 }
10954 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10955 result = strbuf;
10956 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957#if defined(FEAT_CLIENTSERVER)
10958 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010959 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10960 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 result = strbuf;
10962 break;
10963#endif
10964 }
10965
10966 resultlen = (int)STRLEN(result); /* length of new string */
10967 if (src[*usedlen] == '<') /* remove the file name extension */
10968 {
10969 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 resultlen = (int)(s - result);
10972 }
10973#ifdef FEAT_MODIFY_FNAME
10974 else if (!skip_mod)
10975 {
10976 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10977 &resultlen);
10978 if (result == NULL)
10979 {
10980 *errormsg = (char_u *)"";
10981 return NULL;
10982 }
10983 }
10984#endif
10985 }
10986
10987 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10988 {
10989 if (valid != VALID_HEAD + VALID_PATH)
10990 /* xgettext:no-c-format */
10991 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10992 else
10993 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10994 result = NULL;
10995 }
10996 else
10997 result = vim_strnsave(result, resultlen);
10998 vim_free(resultbuf);
10999 return result;
11000}
11001
11002/*
11003 * Concatenate all files in the argument list, separated by spaces, and return
11004 * it in one allocated string.
11005 * Spaces and backslashes in the file names are escaped with a backslash.
11006 * Returns NULL when out of memory.
11007 */
11008 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011009arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010{
11011 int len;
11012 int idx;
11013 char_u *retval = NULL;
11014 char_u *p;
11015
11016 /*
11017 * Do this loop two times:
11018 * first time: compute the total length
11019 * second time: concatenate the names
11020 */
11021 for (;;)
11022 {
11023 len = 0;
11024 for (idx = 0; idx < ARGCOUNT; ++idx)
11025 {
11026 p = alist_name(&ARGLIST[idx]);
11027 if (p != NULL)
11028 {
11029 if (len > 0)
11030 {
11031 /* insert a space in between names */
11032 if (retval != NULL)
11033 retval[len] = ' ';
11034 ++len;
11035 }
11036 for ( ; *p != NUL; ++p)
11037 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011038 if (*p == ' '
11039#ifndef BACKSLASH_IN_FILENAME
11040 || *p == '\\'
11041#endif
11042 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 {
11044 /* insert a backslash */
11045 if (retval != NULL)
11046 retval[len] = '\\';
11047 ++len;
11048 }
11049 if (retval != NULL)
11050 retval[len] = *p;
11051 ++len;
11052 }
11053 }
11054 }
11055
11056 /* second time: break here */
11057 if (retval != NULL)
11058 {
11059 retval[len] = NUL;
11060 break;
11061 }
11062
11063 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011064 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 if (retval == NULL)
11066 break;
11067 }
11068
11069 return retval;
11070}
11071
11072#if defined(FEAT_AUTOCMD) || defined(PROTO)
11073/*
11074 * Expand the <sfile> string in "arg".
11075 *
11076 * Returns an allocated string, or NULL for any error.
11077 */
11078 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011079expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080{
11081 char_u *errormsg;
11082 int len;
11083 char_u *result;
11084 char_u *newres;
11085 char_u *repl;
11086 int srclen;
11087 char_u *p;
11088
11089 result = vim_strsave(arg);
11090 if (result == NULL)
11091 return NULL;
11092
11093 for (p = result; *p; )
11094 {
11095 if (STRNCMP(p, "<sfile>", 7) != 0)
11096 ++p;
11097 else
11098 {
11099 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011100 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 if (errormsg != NULL)
11102 {
11103 if (*errormsg)
11104 emsg(errormsg);
11105 vim_free(result);
11106 return NULL;
11107 }
11108 if (repl == NULL) /* no match (cannot happen) */
11109 {
11110 p += srclen;
11111 continue;
11112 }
11113 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11114 newres = alloc(len);
11115 if (newres == NULL)
11116 {
11117 vim_free(repl);
11118 vim_free(result);
11119 return NULL;
11120 }
11121 mch_memmove(newres, result, (size_t)(p - result));
11122 STRCPY(newres + (p - result), repl);
11123 len = (int)STRLEN(newres);
11124 STRCAT(newres, p + srclen);
11125 vim_free(repl);
11126 vim_free(result);
11127 result = newres;
11128 p = newres + len; /* continue after the match */
11129 }
11130 }
11131
11132 return result;
11133}
11134#endif
11135
11136#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011137static int ses_winsizes(FILE *fd, int restore_size,
11138 win_T *tab_firstwin);
11139static int ses_win_rec(FILE *fd, frame_T *fr);
11140static frame_T *ses_skipframe(frame_T *fr);
11141static int ses_do_frame(frame_T *fr);
11142static int ses_do_win(win_T *wp);
11143static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11144static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
11145static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146
11147/*
11148 * Write openfile commands for the current buffers to an .exrc file.
11149 * Return FAIL on error, OK otherwise.
11150 */
11151 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011152makeopens(
11153 FILE *fd,
11154 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155{
11156 buf_T *buf;
11157 int only_save_windows = TRUE;
11158 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159 int restore_size = TRUE;
11160 win_T *wp;
11161 char_u *sname;
11162 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011163 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011164 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011165 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011166 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011167 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011168 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169
11170 if (ssop_flags & SSOP_BUFFERS)
11171 only_save_windows = FALSE; /* Save ALL buffers */
11172
11173 /*
11174 * Begin by setting the this_session variable, and then other
11175 * sessionable variables.
11176 */
11177#ifdef FEAT_EVAL
11178 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11179 return FAIL;
11180 if (ssop_flags & SSOP_GLOBALS)
11181 if (store_session_globals(fd) == FAIL)
11182 return FAIL;
11183#endif
11184
11185 /*
11186 * Close all windows but one.
11187 */
11188 if (put_line(fd, "silent only") == FAIL)
11189 return FAIL;
11190
11191 /*
11192 * Now a :cd command to the session directory or the current directory
11193 */
11194 if (ssop_flags & SSOP_SESDIR)
11195 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011196 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11197 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 return FAIL;
11199 }
11200 else if (ssop_flags & SSOP_CURDIR)
11201 {
11202 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11203 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011204 || fputs("cd ", fd) < 0
11205 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11206 || put_eol(fd) == FAIL)
11207 {
11208 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 vim_free(sname);
11212 }
11213
11214 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011215 * If there is an empty, unnamed buffer we will wipe it out later.
11216 * Remember the buffer number.
11217 */
11218 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11219 return FAIL;
11220 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11221 return FAIL;
11222 if (put_line(fd, "endif") == FAIL)
11223 return FAIL;
11224
11225 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226 * Now save the current files, current buffer first.
11227 */
11228 if (put_line(fd, "set shortmess=aoO") == FAIL)
11229 return FAIL;
11230
11231 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011232 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233 {
11234 if (!(only_save_windows && buf->b_nwindows == 0)
11235 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11236 && buf->b_fname != NULL
11237 && buf->b_p_bl)
11238 {
11239 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11240 : buf->b_wininfo->wi_fpos.lnum) < 0
11241 || ses_fname(fd, buf, &ssop_flags) == FAIL)
11242 return FAIL;
11243 }
11244 }
11245
11246 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011247 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11249 return FAIL;
11250
11251 if (ssop_flags & SSOP_RESIZE)
11252 {
11253 /* Note: after the restore we still check it worked!*/
11254 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11255 || put_eol(fd) == FAIL)
11256 return FAIL;
11257 }
11258
11259#ifdef FEAT_GUI
11260 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11261 {
11262 int x, y;
11263
11264 if (gui_mch_get_winpos(&x, &y) == OK)
11265 {
11266 /* Note: after the restore we still check it worked!*/
11267 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11268 return FAIL;
11269 }
11270 }
11271#endif
11272
11273 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011274 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11275 * will be displayed when creating the next tab. That resizes the windows
11276 * in the first tab, which may cause problems. Set 'showtabline' to 2
11277 * temporarily to avoid that.
11278 */
11279 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11280 {
11281 if (put_line(fd, "set stal=2") == FAIL)
11282 return FAIL;
11283 restore_stal = TRUE;
11284 }
11285
11286 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011287 * May repeat putting Windows for each tab, when "tabpages" is in
11288 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011289 * Don't use goto_tabpage(), it may change directory and trigger
11290 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011292 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011293 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011294 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011296 int need_tabnew = FALSE;
11297 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011298
Bram Moolenaar18144c82006-04-12 21:52:12 +000011299 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011301 tabpage_T *tp = find_tabpage(tabnr);
11302
11303 if (tp == NULL)
11304 break; /* done all tab pages */
11305 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011306 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011307 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011308 tab_topframe = topframe;
11309 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011310 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011311 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011312 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011313 tab_topframe = tp->tp_topframe;
11314 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011315 if (tabnr > 1)
11316 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318
Bram Moolenaar18144c82006-04-12 21:52:12 +000011319 /*
11320 * Before creating the window layout, try loading one file. If this
11321 * is aborted we don't end up with a number of useless windows.
11322 * This may have side effects! (e.g., compressed or network file).
11323 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011324 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011325 {
11326 if (ses_do_win(wp)
11327 && wp->w_buffer->b_ffname != NULL
11328 && !wp->w_buffer->b_help
11329#ifdef FEAT_QUICKFIX
11330 && !bt_nofile(wp->w_buffer)
11331#endif
11332 )
11333 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011334 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011335 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11336 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011337 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011338 if (!wp->w_arg_idx_invalid)
11339 edited_win = wp;
11340 break;
11341 }
11342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011344 /* If no file got edited create an empty tab page. */
11345 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11346 return FAIL;
11347
Bram Moolenaar18144c82006-04-12 21:52:12 +000011348 /*
11349 * Save current window layout.
11350 */
11351 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011353 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011355 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11356 return FAIL;
11357 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11358 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359
Bram Moolenaar18144c82006-04-12 21:52:12 +000011360 /*
11361 * Check if window sizes can be restored (no windows omitted).
11362 * Remember the window number of the current window after restoring.
11363 */
11364 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011365 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011366 {
11367 if (ses_do_win(wp))
11368 ++nr;
11369 else
11370 restore_size = FALSE;
11371 if (curwin == wp)
11372 cnr = nr;
11373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374
Bram Moolenaar18144c82006-04-12 21:52:12 +000011375 /* Go to the first window. */
11376 if (put_line(fd, "wincmd t") == FAIL)
11377 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011378
Bram Moolenaar18144c82006-04-12 21:52:12 +000011379 /*
11380 * If more than one window, see if sizes can be restored.
11381 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11382 * resized when moving between windows.
11383 * Do this before restoring the view, so that the topline and the
11384 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011385 * winminheight and winminwidth need to be set to avoid an error if the
11386 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011387 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011388 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011389 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011390 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011391 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392
Bram Moolenaar18144c82006-04-12 21:52:12 +000011393 /*
11394 * Restore the view of the window (options, file, cursor, etc.).
11395 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011396 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011397 {
11398 if (!ses_do_win(wp))
11399 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011400 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11401 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011402 return FAIL;
11403 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11404 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011405 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011406 }
11407
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011408 /* The argument index in the first tab page is zero, need to set it in
11409 * each window. For further tab pages it's the window where we do
11410 * "tabedit". */
11411 cur_arg_idx = next_arg_idx;
11412
Bram Moolenaar18144c82006-04-12 21:52:12 +000011413 /*
11414 * Restore cursor to the current window if it's not the first one.
11415 */
11416 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11417 || put_eol(fd) == FAIL))
11418 return FAIL;
11419
11420 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011421 * Restore window sizes again after jumping around in windows, because
11422 * the current window has a minimum size while others may not.
11423 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011424 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011425 return FAIL;
11426
Bram Moolenaar18144c82006-04-12 21:52:12 +000011427 /* Don't continue in another tab page when doing only the current one
11428 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011429 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011430 break;
11431 }
11432
11433 if (ssop_flags & SSOP_TABPAGES)
11434 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011435 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11436 || put_eol(fd) == FAIL)
11437 return FAIL;
11438 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011439 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11440 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011441
Bram Moolenaar9c102382006-05-03 21:26:49 +000011442 /*
11443 * Wipe out an empty unnamed buffer we started in.
11444 */
11445 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11446 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011447 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011448 return FAIL;
11449 if (put_line(fd, "endif") == FAIL)
11450 return FAIL;
11451 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11452 return FAIL;
11453
11454 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11455 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11456 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11457 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011458 /* Re-apply 'winminheight' and 'winminwidth'. */
11459 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11460 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11461 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462
11463 /*
11464 * Lastly, execute the x.vim file if it exists.
11465 */
11466 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11467 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011468 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469 || put_line(fd, "endif") == FAIL)
11470 return FAIL;
11471
11472 return OK;
11473}
11474
11475 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011476ses_winsizes(
11477 FILE *fd,
11478 int restore_size,
11479 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480{
11481 int n = 0;
11482 win_T *wp;
11483
11484 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11485 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011486 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487 {
11488 if (!ses_do_win(wp))
11489 continue;
11490 ++n;
11491
11492 /* restore height when not full height */
11493 if (wp->w_height + wp->w_status_height < topframe->fr_height
11494 && (fprintf(fd,
11495 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11496 n, (long)wp->w_height, Rows / 2, Rows) < 0
11497 || put_eol(fd) == FAIL))
11498 return FAIL;
11499
11500 /* restore width when not full width */
11501 if (wp->w_width < Columns && (fprintf(fd,
11502 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11503 n, (long)wp->w_width, Columns / 2, Columns) < 0
11504 || put_eol(fd) == FAIL))
11505 return FAIL;
11506 }
11507 }
11508 else
11509 {
11510 /* Just equalise window sizes */
11511 if (put_line(fd, "wincmd =") == FAIL)
11512 return FAIL;
11513 }
11514 return OK;
11515}
11516
11517/*
11518 * Write commands to "fd" to recursively create windows for frame "fr",
11519 * horizontally and vertically split.
11520 * After the commands the last window in the frame is the current window.
11521 * Returns FAIL when writing the commands to "fd" fails.
11522 */
11523 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011524ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525{
11526 frame_T *frc;
11527 int count = 0;
11528
11529 if (fr->fr_layout != FR_LEAF)
11530 {
11531 /* Find first frame that's not skipped and then create a window for
11532 * each following one (first frame is already there). */
11533 frc = ses_skipframe(fr->fr_child);
11534 if (frc != NULL)
11535 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11536 {
11537 /* Make window as big as possible so that we have lots of room
11538 * to split. */
11539 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11540 || put_line(fd, fr->fr_layout == FR_COL
11541 ? "split" : "vsplit") == FAIL)
11542 return FAIL;
11543 ++count;
11544 }
11545
11546 /* Go back to the first window. */
11547 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11548 ? "%dwincmd k" : "%dwincmd h", count) < 0
11549 || put_eol(fd) == FAIL))
11550 return FAIL;
11551
11552 /* Recursively create frames/windows in each window of this column or
11553 * row. */
11554 frc = ses_skipframe(fr->fr_child);
11555 while (frc != NULL)
11556 {
11557 ses_win_rec(fd, frc);
11558 frc = ses_skipframe(frc->fr_next);
11559 /* Go to next window. */
11560 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11561 return FAIL;
11562 }
11563 }
11564 return OK;
11565}
11566
11567/*
11568 * Skip frames that don't contain windows we want to save in the Session.
11569 * Returns NULL when there none.
11570 */
11571 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011572ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573{
11574 frame_T *frc;
11575
11576 for (frc = fr; frc != NULL; frc = frc->fr_next)
11577 if (ses_do_frame(frc))
11578 break;
11579 return frc;
11580}
11581
11582/*
11583 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11584 * the Session.
11585 */
11586 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011587ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588{
11589 frame_T *frc;
11590
11591 if (fr->fr_layout == FR_LEAF)
11592 return ses_do_win(fr->fr_win);
11593 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11594 if (ses_do_frame(frc))
11595 return TRUE;
11596 return FALSE;
11597}
11598
11599/*
11600 * Return non-zero if window "wp" is to be stored in the Session.
11601 */
11602 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011603ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604{
11605 if (wp->w_buffer->b_fname == NULL
11606#ifdef FEAT_QUICKFIX
11607 /* When 'buftype' is "nofile" can't restore the window contents. */
11608 || bt_nofile(wp->w_buffer)
11609#endif
11610 )
11611 return (ssop_flags & SSOP_BLANK);
11612 if (wp->w_buffer->b_help)
11613 return (ssop_flags & SSOP_HELP);
11614 return TRUE;
11615}
11616
11617/*
11618 * Write commands to "fd" to restore the view of a window.
11619 * Caller must make sure 'scrolloff' is zero.
11620 */
11621 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011622put_view(
11623 FILE *fd,
11624 win_T *wp,
11625 int add_edit, /* add ":edit" command to view */
11626 unsigned *flagp, /* vop_flags or ssop_flags */
11627 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011628 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011629{
11630 win_T *save_curwin;
11631 int f;
11632 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011633 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634
11635 /* Always restore cursor position for ":mksession". For ":mkview" only
11636 * when 'viewoptions' contains "cursor". */
11637 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11638
11639 /*
11640 * Local argument list.
11641 */
11642 if (wp->w_alist == &global_alist)
11643 {
11644 if (put_line(fd, "argglobal") == FAIL)
11645 return FAIL;
11646 }
11647 else
11648 {
11649 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11650 flagp == &vop_flags
11651 || !(*flagp & SSOP_CURDIR)
11652 || wp->w_localdir != NULL, flagp) == FAIL)
11653 return FAIL;
11654 }
11655
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011656 /* Only when part of a session: restore the argument index. Some
11657 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011658 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011659 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011661 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 || put_eol(fd) == FAIL)
11663 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011664 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665 }
11666
11667 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011668 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669 {
11670 /*
11671 * Load the file.
11672 */
11673 if (wp->w_buffer->b_ffname != NULL
11674#ifdef FEAT_QUICKFIX
11675 && !bt_nofile(wp->w_buffer)
11676#endif
11677 )
11678 {
11679 /*
11680 * Editing a file in this buffer: use ":edit file".
11681 * This may have side effects! (e.g., compressed or network file).
11682 */
11683 if (fputs("edit ", fd) < 0
11684 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11685 return FAIL;
11686 }
11687 else
11688 {
11689 /* No file in this buffer, just make it empty. */
11690 if (put_line(fd, "enew") == FAIL)
11691 return FAIL;
11692#ifdef FEAT_QUICKFIX
11693 if (wp->w_buffer->b_ffname != NULL)
11694 {
11695 /* The buffer does have a name, but it's not a file name. */
11696 if (fputs("file ", fd) < 0
11697 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11698 return FAIL;
11699 }
11700#endif
11701 do_cursor = FALSE;
11702 }
11703 }
11704
11705 /*
11706 * Local mappings and abbreviations.
11707 */
11708 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11709 && makemap(fd, wp->w_buffer) == FAIL)
11710 return FAIL;
11711
11712 /*
11713 * Local options. Need to go to the window temporarily.
11714 * Store only local values when using ":mkview" and when ":mksession" is
11715 * used and 'sessionoptions' doesn't include "options".
11716 * Some folding options are always stored when "folds" is included,
11717 * otherwise the folds would not be restored correctly.
11718 */
11719 save_curwin = curwin;
11720 curwin = wp;
11721 curbuf = curwin->w_buffer;
11722 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11723 f = makeset(fd, OPT_LOCAL,
11724 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11725#ifdef FEAT_FOLDING
11726 else if (*flagp & SSOP_FOLDS)
11727 f = makefoldset(fd);
11728#endif
11729 else
11730 f = OK;
11731 curwin = save_curwin;
11732 curbuf = curwin->w_buffer;
11733 if (f == FAIL)
11734 return FAIL;
11735
11736#ifdef FEAT_FOLDING
11737 /*
11738 * Save Folds when 'buftype' is empty and for help files.
11739 */
11740 if ((*flagp & SSOP_FOLDS)
11741 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011742# ifdef FEAT_QUICKFIX
11743 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11744# endif
11745 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746 {
11747 if (put_folds(fd, wp) == FAIL)
11748 return FAIL;
11749 }
11750#endif
11751
11752 /*
11753 * Set the cursor after creating folds, since that moves the cursor.
11754 */
11755 if (do_cursor)
11756 {
11757
11758 /* Restore the cursor line in the file and relatively in the
11759 * window. Don't use "G", it changes the jumplist. */
11760 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11761 (long)wp->w_cursor.lnum,
11762 (long)(wp->w_cursor.lnum - wp->w_topline),
11763 (long)wp->w_height / 2, (long)wp->w_height) < 0
11764 || put_eol(fd) == FAIL
11765 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11766 || put_line(fd, "exe s:l") == FAIL
11767 || put_line(fd, "normal! zt") == FAIL
11768 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11769 || put_eol(fd) == FAIL)
11770 return FAIL;
11771 /* Restore the cursor column and left offset when not wrapping. */
11772 if (wp->w_cursor.col == 0)
11773 {
11774 if (put_line(fd, "normal! 0") == FAIL)
11775 return FAIL;
11776 }
11777 else
11778 {
11779 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11780 {
11781 if (fprintf(fd,
11782 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011783 (long)wp->w_virtcol + 1,
11784 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785 (long)wp->w_width / 2, (long)wp->w_width) < 0
11786 || put_eol(fd) == FAIL
11787 || put_line(fd, "if s:c > 0") == FAIL
11788 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011789 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11790 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791 || put_eol(fd) == FAIL
11792 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011793 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794 || put_eol(fd) == FAIL
11795 || put_line(fd, "endif") == FAIL)
11796 return FAIL;
11797 }
11798 else
11799 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011800 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801 || put_eol(fd) == FAIL)
11802 return FAIL;
11803 }
11804 }
11805 }
11806
11807 /*
11808 * Local directory.
11809 */
11810 if (wp->w_localdir != NULL)
11811 {
11812 if (fputs("lcd ", fd) < 0
11813 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11814 || put_eol(fd) == FAIL)
11815 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011816 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817 }
11818
11819 return OK;
11820}
11821
11822/*
11823 * Write an argument list to the session file.
11824 * Returns FAIL if writing fails.
11825 */
11826 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011827ses_arglist(
11828 FILE *fd,
11829 char *cmd,
11830 garray_T *gap,
11831 int fullname, /* TRUE: use full path name */
11832 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833{
11834 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011835 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836 char_u *s;
11837
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011838 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11839 return FAIL;
11840 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 return FAIL;
11842 for (i = 0; i < gap->ga_len; ++i)
11843 {
11844 /* NULL file names are skipped (only happens when out of memory). */
11845 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11846 if (s != NULL)
11847 {
11848 if (fullname)
11849 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011850 buf = alloc(MAXPATHL);
11851 if (buf != NULL)
11852 {
11853 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11854 s = buf;
11855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011857 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011858 || ses_put_fname(fd, s, flagp) == FAIL
11859 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011860 {
11861 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011863 }
11864 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 }
11866 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011867 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868}
11869
11870/*
11871 * Write a buffer name to the session file.
11872 * Also ends the line.
11873 * Returns FAIL if writing fails.
11874 */
11875 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011876ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877{
11878 char_u *name;
11879
11880 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011881 * the session file will be sourced.
11882 * Don't do this for ":mkview", we don't know the current directory.
11883 * Don't do this after ":lcd", we don't keep track of what the current
11884 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885 if (buf->b_sfname != NULL
11886 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011887 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011888#ifdef FEAT_AUTOCHDIR
11889 && !p_acd
11890#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011891 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892 name = buf->b_sfname;
11893 else
11894 name = buf->b_ffname;
11895 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11896 return FAIL;
11897 return OK;
11898}
11899
11900/*
11901 * Write a file name to the session file.
11902 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11903 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011904 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905 */
11906 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011907ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908{
11909 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011910 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011911 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912
11913 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011914 if (sname == NULL)
11915 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011917 if (*flagp & SSOP_SLASH)
11918 {
11919 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011920 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011921 if (*p == '\\')
11922 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011924
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011925 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011926 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011928 if (p == NULL)
11929 return FAIL;
11930
11931 /* write the result */
11932 if (fputs((char *)p, fd) < 0)
11933 retval = FAIL;
11934
11935 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 return retval;
11937}
11938
11939/*
11940 * ":loadview [nr]"
11941 */
11942 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011943ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944{
11945 char_u *fname;
11946
11947 fname = get_view_file(*eap->arg);
11948 if (fname != NULL)
11949 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011950 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951 vim_free(fname);
11952 }
11953}
11954
11955/*
11956 * Get the name of the view file for the current buffer.
11957 */
11958 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011959get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960{
11961 int len = 0;
11962 char_u *p, *s;
11963 char_u *retval;
11964 char_u *sname;
11965
11966 if (curbuf->b_ffname == NULL)
11967 {
11968 EMSG(_(e_noname));
11969 return NULL;
11970 }
11971 sname = home_replace_save(NULL, curbuf->b_ffname);
11972 if (sname == NULL)
11973 return NULL;
11974
11975 /*
11976 * We want a file name without separators, because we're not going to make
11977 * a directory.
11978 * "normal" path separator -> "=+"
11979 * "=" -> "=="
11980 * ":" path separator -> "=-"
11981 */
11982 for (p = sname; *p; ++p)
11983 if (*p == '=' || vim_ispathsep(*p))
11984 ++len;
11985 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11986 if (retval != NULL)
11987 {
11988 STRCPY(retval, p_vdir);
11989 add_pathsep(retval);
11990 s = retval + STRLEN(retval);
11991 for (p = sname; *p; ++p)
11992 {
11993 if (*p == '=')
11994 {
11995 *s++ = '=';
11996 *s++ = '=';
11997 }
11998 else if (vim_ispathsep(*p))
11999 {
12000 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020012001#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002 if (*p == ':')
12003 *s++ = '-';
12004 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012006 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007 }
12008 else
12009 *s++ = *p;
12010 }
12011 *s++ = '=';
12012 *s++ = c;
12013 STRCPY(s, ".vim");
12014 }
12015
12016 vim_free(sname);
12017 return retval;
12018}
12019
12020#endif /* FEAT_SESSION */
12021
12022/*
12023 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12024 * Return FAIL for a write error.
12025 */
12026 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012027put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028{
12029 if (
12030#ifdef USE_CRNL
12031 (
12032# ifdef MKSESSION_NL
12033 !mksession_nl &&
12034# endif
12035 (putc('\r', fd) < 0)) ||
12036#endif
12037 (putc('\n', fd) < 0))
12038 return FAIL;
12039 return OK;
12040}
12041
12042/*
12043 * Write a line to "fd".
12044 * Return FAIL for a write error.
12045 */
12046 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012047put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048{
12049 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12050 return FAIL;
12051 return OK;
12052}
12053
12054#ifdef FEAT_VIMINFO
12055/*
12056 * ":rviminfo" and ":wviminfo".
12057 */
12058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012059ex_viminfo(
12060 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061{
12062 char_u *save_viminfo;
12063
12064 save_viminfo = p_viminfo;
12065 if (*p_viminfo == NUL)
12066 p_viminfo = (char_u *)"'100";
12067 if (eap->cmdidx == CMD_rviminfo)
12068 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012069 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12070 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071 EMSG(_("E195: Cannot open viminfo file for reading"));
12072 }
12073 else
12074 write_viminfo(eap->arg, eap->forceit);
12075 p_viminfo = save_viminfo;
12076}
12077#endif
12078
12079#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012080/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012081 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012082 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012085dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087 if (fname == NULL)
12088 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012089 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090}
12091#endif
12092
12093/*
12094 * ":behave {mswin,xterm}"
12095 */
12096 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012097ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098{
12099 if (STRCMP(eap->arg, "mswin") == 0)
12100 {
12101 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12102 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12103 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12104 set_option_value((char_u *)"keymodel", 0L,
12105 (char_u *)"startsel,stopsel", 0);
12106 }
12107 else if (STRCMP(eap->arg, "xterm") == 0)
12108 {
12109 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12110 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12111 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12112 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12113 }
12114 else
12115 EMSG2(_(e_invarg2), eap->arg);
12116}
12117
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012118#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12119/*
12120 * Function given to ExpandGeneric() to obtain the possible arguments of the
12121 * ":behave {mswin,xterm}" command.
12122 */
12123 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012124get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012125{
12126 if (idx == 0)
12127 return (char_u *)"mswin";
12128 if (idx == 1)
12129 return (char_u *)"xterm";
12130 return NULL;
12131}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012132
12133/*
12134 * Function given to ExpandGeneric() to obtain the possible arguments of the
12135 * ":messages {clear}" command.
12136 */
12137 char_u *
12138get_messages_arg(expand_T *xp UNUSED, int idx)
12139{
12140 if (idx == 0)
12141 return (char_u *)"clear";
12142 return NULL;
12143}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012144#endif
12145
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146#ifdef FEAT_AUTOCMD
12147static int filetype_detect = FALSE;
12148static int filetype_plugin = FALSE;
12149static int filetype_indent = FALSE;
12150
12151/*
12152 * ":filetype [plugin] [indent] {on,off,detect}"
12153 * on: Load the filetype.vim file to install autocommands for file types.
12154 * off: Load the ftoff.vim file to remove all autocommands for file types.
12155 * plugin on: load filetype.vim and ftplugin.vim
12156 * plugin off: load ftplugof.vim
12157 * indent on: load filetype.vim and indent.vim
12158 * indent off: load indoff.vim
12159 */
12160 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012161ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162{
12163 char_u *arg = eap->arg;
12164 int plugin = FALSE;
12165 int indent = FALSE;
12166
12167 if (*eap->arg == NUL)
12168 {
12169 /* Print current status. */
12170 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12171 filetype_detect ? "ON" : "OFF",
12172 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12173 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12174 return;
12175 }
12176
12177 /* Accept "plugin" and "indent" in any order. */
12178 for (;;)
12179 {
12180 if (STRNCMP(arg, "plugin", 6) == 0)
12181 {
12182 plugin = TRUE;
12183 arg = skipwhite(arg + 6);
12184 continue;
12185 }
12186 if (STRNCMP(arg, "indent", 6) == 0)
12187 {
12188 indent = TRUE;
12189 arg = skipwhite(arg + 6);
12190 continue;
12191 }
12192 break;
12193 }
12194 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12195 {
12196 if (*arg == 'o' || !filetype_detect)
12197 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012198 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199 filetype_detect = TRUE;
12200 if (plugin)
12201 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012202 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203 filetype_plugin = TRUE;
12204 }
12205 if (indent)
12206 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012207 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208 filetype_indent = TRUE;
12209 }
12210 }
12211 if (*arg == 'd')
12212 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012213 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012214 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215 }
12216 }
12217 else if (STRCMP(arg, "off") == 0)
12218 {
12219 if (plugin || indent)
12220 {
12221 if (plugin)
12222 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012223 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224 filetype_plugin = FALSE;
12225 }
12226 if (indent)
12227 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012228 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229 filetype_indent = FALSE;
12230 }
12231 }
12232 else
12233 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012234 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235 filetype_detect = FALSE;
12236 }
12237 }
12238 else
12239 EMSG2(_(e_invarg2), arg);
12240}
12241
12242/*
12243 * ":setfiletype {name}"
12244 */
12245 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012246ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247{
12248 if (!did_filetype)
12249 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
12250}
12251#endif
12252
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012254ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255{
12256#ifdef FEAT_DIGRAPHS
12257 if (*eap->arg != NUL)
12258 putdigraph(eap->arg);
12259 else
12260 listdigraphs();
12261#else
12262 EMSG(_("E196: No digraphs in this version"));
12263#endif
12264}
12265
12266 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012267ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268{
12269 int flags = 0;
12270
12271 if (eap->cmdidx == CMD_setlocal)
12272 flags = OPT_LOCAL;
12273 else if (eap->cmdidx == CMD_setglobal)
12274 flags = OPT_GLOBAL;
12275#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12276 if (cmdmod.browse && flags == 0)
12277 ex_options(eap);
12278 else
12279#endif
12280 (void)do_set(eap->arg, flags);
12281}
12282
12283#ifdef FEAT_SEARCH_EXTRA
12284/*
12285 * ":nohlsearch"
12286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012288ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012290 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012291 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292}
12293
12294/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012295 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296 * Sets nextcmd to the start of the next command, if any. Also called when
12297 * skipping commands to find the next command.
12298 */
12299 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012300ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301{
12302 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012303 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012304 char_u *end;
12305 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012306 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012307
12308 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012309 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012310 else
12311 {
12312 EMSG(e_invcmd);
12313 return;
12314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315
12316 /* First clear any old pattern. */
12317 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012318 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012319
12320 if (ends_excmd(*eap->arg))
12321 end = eap->arg;
12322 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012323 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324 end = eap->arg + 4;
12325 else
12326 {
12327 p = skiptowhite(eap->arg);
12328 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012329 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330 p = skipwhite(p);
12331 if (*p == NUL)
12332 {
12333 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012334 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335 EMSG2(_(e_invarg2), eap->arg);
12336 return;
12337 }
12338 end = skip_regexp(p + 1, *p, TRUE, NULL);
12339 if (!eap->skip)
12340 {
12341 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12342 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012343 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 eap->errmsg = e_trailing;
12345 return;
12346 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012347 if (*end != *p)
12348 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012349 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012350 EMSG2(_(e_invarg2), p);
12351 return;
12352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353
12354 c = *end;
12355 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012356 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012357 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012358 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359 }
12360 }
12361 eap->nextcmd = find_nextcmd(end);
12362}
12363#endif
12364
12365#ifdef FEAT_CRYPT
12366/*
12367 * ":X": Get crypt key
12368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012370ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012372 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012373 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374}
12375#endif
12376
12377#ifdef FEAT_FOLDING
12378 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012379ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380{
12381 if (foldManualAllowed(TRUE))
12382 foldCreate(eap->line1, eap->line2);
12383}
12384
12385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012386ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387{
12388 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12389 eap->forceit, FALSE);
12390}
12391
12392 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012393ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394{
12395 linenr_T lnum;
12396
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012397#ifdef FEAT_CLIPBOARD
12398 start_global_changes();
12399#endif
12400
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401 /* First set the marks for all lines closed/open. */
12402 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12403 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12404 ml_setmarked(lnum);
12405
12406 /* Execute the command on the marked lines. */
12407 global_exe(eap->arg);
12408 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012409#ifdef FEAT_CLIPBOARD
12410 end_global_changes();
12411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412}
12413#endif