blob: 17fec33ecd0328afc10dbd34c7b9a47647338806 [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
569static int command_count = 539;
570
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 {
3253 if (command_count != (int)CMD_SIZE)
3254 {
3255 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3256 getout(1);
3257 }
3258
3259 /* Use a precomputed index for fast look-up in cmdnames[]
3260 * taking into account the first 2 letters of eap->cmd. */
3261 int c1 = eap->cmd[0];
3262 int c2 = eap->cmd[1];
3263 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3264 if (ASCII_ISLOWER(c2))
3265 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003268 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
3270 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3271 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3272 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3273 (size_t)len) == 0)
3274 {
3275#ifdef FEAT_EVAL
3276 if (full != NULL
3277 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3278 *full = TRUE;
3279#endif
3280 break;
3281 }
3282
3283#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003284 /* Look for a user defined command as a last resort. Let ":Print" be
3285 * overruled by a user defined command. */
3286 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3287 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003289 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 while (ASCII_ISALNUM(*p))
3291 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003292 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 }
3294#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003295 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 eap->cmdidx = CMD_SIZE;
3297 }
3298
3299 return p;
3300}
3301
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003302#ifdef FEAT_USR_CMDS
3303/*
3304 * Search for a user command that matches "eap->cmd".
3305 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3306 * Return a pointer to just after the command.
3307 * Return NULL if there is no matching command.
3308 */
3309 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003310find_ucmd(
3311 exarg_T *eap,
3312 char_u *p, /* end of the command (possibly including count) */
3313 int *full, /* set to TRUE for a full match */
3314 expand_T *xp, /* used for completion, NULL otherwise */
3315 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003316{
3317 int len = (int)(p - eap->cmd);
3318 int j, k, matchlen = 0;
3319 ucmd_T *uc;
3320 int found = FALSE;
3321 int possible = FALSE;
3322 char_u *cp, *np; /* Point into typed cmd and test name */
3323 garray_T *gap;
3324 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3325 only full match global is accepted. */
3326
3327 /*
3328 * Look for buffer-local user commands first, then global ones.
3329 */
3330 gap = &curbuf->b_ucmds;
3331 for (;;)
3332 {
3333 for (j = 0; j < gap->ga_len; ++j)
3334 {
3335 uc = USER_CMD_GA(gap, j);
3336 cp = eap->cmd;
3337 np = uc->uc_name;
3338 k = 0;
3339 while (k < len && *np != NUL && *cp++ == *np++)
3340 k++;
3341 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3342 {
3343 /* If finding a second match, the command is ambiguous. But
3344 * not if a buffer-local command wasn't a full match and a
3345 * global command is a full match. */
3346 if (k == len && found && *np != NUL)
3347 {
3348 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003349 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003350 amb_local = TRUE;
3351 }
3352
3353 if (!found || (k == len && *np == NUL))
3354 {
3355 /* If we matched up to a digit, then there could
3356 * be another command including the digit that we
3357 * should use instead.
3358 */
3359 if (k == len)
3360 found = TRUE;
3361 else
3362 possible = TRUE;
3363
3364 if (gap == &ucmds)
3365 eap->cmdidx = CMD_USER;
3366 else
3367 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003368 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003369 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003370 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003371
3372# ifdef FEAT_CMDL_COMPL
3373 if (compl != NULL)
3374 *compl = uc->uc_compl;
3375# ifdef FEAT_EVAL
3376 if (xp != NULL)
3377 {
3378 xp->xp_arg = uc->uc_compl_arg;
3379 xp->xp_scriptID = uc->uc_scriptID;
3380 }
3381# endif
3382# endif
3383 /* Do not search for further abbreviations
3384 * if this is an exact match. */
3385 matchlen = k;
3386 if (k == len && *np == NUL)
3387 {
3388 if (full != NULL)
3389 *full = TRUE;
3390 amb_local = FALSE;
3391 break;
3392 }
3393 }
3394 }
3395 }
3396
3397 /* Stop if we found a full match or searched all. */
3398 if (j < gap->ga_len || gap == &ucmds)
3399 break;
3400 gap = &ucmds;
3401 }
3402
3403 /* Only found ambiguous matches. */
3404 if (amb_local)
3405 {
3406 if (xp != NULL)
3407 xp->xp_context = EXPAND_UNSUCCESSFUL;
3408 return NULL;
3409 }
3410
3411 /* The match we found may be followed immediately by a number. Move "p"
3412 * back to point to it. */
3413 if (found || possible)
3414 return p + (matchlen - len);
3415 return p;
3416}
3417#endif
3418
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003420static struct cmdmod
3421{
3422 char *name;
3423 int minlen;
3424 int has_count; /* :123verbose :3tab */
3425} cmdmods[] = {
3426 {"aboveleft", 3, FALSE},
3427 {"belowright", 3, FALSE},
3428 {"botright", 2, FALSE},
3429 {"browse", 3, FALSE},
3430 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003431 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003432 {"hide", 3, FALSE},
3433 {"keepalt", 5, FALSE},
3434 {"keepjumps", 5, FALSE},
3435 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003436 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003437 {"leftabove", 5, FALSE},
3438 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003439 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003440 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003441 {"rightbelow", 6, FALSE},
3442 {"sandbox", 3, FALSE},
3443 {"silent", 3, FALSE},
3444 {"tab", 3, TRUE},
3445 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003446 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003447 {"verbose", 4, TRUE},
3448 {"vertical", 4, FALSE},
3449};
3450
3451/*
3452 * Return length of a command modifier (including optional count).
3453 * Return zero when it's not a modifier.
3454 */
3455 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003456modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003457{
3458 int i, j;
3459 char_u *p = cmd;
3460
3461 if (VIM_ISDIGIT(*cmd))
3462 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003463 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003464 {
3465 for (j = 0; p[j] != NUL; ++j)
3466 if (p[j] != cmdmods[i].name[j])
3467 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003468 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003469 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003470 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003471 }
3472 return 0;
3473}
3474
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475/*
3476 * Return > 0 if an Ex command "name" exists.
3477 * Return 2 if there is an exact match.
3478 * Return 3 if there is an ambiguous match.
3479 */
3480 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003481cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482{
3483 exarg_T ea;
3484 int full = FALSE;
3485 int i;
3486 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003487 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
3489 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003490 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
3492 for (j = 0; name[j] != NUL; ++j)
3493 if (name[j] != cmdmods[i].name[j])
3494 break;
3495 if (name[j] == NUL && j >= cmdmods[i].minlen)
3496 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3497 }
3498
Bram Moolenaara9587612006-05-04 21:47:50 +00003499 /* Check built-in commands and user defined commands.
3500 * For ":2match" and ":3match" we need to skip the number. */
3501 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003503 p = find_command(&ea, &full);
3504 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003506 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3507 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003508 if (*skipwhite(p) != NUL)
3509 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3511}
3512#endif
3513
3514/*
3515 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3516 * we don't need/want deleted. Maybe this could be done better if we didn't
3517 * repeat all this stuff. The only problem is that they may not stay
3518 * perfectly compatible with each other, but then the command line syntax
3519 * probably won't change that much -- webb.
3520 */
3521 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003522set_one_cmd_context(
3523 expand_T *xp,
3524 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525{
3526 char_u *p;
3527 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003528 int len = 0;
3529 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3531 int compl = EXPAND_NOTHING;
3532#endif
3533#ifdef FEAT_CMDL_COMPL
3534 int delim;
3535#endif
3536 int forceit = FALSE;
3537 int usefilter = FALSE; /* filter instead of file name */
3538
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003539 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 xp->xp_pattern = buff;
3541 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003542 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
3544/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003545 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 */
3547 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3548 ;
3549 xp->xp_pattern = cmd;
3550
3551 if (*cmd == NUL)
3552 return NULL;
3553 if (*cmd == '"') /* ignore comment lines */
3554 {
3555 xp->xp_context = EXPAND_NOTHING;
3556 return NULL;
3557 }
3558
3559/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003560 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 */
3562 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 xp->xp_pattern = cmd;
3564 if (*cmd == NUL)
3565 return NULL;
3566 if (*cmd == '"')
3567 {
3568 xp->xp_context = EXPAND_NOTHING;
3569 return NULL;
3570 }
3571
3572 if (*cmd == '|' || *cmd == '\n')
3573 return cmd + 1; /* There's another command */
3574
3575 /*
3576 * Isolate the command and search for it in the command table.
3577 * Exceptions:
3578 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003579 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3581 */
3582 if (*cmd == 'k' && cmd[1] != 'e')
3583 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003584 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 p = cmd + 1;
3586 }
3587 else
3588 {
3589 p = cmd;
3590 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3591 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003592 /* a user command may contain digits */
3593 if (ASCII_ISUPPER(cmd[0]))
3594 while (ASCII_ISALNUM(*p) || *p == '*')
3595 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003596 /* for python 3.x: ":py3*" commands completion */
3597 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003598 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003599 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003600 while (ASCII_ISALPHA(*p) || *p == '*')
3601 ++p;
3602 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003603 /* check for non-alpha command */
3604 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3605 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003606 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003608 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 {
3610 xp->xp_context = EXPAND_UNSUCCESSFUL;
3611 return NULL;
3612 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003613 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003614 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3615 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3616 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 break;
3618
3619#ifdef FEAT_USR_CMDS
3620 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3622 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623#endif
3624 }
3625
3626 /*
3627 * If the cursor is touching the command, and it ends in an alpha-numeric
3628 * character, complete the command name.
3629 */
3630 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3631 return NULL;
3632
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003633 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
3635 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3636 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003637 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 p = cmd + 1;
3639 }
3640#ifdef FEAT_USR_CMDS
3641 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3642 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003643 ea.cmd = cmd;
3644 p = find_ucmd(&ea, p, NULL, xp,
3645# if defined(FEAT_CMDL_COMPL)
3646 &compl
3647# else
3648 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003650 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003651 if (p == NULL)
3652 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 }
3654#endif
3655 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003656 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
3658 /* Not still touching the command and it was an illegal one */
3659 xp->xp_context = EXPAND_UNSUCCESSFUL;
3660 return NULL;
3661 }
3662
3663 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3664
3665 if (*p == '!') /* forced commands */
3666 {
3667 forceit = TRUE;
3668 ++p;
3669 }
3670
3671/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003672 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003674 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003675 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676
3677 arg = skipwhite(p);
3678
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003679 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 {
3681 if (*arg == '>') /* append */
3682 {
3683 if (*++arg == '>')
3684 ++arg;
3685 arg = skipwhite(arg);
3686 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003687 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 {
3689 ++arg;
3690 usefilter = TRUE;
3691 }
3692 }
3693
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003694 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 {
3696 usefilter = forceit; /* :r! filter if forced */
3697 if (*arg == '!') /* :r !filter */
3698 {
3699 ++arg;
3700 usefilter = TRUE;
3701 }
3702 }
3703
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003704 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 {
3706 while (*arg == *cmd) /* allow any number of '>' or '<' */
3707 ++arg;
3708 arg = skipwhite(arg);
3709 }
3710
3711 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003712 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 {
3714 /* Check if we're in the +command */
3715 p = arg + 1;
3716 arg = skip_cmd_arg(arg, FALSE);
3717
3718 /* Still touching the command after '+'? */
3719 if (*arg == NUL)
3720 return p;
3721
3722 /* Skip space(s) after +command to get to the real argument */
3723 arg = skipwhite(arg);
3724 }
3725
3726 /*
3727 * Check for '|' to separate commands and '"' to start comments.
3728 * Don't do this for ":read !cmd" and ":write !cmd".
3729 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003730 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 {
3732 p = arg;
3733 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003734 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 p += 2;
3736 while (*p)
3737 {
3738 if (*p == Ctrl_V)
3739 {
3740 if (p[1] != NUL)
3741 ++p;
3742 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003743 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 || *p == '|' || *p == '\n')
3745 {
3746 if (*(p - 1) != '\\')
3747 {
3748 if (*p == '|' || *p == '\n')
3749 return p + 1;
3750 return NULL; /* It's a comment */
3751 }
3752 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003753 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 }
3755 }
3756
3757 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003758 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 vim_strchr((char_u *)"|\"", *arg) == NULL)
3760 return NULL;
3761
3762 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003763 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003765 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003766 while (*p && p < buff + len)
3767 {
3768 if (*p == ' ' || *p == TAB)
3769 {
3770 /* argument starts after a space */
3771 xp->xp_pattern = ++p;
3772 }
3773 else
3774 {
3775 if (*p == '\\' && *(p + 1) != NUL)
3776 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003777 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003778 }
3779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003781 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003783 int c;
3784 int in_quote = FALSE;
3785 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786
3787 /*
3788 * Allow spaces within back-quotes to count as part of the argument
3789 * being expanded.
3790 */
3791 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003792 p = xp->xp_pattern;
3793 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003795#ifdef FEAT_MBYTE
3796 if (has_mbyte)
3797 c = mb_ptr2char(p);
3798 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003800 c = *p;
3801 if (c == '\\' && p[1] != NUL)
3802 ++p;
3803 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
3805 if (!in_quote)
3806 {
3807 xp->xp_pattern = p;
3808 bow = p + 1;
3809 }
3810 in_quote = !in_quote;
3811 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003812 /* An argument can contain just about everything, except
3813 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003814 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003815#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003816 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003817#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003818 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003819 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003820 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003821 while (*p != NUL)
3822 {
3823#ifdef FEAT_MBYTE
3824 if (has_mbyte)
3825 c = mb_ptr2char(p);
3826 else
3827#endif
3828 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003829 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003830 break;
3831#ifdef FEAT_MBYTE
3832 if (has_mbyte)
3833 len = (*mb_ptr2len)(p);
3834 else
3835#endif
3836 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003837 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003838 }
3839 if (in_quote)
3840 bow = p;
3841 else
3842 xp->xp_pattern = p;
3843 p -= len;
3844 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003845 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 }
3847
3848 /*
3849 * If we are still inside the quotes, and we passed a space, just
3850 * expand from there.
3851 */
3852 if (bow != NULL && in_quote)
3853 xp->xp_pattern = bow;
3854 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003855
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003856 /* For a shell command more chars need to be escaped. */
3857 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003858 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003859#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003860 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003861#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003862 /* When still after the command name expand executables. */
3863 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003864 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866
3867 /* Check for environment variable */
3868 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003869#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 || *xp->xp_pattern == '%'
3871#endif
3872 )
3873 {
3874 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3875 if (!vim_isIDc(*p))
3876 break;
3877 if (*p == NUL)
3878 {
3879 xp->xp_context = EXPAND_ENV_VARS;
3880 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003881#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3882 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003883 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003884 compl = EXPAND_ENV_VARS;
3885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 }
3887 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003888#if defined(FEAT_CMDL_COMPL)
3889 /* Check for user names */
3890 if (*xp->xp_pattern == '~')
3891 {
3892 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3893 ;
3894 /* Complete ~user only if it partially matches a user name.
3895 * A full match ~user<Tab> will be replaced by user's home
3896 * directory i.e. something like ~user<Tab> -> /home/user/ */
3897 if (*p == NUL && p > xp->xp_pattern + 1
3898 && match_user(xp->xp_pattern + 1) == 1)
3899 {
3900 xp->xp_context = EXPAND_USER;
3901 ++xp->xp_pattern;
3902 }
3903 }
3904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 }
3906
3907/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003908 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003910 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003912 case CMD_find:
3913 case CMD_sfind:
3914 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003915 if (xp->xp_context == EXPAND_FILES)
3916 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003917 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 case CMD_cd:
3919 case CMD_chdir:
3920 case CMD_lcd:
3921 case CMD_lchdir:
3922 if (xp->xp_context == EXPAND_FILES)
3923 xp->xp_context = EXPAND_DIRECTORIES;
3924 break;
3925 case CMD_help:
3926 xp->xp_context = EXPAND_HELP;
3927 xp->xp_pattern = arg;
3928 break;
3929
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003930 /* Command modifiers: return the argument.
3931 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003933 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 case CMD_belowright:
3935 case CMD_botright:
3936 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003937 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003938 case CMD_cdo:
3939 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003941 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 case CMD_folddoclosed:
3943 case CMD_folddoopen:
3944 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003945 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 case CMD_keepjumps:
3947 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003948 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003949 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003951 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003953 case CMD_noautocmd:
3954 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003956 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003958 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003959 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 case CMD_topleft:
3961 case CMD_verbose:
3962 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003963 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 return arg;
3965
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003966 case CMD_filter:
3967 if (*arg != NUL)
3968 arg = skip_vimgrep_pat(arg, NULL, NULL);
3969 if (arg == NULL || *arg == NUL)
3970 {
3971 xp->xp_context = EXPAND_NOTHING;
3972 return NULL;
3973 }
3974 return skipwhite(arg);
3975
Bram Moolenaar4f688582007-07-24 12:34:30 +00003976#ifdef FEAT_CMDL_COMPL
3977# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 case CMD_match:
3979 if (*arg == NUL || !ends_excmd(*arg))
3980 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003981 /* also complete "None" */
3982 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 arg = skipwhite(skiptowhite(arg));
3984 if (*arg != NUL)
3985 {
3986 xp->xp_context = EXPAND_NOTHING;
3987 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3988 }
3989 }
3990 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003991# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993/*
3994 * All completion for the +cmdline_compl feature goes here.
3995 */
3996
3997# ifdef FEAT_USR_CMDS
3998 case CMD_command:
3999 /* Check for attributes */
4000 while (*arg == '-')
4001 {
4002 arg++; /* Skip "-" */
4003 p = skiptowhite(arg);
4004 if (*p == NUL)
4005 {
4006 /* Cursor is still in the attribute */
4007 p = vim_strchr(arg, '=');
4008 if (p == NULL)
4009 {
4010 /* No "=", so complete attribute names */
4011 xp->xp_context = EXPAND_USER_CMD_FLAGS;
4012 xp->xp_pattern = arg;
4013 return NULL;
4014 }
4015
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004016 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 * their arguments as well.
4018 */
4019 if (STRNICMP(arg, "complete", p - arg) == 0)
4020 {
4021 xp->xp_context = EXPAND_USER_COMPLETE;
4022 xp->xp_pattern = p + 1;
4023 return NULL;
4024 }
4025 else if (STRNICMP(arg, "nargs", p - arg) == 0)
4026 {
4027 xp->xp_context = EXPAND_USER_NARGS;
4028 xp->xp_pattern = p + 1;
4029 return NULL;
4030 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004031 else if (STRNICMP(arg, "addr", p - arg) == 0)
4032 {
4033 xp->xp_context = EXPAND_USER_ADDR_TYPE;
4034 xp->xp_pattern = p + 1;
4035 return NULL;
4036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 return NULL;
4038 }
4039 arg = skipwhite(p);
4040 }
4041
4042 /* After the attributes comes the new command name */
4043 p = skiptowhite(arg);
4044 if (*p == NUL)
4045 {
4046 xp->xp_context = EXPAND_USER_COMMANDS;
4047 xp->xp_pattern = arg;
4048 break;
4049 }
4050
4051 /* And finally comes a normal command */
4052 return skipwhite(p);
4053
4054 case CMD_delcommand:
4055 xp->xp_context = EXPAND_USER_COMMANDS;
4056 xp->xp_pattern = arg;
4057 break;
4058# endif
4059
4060 case CMD_global:
4061 case CMD_vglobal:
4062 delim = *arg; /* get the delimiter */
4063 if (delim)
4064 ++arg; /* skip delimiter if there is one */
4065
4066 while (arg[0] != NUL && arg[0] != delim)
4067 {
4068 if (arg[0] == '\\' && arg[1] != NUL)
4069 ++arg;
4070 ++arg;
4071 }
4072 if (arg[0] != NUL)
4073 return arg + 1;
4074 break;
4075 case CMD_and:
4076 case CMD_substitute:
4077 delim = *arg;
4078 if (delim)
4079 {
4080 /* skip "from" part */
4081 ++arg;
4082 arg = skip_regexp(arg, delim, p_magic, NULL);
4083 }
4084 /* skip "to" part */
4085 while (arg[0] != NUL && arg[0] != delim)
4086 {
4087 if (arg[0] == '\\' && arg[1] != NUL)
4088 ++arg;
4089 ++arg;
4090 }
4091 if (arg[0] != NUL) /* skip delimiter */
4092 ++arg;
4093 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4094 ++arg;
4095 if (arg[0] != NUL)
4096 return arg;
4097 break;
4098 case CMD_isearch:
4099 case CMD_dsearch:
4100 case CMD_ilist:
4101 case CMD_dlist:
4102 case CMD_ijump:
4103 case CMD_psearch:
4104 case CMD_djump:
4105 case CMD_isplit:
4106 case CMD_dsplit:
4107 arg = skipwhite(skipdigits(arg)); /* skip count */
4108 if (*arg == '/') /* Match regexp, not just whole words */
4109 {
4110 for (++arg; *arg && *arg != '/'; arg++)
4111 if (*arg == '\\' && arg[1] != NUL)
4112 arg++;
4113 if (*arg)
4114 {
4115 arg = skipwhite(arg + 1);
4116
4117 /* Check for trailing illegal characters */
4118 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4119 xp->xp_context = EXPAND_NOTHING;
4120 else
4121 return arg;
4122 }
4123 }
4124 break;
4125#ifdef FEAT_AUTOCMD
4126 case CMD_autocmd:
4127 return set_context_in_autocmd(xp, arg, FALSE);
4128
4129 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004130 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 return set_context_in_autocmd(xp, arg, TRUE);
4132#endif
4133 case CMD_set:
4134 set_context_in_set_cmd(xp, arg, 0);
4135 break;
4136 case CMD_setglobal:
4137 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4138 break;
4139 case CMD_setlocal:
4140 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4141 break;
4142 case CMD_tag:
4143 case CMD_stag:
4144 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004145 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 case CMD_tselect:
4147 case CMD_stselect:
4148 case CMD_ptselect:
4149 case CMD_tjump:
4150 case CMD_stjump:
4151 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004152 if (*p_wop != NUL)
4153 xp->xp_context = EXPAND_TAGS_LISTFILES;
4154 else
4155 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 xp->xp_pattern = arg;
4157 break;
4158 case CMD_augroup:
4159 xp->xp_context = EXPAND_AUGROUP;
4160 xp->xp_pattern = arg;
4161 break;
4162#ifdef FEAT_SYN_HL
4163 case CMD_syntax:
4164 set_context_in_syntax_cmd(xp, arg);
4165 break;
4166#endif
4167#ifdef FEAT_EVAL
4168 case CMD_let:
4169 case CMD_if:
4170 case CMD_elseif:
4171 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004172 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 case CMD_echo:
4174 case CMD_echon:
4175 case CMD_execute:
4176 case CMD_echomsg:
4177 case CMD_echoerr:
4178 case CMD_call:
4179 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004180 case CMD_cexpr:
4181 case CMD_caddexpr:
4182 case CMD_cgetexpr:
4183 case CMD_lexpr:
4184 case CMD_laddexpr:
4185 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004186 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 break;
4188
4189 case CMD_unlet:
4190 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4191 arg = xp->xp_pattern + 1;
4192 xp->xp_context = EXPAND_USER_VARS;
4193 xp->xp_pattern = arg;
4194 break;
4195
4196 case CMD_function:
4197 case CMD_delfunction:
4198 xp->xp_context = EXPAND_USER_FUNC;
4199 xp->xp_pattern = arg;
4200 break;
4201
4202 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004203 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 break;
4205#endif
4206 case CMD_highlight:
4207 set_context_in_highlight_cmd(xp, arg);
4208 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004209#ifdef FEAT_CSCOPE
4210 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004211 case CMD_lcscope:
4212 case CMD_scscope:
4213 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004214 break;
4215#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004216#ifdef FEAT_SIGNS
4217 case CMD_sign:
4218 set_context_in_sign_cmd(xp, arg);
4219 break;
4220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221#ifdef FEAT_LISTCMDS
4222 case CMD_bdelete:
4223 case CMD_bwipeout:
4224 case CMD_bunload:
4225 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4226 arg = xp->xp_pattern + 1;
4227 /*FALLTHROUGH*/
4228 case CMD_buffer:
4229 case CMD_sbuffer:
4230 case CMD_checktime:
4231 xp->xp_context = EXPAND_BUFFERS;
4232 xp->xp_pattern = arg;
4233 break;
4234#endif
4235#ifdef FEAT_USR_CMDS
4236 case CMD_USER:
4237 case CMD_USER_BUF:
4238 if (compl != EXPAND_NOTHING)
4239 {
4240 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004241 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 {
4243# ifdef FEAT_MENU
4244 if (compl == EXPAND_MENUS)
4245 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4246# endif
4247 if (compl == EXPAND_COMMANDS)
4248 return arg;
4249 if (compl == EXPAND_MAPPINGS)
4250 return set_context_in_map_cmd(xp, (char_u *)"map",
4251 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004252 /* Find start of last argument. */
4253 p = arg;
4254 while (*p)
4255 {
4256 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004257 /* argument starts after a space */
4258 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004259 else if (*p == '\\' && *(p + 1) != NUL)
4260 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004261 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 xp->xp_pattern = arg;
4264 }
4265 xp->xp_context = compl;
4266 }
4267 break;
4268#endif
4269 case CMD_map: case CMD_noremap:
4270 case CMD_nmap: case CMD_nnoremap:
4271 case CMD_vmap: case CMD_vnoremap:
4272 case CMD_omap: case CMD_onoremap:
4273 case CMD_imap: case CMD_inoremap:
4274 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004275 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004276 case CMD_smap: case CMD_snoremap:
4277 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004279 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 case CMD_unmap:
4281 case CMD_nunmap:
4282 case CMD_vunmap:
4283 case CMD_ounmap:
4284 case CMD_iunmap:
4285 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004286 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004287 case CMD_sunmap:
4288 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004290 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 case CMD_abbreviate: case CMD_noreabbrev:
4292 case CMD_cabbrev: case CMD_cnoreabbrev:
4293 case CMD_iabbrev: case CMD_inoreabbrev:
4294 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004295 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 case CMD_unabbreviate:
4297 case CMD_cunabbrev:
4298 case CMD_iunabbrev:
4299 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004300 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301#ifdef FEAT_MENU
4302 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4303 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4304 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4305 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4306 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4307 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4308 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4309 case CMD_tmenu: case CMD_tunmenu:
4310 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4311 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4312#endif
4313
4314 case CMD_colorscheme:
4315 xp->xp_context = EXPAND_COLORS;
4316 xp->xp_pattern = arg;
4317 break;
4318
4319 case CMD_compiler:
4320 xp->xp_context = EXPAND_COMPILER;
4321 xp->xp_pattern = arg;
4322 break;
4323
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004324 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004325 xp->xp_context = EXPAND_OWNSYNTAX;
4326 xp->xp_pattern = arg;
4327 break;
4328
4329 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004330 xp->xp_context = EXPAND_FILETYPE;
4331 xp->xp_pattern = arg;
4332 break;
4333
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004334 case CMD_packadd:
4335 xp->xp_context = EXPAND_PACKADD;
4336 xp->xp_pattern = arg;
4337 break;
4338
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4340 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4341 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004342 p = skiptowhite(arg);
4343 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 {
4345 xp->xp_context = EXPAND_LANGUAGE;
4346 xp->xp_pattern = arg;
4347 }
4348 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004349 {
4350 if ( STRNCMP(arg, "messages", p - arg) == 0
4351 || STRNCMP(arg, "ctype", p - arg) == 0
4352 || STRNCMP(arg, "time", p - arg) == 0)
4353 {
4354 xp->xp_context = EXPAND_LOCALES;
4355 xp->xp_pattern = skipwhite(p);
4356 }
4357 else
4358 xp->xp_context = EXPAND_NOTHING;
4359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 break;
4361#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004362#if defined(FEAT_PROFILE)
4363 case CMD_profile:
4364 set_context_in_profile_cmd(xp, arg);
4365 break;
4366#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004367 case CMD_behave:
4368 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004369 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004370 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004372 case CMD_messages:
4373 xp->xp_context = EXPAND_MESSAGES;
4374 xp->xp_pattern = arg;
4375 break;
4376
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004377#if defined(FEAT_CMDHIST)
4378 case CMD_history:
4379 xp->xp_context = EXPAND_HISTORY;
4380 xp->xp_pattern = arg;
4381 break;
4382#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004383#if defined(FEAT_PROFILE)
4384 case CMD_syntime:
4385 xp->xp_context = EXPAND_SYNTIME;
4386 xp->xp_pattern = arg;
4387 break;
4388#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004389
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390#endif /* FEAT_CMDL_COMPL */
4391
4392 default:
4393 break;
4394 }
4395 return NULL;
4396}
4397
4398/*
4399 * skip a range specifier of the form: addr [,addr] [;addr] ..
4400 *
4401 * Backslashed delimiters after / or ? will be skipped, and commands will
4402 * not be expanded between /'s and ?'s or after "'".
4403 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004404 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 * Returns the "cmd" pointer advanced to beyond the range.
4406 */
4407 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004408skip_range(
4409 char_u *cmd,
4410 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004412 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004414 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004416 if (*cmd == '\\')
4417 {
4418 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4419 ++cmd;
4420 else
4421 break;
4422 }
4423 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 {
4425 if (*++cmd == NUL && ctx != NULL)
4426 *ctx = EXPAND_NOTHING;
4427 }
4428 else if (*cmd == '/' || *cmd == '?')
4429 {
4430 delim = *cmd++;
4431 while (*cmd != NUL && *cmd != delim)
4432 if (*cmd++ == '\\' && *cmd != NUL)
4433 ++cmd;
4434 if (*cmd == NUL && ctx != NULL)
4435 *ctx = EXPAND_NOTHING;
4436 }
4437 if (*cmd != NUL)
4438 ++cmd;
4439 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004440
4441 /* Skip ":" and white space. */
4442 while (*cmd == ':')
4443 cmd = skipwhite(cmd + 1);
4444
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 return cmd;
4446}
4447
4448/*
4449 * get a single EX address
4450 *
4451 * Set ptr to the next character after the part that was interpreted.
4452 * Set ptr to NULL when an error is encountered.
4453 *
4454 * Return MAXLNUM when no Ex address was found.
4455 */
4456 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004457get_address(
4458 exarg_T *eap UNUSED,
4459 char_u **ptr,
4460 int addr_type, /* flag: one of ADDR_LINES, ... */
4461 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004462 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004463 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464{
4465 int c;
4466 int i;
4467 long n;
4468 char_u *cmd;
4469 pos_T pos;
4470 pos_T *fp;
4471 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004472 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
4474 cmd = skipwhite(*ptr);
4475 lnum = MAXLNUM;
4476 do
4477 {
4478 switch (*cmd)
4479 {
4480 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004481 ++cmd;
4482 switch (addr_type)
4483 {
4484 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 lnum = curwin->w_cursor.lnum;
4486 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004487 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004488 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004489 break;
4490 case ADDR_ARGUMENTS:
4491 lnum = curwin->w_arg_idx + 1;
4492 break;
4493 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004494 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004495 lnum = curbuf->b_fnum;
4496 break;
4497 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004498 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004499 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004500 case ADDR_TABS_RELATIVE:
4501 EMSG(_(e_invrange));
4502 cmd = NULL;
4503 goto error;
4504 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004505#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004506 case ADDR_QUICKFIX:
4507 lnum = qf_get_cur_valid_idx(eap);
4508 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004509#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004510 }
4511 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512
4513 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004514 ++cmd;
4515 switch (addr_type)
4516 {
4517 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 lnum = curbuf->b_ml.ml_line_count;
4519 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004520 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004521 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004522 break;
4523 case ADDR_ARGUMENTS:
4524 lnum = ARGCOUNT;
4525 break;
4526 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004527 buf = lastbuf;
4528 while (buf->b_ml.ml_mfp == NULL)
4529 {
4530 if (buf->b_prev == NULL)
4531 break;
4532 buf = buf->b_prev;
4533 }
4534 lnum = buf->b_fnum;
4535 break;
4536 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004537 lnum = lastbuf->b_fnum;
4538 break;
4539 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004540 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004541 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004542 case ADDR_TABS_RELATIVE:
4543 EMSG(_(e_invrange));
4544 cmd = NULL;
4545 goto error;
4546 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004547#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004548 case ADDR_QUICKFIX:
4549 lnum = qf_get_size(eap);
4550 if (lnum == 0)
4551 lnum = 1;
4552 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004553#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004554 }
4555 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
4557 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004558 if (*++cmd == NUL)
4559 {
4560 cmd = NULL;
4561 goto error;
4562 }
4563 if (addr_type != ADDR_LINES)
4564 {
4565 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004566 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004567 goto error;
4568 }
4569 if (skip)
4570 ++cmd;
4571 else
4572 {
4573 /* Only accept a mark in another file when it is
4574 * used by itself: ":'M". */
4575 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4576 ++cmd;
4577 if (fp == (pos_T *)-1)
4578 /* Jumped to another file. */
4579 lnum = curwin->w_cursor.lnum;
4580 else
4581 {
4582 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
4584 cmd = NULL;
4585 goto error;
4586 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004587 lnum = fp->lnum;
4588 }
4589 }
4590 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591
4592 case '/':
4593 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004594 c = *cmd++;
4595 if (addr_type != ADDR_LINES)
4596 {
4597 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004598 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004599 goto error;
4600 }
4601 if (skip) /* skip "/pat/" */
4602 {
4603 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4604 if (*cmd == c)
4605 ++cmd;
4606 }
4607 else
4608 {
4609 pos = curwin->w_cursor; /* save curwin->w_cursor */
4610 /*
4611 * When '/' or '?' follows another address, start
4612 * from there.
4613 */
4614 if (lnum != MAXLNUM)
4615 curwin->w_cursor.lnum = lnum;
4616 /*
4617 * Start a forward search at the end of the line.
4618 * Start a backward search at the start of the line.
4619 * This makes sure we never match in the current
4620 * line, and can match anywhere in the
4621 * next/previous line.
4622 */
4623 if (c == '/')
4624 curwin->w_cursor.col = MAXCOL;
4625 else
4626 curwin->w_cursor.col = 0;
4627 searchcmdlen = 0;
4628 if (!do_search(NULL, c, cmd, 1L,
4629 SEARCH_HIS | SEARCH_MSG, NULL))
4630 {
4631 curwin->w_cursor = pos;
4632 cmd = NULL;
4633 goto error;
4634 }
4635 lnum = curwin->w_cursor.lnum;
4636 curwin->w_cursor = pos;
4637 /* adjust command string pointer */
4638 cmd += searchcmdlen;
4639 }
4640 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
4642 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004643 ++cmd;
4644 if (addr_type != ADDR_LINES)
4645 {
4646 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004647 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004648 goto error;
4649 }
4650 if (*cmd == '&')
4651 i = RE_SUBST;
4652 else if (*cmd == '?' || *cmd == '/')
4653 i = RE_SEARCH;
4654 else
4655 {
4656 EMSG(_(e_backslash));
4657 cmd = NULL;
4658 goto error;
4659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004661 if (!skip)
4662 {
4663 /*
4664 * When search follows another address, start from
4665 * there.
4666 */
4667 if (lnum != MAXLNUM)
4668 pos.lnum = lnum;
4669 else
4670 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004672 /*
4673 * Start the search just like for the above
4674 * do_search().
4675 */
4676 if (*cmd != '?')
4677 pos.col = MAXCOL;
4678 else
4679 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004680#ifdef FEAT_VIRTUALEDIT
4681 pos.coladd = 0;
4682#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004683 if (searchit(curwin, curbuf, &pos,
4684 *cmd == '?' ? BACKWARD : FORWARD,
4685 (char_u *)"", 1L, SEARCH_MSG,
4686 i, (linenr_T)0, NULL) != FAIL)
4687 lnum = pos.lnum;
4688 else
4689 {
4690 cmd = NULL;
4691 goto error;
4692 }
4693 }
4694 ++cmd;
4695 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696
4697 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004698 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4699 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 }
4701
4702 for (;;)
4703 {
4704 cmd = skipwhite(cmd);
4705 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4706 break;
4707
4708 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004709 {
4710 switch (addr_type)
4711 {
4712 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004713 /* "+1" is same as ".+1" */
4714 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004715 break;
4716 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004717 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004718 break;
4719 case ADDR_ARGUMENTS:
4720 lnum = curwin->w_arg_idx + 1;
4721 break;
4722 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004723 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004724 lnum = curbuf->b_fnum;
4725 break;
4726 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004727 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004728 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004729 case ADDR_TABS_RELATIVE:
4730 lnum = 1;
4731 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004732#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004733 case ADDR_QUICKFIX:
4734 lnum = qf_get_cur_valid_idx(eap);
4735 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004736#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004737 }
4738 }
4739
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 if (VIM_ISDIGIT(*cmd))
4741 i = '+'; /* "number" is same as "+number" */
4742 else
4743 i = *cmd++;
4744 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4745 n = 1;
4746 else
4747 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004748
4749 if (addr_type == ADDR_TABS_RELATIVE)
4750 {
4751 EMSG(_(e_invrange));
4752 cmd = NULL;
4753 goto error;
4754 }
4755 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004756 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004757 lnum = compute_buffer_local_count(
4758 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004760 {
4761#ifdef FEAT_FOLDING
4762 /* Relative line addressing, need to adjust for folded lines
4763 * now, but only do it after the first address. */
4764 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4765 && address_count >= 2)
4766 (void)hasFolding(lnum, NULL, &lnum);
4767#endif
4768 if (i == '-')
4769 lnum -= n;
4770 else
4771 lnum += n;
4772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 }
4774 } while (*cmd == '/' || *cmd == '?');
4775
4776error:
4777 *ptr = cmd;
4778 return lnum;
4779}
4780
4781/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004782 * Get flags from an Ex command argument.
4783 */
4784 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004785get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004786{
4787 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4788 {
4789 if (*eap->arg == 'l')
4790 eap->flags |= EXFLAG_LIST;
4791 else if (*eap->arg == 'p')
4792 eap->flags |= EXFLAG_PRINT;
4793 else
4794 eap->flags |= EXFLAG_NR;
4795 eap->arg = skipwhite(eap->arg + 1);
4796 }
4797}
4798
4799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 * Function called for command which is Not Implemented. NI!
4801 */
4802 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004803ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804{
4805 if (!eap->skip)
4806 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4807}
4808
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004809#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810/*
4811 * Function called for script command which is Not Implemented. NI!
4812 * Skips over ":perl <<EOF" constructs.
4813 */
4814 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004815ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816{
4817 if (!eap->skip)
4818 ex_ni(eap);
4819 else
4820 vim_free(script_get(eap, eap->arg));
4821}
4822#endif
4823
4824/*
4825 * Check range in Ex command for validity.
4826 * Return NULL when valid, error message when invalid.
4827 */
4828 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004829invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004831 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 if ( eap->line1 < 0
4833 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004834 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004836
4837 if (eap->argt & RANGE)
4838 {
4839 switch(eap->addr_type)
4840 {
4841 case ADDR_LINES:
4842 if (!(eap->argt & NOTADR)
4843 && eap->line2 > curbuf->b_ml.ml_line_count
4844#ifdef FEAT_DIFF
4845 + (eap->cmdidx == CMD_diffget)
4846#endif
4847 )
4848 return (char_u *)_(e_invrange);
4849 break;
4850 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004851 /* add 1 if ARGCOUNT is 0 */
4852 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004853 return (char_u *)_(e_invrange);
4854 break;
4855 case ADDR_BUFFERS:
4856 if (eap->line1 < firstbuf->b_fnum
4857 || eap->line2 > lastbuf->b_fnum)
4858 return (char_u *)_(e_invrange);
4859 break;
4860 case ADDR_LOADED_BUFFERS:
4861 buf = firstbuf;
4862 while (buf->b_ml.ml_mfp == NULL)
4863 {
4864 if (buf->b_next == NULL)
4865 return (char_u *)_(e_invrange);
4866 buf = buf->b_next;
4867 }
4868 if (eap->line1 < buf->b_fnum)
4869 return (char_u *)_(e_invrange);
4870 buf = lastbuf;
4871 while (buf->b_ml.ml_mfp == NULL)
4872 {
4873 if (buf->b_prev == NULL)
4874 return (char_u *)_(e_invrange);
4875 buf = buf->b_prev;
4876 }
4877 if (eap->line2 > buf->b_fnum)
4878 return (char_u *)_(e_invrange);
4879 break;
4880 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004881 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004882 return (char_u *)_(e_invrange);
4883 break;
4884 case ADDR_TABS:
4885 if (eap->line2 > LAST_TAB_NR)
4886 return (char_u *)_(e_invrange);
4887 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004888 case ADDR_TABS_RELATIVE:
4889 /* Do nothing */
4890 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004891#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004892 case ADDR_QUICKFIX:
4893 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4894 return (char_u *)_(e_invrange);
4895 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004896#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004897 }
4898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 return NULL;
4900}
4901
4902/*
4903 * Correct the range for zero line number, if required.
4904 */
4905 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004906correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907{
4908 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4909 {
4910 if (eap->line1 == 0)
4911 eap->line1 = 1;
4912 if (eap->line2 == 0)
4913 eap->line2 = 1;
4914 }
4915}
4916
Bram Moolenaar748bf032005-02-02 23:04:36 +00004917#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004918static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004919
4920/*
4921 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4922 * pattern. Otherwise return eap->arg.
4923 */
4924 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004925skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004926{
4927 char_u *p = eap->arg;
4928
Bram Moolenaara37420f2006-02-04 22:37:47 +00004929 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4930 || eap->cmdidx == CMD_vimgrepadd
4931 || eap->cmdidx == CMD_lvimgrepadd
4932 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004933 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004934 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004935 if (p == NULL)
4936 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004937 }
4938 return p;
4939}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004940
4941/*
4942 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4943 * in the command line, so that things like % get expanded.
4944 */
4945 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004946replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004947{
4948 char_u *new_cmdline;
4949 char_u *program;
4950 char_u *pos;
4951 char_u *ptr;
4952 int len;
4953 int i;
4954
4955 /*
4956 * Don't do it when ":vimgrep" is used for ":grep".
4957 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004958 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4959 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4960 || eap->cmdidx == CMD_grepadd
4961 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004962 && !grep_internal(eap->cmdidx))
4963 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004964 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4965 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004966 {
4967 if (*curbuf->b_p_gp == NUL)
4968 program = p_gp;
4969 else
4970 program = curbuf->b_p_gp;
4971 }
4972 else
4973 {
4974 if (*curbuf->b_p_mp == NUL)
4975 program = p_mp;
4976 else
4977 program = curbuf->b_p_mp;
4978 }
4979
4980 p = skipwhite(p);
4981
4982 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4983 {
4984 /* replace $* by given arguments */
4985 i = 1;
4986 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4987 ++i;
4988 len = (int)STRLEN(p);
4989 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4990 if (new_cmdline == NULL)
4991 return NULL; /* out of memory */
4992 ptr = new_cmdline;
4993 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4994 {
4995 i = (int)(pos - program);
4996 STRNCPY(ptr, program, i);
4997 STRCPY(ptr += i, p);
4998 ptr += len;
4999 program = pos + 2;
5000 }
5001 STRCPY(ptr, program);
5002 }
5003 else
5004 {
5005 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
5006 if (new_cmdline == NULL)
5007 return NULL; /* out of memory */
5008 STRCPY(new_cmdline, program);
5009 STRCAT(new_cmdline, " ");
5010 STRCAT(new_cmdline, p);
5011 }
5012 msg_make(p);
5013
5014 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
5015 vim_free(*cmdlinep);
5016 *cmdlinep = new_cmdline;
5017 p = new_cmdline;
5018 }
5019 return p;
5020}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005021#endif
5022
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023/*
5024 * Expand file name in Ex command argument.
5025 * Return FAIL for failure, OK otherwise.
5026 */
5027 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005028expand_filename(
5029 exarg_T *eap,
5030 char_u **cmdlinep,
5031 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032{
5033 int has_wildcards; /* need to expand wildcards */
5034 char_u *repl;
5035 int srclen;
5036 char_u *p;
5037 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005038 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039
Bram Moolenaar748bf032005-02-02 23:04:36 +00005040#ifdef FEAT_QUICKFIX
5041 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
5042 p = skip_grep_pat(eap);
5043#else
5044 p = eap->arg;
5045#endif
5046
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 /*
5048 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5049 * the file name contains a wildcard it should not cause expanding.
5050 * (it will be expanded anyway if there is a wildcard before replacing).
5051 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005052 has_wildcards = mch_has_wildcard(p);
5053 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005055#ifdef FEAT_EVAL
5056 /* Skip over `=expr`, wildcards in it are not expanded. */
5057 if (p[0] == '`' && p[1] == '=')
5058 {
5059 p += 2;
5060 (void)skip_expr(&p);
5061 if (*p == '`')
5062 ++p;
5063 continue;
5064 }
5065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 /*
5067 * Quick check if this cannot be the start of a special string.
5068 * Also removes backslash before '%', '#' and '<'.
5069 */
5070 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5071 {
5072 ++p;
5073 continue;
5074 }
5075
5076 /*
5077 * Try to find a match at this position.
5078 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005079 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5080 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 if (*errormsgp != NULL) /* error detected */
5082 return FAIL;
5083 if (repl == NULL) /* no match found */
5084 {
5085 p += srclen;
5086 continue;
5087 }
5088
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005089 /* Wildcards won't be expanded below, the replacement is taken
5090 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5091 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5092 {
5093 char_u *l = repl;
5094
5095 repl = expand_env_save(repl);
5096 vim_free(l);
5097 }
5098
Bram Moolenaar63b92542007-03-27 14:57:09 +00005099 /* Need to escape white space et al. with a backslash.
5100 * Don't do this for:
5101 * - replacement that already has been escaped: "##"
5102 * - shell commands (may have to use quotes instead).
5103 * - non-unix systems when there is a single argument (spaces don't
5104 * separate arguments then).
5105 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005107 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 && eap->cmdidx != CMD_bang
5109 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00005110 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00005112 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00005114 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115#ifndef UNIX
5116 && !(eap->argt & NOSPC)
5117#endif
5118 )
5119 {
5120 char_u *l;
5121#ifdef BACKSLASH_IN_FILENAME
5122 /* Don't escape a backslash here, because rem_backslash() doesn't
5123 * remove it later. */
5124 static char_u *nobslash = (char_u *)" \t\"|";
5125# define ESCAPE_CHARS nobslash
5126#else
5127# define ESCAPE_CHARS escape_chars
5128#endif
5129
5130 for (l = repl; *l; ++l)
5131 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5132 {
5133 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5134 if (l != NULL)
5135 {
5136 vim_free(repl);
5137 repl = l;
5138 }
5139 break;
5140 }
5141 }
5142
5143 /* For a shell command a '!' must be escaped. */
5144 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005145 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
5147 char_u *l;
5148
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005149 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 if (l != NULL)
5151 {
5152 vim_free(repl);
5153 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 }
5155 }
5156
5157 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5158 vim_free(repl);
5159 if (p == NULL)
5160 return FAIL;
5161 }
5162
5163 /*
5164 * One file argument: Expand wildcards.
5165 * Don't do this with ":r !command" or ":w !command".
5166 */
5167 if ((eap->argt & NOSPC) && !eap->usefilter)
5168 {
5169 /*
5170 * May do this twice:
5171 * 1. Replace environment variables.
5172 * 2. Replace any other wildcards, remove backslashes.
5173 */
5174 for (n = 1; n <= 2; ++n)
5175 {
5176 if (n == 2)
5177 {
5178#ifdef UNIX
5179 /*
5180 * Only for Unix we check for more than one file name.
5181 * For other systems spaces are considered to be part
5182 * of the file name.
5183 * Only check here if there is no wildcard, otherwise
5184 * ExpandOne() will check for errors. This allows
5185 * ":e `ls ve*.c`" on Unix.
5186 */
5187 if (!has_wildcards)
5188 for (p = eap->arg; *p; ++p)
5189 {
5190 /* skip escaped characters */
5191 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5192 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005193 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
5195 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5196 return FAIL;
5197 }
5198 }
5199#endif
5200
5201 /*
5202 * Halve the number of backslashes (this is Vi compatible).
5203 * For Unix and OS/2, when wildcards are expanded, this is
5204 * done by ExpandOne() below.
5205 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005206#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 if (!has_wildcards)
5208#endif
5209 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 }
5211
5212 if (has_wildcards)
5213 {
5214 if (n == 1)
5215 {
5216 /*
5217 * First loop: May expand environment variables. This
5218 * can be done much faster with expand_env() than with
5219 * something else (e.g., calling a shell).
5220 * After expanding environment variables, check again
5221 * if there are still wildcards present.
5222 */
5223 if (vim_strchr(eap->arg, '$') != NULL
5224 || vim_strchr(eap->arg, '~') != NULL)
5225 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005226 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005227 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 has_wildcards = mch_has_wildcard(NameBuff);
5229 p = NameBuff;
5230 }
5231 else
5232 p = NULL;
5233 }
5234 else /* n == 2 */
5235 {
5236 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005237 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
5239 ExpandInit(&xpc);
5240 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005241 if (p_wic)
5242 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005244 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 if (p == NULL)
5246 return FAIL;
5247 }
5248 if (p != NULL)
5249 {
5250 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5251 p, cmdlinep);
5252 if (n == 2) /* p came from ExpandOne() */
5253 vim_free(p);
5254 }
5255 }
5256 }
5257 }
5258 return OK;
5259}
5260
5261/*
5262 * Replace part of the command line, keeping eap->cmd, eap->arg and
5263 * eap->nextcmd correct.
5264 * "src" points to the part that is to be replaced, of length "srclen".
5265 * "repl" is the replacement string.
5266 * Returns a pointer to the character after the replaced string.
5267 * Returns NULL for failure.
5268 */
5269 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005270repl_cmdline(
5271 exarg_T *eap,
5272 char_u *src,
5273 int srclen,
5274 char_u *repl,
5275 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276{
5277 int len;
5278 int i;
5279 char_u *new_cmdline;
5280
5281 /*
5282 * The new command line is build in new_cmdline[].
5283 * First allocate it.
5284 * Careful: a "+cmd" argument may have been NUL terminated.
5285 */
5286 len = (int)STRLEN(repl);
5287 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005288 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5290 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5291 return NULL; /* out of memory! */
5292
5293 /*
5294 * Copy the stuff before the expanded part.
5295 * Copy the expanded stuff.
5296 * Copy what came after the expanded part.
5297 * Copy the next commands, if there are any.
5298 */
5299 i = (int)(src - *cmdlinep); /* length of part before match */
5300 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005301
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 mch_memmove(new_cmdline + i, repl, (size_t)len);
5303 i += len; /* remember the end of the string */
5304 STRCPY(new_cmdline + i, src + srclen);
5305 src = new_cmdline + i; /* remember where to continue */
5306
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005307 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 {
5309 i = (int)STRLEN(new_cmdline) + 1;
5310 STRCPY(new_cmdline + i, eap->nextcmd);
5311 eap->nextcmd = new_cmdline + i;
5312 }
5313 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5314 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5315 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5316 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5317 vim_free(*cmdlinep);
5318 *cmdlinep = new_cmdline;
5319
5320 return src;
5321}
5322
5323/*
5324 * Check for '|' to separate commands and '"' to start comments.
5325 */
5326 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005327separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328{
5329 char_u *p;
5330
Bram Moolenaar86b68352004-12-27 21:59:20 +00005331#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005332 p = skip_grep_pat(eap);
5333#else
5334 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005335#endif
5336
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005337 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 {
5339 if (*p == Ctrl_V)
5340 {
5341 if (eap->argt & (USECTRLV | XFILE))
5342 ++p; /* skip CTRL-V and next char */
5343 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005344 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005345 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 if (*p == NUL) /* stop at NUL after CTRL-V */
5347 break;
5348 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005349
5350#ifdef FEAT_EVAL
5351 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005352 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005353 {
5354 p += 2;
5355 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005356 }
5357#endif
5358
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 /* Check for '"': start of comment or '|': next command */
5360 /* :@" and :*" do not start a comment!
5361 * :redir @" doesn't either. */
5362 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5363 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5364 || p != eap->arg)
5365 && (eap->cmdidx != CMD_redir
5366 || p != eap->arg + 1 || p[-1] != '@'))
5367 || *p == '|' || *p == '\n')
5368 {
5369 /*
5370 * We remove the '\' before the '|', unless USECTRLV is used
5371 * AND 'b' is present in 'cpoptions'.
5372 */
5373 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5374 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5375 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005376 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 --p;
5378 }
5379 else
5380 {
5381 eap->nextcmd = check_nextcmd(p);
5382 *p = NUL;
5383 break;
5384 }
5385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005387
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5389 del_trailing_spaces(eap->arg);
5390}
5391
5392/*
5393 * get + command from ex argument
5394 */
5395 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005396getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397{
5398 char_u *arg = *argp;
5399 char_u *command = NULL;
5400
5401 if (*arg == '+') /* +[command] */
5402 {
5403 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005404 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 command = dollar_command;
5406 else
5407 {
5408 command = arg;
5409 arg = skip_cmd_arg(command, TRUE);
5410 if (*arg != NUL)
5411 *arg++ = NUL; /* terminate command with NUL */
5412 }
5413
5414 arg = skipwhite(arg); /* skip over spaces */
5415 *argp = arg;
5416 }
5417 return command;
5418}
5419
5420/*
5421 * Find end of "+command" argument. Skip over "\ " and "\\".
5422 */
5423 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005424skip_cmd_arg(
5425 char_u *p,
5426 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427{
5428 while (*p && !vim_isspace(*p))
5429 {
5430 if (*p == '\\' && p[1] != NUL)
5431 {
5432 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005433 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 else
5435 ++p;
5436 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005437 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 }
5439 return p;
5440}
5441
5442/*
5443 * Get "++opt=arg" argument.
5444 * Return FAIL or OK.
5445 */
5446 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005447getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448{
5449 char_u *arg = eap->arg + 2;
5450 int *pp = NULL;
5451#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005452 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 char_u *p;
5454#endif
5455
5456 /* ":edit ++[no]bin[ary] file" */
5457 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5458 {
5459 if (*arg == 'n')
5460 {
5461 arg += 2;
5462 eap->force_bin = FORCE_NOBIN;
5463 }
5464 else
5465 eap->force_bin = FORCE_BIN;
5466 if (!checkforcmd(&arg, "binary", 3))
5467 return FAIL;
5468 eap->arg = skipwhite(arg);
5469 return OK;
5470 }
5471
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005472 /* ":read ++edit file" */
5473 if (STRNCMP(arg, "edit", 4) == 0)
5474 {
5475 eap->read_edit = TRUE;
5476 eap->arg = skipwhite(arg + 4);
5477 return OK;
5478 }
5479
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 if (STRNCMP(arg, "ff", 2) == 0)
5481 {
5482 arg += 2;
5483 pp = &eap->force_ff;
5484 }
5485 else if (STRNCMP(arg, "fileformat", 10) == 0)
5486 {
5487 arg += 10;
5488 pp = &eap->force_ff;
5489 }
5490#ifdef FEAT_MBYTE
5491 else if (STRNCMP(arg, "enc", 3) == 0)
5492 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005493 if (STRNCMP(arg, "encoding", 8) == 0)
5494 arg += 8;
5495 else
5496 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 pp = &eap->force_enc;
5498 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005499 else if (STRNCMP(arg, "bad", 3) == 0)
5500 {
5501 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005502 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504#endif
5505
5506 if (pp == NULL || *arg != '=')
5507 return FAIL;
5508
5509 ++arg;
5510 *pp = (int)(arg - eap->cmd);
5511 arg = skip_cmd_arg(arg, FALSE);
5512 eap->arg = skipwhite(arg);
5513 *arg = NUL;
5514
5515#ifdef FEAT_MBYTE
5516 if (pp == &eap->force_ff)
5517 {
5518#endif
5519 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5520 return FAIL;
5521#ifdef FEAT_MBYTE
5522 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005523 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 {
5525 /* Make 'fileencoding' lower case. */
5526 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5527 *p = TOLOWER_ASC(*p);
5528 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005529 else
5530 {
5531 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5532 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005533 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005534 if (STRICMP(p, "keep") == 0)
5535 eap->bad_char = BAD_KEEP;
5536 else if (STRICMP(p, "drop") == 0)
5537 eap->bad_char = BAD_DROP;
5538 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5539 eap->bad_char = *p;
5540 else
5541 return FAIL;
5542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543#endif
5544
5545 return OK;
5546}
5547
5548/*
5549 * ":abbreviate" and friends.
5550 */
5551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005552ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553{
5554 do_exmap(eap, TRUE); /* almost the same as mapping */
5555}
5556
5557/*
5558 * ":map" and friends.
5559 */
5560 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005561ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562{
5563 /*
5564 * If we are sourcing .exrc or .vimrc in current directory we
5565 * print the mappings for security reasons.
5566 */
5567 if (secure)
5568 {
5569 secure = 2;
5570 msg_outtrans(eap->cmd);
5571 msg_putchar('\n');
5572 }
5573 do_exmap(eap, FALSE);
5574}
5575
5576/*
5577 * ":unmap" and friends.
5578 */
5579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005580ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
5582 do_exmap(eap, FALSE);
5583}
5584
5585/*
5586 * ":mapclear" and friends.
5587 */
5588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005589ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590{
5591 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5592}
5593
5594/*
5595 * ":abclear" and friends.
5596 */
5597 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005598ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599{
5600 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5601}
5602
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005603#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005605ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606{
5607 /*
5608 * Disallow auto commands from .exrc and .vimrc in current
5609 * directory for security reasons.
5610 */
5611 if (secure)
5612 {
5613 secure = 2;
5614 eap->errmsg = e_curdir;
5615 }
5616 else if (eap->cmdidx == CMD_autocmd)
5617 do_autocmd(eap->arg, eap->forceit);
5618 else
5619 do_augroup(eap->arg, eap->forceit);
5620}
5621
5622/*
5623 * ":doautocmd": Apply the automatic commands to the current buffer.
5624 */
5625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005626ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005628 char_u *arg = eap->arg;
5629 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005630 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005631
Bram Moolenaar1610d052016-06-09 22:53:01 +02005632 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5633 /* Only when there is no <nomodeline>. */
5634 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005635 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636}
5637#endif
5638
5639#ifdef FEAT_LISTCMDS
5640/*
5641 * :[N]bunload[!] [N] [bufname] unload buffer
5642 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5643 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5644 */
5645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005646ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647{
5648 eap->errmsg = do_bufdel(
5649 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5650 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5651 : DOBUF_UNLOAD, eap->arg,
5652 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5653}
5654
5655/*
5656 * :[N]buffer [N] to buffer N
5657 * :[N]sbuffer [N] to buffer N
5658 */
5659 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005660ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661{
5662 if (*eap->arg)
5663 eap->errmsg = e_trailing;
5664 else
5665 {
5666 if (eap->addr_count == 0) /* default is current buffer */
5667 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5668 else
5669 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005670 if (eap->do_ecmd_cmd != NULL)
5671 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
5673}
5674
5675/*
5676 * :[N]bmodified [N] to next mod. buffer
5677 * :[N]sbmodified [N] to next mod. buffer
5678 */
5679 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005680ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681{
5682 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005683 if (eap->do_ecmd_cmd != NULL)
5684 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685}
5686
5687/*
5688 * :[N]bnext [N] to next buffer
5689 * :[N]sbnext [N] split and to next buffer
5690 */
5691 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005692ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693{
5694 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005695 if (eap->do_ecmd_cmd != NULL)
5696 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697}
5698
5699/*
5700 * :[N]bNext [N] to previous buffer
5701 * :[N]bprevious [N] to previous buffer
5702 * :[N]sbNext [N] split and to previous buffer
5703 * :[N]sbprevious [N] split and to previous buffer
5704 */
5705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005706ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
5708 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005709 if (eap->do_ecmd_cmd != NULL)
5710 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711}
5712
5713/*
5714 * :brewind to first buffer
5715 * :bfirst to first buffer
5716 * :sbrewind split and to first buffer
5717 * :sbfirst split and to first buffer
5718 */
5719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005720ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721{
5722 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005723 if (eap->do_ecmd_cmd != NULL)
5724 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725}
5726
5727/*
5728 * :blast to last buffer
5729 * :sblast split and to last buffer
5730 */
5731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005732ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733{
5734 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005735 if (eap->do_ecmd_cmd != NULL)
5736 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737}
5738#endif
5739
5740 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005741ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742{
5743 return (c == NUL || c == '|' || c == '"' || c == '\n');
5744}
5745
5746#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5747 || defined(PROTO)
5748/*
5749 * Return the next command, after the first '|' or '\n'.
5750 * Return NULL if not found.
5751 */
5752 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005753find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754{
5755 while (*p != '|' && *p != '\n')
5756 {
5757 if (*p == NUL)
5758 return NULL;
5759 ++p;
5760 }
5761 return (p + 1);
5762}
5763#endif
5764
5765/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005766 * Check if *p is a separator between Ex commands, skipping over white space.
5767 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 */
5769 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005770check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005772 char_u *s = skipwhite(p);
5773
5774 if (*s == '|' || *s == '\n')
5775 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 else
5777 return NULL;
5778}
5779
5780/*
5781 * - if there are more files to edit
5782 * - and this is the last window
5783 * - and forceit not used
5784 * - and not repeated twice on a row
5785 * return FAIL and give error message if 'message' TRUE
5786 * return OK otherwise
5787 */
5788 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005789check_more(
5790 int message, /* when FALSE check only, no messages */
5791 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792{
5793 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5794
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005795 if (!forceit && only_one_window()
5796 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 {
5798 if (message)
5799 {
5800#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5801 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5802 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005803 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804
5805 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005806 vim_strncpy(buff,
5807 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005808 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005810 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 _("%d more files to edit. Quit anyway?"), n);
5812 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5813 return OK;
5814 return FAIL;
5815 }
5816#endif
5817 if (n == 1)
5818 EMSG(_("E173: 1 more file to edit"));
5819 else
5820 EMSGN(_("E173: %ld more files to edit"), n);
5821 quitmore = 2; /* next try to quit is allowed */
5822 }
5823 return FAIL;
5824 }
5825 return OK;
5826}
5827
5828#ifdef FEAT_CMDL_COMPL
5829/*
5830 * Function given to ExpandGeneric() to obtain the list of command names.
5831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005833get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834{
5835 if (idx >= (int)CMD_SIZE)
5836# ifdef FEAT_USR_CMDS
5837 return get_user_command_name(idx);
5838# else
5839 return NULL;
5840# endif
5841 return cmdnames[idx].cmd_name;
5842}
5843#endif
5844
5845#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005846static 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);
5847static void uc_list(char_u *name, size_t name_len);
5848static 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);
5849static char_u *uc_split_args(char_u *arg, size_t *lenp);
5850static 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 +00005851
5852 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005853uc_add_command(
5854 char_u *name,
5855 size_t name_len,
5856 char_u *rep,
5857 long argt,
5858 long def,
5859 int flags,
5860 int compl,
5861 char_u *compl_arg,
5862 int addr_type,
5863 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864{
5865 ucmd_T *cmd = NULL;
5866 char_u *p;
5867 int i;
5868 int cmp = 1;
5869 char_u *rep_buf = NULL;
5870 garray_T *gap;
5871
Bram Moolenaar9c102382006-05-03 21:26:49 +00005872 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 if (rep_buf == NULL)
5874 {
5875 /* Can't replace termcodes - try using the string as is */
5876 rep_buf = vim_strsave(rep);
5877
5878 /* Give up if out of memory */
5879 if (rep_buf == NULL)
5880 return FAIL;
5881 }
5882
5883 /* get address of growarray: global or in curbuf */
5884 if (flags & UC_BUFFER)
5885 {
5886 gap = &curbuf->b_ucmds;
5887 if (gap->ga_itemsize == 0)
5888 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5889 }
5890 else
5891 gap = &ucmds;
5892
5893 /* Search for the command in the already defined commands. */
5894 for (i = 0; i < gap->ga_len; ++i)
5895 {
5896 size_t len;
5897
5898 cmd = USER_CMD_GA(gap, i);
5899 len = STRLEN(cmd->uc_name);
5900 cmp = STRNCMP(name, cmd->uc_name, name_len);
5901 if (cmp == 0)
5902 {
5903 if (name_len < len)
5904 cmp = -1;
5905 else if (name_len > len)
5906 cmp = 1;
5907 }
5908
5909 if (cmp == 0)
5910 {
5911 if (!force)
5912 {
5913 EMSG(_("E174: Command already exists: add ! to replace it"));
5914 goto fail;
5915 }
5916
5917 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005918 cmd->uc_rep = NULL;
5919#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5920 vim_free(cmd->uc_compl_arg);
5921 cmd->uc_compl_arg = NULL;
5922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 break;
5924 }
5925
5926 /* Stop as soon as we pass the name to add */
5927 if (cmp < 0)
5928 break;
5929 }
5930
5931 /* Extend the array unless we're replacing an existing command */
5932 if (cmp != 0)
5933 {
5934 if (ga_grow(gap, 1) != OK)
5935 goto fail;
5936 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5937 goto fail;
5938
5939 cmd = USER_CMD_GA(gap, i);
5940 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5941
5942 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943
5944 cmd->uc_name = p;
5945 }
5946
5947 cmd->uc_rep = rep_buf;
5948 cmd->uc_argt = argt;
5949 cmd->uc_def = def;
5950 cmd->uc_compl = compl;
5951#ifdef FEAT_EVAL
5952 cmd->uc_scriptID = current_SID;
5953# ifdef FEAT_CMDL_COMPL
5954 cmd->uc_compl_arg = compl_arg;
5955# endif
5956#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005957 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958
5959 return OK;
5960
5961fail:
5962 vim_free(rep_buf);
5963#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5964 vim_free(compl_arg);
5965#endif
5966 return FAIL;
5967}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005970#if defined(FEAT_USR_CMDS)
5971static struct
5972{
5973 int expand;
5974 char *name;
5975} addr_type_complete[] =
5976{
5977 {ADDR_ARGUMENTS, "arguments"},
5978 {ADDR_LINES, "lines"},
5979 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5980 {ADDR_TABS, "tabs"},
5981 {ADDR_BUFFERS, "buffers"},
5982 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005983 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005984 {-1, NULL}
5985};
5986#endif
5987
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005988#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989/*
5990 * List of names for completion for ":command" with the EXPAND_ flag.
5991 * Must be alphabetical for completion.
5992 */
5993static struct
5994{
5995 int expand;
5996 char *name;
5997} command_complete[] =
5998{
5999 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02006000 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02006002 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02006004 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00006005#if defined(FEAT_CSCOPE)
6006 {EXPAND_CSCOPE, "cscope"},
6007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6009 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00006010 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011#endif
6012 {EXPAND_DIRECTORIES, "dir"},
6013 {EXPAND_ENV_VARS, "environment"},
6014 {EXPAND_EVENTS, "event"},
6015 {EXPAND_EXPRESSION, "expression"},
6016 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02006017 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02006018 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 {EXPAND_FUNCTIONS, "function"},
6020 {EXPAND_HELP, "help"},
6021 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02006022#if defined(FEAT_CMDHIST)
6023 {EXPAND_HISTORY, "history"},
6024#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02006025#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02006026 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02006027 {EXPAND_LOCALES, "locale"},
6028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 {EXPAND_MAPPINGS, "mapping"},
6030 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02006031 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02006032 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006033#if defined(FEAT_PROFILE)
6034 {EXPAND_SYNTIME, "syntime"},
6035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01006037 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006038 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00006039#if defined(FEAT_SIGNS)
6040 {EXPAND_SIGN, "sign"},
6041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 {EXPAND_TAGS, "tag"},
6043 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02006044 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 {EXPAND_USER_VARS, "var"},
6046 {0, NULL}
6047};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006050#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006052uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053{
6054 int i, j;
6055 int found = FALSE;
6056 ucmd_T *cmd;
6057 int len;
6058 long a;
6059 garray_T *gap;
6060
6061 gap = &curbuf->b_ucmds;
6062 for (;;)
6063 {
6064 for (i = 0; i < gap->ga_len; ++i)
6065 {
6066 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006067 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068
Bram Moolenaard29459b2016-08-26 22:29:11 +02006069 /* Skip commands which don't match the requested prefix and
6070 * commands filtered out. */
6071 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6072 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 continue;
6074
6075 /* Put out the title first time */
6076 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006077 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 found = TRUE;
6079 msg_putchar('\n');
6080 if (got_int)
6081 break;
6082
6083 /* Special cases */
6084 msg_putchar(a & BANG ? '!' : ' ');
6085 msg_putchar(a & REGSTR ? '"' : ' ');
6086 msg_putchar(gap != &ucmds ? 'b' : ' ');
6087 msg_putchar(' ');
6088
Bram Moolenaar8820b482017-03-16 17:23:31 +01006089 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 len = (int)STRLEN(cmd->uc_name) + 4;
6091
6092 do {
6093 msg_putchar(' ');
6094 ++len;
6095 } while (len < 16);
6096
6097 len = 0;
6098
6099 /* Arguments */
6100 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6101 {
6102 case 0: IObuff[len++] = '0'; break;
6103 case (EXTRA): IObuff[len++] = '*'; break;
6104 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6105 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6106 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6107 }
6108
6109 do {
6110 IObuff[len++] = ' ';
6111 } while (len < 5);
6112
6113 /* Range */
6114 if (a & (RANGE|COUNT))
6115 {
6116 if (a & COUNT)
6117 {
6118 /* -count=N */
6119 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6120 len += (int)STRLEN(IObuff + len);
6121 }
6122 else if (a & DFLALL)
6123 IObuff[len++] = '%';
6124 else if (cmd->uc_def >= 0)
6125 {
6126 /* -range=N */
6127 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6128 len += (int)STRLEN(IObuff + len);
6129 }
6130 else
6131 IObuff[len++] = '.';
6132 }
6133
6134 do {
6135 IObuff[len++] = ' ';
6136 } while (len < 11);
6137
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006138 /* Address Type */
6139 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6140 if (addr_type_complete[j].expand != ADDR_LINES
6141 && addr_type_complete[j].expand == cmd->uc_addr_type)
6142 {
6143 STRCPY(IObuff + len, addr_type_complete[j].name);
6144 len += (int)STRLEN(IObuff + len);
6145 break;
6146 }
6147
6148 do {
6149 IObuff[len++] = ' ';
6150 } while (len < 21);
6151
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 /* Completion */
6153 for (j = 0; command_complete[j].expand != 0; ++j)
6154 if (command_complete[j].expand == cmd->uc_compl)
6155 {
6156 STRCPY(IObuff + len, command_complete[j].name);
6157 len += (int)STRLEN(IObuff + len);
6158 break;
6159 }
6160
6161 do {
6162 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006163 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164
6165 IObuff[len] = '\0';
6166 msg_outtrans(IObuff);
6167
6168 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006169#ifdef FEAT_EVAL
6170 if (p_verbose > 0)
6171 last_set_msg(cmd->uc_scriptID);
6172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 out_flush();
6174 ui_breakcheck();
6175 if (got_int)
6176 break;
6177 }
6178 if (gap == &ucmds || i < gap->ga_len)
6179 break;
6180 gap = &ucmds;
6181 }
6182
6183 if (!found)
6184 MSG(_("No user-defined commands found"));
6185}
6186
6187 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006188uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189{
6190 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6191 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6192 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6193 0xb9, 0x7f, 0};
6194 int i;
6195
6196 for (i = 0; fcmd[i]; ++i)
6197 IObuff[i] = fcmd[i] - 0x40;
6198 IObuff[i] = 0;
6199 return IObuff;
6200}
6201
6202 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006203uc_scan_attr(
6204 char_u *attr,
6205 size_t len,
6206 long *argt,
6207 long *def,
6208 int *flags,
6209 int *compl,
6210 char_u **compl_arg,
6211 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212{
6213 char_u *p;
6214
6215 if (len == 0)
6216 {
6217 EMSG(_("E175: No attribute specified"));
6218 return FAIL;
6219 }
6220
6221 /* First, try the simple attributes (no arguments) */
6222 if (STRNICMP(attr, "bang", len) == 0)
6223 *argt |= BANG;
6224 else if (STRNICMP(attr, "buffer", len) == 0)
6225 *flags |= UC_BUFFER;
6226 else if (STRNICMP(attr, "register", len) == 0)
6227 *argt |= REGSTR;
6228 else if (STRNICMP(attr, "bar", len) == 0)
6229 *argt |= TRLBAR;
6230 else
6231 {
6232 int i;
6233 char_u *val = NULL;
6234 size_t vallen = 0;
6235 size_t attrlen = len;
6236
6237 /* Look for the attribute name - which is the part before any '=' */
6238 for (i = 0; i < (int)len; ++i)
6239 {
6240 if (attr[i] == '=')
6241 {
6242 val = &attr[i + 1];
6243 vallen = len - i - 1;
6244 attrlen = i;
6245 break;
6246 }
6247 }
6248
6249 if (STRNICMP(attr, "nargs", attrlen) == 0)
6250 {
6251 if (vallen == 1)
6252 {
6253 if (*val == '0')
6254 /* Do nothing - this is the default */;
6255 else if (*val == '1')
6256 *argt |= (EXTRA | NOSPC | NEEDARG);
6257 else if (*val == '*')
6258 *argt |= EXTRA;
6259 else if (*val == '?')
6260 *argt |= (EXTRA | NOSPC);
6261 else if (*val == '+')
6262 *argt |= (EXTRA | NEEDARG);
6263 else
6264 goto wrong_nargs;
6265 }
6266 else
6267 {
6268wrong_nargs:
6269 EMSG(_("E176: Invalid number of arguments"));
6270 return FAIL;
6271 }
6272 }
6273 else if (STRNICMP(attr, "range", attrlen) == 0)
6274 {
6275 *argt |= RANGE;
6276 if (vallen == 1 && *val == '%')
6277 *argt |= DFLALL;
6278 else if (val != NULL)
6279 {
6280 p = val;
6281 if (*def >= 0)
6282 {
6283two_count:
6284 EMSG(_("E177: Count cannot be specified twice"));
6285 return FAIL;
6286 }
6287
6288 *def = getdigits(&p);
6289 *argt |= (ZEROR | NOTADR);
6290
6291 if (p != val + vallen || vallen == 0)
6292 {
6293invalid_count:
6294 EMSG(_("E178: Invalid default value for count"));
6295 return FAIL;
6296 }
6297 }
6298 }
6299 else if (STRNICMP(attr, "count", attrlen) == 0)
6300 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006301 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302
6303 if (val != NULL)
6304 {
6305 p = val;
6306 if (*def >= 0)
6307 goto two_count;
6308
6309 *def = getdigits(&p);
6310
6311 if (p != val + vallen)
6312 goto invalid_count;
6313 }
6314
6315 if (*def < 0)
6316 *def = 0;
6317 }
6318 else if (STRNICMP(attr, "complete", attrlen) == 0)
6319 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 if (val == NULL)
6321 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006322 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 return FAIL;
6324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006326 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6327 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006330 else if (STRNICMP(attr, "addr", attrlen) == 0)
6331 {
6332 *argt |= RANGE;
6333 if (val == NULL)
6334 {
6335 EMSG(_("E179: argument required for -addr"));
6336 return FAIL;
6337 }
6338 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6339 == FAIL)
6340 return FAIL;
6341 if (addr_type_arg != ADDR_LINES)
6342 *argt |= (ZEROR | NOTADR) ;
6343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 else
6345 {
6346 char_u ch = attr[len];
6347 attr[len] = '\0';
6348 EMSG2(_("E181: Invalid attribute: %s"), attr);
6349 attr[len] = ch;
6350 return FAIL;
6351 }
6352 }
6353
6354 return OK;
6355}
6356
Bram Moolenaara850a712009-01-28 14:42:59 +00006357/*
6358 * ":command ..."
6359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006361ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362{
6363 char_u *name;
6364 char_u *end;
6365 char_u *p;
6366 long argt = 0;
6367 long def = -1;
6368 int flags = 0;
6369 int compl = EXPAND_NOTHING;
6370 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006371 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006373 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374
6375 p = eap->arg;
6376
6377 /* Check for attributes */
6378 while (*p == '-')
6379 {
6380 ++p;
6381 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006382 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 == FAIL)
6384 return;
6385 p = skipwhite(end);
6386 }
6387
6388 /* Get the name (if any) and skip to the following argument */
6389 name = p;
6390 if (ASCII_ISALPHA(*p))
6391 while (ASCII_ISALNUM(*p))
6392 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006393 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 {
6395 EMSG(_("E182: Invalid command name"));
6396 return;
6397 }
6398 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006399 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400
6401 /* If there is nothing after the name, and no attributes were specified,
6402 * we are listing commands
6403 */
6404 p = skipwhite(end);
6405 if (!has_attr && ends_excmd(*p))
6406 {
6407 uc_list(name, end - name);
6408 }
6409 else if (!ASCII_ISUPPER(*name))
6410 {
6411 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6412 return;
6413 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006414 else if ((name_len == 1 && *name == 'X')
6415 || (name_len <= 4
6416 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6417 {
6418 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6419 return;
6420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 else
6422 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006423 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424}
6425
6426/*
6427 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 * Clear all user commands, global and for current buffer.
6429 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006430 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006431ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432{
6433 uc_clear(&ucmds);
6434 uc_clear(&curbuf->b_ucmds);
6435}
6436
6437/*
6438 * Clear all user commands for "gap".
6439 */
6440 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006441uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442{
6443 int i;
6444 ucmd_T *cmd;
6445
6446 for (i = 0; i < gap->ga_len; ++i)
6447 {
6448 cmd = USER_CMD_GA(gap, i);
6449 vim_free(cmd->uc_name);
6450 vim_free(cmd->uc_rep);
6451# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6452 vim_free(cmd->uc_compl_arg);
6453# endif
6454 }
6455 ga_clear(gap);
6456}
6457
6458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006459ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460{
6461 int i = 0;
6462 ucmd_T *cmd = NULL;
6463 int cmp = -1;
6464 garray_T *gap;
6465
6466 gap = &curbuf->b_ucmds;
6467 for (;;)
6468 {
6469 for (i = 0; i < gap->ga_len; ++i)
6470 {
6471 cmd = USER_CMD_GA(gap, i);
6472 cmp = STRCMP(eap->arg, cmd->uc_name);
6473 if (cmp <= 0)
6474 break;
6475 }
6476 if (gap == &ucmds || cmp == 0)
6477 break;
6478 gap = &ucmds;
6479 }
6480
6481 if (cmp != 0)
6482 {
6483 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6484 return;
6485 }
6486
6487 vim_free(cmd->uc_name);
6488 vim_free(cmd->uc_rep);
6489# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6490 vim_free(cmd->uc_compl_arg);
6491# endif
6492
6493 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494
6495 if (i < gap->ga_len)
6496 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6497}
6498
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006499/*
6500 * split and quote args for <f-args>
6501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006503uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504{
6505 char_u *buf;
6506 char_u *p;
6507 char_u *q;
6508 int len;
6509
6510 /* Precalculate length */
6511 p = arg;
6512 len = 2; /* Initial and final quotes */
6513
6514 while (*p)
6515 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006516 if (p[0] == '\\' && p[1] == '\\')
6517 {
6518 len += 2;
6519 p += 2;
6520 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006521 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 {
6523 len += 1;
6524 p += 2;
6525 }
6526 else if (*p == '\\' || *p == '"')
6527 {
6528 len += 2;
6529 p += 1;
6530 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006531 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 {
6533 p = skipwhite(p);
6534 if (*p == NUL)
6535 break;
6536 len += 3; /* "," */
6537 }
6538 else
6539 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006540#ifdef FEAT_MBYTE
6541 int charlen = (*mb_ptr2len)(p);
6542 len += charlen;
6543 p += charlen;
6544#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 ++len;
6546 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 }
6549 }
6550
6551 buf = alloc(len + 1);
6552 if (buf == NULL)
6553 {
6554 *lenp = 0;
6555 return buf;
6556 }
6557
6558 p = arg;
6559 q = buf;
6560 *q++ = '"';
6561 while (*p)
6562 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006563 if (p[0] == '\\' && p[1] == '\\')
6564 {
6565 *q++ = '\\';
6566 *q++ = '\\';
6567 p += 2;
6568 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006569 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 {
6571 *q++ = p[1];
6572 p += 2;
6573 }
6574 else if (*p == '\\' || *p == '"')
6575 {
6576 *q++ = '\\';
6577 *q++ = *p++;
6578 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006579 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 {
6581 p = skipwhite(p);
6582 if (*p == NUL)
6583 break;
6584 *q++ = '"';
6585 *q++ = ',';
6586 *q++ = '"';
6587 }
6588 else
6589 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006590 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 }
6592 }
6593 *q++ = '"';
6594 *q = 0;
6595
6596 *lenp = len;
6597 return buf;
6598}
6599
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006600 static size_t
6601add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6602{
6603 size_t result;
6604
6605 result = STRLEN(mod_str);
6606 if (*multi_mods)
6607 result += 1;
6608 if (buf != NULL)
6609 {
6610 if (*multi_mods)
6611 STRCAT(buf, " ");
6612 STRCAT(buf, mod_str);
6613 }
6614
6615 *multi_mods = 1;
6616
6617 return result;
6618}
6619
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620/*
6621 * Check for a <> code in a user command.
6622 * "code" points to the '<'. "len" the length of the <> (inclusive).
6623 * "buf" is where the result is to be added.
6624 * "split_buf" points to a buffer used for splitting, caller should free it.
6625 * "split_len" is the length of what "split_buf" contains.
6626 * Returns the length of the replacement, which has been added to "buf".
6627 * Returns -1 if there was no match, and only the "<" has been copied.
6628 */
6629 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006630uc_check_code(
6631 char_u *code,
6632 size_t len,
6633 char_u *buf,
6634 ucmd_T *cmd, /* the user command we're expanding */
6635 exarg_T *eap, /* ex arguments */
6636 char_u **split_buf,
6637 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638{
6639 size_t result = 0;
6640 char_u *p = code + 1;
6641 size_t l = len - 2;
6642 int quote = 0;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006643 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_MODS,
6644 ct_REGISTER, ct_LT, ct_NONE } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645
6646 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6647 {
6648 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6649 p += 2;
6650 l -= 2;
6651 }
6652
Bram Moolenaar371d5402006-03-20 21:47:49 +00006653 ++l;
6654 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006656 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006658 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006660 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006662 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006664 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006666 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006668 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006670 else if (STRNICMP(p, "mods>", l) == 0)
6671 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672
6673 switch (type)
6674 {
6675 case ct_ARGS:
6676 /* Simple case first */
6677 if (*eap->arg == NUL)
6678 {
6679 if (quote == 1)
6680 {
6681 result = 2;
6682 if (buf != NULL)
6683 STRCPY(buf, "''");
6684 }
6685 else
6686 result = 0;
6687 break;
6688 }
6689
6690 /* When specified there is a single argument don't split it.
6691 * Works for ":Cmd %" when % is "a b c". */
6692 if ((eap->argt & NOSPC) && quote == 2)
6693 quote = 1;
6694
6695 switch (quote)
6696 {
6697 case 0: /* No quoting, no splitting */
6698 result = STRLEN(eap->arg);
6699 if (buf != NULL)
6700 STRCPY(buf, eap->arg);
6701 break;
6702 case 1: /* Quote, but don't split */
6703 result = STRLEN(eap->arg) + 2;
6704 for (p = eap->arg; *p; ++p)
6705 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006706#ifdef FEAT_MBYTE
6707 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6708 /* DBCS can contain \ in a trail byte, skip the
6709 * double-byte character. */
6710 ++p;
6711 else
6712#endif
6713 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 ++result;
6715 }
6716
6717 if (buf != NULL)
6718 {
6719 *buf++ = '"';
6720 for (p = eap->arg; *p; ++p)
6721 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006722#ifdef FEAT_MBYTE
6723 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6724 /* DBCS can contain \ in a trail byte, copy the
6725 * double-byte character to avoid escaping. */
6726 *buf++ = *p++;
6727 else
6728#endif
6729 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 *buf++ = '\\';
6731 *buf++ = *p;
6732 }
6733 *buf = '"';
6734 }
6735
6736 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006737 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 /* This is hard, so only do it once, and cache the result */
6739 if (*split_buf == NULL)
6740 *split_buf = uc_split_args(eap->arg, split_len);
6741
6742 result = *split_len;
6743 if (buf != NULL && result != 0)
6744 STRCPY(buf, *split_buf);
6745
6746 break;
6747 }
6748 break;
6749
6750 case ct_BANG:
6751 result = eap->forceit ? 1 : 0;
6752 if (quote)
6753 result += 2;
6754 if (buf != NULL)
6755 {
6756 if (quote)
6757 *buf++ = '"';
6758 if (eap->forceit)
6759 *buf++ = '!';
6760 if (quote)
6761 *buf = '"';
6762 }
6763 break;
6764
6765 case ct_LINE1:
6766 case ct_LINE2:
6767 case ct_COUNT:
6768 {
6769 char num_buf[20];
6770 long num = (type == ct_LINE1) ? eap->line1 :
6771 (type == ct_LINE2) ? eap->line2 :
6772 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6773 size_t num_len;
6774
6775 sprintf(num_buf, "%ld", num);
6776 num_len = STRLEN(num_buf);
6777 result = num_len;
6778
6779 if (quote)
6780 result += 2;
6781
6782 if (buf != NULL)
6783 {
6784 if (quote)
6785 *buf++ = '"';
6786 STRCPY(buf, num_buf);
6787 buf += num_len;
6788 if (quote)
6789 *buf = '"';
6790 }
6791
6792 break;
6793 }
6794
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006795 case ct_MODS:
6796 {
6797 int multi_mods = 0;
6798 typedef struct {
6799 int *varp;
6800 char *name;
6801 } mod_entry_T;
6802 static mod_entry_T mod_entries[] = {
6803#ifdef FEAT_BROWSE_CMD
6804 {&cmdmod.browse, "browse"},
6805#endif
6806#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6807 {&cmdmod.confirm, "confirm"},
6808#endif
6809 {&cmdmod.hide, "hide"},
6810 {&cmdmod.keepalt, "keepalt"},
6811 {&cmdmod.keepjumps, "keepjumps"},
6812 {&cmdmod.keepmarks, "keepmarks"},
6813 {&cmdmod.keeppatterns, "keeppatterns"},
6814 {&cmdmod.lockmarks, "lockmarks"},
6815 {&cmdmod.noswapfile, "noswapfile"},
6816 {NULL, NULL}
6817 };
6818 int i;
6819
6820 result = quote ? 2 : 0;
6821 if (buf != NULL)
6822 {
6823 if (quote)
6824 *buf++ = '"';
6825 *buf = '\0';
6826 }
6827
6828#ifdef FEAT_WINDOWS
6829 /* :aboveleft and :leftabove */
6830 if (cmdmod.split & WSP_ABOVE)
6831 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6832 /* :belowright and :rightbelow */
6833 if (cmdmod.split & WSP_BELOW)
6834 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6835 /* :botright */
6836 if (cmdmod.split & WSP_BOT)
6837 result += add_cmd_modifier(buf, "botright", &multi_mods);
6838#endif
6839
6840 /* the modifiers that are simple flags */
6841 for (i = 0; mod_entries[i].varp != NULL; ++i)
6842 if (*mod_entries[i].varp)
6843 result += add_cmd_modifier(buf, mod_entries[i].name,
6844 &multi_mods);
6845
6846 /* TODO: How to support :noautocmd? */
6847#ifdef HAVE_SANDBOX
6848 /* TODO: How to support :sandbox?*/
6849#endif
6850 /* :silent */
6851 if (msg_silent > 0)
6852 result += add_cmd_modifier(buf,
6853 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6854#ifdef FEAT_WINDOWS
6855 /* :tab */
6856 if (cmdmod.tab > 0)
6857 result += add_cmd_modifier(buf, "tab", &multi_mods);
6858 /* :topleft */
6859 if (cmdmod.split & WSP_TOP)
6860 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6861#endif
6862 /* TODO: How to support :unsilent?*/
6863 /* :verbose */
6864 if (p_verbose > 0)
6865 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6866#ifdef FEAT_WINDOWS
6867 /* :vertical */
6868 if (cmdmod.split & WSP_VERT)
6869 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6870#endif
6871 if (quote && buf != NULL)
6872 {
6873 buf += result - 2;
6874 *buf = '"';
6875 }
6876 break;
6877 }
6878
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 case ct_REGISTER:
6880 result = eap->regname ? 1 : 0;
6881 if (quote)
6882 result += 2;
6883 if (buf != NULL)
6884 {
6885 if (quote)
6886 *buf++ = '\'';
6887 if (eap->regname)
6888 *buf++ = eap->regname;
6889 if (quote)
6890 *buf = '\'';
6891 }
6892 break;
6893
6894 case ct_LT:
6895 result = 1;
6896 if (buf != NULL)
6897 *buf = '<';
6898 break;
6899
6900 default:
6901 /* Not recognized: just copy the '<' and return -1. */
6902 result = (size_t)-1;
6903 if (buf != NULL)
6904 *buf = '<';
6905 break;
6906 }
6907
6908 return result;
6909}
6910
6911 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006912do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913{
6914 char_u *buf;
6915 char_u *p;
6916 char_u *q;
6917
6918 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006919 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006920 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 size_t len, totlen;
6922
6923 size_t split_len = 0;
6924 char_u *split_buf = NULL;
6925 ucmd_T *cmd;
6926#ifdef FEAT_EVAL
6927 scid_T save_current_SID = current_SID;
6928#endif
6929
6930 if (eap->cmdidx == CMD_USER)
6931 cmd = USER_CMD(eap->useridx);
6932 else
6933 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6934
6935 /*
6936 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006937 * First round: "buf" is NULL, compute length, allocate "buf".
6938 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 */
6940 buf = NULL;
6941 for (;;)
6942 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006943 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006944 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006946
6947 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006949 start = vim_strchr(p, '<');
6950 if (start != NULL)
6951 end = vim_strchr(start + 1, '>');
6952 if (buf != NULL)
6953 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006954 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6955 ;
6956 if (*ksp == K_SPECIAL
6957 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006958 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6959# ifdef FEAT_GUI
6960 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6961# endif
6962 ))
6963 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006964 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006965 * KS_SPECIAL KE_FILLER, like for mappings, but
6966 * do_cmdline() doesn't handle that, so convert it back.
6967 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6968 len = ksp - p;
6969 if (len > 0)
6970 {
6971 mch_memmove(q, p, len);
6972 q += len;
6973 }
6974 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6975 p = ksp + 3;
6976 continue;
6977 }
6978 }
6979
6980 /* break if there no <item> is found */
6981 if (start == NULL || end == NULL)
6982 break;
6983
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 /* Include the '>' */
6985 ++end;
6986
6987 /* Take everything up to the '<' */
6988 len = start - p;
6989 if (buf == NULL)
6990 totlen += len;
6991 else
6992 {
6993 mch_memmove(q, p, len);
6994 q += len;
6995 }
6996
6997 len = uc_check_code(start, end - start, q, cmd, eap,
6998 &split_buf, &split_len);
6999 if (len == (size_t)-1)
7000 {
7001 /* no match, continue after '<' */
7002 p = start + 1;
7003 len = 1;
7004 }
7005 else
7006 p = end;
7007 if (buf == NULL)
7008 totlen += len;
7009 else
7010 q += len;
7011 }
7012 if (buf != NULL) /* second time here, finished */
7013 {
7014 STRCPY(q, p);
7015 break;
7016 }
7017
7018 totlen += STRLEN(p); /* Add on the trailing characters */
7019 buf = alloc((unsigned)(totlen + 1));
7020 if (buf == NULL)
7021 {
7022 vim_free(split_buf);
7023 return;
7024 }
7025 }
7026
7027#ifdef FEAT_EVAL
7028 current_SID = cmd->uc_scriptID;
7029#endif
7030 (void)do_cmdline(buf, eap->getline, eap->cookie,
7031 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
7032#ifdef FEAT_EVAL
7033 current_SID = save_current_SID;
7034#endif
7035 vim_free(buf);
7036 vim_free(split_buf);
7037}
7038
7039# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7040 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007041get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
7043 return get_user_commands(NULL, idx - (int)CMD_SIZE);
7044}
7045
7046/*
7047 * Function given to ExpandGeneric() to obtain the list of user command names.
7048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007050get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051{
7052 if (idx < curbuf->b_ucmds.ga_len)
7053 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
7054 idx -= curbuf->b_ucmds.ga_len;
7055 if (idx < ucmds.ga_len)
7056 return USER_CMD(idx)->uc_name;
7057 return NULL;
7058}
7059
7060/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007061 * Function given to ExpandGeneric() to obtain the list of user address type names.
7062 */
7063 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007064get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007065{
7066 return (char_u *)addr_type_complete[idx].name;
7067}
7068
7069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 * Function given to ExpandGeneric() to obtain the list of user command
7071 * attributes.
7072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007074get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075{
7076 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007077 {"addr", "bang", "bar", "buffer", "complete",
7078 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007080 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 return NULL;
7082 return (char_u *)user_cmd_flags[idx];
7083}
7084
7085/*
7086 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007089get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090{
7091 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7092
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007093 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 return NULL;
7095 return (char_u *)user_cmd_nargs[idx];
7096}
7097
7098/*
7099 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007102get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103{
7104 return (char_u *)command_complete[idx].name;
7105}
7106# endif /* FEAT_CMDL_COMPL */
7107
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007108/*
7109 * Parse address type argument
7110 */
7111 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007112parse_addr_type_arg(
7113 char_u *value,
7114 int vallen,
7115 long *argt,
7116 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007117{
7118 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007119
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007120 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7121 {
7122 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7123 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7124 if (a && b)
7125 {
7126 *addr_type_arg = addr_type_complete[i].expand;
7127 break;
7128 }
7129 }
7130
7131 if (addr_type_complete[i].expand == -1)
7132 {
7133 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007134
Bram Moolenaar1c465442017-03-12 20:10:05 +01007135 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007136 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007137 err[i] = NUL;
7138 EMSG2(_("E180: Invalid address type value: %s"), err);
7139 return FAIL;
7140 }
7141
7142 if (*addr_type_arg != ADDR_LINES)
7143 *argt |= NOTADR;
7144
7145 return OK;
7146}
7147
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148#endif /* FEAT_USR_CMDS */
7149
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007150#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7151/*
7152 * Parse a completion argument "value[vallen]".
7153 * The detected completion goes in "*complp", argument type in "*argt".
7154 * When there is an argument, for function and user defined completion, it's
7155 * copied to allocated memory and stored in "*compl_arg".
7156 * Returns FAIL if something is wrong.
7157 */
7158 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007159parse_compl_arg(
7160 char_u *value,
7161 int vallen,
7162 int *complp,
7163 long *argt,
7164 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007165{
7166 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007167# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007168 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007169# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007170 int i;
7171 int valend = vallen;
7172
7173 /* Look for any argument part - which is the part after any ',' */
7174 for (i = 0; i < vallen; ++i)
7175 {
7176 if (value[i] == ',')
7177 {
7178 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007179# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007180 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007181# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007182 valend = i;
7183 break;
7184 }
7185 }
7186
7187 for (i = 0; command_complete[i].expand != 0; ++i)
7188 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007189 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007190 && STRNCMP(value, command_complete[i].name, valend) == 0)
7191 {
7192 *complp = command_complete[i].expand;
7193 if (command_complete[i].expand == EXPAND_BUFFERS)
7194 *argt |= BUFNAME;
7195 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7196 || command_complete[i].expand == EXPAND_FILES)
7197 *argt |= XFILE;
7198 break;
7199 }
7200 }
7201
7202 if (command_complete[i].expand == 0)
7203 {
7204 EMSG2(_("E180: Invalid complete value: %s"), value);
7205 return FAIL;
7206 }
7207
7208# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7209 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7210 && arg != NULL)
7211# else
7212 if (arg != NULL)
7213# endif
7214 {
7215 EMSG(_("E468: Completion argument only allowed for custom completion"));
7216 return FAIL;
7217 }
7218
7219# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7220 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7221 && arg == NULL)
7222 {
7223 EMSG(_("E467: Custom completion requires a function argument"));
7224 return FAIL;
7225 }
7226
7227 if (arg != NULL)
7228 *compl_arg = vim_strnsave(arg, (int)arglen);
7229# endif
7230 return OK;
7231}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007232
7233 int
7234cmdcomplete_str_to_type(char_u *complete_str)
7235{
7236 int i;
7237
7238 for (i = 0; command_complete[i].expand != 0; ++i)
7239 if (STRCMP(complete_str, command_complete[i].name) == 0)
7240 return command_complete[i].expand;
7241
7242 return EXPAND_NOTHING;
7243}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007244#endif
7245
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007247ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248{
Bram Moolenaare6850792010-05-14 15:28:44 +02007249 if (*eap->arg == NUL)
7250 {
7251#ifdef FEAT_EVAL
7252 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7253 char_u *p = NULL;
7254
7255 if (expr != NULL)
7256 {
7257 ++emsg_off;
7258 p = eval_to_string(expr, NULL, FALSE);
7259 --emsg_off;
7260 vim_free(expr);
7261 }
7262 if (p != NULL)
7263 {
7264 MSG(p);
7265 vim_free(p);
7266 }
7267 else
7268 MSG("default");
7269#else
7270 MSG(_("unknown"));
7271#endif
7272 }
7273 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007274 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275}
7276
7277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007278ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279{
7280 if (*eap->arg == NUL && eap->cmd[2] == '!')
7281 MSG(_("Greetings, Vim user!"));
7282 do_highlight(eap->arg, eap->forceit, FALSE);
7283}
7284
7285
7286/*
7287 * Call this function if we thought we were going to exit, but we won't
7288 * (because of an error). May need to restore the terminal mode.
7289 */
7290 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007291not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292{
7293 exiting = FALSE;
7294 settmode(TMODE_RAW);
7295}
7296
7297/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007298 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 */
7300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007301ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007303#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007304 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007305#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007306
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307#ifdef FEAT_CMDWIN
7308 if (cmdwin_type != 0)
7309 {
7310 cmdwin_result = Ctrl_C;
7311 return;
7312 }
7313#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007314 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007315 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007316 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007317 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007318 return;
7319 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007320#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007321 if (eap->addr_count > 0)
7322 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007323 int wnr = eap->line2;
7324
7325 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7326 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007327 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007328 }
7329 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007330#endif
7331#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007332 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007333#endif
7334
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007335#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007336 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007337 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007338 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007339 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007340 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007341 return;
7342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343
7344#ifdef FEAT_NETBEANS_INTG
7345 netbeansForcedQuit = eap->forceit;
7346#endif
7347
7348 /*
7349 * If there are more files or windows we won't exit.
7350 */
7351 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7352 exiting = TRUE;
7353 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007354 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7355 | (eap->forceit ? CCGD_FORCEIT : 0)
7356 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007358 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 {
7360 not_exiting();
7361 }
7362 else
7363 {
7364#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007365 /* quit last window
7366 * Note: only_one_window() returns true, even so a help window is
7367 * still open. In that case only quit, if no address has been
7368 * specified. Example:
7369 * :h|wincmd w|1q - don't quit
7370 * :h|wincmd w|q - quit
7371 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007372 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373#endif
7374 getout(0);
7375#ifdef FEAT_WINDOWS
7376# ifdef FEAT_GUI
7377 need_mouse_correct = TRUE;
7378# endif
7379 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007380 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381#endif
7382 }
7383}
7384
7385/*
7386 * ":cquit".
7387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007389ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
7391 getout(1); /* this does not always pass on the exit code to the Manx
7392 compiler. why? */
7393}
7394
7395/*
7396 * ":qall": try to quit all windows
7397 */
7398 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007399ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400{
7401# ifdef FEAT_CMDWIN
7402 if (cmdwin_type != 0)
7403 {
7404 if (eap->forceit)
7405 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7406 else
7407 cmdwin_result = K_XF2;
7408 return;
7409 }
7410# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007411
7412 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007413 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007414 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007415 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007416 return;
7417 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007418#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007419 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7420 /* Refuse to quit when locked or when the buffer in the last window is
7421 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007422 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007423 return;
7424#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007425
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007427 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 getout(0);
7429 not_exiting();
7430}
7431
Bram Moolenaard9967712006-03-11 21:18:15 +00007432#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433/*
7434 * ":close": close current window, unless it is the last one
7435 */
7436 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007437ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007439 win_T *win;
7440 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441# ifdef FEAT_CMDWIN
7442 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007443 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 else
7445# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007446 if (!text_locked()
7447#ifdef FEAT_AUTOCMD
7448 && !curbuf_locked()
7449#endif
7450 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007451 {
7452 if (eap->addr_count == 0)
7453 ex_win_close(eap->forceit, curwin, NULL);
7454 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007455 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007456 {
7457 winnr++;
7458 if (winnr == eap->line2)
7459 break;
7460 }
7461 if (win == NULL)
7462 win = lastwin;
7463 ex_win_close(eap->forceit, win, NULL);
7464 }
7465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466}
7467
Bram Moolenaard9967712006-03-11 21:18:15 +00007468# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007469/*
7470 * ":pclose": Close any preview window.
7471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007473ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007474{
7475 win_T *win;
7476
Bram Moolenaar29323592016-07-24 22:04:11 +02007477 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007478 if (win->w_p_pvw)
7479 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007480 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007481 break;
7482 }
7483}
Bram Moolenaard9967712006-03-11 21:18:15 +00007484# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007485
Bram Moolenaarf740b292006-02-16 22:11:02 +00007486/*
7487 * Close window "win" and take care of handling closing the last window for a
7488 * modified buffer.
7489 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007491ex_win_close(
7492 int forceit,
7493 win_T *win,
7494 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495{
7496 int need_hide;
7497 buf_T *buf = win->w_buffer;
7498
7499 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007500 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007502# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 if ((p_confirm || cmdmod.confirm) && p_write)
7504 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007505 bufref_T bufref;
7506
7507 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007509 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 return;
7511 need_hide = FALSE;
7512 }
7513 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007514# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 {
7516 EMSG(_(e_nowrtmsg));
7517 return;
7518 }
7519 }
7520
Bram Moolenaard9967712006-03-11 21:18:15 +00007521# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007523# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007524
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007526 if (tp == NULL)
7527 win_close(win, !need_hide && !P_HID(buf));
7528 else
7529 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530}
7531
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007533 * Handle the argument for a tabpage related ex command.
7534 * Returns a tabpage number.
7535 * When an error is encountered then eap->errmsg is set.
7536 */
7537 static int
7538get_tabpage_arg(exarg_T *eap)
7539{
7540 int tab_number;
7541 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7542
7543 if (eap->arg && *eap->arg != NUL)
7544 {
7545 char_u *p = eap->arg;
7546 char_u *p_save;
7547 int relative = 0; /* argument +N/-N means: go to N places to the
7548 * right/left relative to the current position. */
7549
7550 if (*p == '-')
7551 {
7552 relative = -1;
7553 p++;
7554 }
7555 else if (*p == '+')
7556 {
7557 relative = 1;
7558 p++;
7559 }
7560
7561 p_save = p;
7562 tab_number = getdigits(&p);
7563
7564 if (relative == 0)
7565 {
7566 if (STRCMP(p, "$") == 0)
7567 tab_number = LAST_TAB_NR;
7568 else if (p == p_save || *p_save == '-' || *p != NUL
7569 || tab_number > LAST_TAB_NR)
7570 {
7571 /* No numbers as argument. */
7572 eap->errmsg = e_invarg;
7573 goto theend;
7574 }
7575 }
7576 else
7577 {
7578 if (*p_save == NUL)
7579 tab_number = 1;
7580 else if (p == p_save || *p_save == '-' || *p != NUL
7581 || tab_number == 0)
7582 {
7583 /* No numbers as argument. */
7584 eap->errmsg = e_invarg;
7585 goto theend;
7586 }
7587 tab_number = tab_number * relative + tabpage_index(curtab);
7588 if (!unaccept_arg0 && relative == -1)
7589 --tab_number;
7590 }
7591 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7592 eap->errmsg = e_invarg;
7593 }
7594 else if (eap->addr_count > 0)
7595 {
7596 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007597 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007598 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007599 tab_number = 0;
7600 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007601 else
7602 {
7603 tab_number = eap->line2;
7604 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7605 {
7606 --tab_number;
7607 if (tab_number < unaccept_arg0)
7608 eap->errmsg = e_invarg;
7609 }
7610 }
7611 }
7612 else
7613 {
7614 switch (eap->cmdidx)
7615 {
7616 case CMD_tabnext:
7617 tab_number = tabpage_index(curtab) + 1;
7618 if (tab_number > LAST_TAB_NR)
7619 tab_number = 1;
7620 break;
7621 case CMD_tabmove:
7622 tab_number = LAST_TAB_NR;
7623 break;
7624 default:
7625 tab_number = tabpage_index(curtab);
7626 }
7627 }
7628
7629theend:
7630 return tab_number;
7631}
7632
7633/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007634 * ":tabclose": close current tab page, unless it is the last one.
7635 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 */
7637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007638ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007640 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007641 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007642
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007643# ifdef FEAT_CMDWIN
7644 if (cmdwin_type != 0)
7645 cmdwin_result = K_IGNORE;
7646 else
7647# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007648 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007649 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007650 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007652 tab_number = get_tabpage_arg(eap);
7653 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007654 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007655 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007656 if (tp == NULL)
7657 {
7658 beep_flush();
7659 return;
7660 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007661 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007662 {
7663 tabpage_close_other(tp, eap->forceit);
7664 return;
7665 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007666 else if (!text_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007667#ifdef FEAT_AUTOCMD
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007668 && !curbuf_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007669#endif
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007670 )
7671 tabpage_close(eap->forceit);
7672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007674}
7675
7676/*
7677 * ":tabonly": close all tab pages except the current one
7678 */
7679 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007680ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007681{
7682 tabpage_T *tp;
7683 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007684 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007685
7686# ifdef FEAT_CMDWIN
7687 if (cmdwin_type != 0)
7688 cmdwin_result = K_IGNORE;
7689 else
7690# endif
7691 if (first_tabpage->tp_next == NULL)
7692 MSG(_("Already only one tab page"));
7693 else
7694 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007695 tab_number = get_tabpage_arg(eap);
7696 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007697 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007698 goto_tabpage(tab_number);
7699 /* Repeat this up to a 1000 times, because autocommands may
7700 * mess up the lists. */
7701 for (done = 0; done < 1000; ++done)
7702 {
7703 FOR_ALL_TABPAGES(tp)
7704 if (tp->tp_topframe != topframe)
7705 {
7706 tabpage_close_other(tp, eap->forceit);
7707 /* if we failed to close it quit */
7708 if (valid_tabpage(tp))
7709 done = 1000;
7710 /* start over, "tp" is now invalid */
7711 break;
7712 }
7713 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007714 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007715 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007716 }
7717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719
7720/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007721 * Close the current tab page.
7722 */
7723 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007724tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007725{
7726 /* First close all the windows but the current one. If that worked then
7727 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007728 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007729 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007730 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007731 ex_win_close(forceit, curwin, NULL);
7732# ifdef FEAT_GUI
7733 need_mouse_correct = TRUE;
7734# endif
7735}
7736
7737/*
7738 * Close tab page "tp", which is not the current tab page.
7739 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007740 * Also takes care of the tab pages line disappearing when closing the
7741 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007742 */
7743 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007744tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007745{
7746 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007747 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007748 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007749
7750 /* Limit to 1000 windows, autocommands may add a window while we close
7751 * one. OK, so I'm paranoid... */
7752 while (++done < 1000)
7753 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007754 wp = tp->tp_firstwin;
7755 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007756
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007757 /* Autocommands may delete the tab page under our fingers and we may
7758 * fail to close a window with a modified buffer. */
7759 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007760 break;
7761 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007762
Bram Moolenaar29323592016-07-24 22:04:11 +02007763#ifdef FEAT_AUTOCMD
7764 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7765#endif
7766
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007767 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007768 if (h != tabline_height())
7769 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007770}
7771
7772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 * ":only".
7774 */
7775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007776ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007778 win_T *wp;
7779 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780# ifdef FEAT_GUI
7781 need_mouse_correct = TRUE;
7782# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007783 if (eap->addr_count > 0)
7784 {
7785 wnr = eap->line2;
7786 for (wp = firstwin; --wnr > 0; )
7787 {
7788 if (wp->w_next == NULL)
7789 break;
7790 else
7791 wp = wp->w_next;
7792 }
7793 win_goto(wp);
7794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 close_others(TRUE, eap->forceit);
7796}
7797
7798/*
7799 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007800 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007802 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007803ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804{
7805 if (eap->addr_count == 0)
7806 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007807 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808}
7809#endif /* FEAT_WINDOWS */
7810
7811 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007812ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007814 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815#ifdef FEAT_WINDOWS
Bram Moolenaar2256c992016-11-15 21:17:07 +01007816 if (!eap->skip)
7817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818# ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007819 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820# endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007821 if (eap->addr_count == 0)
7822 win_close(curwin, FALSE); /* don't free buffer */
7823 else
7824 {
7825 int winnr = 0;
7826 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007827
Bram Moolenaar2256c992016-11-15 21:17:07 +01007828 FOR_ALL_WINDOWS(win)
7829 {
7830 winnr++;
7831 if (winnr == eap->line2)
7832 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007833 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007834 if (win == NULL)
7835 win = lastwin;
7836 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840}
7841
7842/*
7843 * ":stop" and ":suspend": Suspend Vim.
7844 */
7845 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007846ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847{
7848 /*
7849 * Disallow suspending for "rvim".
7850 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007851 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 {
7853 if (!eap->forceit)
7854 autowrite_all();
7855 windgoto((int)Rows - 1, 0);
7856 out_char('\n');
7857 out_flush();
7858 stoptermcap();
7859 out_flush(); /* needed for SUN to restore xterm buffer */
7860#ifdef FEAT_TITLE
7861 mch_restore_title(3); /* restore window titles */
7862#endif
7863 ui_suspend(); /* call machine specific function */
7864#ifdef FEAT_TITLE
7865 maketitle();
7866 resettitle(); /* force updating the title */
7867#endif
7868 starttermcap();
7869 scroll_start(); /* scroll screen before redrawing */
7870 redraw_later_clear();
7871 shell_resized(); /* may have resized window */
7872 }
7873}
7874
7875/*
7876 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7877 */
7878 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007879ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880{
7881#ifdef FEAT_CMDWIN
7882 if (cmdwin_type != 0)
7883 {
7884 cmdwin_result = Ctrl_C;
7885 return;
7886 }
7887#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007888 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007889 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007890 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007891 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007892 return;
7893 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007894#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007895 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7896 /* Refuse to quit when locked or when the buffer in the last window is
7897 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007898 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007899 return;
7900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901
7902 /*
7903 * if more files or windows we won't exit
7904 */
7905 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7906 exiting = TRUE;
7907 if ( ((eap->cmdidx == CMD_wq
7908 || curbufIsChanged())
7909 && do_write(eap) == FAIL)
7910 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007911 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {
7913 not_exiting();
7914 }
7915 else
7916 {
7917#ifdef FEAT_WINDOWS
7918 if (only_one_window()) /* quit last window, exit Vim */
7919#endif
7920 getout(0);
7921#ifdef FEAT_WINDOWS
7922# ifdef FEAT_GUI
7923 need_mouse_correct = TRUE;
7924# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007925 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 win_close(curwin, !P_HID(curwin->w_buffer));
7927#endif
7928 }
7929}
7930
7931/*
7932 * ":print", ":list", ":number".
7933 */
7934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007935ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007937 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7938 EMSG(_(e_emptybuf));
7939 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007941 for ( ;!got_int; ui_breakcheck())
7942 {
7943 print_line(eap->line1,
7944 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7945 || (eap->flags & EXFLAG_NR)),
7946 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7947 if (++eap->line1 > eap->line2)
7948 break;
7949 out_flush(); /* show one line at a time */
7950 }
7951 setpcmark();
7952 /* put cursor at last line */
7953 curwin->w_cursor.lnum = eap->line2;
7954 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 }
7956
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958}
7959
7960#ifdef FEAT_BYTEOFF
7961 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007962ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963{
7964 goto_byte(eap->line2);
7965}
7966#endif
7967
7968/*
7969 * ":shell".
7970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007972ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973{
7974 do_shell(NULL, 0);
7975}
7976
7977#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7978 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007979 || defined(FEAT_GUI_MSWIN) \
7980 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 || defined(PROTO)
7982
7983/*
7984 * Handle a file drop. The code is here because a drop is *nearly* like an
7985 * :args command, but not quite (we have a list of exact filenames, so we
7986 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7987 * code is very similar to :args and hence needs access to a lot of the static
7988 * functions in this file.
7989 *
7990 * The list should be allocated using alloc(), as should each item in the
7991 * list. This function takes over responsibility for freeing the list.
7992 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007993 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007994 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 */
7996 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007997handle_drop(
7998 int filec, /* the number of files dropped */
7999 char_u **filev, /* the list of files dropped */
8000 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001{
8002 exarg_T ea;
8003 int save_msg_scroll = msg_scroll;
8004
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008005 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008006 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008008#ifdef FEAT_AUTOCMD
8009 if (curbuf_locked())
8010 return;
8011#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008012 /* When the screen is being updated we should not change buffers and
8013 * windows structures, it may cause freed memory to be used. */
8014 if (updating_screen)
8015 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016
8017 /* Check whether the current buffer is changed. If so, we will need
8018 * to split the current window or data could be lost.
8019 * We don't need to check if the 'hidden' option is set, as in this
8020 * case the buffer won't be lost.
8021 */
8022 if (!P_HID(curbuf) && !split)
8023 {
8024 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008025 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 --emsg_off;
8027 }
8028 if (split)
8029 {
8030# ifdef FEAT_WINDOWS
8031 if (win_split(0, 0) == FAIL)
8032 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008033 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034
8035 /* When splitting the window, create a new alist. Otherwise the
8036 * existing one is overwritten. */
8037 alist_unlink(curwin->w_alist);
8038 alist_new();
8039# else
8040 return; /* can't split, always fail */
8041# endif
8042 }
8043
8044 /*
8045 * Set up the new argument list.
8046 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00008047 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048
8049 /*
8050 * Move to the first file.
8051 */
8052 /* Fake up a minimal "next" command for do_argfile() */
8053 vim_memset(&ea, 0, sizeof(ea));
8054 ea.cmd = (char_u *)"next";
8055 do_argfile(&ea, 0);
8056
8057 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
8058 * mode that is not needed here. */
8059 need_start_insertmode = FALSE;
8060
8061 /* Restore msg_scroll, otherwise a following command may cause scrolling
8062 * unexpectedly. The screen will be redrawn by the caller, thus
8063 * msg_scroll being set by displaying a message is irrelevant. */
8064 msg_scroll = save_msg_scroll;
8065}
8066#endif
8067
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068/*
8069 * Clear an argument list: free all file names and reset it to zero entries.
8070 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008071 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008072alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073{
8074 while (--al->al_ga.ga_len >= 0)
8075 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8076 ga_clear(&al->al_ga);
8077}
8078
8079/*
8080 * Init an argument list.
8081 */
8082 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008083alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084{
8085 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8086}
8087
8088#if defined(FEAT_WINDOWS) || defined(PROTO)
8089
8090/*
8091 * Remove a reference from an argument list.
8092 * Ignored when the argument list is the global one.
8093 * If the argument list is no longer used by any window, free it.
8094 */
8095 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008096alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097{
8098 if (al != &global_alist && --al->al_refcount <= 0)
8099 {
8100 alist_clear(al);
8101 vim_free(al);
8102 }
8103}
8104
8105# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
8106/*
8107 * Create a new argument list and use it for the current window.
8108 */
8109 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008110alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111{
8112 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8113 if (curwin->w_alist == NULL)
8114 {
8115 curwin->w_alist = &global_alist;
8116 ++global_alist.al_refcount;
8117 }
8118 else
8119 {
8120 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008121 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 alist_init(curwin->w_alist);
8123 }
8124}
8125# endif
8126#endif
8127
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008128#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129/*
8130 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008131 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8132 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 */
8134 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008135alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136{
8137 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008138 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 char_u **new_arg_files;
8140 int new_arg_file_count;
8141 char_u *save_p_su = p_su;
8142 int i;
8143
8144 /* Don't use 'suffixes' here. This should work like the shell did the
8145 * expansion. Also, the vimrc file isn't read yet, thus the user
8146 * can't set the options. */
8147 p_su = empty_option;
8148 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8149 if (old_arg_files != NULL)
8150 {
8151 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008152 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8153 old_arg_count = GARGCOUNT;
8154 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008156 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 && new_arg_file_count > 0)
8158 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008159 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8160 TRUE, fnum_list, fnum_len);
8161 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 }
8164 p_su = save_p_su;
8165}
8166#endif
8167
8168/*
8169 * Set the argument list for the current window.
8170 * Takes over the allocated files[] and the allocated fnames in it.
8171 */
8172 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008173alist_set(
8174 alist_T *al,
8175 int count,
8176 char_u **files,
8177 int use_curbuf,
8178 int *fnum_list,
8179 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180{
8181 int i;
8182
8183 alist_clear(al);
8184 if (ga_grow(&al->al_ga, count) == OK)
8185 {
8186 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008187 {
8188 if (got_int)
8189 {
8190 /* When adding many buffers this can take a long time. Allow
8191 * interrupting here. */
8192 while (i < count)
8193 vim_free(files[i++]);
8194 break;
8195 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008196
8197 /* May set buffer name of a buffer previously used for the
8198 * argument list, so that it's re-used by alist_add. */
8199 if (fnum_list != NULL && i < fnum_len)
8200 buf_set_name(fnum_list[i], files[i]);
8201
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008203 ui_breakcheck();
8204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 vim_free(files);
8206 }
8207 else
8208 FreeWild(count, files);
8209#ifdef FEAT_WINDOWS
8210 if (al == &global_alist)
8211#endif
8212 arg_had_last = FALSE;
8213}
8214
8215/*
8216 * Add file "fname" to argument list "al".
8217 * "fname" must have been allocated and "al" must have been checked for room.
8218 */
8219 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008220alist_add(
8221 alist_T *al,
8222 char_u *fname,
8223 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224{
8225 if (fname == NULL) /* don't add NULL file names */
8226 return;
8227#ifdef BACKSLASH_IN_FILENAME
8228 slash_adjust(fname);
8229#endif
8230 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8231 if (set_fnum > 0)
8232 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8233 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8234 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235}
8236
8237#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8238/*
8239 * Adjust slashes in file names. Called after 'shellslash' was set.
8240 */
8241 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008242alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243{
8244 int i;
8245# ifdef FEAT_WINDOWS
8246 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008247 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248# endif
8249
8250 for (i = 0; i < GARGCOUNT; ++i)
8251 if (GARGLIST[i].ae_fname != NULL)
8252 slash_adjust(GARGLIST[i].ae_fname);
8253# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00008254 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 if (wp->w_alist != &global_alist)
8256 for (i = 0; i < WARGCOUNT(wp); ++i)
8257 if (WARGLIST(wp)[i].ae_fname != NULL)
8258 slash_adjust(WARGLIST(wp)[i].ae_fname);
8259# endif
8260}
8261#endif
8262
8263/*
8264 * ":preserve".
8265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008267ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008269 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 ml_preserve(curbuf, TRUE);
8271}
8272
8273/*
8274 * ":recover".
8275 */
8276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008277ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278{
8279 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8280 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008281 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8282 | CCGD_MULTWIN
8283 | (eap->forceit ? CCGD_FORCEIT : 0)
8284 | CCGD_EXCMD)
8285
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 && (*eap->arg == NUL
8287 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8288 ml_recover();
8289 recoverymode = FALSE;
8290}
8291
8292/*
8293 * Command modifier used in a wrong way.
8294 */
8295 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008296ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297{
8298 eap->errmsg = e_invcmd;
8299}
8300
8301#ifdef FEAT_WINDOWS
8302/*
8303 * :sview [+command] file split window with new file, read-only
8304 * :split [[+command] file] split window with current or new file
8305 * :vsplit [[+command] file] split window vertically with current or new file
8306 * :new [[+command] file] split window with no or new file
8307 * :vnew [[+command] file] split vertically window with no or new file
8308 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008309 *
8310 * :tabedit open new Tab page with empty window
8311 * :tabedit [+command] file open new Tab page and edit "file"
8312 * :tabnew [[+command] file] just like :tabedit
8313 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 */
8315 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008316ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008318 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008319# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008321# endif
8322# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008324# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008326# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008328# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008330# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008332 * quickfix windows. But it's OK when doing ":tab split". */
8333 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {
8335 if (eap->cmdidx == CMD_split)
8336 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 if (eap->cmdidx == CMD_vsplit)
8338 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008340# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008342# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008343 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 {
8345 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8346 FNAME_MESS, TRUE, curbuf->b_ffname);
8347 if (fname == NULL)
8348 goto theend;
8349 eap->arg = fname;
8350 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008351# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008355# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 && eap->cmdidx != CMD_new)
8359 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008360# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008361 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008362# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008363 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008364# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008365 au_has_group((char_u *)"FileExplorer"))
8366 {
8367 /* No browsing supported but we do have the file explorer:
8368 * Edit the directory. */
8369 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8370 eap->arg = (char_u *)".";
8371 }
8372 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008373# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008374 {
8375 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008377 if (fname == NULL)
8378 goto theend;
8379 eap->arg = fname;
8380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 }
8382 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008385 /*
8386 * Either open new tab page or split the window.
8387 */
8388 if (eap->cmdidx == CMD_tabedit
8389 || eap->cmdidx == CMD_tabfind
8390 || eap->cmdidx == CMD_tabnew)
8391 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008392 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8393 : eap->addr_count == 0 ? 0
8394 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008395 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008396 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008397
8398 /* set the alternate buffer for the window we came from */
8399 if (curwin != old_curwin
8400 && win_valid(old_curwin)
8401 && old_curwin->w_buffer != curbuf
8402 && !cmdmod.keepalt)
8403 old_curwin->w_alt_fnum = curbuf->b_fnum;
8404 }
8405 }
8406 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8408 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008409# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 /* Reset 'scrollbind' when editing another file, but keep it when
8411 * doing ":split" without arguments. */
8412 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008413# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008415# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008417 {
8418 RESET_BINDING(curwin);
8419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 else
8421 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008422# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 do_exedit(eap, old_curwin);
8424 }
8425
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008426# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008428# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008430# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431theend:
8432 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008433# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008435
8436/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008437 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008438 */
8439 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008440tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008441{
8442 exarg_T ea;
8443
8444 vim_memset(&ea, 0, sizeof(ea));
8445 ea.cmdidx = CMD_tabnew;
8446 ea.cmd = (char_u *)"tabn";
8447 ea.arg = (char_u *)"";
8448 ex_splitview(&ea);
8449}
8450
8451/*
8452 * :tabnext command
8453 */
8454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008455ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008456{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008457 int tab_number;
8458
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008459 switch (eap->cmdidx)
8460 {
8461 case CMD_tabfirst:
8462 case CMD_tabrewind:
8463 goto_tabpage(1);
8464 break;
8465 case CMD_tablast:
8466 goto_tabpage(9999);
8467 break;
8468 case CMD_tabprevious:
8469 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008470 if (eap->arg && *eap->arg != NUL)
8471 {
8472 char_u *p = eap->arg;
8473 char_u *p_save = p;
8474
8475 tab_number = getdigits(&p);
8476 if (p == p_save || *p_save == '-' || *p != NUL
8477 || tab_number == 0)
8478 {
8479 /* No numbers as argument. */
8480 eap->errmsg = e_invarg;
8481 return;
8482 }
8483 }
8484 else
8485 {
8486 if (eap->addr_count == 0)
8487 tab_number = 1;
8488 else
8489 {
8490 tab_number = eap->line2;
8491 if (tab_number < 1)
8492 {
8493 eap->errmsg = e_invrange;
8494 return;
8495 }
8496 }
8497 }
8498 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008499 break;
8500 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008501 tab_number = get_tabpage_arg(eap);
8502 if (eap->errmsg == NULL)
8503 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008504 break;
8505 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008506}
8507
8508/*
8509 * :tabmove command
8510 */
8511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008512ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008513{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008514 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008515
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008516 tab_number = get_tabpage_arg(eap);
8517 if (eap->errmsg == NULL)
8518 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008519}
8520
8521/*
8522 * :tabs command: List tabs and their contents.
8523 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008524 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008525ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008526{
8527 tabpage_T *tp;
8528 win_T *wp;
8529 int tabcount = 1;
8530
8531 msg_start();
8532 msg_scroll = TRUE;
8533 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8534 {
8535 msg_putchar('\n');
8536 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008537 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008538 out_flush(); /* output one line at a time */
8539 ui_breakcheck();
8540
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008541 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008542 wp = firstwin;
8543 else
8544 wp = tp->tp_firstwin;
8545 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8546 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008547 msg_putchar('\n');
8548 msg_putchar(wp == curwin ? '>' : ' ');
8549 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008550 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8551 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008552 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008553 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008554 else
8555 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8556 IObuff, IOSIZE, TRUE);
8557 msg_outtrans(IObuff);
8558 out_flush(); /* output one line at a time */
8559 ui_breakcheck();
8560 }
8561 }
8562}
8563
8564#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565
8566/*
8567 * ":mode": Set screen mode.
8568 * If no argument given, just get the screen size and redraw.
8569 */
8570 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008571ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572{
8573 if (*eap->arg == NUL)
8574 shell_resized();
8575 else
8576 mch_screenmode(eap->arg);
8577}
8578
8579#ifdef FEAT_WINDOWS
8580/*
8581 * ":resize".
8582 * set, increment or decrement current window height
8583 */
8584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008585ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586{
8587 int n;
8588 win_T *wp = curwin;
8589
8590 if (eap->addr_count > 0)
8591 {
8592 n = eap->line2;
8593 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8594 ;
8595 }
8596
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008597# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008599# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 if (cmdmod.split & WSP_VERT)
8602 {
8603 if (*eap->arg == '-' || *eap->arg == '+')
8604 n += W_WIDTH(curwin);
8605 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8606 n = 9999;
8607 win_setwidth_win((int)n, wp);
8608 }
8609 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 {
8611 if (*eap->arg == '-' || *eap->arg == '+')
8612 n += curwin->w_height;
8613 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8614 n = 9999;
8615 win_setheight_win((int)n, wp);
8616 }
8617}
8618#endif
8619
8620/*
8621 * ":find [+command] <file>" command.
8622 */
8623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008624ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625{
8626#ifdef FEAT_SEARCHPATH
8627 char_u *fname;
8628 int count;
8629
8630 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8631 TRUE, curbuf->b_ffname);
8632 if (eap->addr_count > 0)
8633 {
8634 /* Repeat finding the file "count" times. This matters when it
8635 * appears several times in the path. */
8636 count = eap->line2;
8637 while (fname != NULL && --count > 0)
8638 {
8639 vim_free(fname);
8640 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8641 FALSE, curbuf->b_ffname);
8642 }
8643 }
8644
8645 if (fname != NULL)
8646 {
8647 eap->arg = fname;
8648#endif
8649 do_exedit(eap, NULL);
8650#ifdef FEAT_SEARCHPATH
8651 vim_free(fname);
8652 }
8653#endif
8654}
8655
8656/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008657 * ":open" simulation: for now just work like ":visual".
8658 */
8659 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008660ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008661{
8662 regmatch_T regmatch;
8663 char_u *p;
8664
8665 curwin->w_cursor.lnum = eap->line2;
8666 beginline(BL_SOL | BL_FIX);
8667 if (*eap->arg == '/')
8668 {
8669 /* ":open /pattern/": put cursor in column found with pattern */
8670 ++eap->arg;
8671 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8672 *p = NUL;
8673 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8674 if (regmatch.regprog != NULL)
8675 {
8676 regmatch.rm_ic = p_ic;
8677 p = ml_get_curline();
8678 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008679 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008680 else
8681 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008682 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008683 }
8684 /* Move to the NUL, ignore any other arguments. */
8685 eap->arg += STRLEN(eap->arg);
8686 }
8687 check_cursor();
8688
8689 eap->cmdidx = CMD_visual;
8690 do_exedit(eap, NULL);
8691}
8692
8693/*
8694 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 */
8696 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008697ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698{
8699 do_exedit(eap, NULL);
8700}
8701
8702/*
8703 * ":edit <file>" command and alikes.
8704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008706do_exedit(
8707 exarg_T *eap,
8708 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709{
8710 int n;
8711#ifdef FEAT_WINDOWS
8712 int need_hide;
8713#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008714 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715
8716 /*
8717 * ":vi" command ends Ex mode.
8718 */
8719 if (exmode_active && (eap->cmdidx == CMD_visual
8720 || eap->cmdidx == CMD_view))
8721 {
8722 exmode_active = FALSE;
8723 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008724 {
8725 /* Special case: ":global/pat/visual\NLvi-commands" */
8726 if (global_busy)
8727 {
8728 int rd = RedrawingDisabled;
8729 int nwr = no_wait_return;
8730 int ms = msg_scroll;
8731#ifdef FEAT_GUI
8732 int he = hold_gui_events;
8733#endif
8734
8735 if (eap->nextcmd != NULL)
8736 {
8737 stuffReadbuff(eap->nextcmd);
8738 eap->nextcmd = NULL;
8739 }
8740
8741 if (exmode_was != EXMODE_VIM)
8742 settmode(TMODE_RAW);
8743 RedrawingDisabled = 0;
8744 no_wait_return = 0;
8745 need_wait_return = FALSE;
8746 msg_scroll = 0;
8747#ifdef FEAT_GUI
8748 hold_gui_events = 0;
8749#endif
8750 must_redraw = CLEAR;
8751
8752 main_loop(FALSE, TRUE);
8753
8754 RedrawingDisabled = rd;
8755 no_wait_return = nwr;
8756 msg_scroll = ms;
8757#ifdef FEAT_GUI
8758 hold_gui_events = he;
8759#endif
8760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 }
8764
8765 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008766 || eap->cmdidx == CMD_tabnew
8767 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008768#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 || eap->cmdidx == CMD_vnew
8770#endif
8771 ) && *eap->arg == NUL)
8772 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008773 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 setpcmark();
8775 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008776 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8777 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 }
8779 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008780#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 && eap->cmdidx != CMD_vsplit
8782#endif
8783 )
8784 || *eap->arg != NUL
8785#ifdef FEAT_BROWSE
8786 || cmdmod.browse
8787#endif
8788 )
8789 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008790#ifdef FEAT_AUTOCMD
8791 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8792 * can bring us here, others are stopped earlier. */
8793 if (*eap->arg != NUL && curbuf_locked())
8794 return;
8795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 n = readonlymode;
8797 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8798 readonlymode = TRUE;
8799 else if (eap->cmdidx == CMD_enew)
8800 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8801 empty buffer */
8802 setpcmark();
8803 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8804 NULL, eap,
8805 /* ":edit" goes to first line if Vi compatible */
8806 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8807 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8808 ? ECMD_ONE : eap->do_ecmd_lnum,
8809 (P_HID(curbuf) ? ECMD_HIDE : 0)
8810 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008811 /* after a split we can use an existing buffer */
8812 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813#ifdef FEAT_LISTCMDS
8814 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8815#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008816 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 {
8818 /* Editing the file failed. If the window was split, close it. */
8819#ifdef FEAT_WINDOWS
8820 if (old_curwin != NULL)
8821 {
8822 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8823 if (!need_hide || P_HID(curbuf))
8824 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008825# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8826 cleanup_T cs;
8827
8828 /* Reset the error/interrupt/exception state here so that
8829 * aborting() returns FALSE when closing a window. */
8830 enter_cleanup(&cs);
8831# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832# ifdef FEAT_GUI
8833 need_mouse_correct = TRUE;
8834# endif
8835 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008836
8837# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8838 /* Restore the error/interrupt/exception state if not
8839 * discarded by a new aborting error, interrupt, or
8840 * uncaught exception. */
8841 leave_cleanup(&cs);
8842# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 }
8844 }
8845#endif
8846 }
8847 else if (readonlymode && curbuf->b_nwindows == 1)
8848 {
8849 /* When editing an already visited buffer, 'readonly' won't be set
8850 * but the previous value is kept. With ":view" and ":sview" we
8851 * want the file to be readonly, except when another window is
8852 * editing the same buffer. */
8853 curbuf->b_p_ro = TRUE;
8854 }
8855 readonlymode = n;
8856 }
8857 else
8858 {
8859 if (eap->do_ecmd_cmd != NULL)
8860 do_cmdline_cmd(eap->do_ecmd_cmd);
8861#ifdef FEAT_TITLE
8862 n = curwin->w_arg_idx_invalid;
8863#endif
8864 check_arg_idx(curwin);
8865#ifdef FEAT_TITLE
8866 if (n != curwin->w_arg_idx_invalid)
8867 maketitle();
8868#endif
8869 }
8870
8871#ifdef FEAT_WINDOWS
8872 /*
8873 * if ":split file" worked, set alternate file name in old window to new
8874 * file
8875 */
8876 if (old_curwin != NULL
8877 && *eap->arg != NUL
8878 && curwin != old_curwin
8879 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008880 && old_curwin->w_buffer != curbuf
8881 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 old_curwin->w_alt_fnum = curbuf->b_fnum;
8883#endif
8884
8885 ex_no_reprint = TRUE;
8886}
8887
8888#ifndef FEAT_GUI
8889/*
8890 * ":gui" and ":gvim" when there is no GUI.
8891 */
8892 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008893ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894{
8895 eap->errmsg = e_nogvim;
8896}
8897#endif
8898
8899#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8900 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008901ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902{
8903 gui_make_tearoff(eap->arg);
8904}
8905#endif
8906
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008907#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008909ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008911 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912}
8913#endif
8914
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008916ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917{
8918 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8919 MSG(_("No swap file"));
8920 else
8921 msg(curbuf->b_ml.ml_mfp->mf_fname);
8922}
8923
8924/*
8925 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8926 * offset.
8927 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008930ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931{
8932#ifdef FEAT_SCROLLBIND
8933 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008934 win_T *save_curwin = curwin;
8935 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 long topline;
8937 long y;
8938 linenr_T old_linenr = curwin->w_cursor.lnum;
8939
8940 setpcmark();
8941
8942 /*
8943 * determine max topline
8944 */
8945 if (curwin->w_p_scb)
8946 {
8947 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008948 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 {
8950 if (wp->w_p_scb && wp->w_buffer)
8951 {
8952 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8953 if (topline > y)
8954 topline = y;
8955 }
8956 }
8957 if (topline < 1)
8958 topline = 1;
8959 }
8960 else
8961 {
8962 topline = 1;
8963 }
8964
8965
8966 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008967 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008969 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 {
8971 if (curwin->w_p_scb)
8972 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008973 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 y = topline - curwin->w_topline;
8975 if (y > 0)
8976 scrollup(y, TRUE);
8977 else
8978 scrolldown(-y, TRUE);
8979 curwin->w_scbind_pos = topline;
8980 redraw_later(VALID);
8981 cursor_correct();
8982#ifdef FEAT_WINDOWS
8983 curwin->w_redr_status = TRUE;
8984#endif
8985 }
8986 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008987 curwin = save_curwin;
8988 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 if (curwin->w_p_scb)
8990 {
8991 did_syncbind = TRUE;
8992 checkpcmark();
8993 if (old_linenr != curwin->w_cursor.lnum)
8994 {
8995 char_u ctrl_o[2];
8996
8997 ctrl_o[0] = Ctrl_O;
8998 ctrl_o[1] = 0;
8999 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
9000 }
9001 }
9002#endif
9003}
9004
9005
9006 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009007ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008{
Bram Moolenaardf177f62005-02-22 08:39:57 +00009009 int i;
9010 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
9011 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012
9013 if (eap->usefilter) /* :r!cmd */
9014 do_bang(1, eap, FALSE, FALSE, TRUE);
9015 else
9016 {
9017 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
9018 return;
9019
9020#ifdef FEAT_BROWSE
9021 if (cmdmod.browse)
9022 {
9023 char_u *browseFile;
9024
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009025 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 NULL, NULL, NULL, curbuf);
9027 if (browseFile != NULL)
9028 {
9029 i = readfile(browseFile, NULL,
9030 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9031 vim_free(browseFile);
9032 }
9033 else
9034 i = OK;
9035 }
9036 else
9037#endif
9038 if (*eap->arg == NUL)
9039 {
9040 if (check_fname() == FAIL) /* check for no file name */
9041 return;
9042 i = readfile(curbuf->b_ffname, curbuf->b_fname,
9043 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9044 }
9045 else
9046 {
9047 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
9048 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
9049 i = readfile(eap->arg, NULL,
9050 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9051
9052 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01009053 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 {
9055#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
9056 if (!aborting())
9057#endif
9058 EMSG2(_(e_notopen), eap->arg);
9059 }
9060 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009061 {
9062 if (empty && exmode_active)
9063 {
9064 /* Delete the empty line that remains. Historically ex does
9065 * this but vi doesn't. */
9066 if (eap->line2 == 0)
9067 lnum = curbuf->b_ml.ml_line_count;
9068 else
9069 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009070 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009071 {
9072 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009073 if (curwin->w_cursor.lnum > 1
9074 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009075 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009076 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009077 }
9078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 }
9082}
9083
Bram Moolenaarea408852005-06-25 22:49:46 +00009084static char_u *prev_dir = NULL;
9085
9086#if defined(EXITFREE) || defined(PROTO)
9087 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009088free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009089{
9090 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00009091 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00009092
9093 vim_free(globaldir);
9094 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00009095}
9096#endif
9097
Bram Moolenaarf4258302013-06-02 18:20:17 +02009098/*
9099 * Deal with the side effects of changing the current directory.
9100 * When "local" is TRUE then this was after an ":lcd" command.
9101 */
9102 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009103post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009104{
9105 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01009106 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02009107 if (local)
9108 {
9109 /* If still in global directory, need to remember current
9110 * directory as global directory. */
9111 if (globaldir == NULL && prev_dir != NULL)
9112 globaldir = vim_strsave(prev_dir);
9113 /* Remember this local directory for the window. */
9114 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9115 curwin->w_localdir = vim_strsave(NameBuff);
9116 }
9117 else
9118 {
9119 /* We are now in the global directory, no need to remember its
9120 * name. */
9121 vim_free(globaldir);
9122 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02009123 }
9124
9125 shorten_fnames(TRUE);
9126}
9127
Bram Moolenaarea408852005-06-25 22:49:46 +00009128
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129/*
9130 * ":cd", ":lcd", ":chdir" and ":lchdir".
9131 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009132 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009133ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 char_u *new_dir;
9136 char_u *tofree;
9137
9138 new_dir = eap->arg;
9139#if !defined(UNIX) && !defined(VMS)
9140 /* for non-UNIX ":cd" means: print current directory */
9141 if (*new_dir == NUL)
9142 ex_pwd(NULL);
9143 else
9144#endif
9145 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009146#ifdef FEAT_AUTOCMD
9147 if (allbuf_locked())
9148 return;
9149#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009150 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9151 && !eap->forceit)
9152 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009153 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009154 return;
9155 }
9156
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 /* ":cd -": Change to previous directory */
9158 if (STRCMP(new_dir, "-") == 0)
9159 {
9160 if (prev_dir == NULL)
9161 {
9162 EMSG(_("E186: No previous directory"));
9163 return;
9164 }
9165 new_dir = prev_dir;
9166 }
9167
9168 /* Save current directory for next ":cd -" */
9169 tofree = prev_dir;
9170 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9171 prev_dir = vim_strsave(NameBuff);
9172 else
9173 prev_dir = NULL;
9174
9175#if defined(UNIX) || defined(VMS)
9176 /* for UNIX ":cd" means: go to home directory */
9177 if (*new_dir == NUL)
9178 {
9179 /* use NameBuff for home directory name */
9180# ifdef VMS
9181 char_u *p;
9182
9183 p = mch_getenv((char_u *)"SYS$LOGIN");
9184 if (p == NULL || *p == NUL) /* empty is the same as not set */
9185 NameBuff[0] = NUL;
9186 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009187 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188# else
9189 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9190# endif
9191 new_dir = NameBuff;
9192 }
9193#endif
9194 if (new_dir == NULL || vim_chdir(new_dir))
9195 EMSG(_(e_failed));
9196 else
9197 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02009198 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199
9200 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009201 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 ex_pwd(eap);
9203 }
9204 vim_free(tofree);
9205 }
9206}
9207
9208/*
9209 * ":pwd".
9210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009212ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213{
9214 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9215 {
9216#ifdef BACKSLASH_IN_FILENAME
9217 slash_adjust(NameBuff);
9218#endif
9219 msg(NameBuff);
9220 }
9221 else
9222 EMSG(_("E187: Unknown"));
9223}
9224
9225/*
9226 * ":=".
9227 */
9228 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009229ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230{
9231 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009232 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233}
9234
9235 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009236ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009238 int n;
9239 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240
9241 if (cursor_valid())
9242 {
9243 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9244 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01009245 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009247
9248 len = eap->line2;
9249 switch (*eap->arg)
9250 {
9251 case 'm': break;
9252 case NUL: len *= 1000L; break;
9253 default: EMSG2(_(e_invarg2), eap->arg); return;
9254 }
9255 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256}
9257
9258/*
9259 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9260 */
9261 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009262do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263{
9264 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009265 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266
9267 cursor_on();
9268 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009269 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009271 wait_now = msec - done > 1000L ? 1000L : msec - done;
9272#ifdef FEAT_TIMERS
9273 {
9274 long due_time = check_due_timer();
9275
9276 if (due_time > 0 && due_time < wait_now)
9277 wait_now = due_time;
9278 }
9279#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009280#ifdef FEAT_JOB_CHANNEL
9281 if (has_any_channel() && wait_now > 100L)
9282 wait_now = 100L;
9283#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009284 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009285#ifdef FEAT_JOB_CHANNEL
9286 if (has_any_channel())
9287 ui_breakcheck_force(TRUE);
9288 else
9289#endif
9290 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009291#ifdef MESSAGE_QUEUE
9292 /* Process the netbeans and clientserver messages that may have been
9293 * received in the call to ui_breakcheck() when the GUI is in use. This
9294 * may occur when running a test case. */
9295 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 }
9298}
9299
9300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009301do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302{
9303 int mode;
9304 char_u *cmdp;
9305
9306 cmdp = eap->cmd;
9307 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9308
9309 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9310 eap->arg, mode, isabbrev))
9311 {
9312 case 1: EMSG(_(e_invarg));
9313 break;
9314 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9315 break;
9316 }
9317}
9318
9319/*
9320 * ":winsize" command (obsolete).
9321 */
9322 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009323ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324{
9325 int w, h;
9326 char_u *arg = eap->arg;
9327 char_u *p;
9328
9329 w = getdigits(&arg);
9330 arg = skipwhite(arg);
9331 p = arg;
9332 h = getdigits(&arg);
9333 if (*p != NUL && *arg == NUL)
9334 set_shellsize(w, h, TRUE);
9335 else
9336 EMSG(_("E465: :winsize requires two number arguments"));
9337}
9338
9339#ifdef FEAT_WINDOWS
9340 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009341ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342{
9343 int xchar = NUL;
9344 char_u *p;
9345
9346 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9347 {
9348 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9349 if (eap->arg[1] == NUL)
9350 {
9351 EMSG(_(e_invarg));
9352 return;
9353 }
9354 xchar = eap->arg[1];
9355 p = eap->arg + 2;
9356 }
9357 else
9358 p = eap->arg + 1;
9359
9360 eap->nextcmd = check_nextcmd(p);
9361 p = skipwhite(p);
9362 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9363 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009364 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 {
9366 /* Pass flags on for ":vertical wincmd ]". */
9367 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009368 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9370 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009371 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 }
9373}
9374#endif
9375
Bram Moolenaar843ee412004-06-30 16:16:41 +00009376#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377/*
9378 * ":winpos".
9379 */
9380 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009381ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382{
9383 int x, y;
9384 char_u *arg = eap->arg;
9385 char_u *p;
9386
9387 if (*arg == NUL)
9388 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009389# if defined(FEAT_GUI) || defined(MSWIN)
9390# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009392# else
9393 if (mch_get_winpos(&x, &y) != FAIL)
9394# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 {
9396 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9397 msg(IObuff);
9398 }
9399 else
9400# endif
9401 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9402 }
9403 else
9404 {
9405 x = getdigits(&arg);
9406 arg = skipwhite(arg);
9407 p = arg;
9408 y = getdigits(&arg);
9409 if (*p == NUL || *arg != NUL)
9410 {
9411 EMSG(_("E466: :winpos requires two number arguments"));
9412 return;
9413 }
9414# ifdef FEAT_GUI
9415 if (gui.in_use)
9416 gui_mch_set_winpos(x, y);
9417 else if (gui.starting)
9418 {
9419 /* Remember the coordinates for when the window is opened. */
9420 gui_win_x = x;
9421 gui_win_y = y;
9422 }
9423# ifdef HAVE_TGETENT
9424 else
9425# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009426# else
9427# ifdef MSWIN
9428 mch_set_winpos(x, y);
9429# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430# endif
9431# ifdef HAVE_TGETENT
9432 if (*T_CWP)
9433 term_set_winpos(x, y);
9434# endif
9435 }
9436}
9437#endif
9438
9439/*
9440 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9441 */
9442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009443ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444{
9445 oparg_T oa;
9446
9447 clear_oparg(&oa);
9448 oa.regname = eap->regname;
9449 oa.start.lnum = eap->line1;
9450 oa.end.lnum = eap->line2;
9451 oa.line_count = eap->line2 - eap->line1 + 1;
9452 oa.motion_type = MLINE;
9453#ifdef FEAT_VIRTUALEDIT
9454 virtual_op = FALSE;
9455#endif
9456 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9457 {
9458 setpcmark();
9459 curwin->w_cursor.lnum = eap->line1;
9460 beginline(BL_SOL | BL_FIX);
9461 }
9462
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009463 if (VIsual_active)
9464 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009465
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 switch (eap->cmdidx)
9467 {
9468 case CMD_delete:
9469 oa.op_type = OP_DELETE;
9470 op_delete(&oa);
9471 break;
9472
9473 case CMD_yank:
9474 oa.op_type = OP_YANK;
9475 (void)op_yank(&oa, FALSE, TRUE);
9476 break;
9477
9478 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009479 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009481 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9482#else
9483 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009485 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 oa.op_type = OP_RSHIFT;
9487 else
9488 oa.op_type = OP_LSHIFT;
9489 op_shift(&oa, FALSE, eap->amount);
9490 break;
9491 }
9492#ifdef FEAT_VIRTUALEDIT
9493 virtual_op = MAYBE;
9494#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009495 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496}
9497
9498/*
9499 * ":put".
9500 */
9501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009502ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503{
9504 /* ":0put" works like ":1put!". */
9505 if (eap->line2 == 0)
9506 {
9507 eap->line2 = 1;
9508 eap->forceit = TRUE;
9509 }
9510 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009511 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9512 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513}
9514
9515/*
9516 * Handle ":copy" and ":move".
9517 */
9518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009519ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520{
9521 long n;
9522
Bram Moolenaarded27822017-01-02 14:27:34 +01009523 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 if (eap->arg == NULL) /* error detected */
9525 {
9526 eap->nextcmd = NULL;
9527 return;
9528 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009529 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530
9531 /*
9532 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9533 */
9534 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9535 {
9536 EMSG(_(e_invaddr));
9537 return;
9538 }
9539
9540 if (eap->cmdidx == CMD_move)
9541 {
9542 if (do_move(eap->line1, eap->line2, n) == FAIL)
9543 return;
9544 }
9545 else
9546 ex_copy(eap->line1, eap->line2, n);
9547 u_clearline();
9548 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009549 ex_may_print(eap);
9550}
9551
9552/*
9553 * Print the current line if flags were given to the Ex command.
9554 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009555 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009556ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009557{
9558 if (eap->flags != 0)
9559 {
9560 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9561 (eap->flags & EXFLAG_LIST));
9562 ex_no_reprint = TRUE;
9563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564}
9565
9566/*
9567 * ":smagic" and ":snomagic".
9568 */
9569 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009570ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571{
9572 int magic_save = p_magic;
9573
9574 p_magic = (eap->cmdidx == CMD_smagic);
9575 do_sub(eap);
9576 p_magic = magic_save;
9577}
9578
9579/*
9580 * ":join".
9581 */
9582 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009583ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584{
9585 curwin->w_cursor.lnum = eap->line1;
9586 if (eap->line1 == eap->line2)
9587 {
9588 if (eap->addr_count >= 2) /* :2,2join does nothing */
9589 return;
9590 if (eap->line2 == curbuf->b_ml.ml_line_count)
9591 {
9592 beep_flush();
9593 return;
9594 }
9595 ++eap->line2;
9596 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009597 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009599 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600}
9601
9602/*
9603 * ":[addr]@r" or ":[addr]*r": execute register
9604 */
9605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009606ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607{
9608 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009609 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610
9611 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009612 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613
9614#ifdef USE_ON_FLY_SCROLL
9615 dont_scroll = TRUE; /* disallow scrolling here */
9616#endif
9617
9618 /* get the register name. No name means to use the previous one */
9619 c = *eap->arg;
9620 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9621 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009622 /* Put the register in the typeahead buffer with the "silent" flag. */
9623 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9624 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009625 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 else
9629 {
9630 int save_efr = exec_from_reg;
9631
9632 exec_from_reg = TRUE;
9633
9634 /*
9635 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009636 * Continue until the stuff buffer is empty and all added characters
9637 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009639 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9641
9642 exec_from_reg = save_efr;
9643 }
9644}
9645
9646/*
9647 * ":!".
9648 */
9649 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009650ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651{
9652 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9653}
9654
9655/*
9656 * ":undo".
9657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009659ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009661 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009662 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009663 else
9664 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665}
9666
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009667#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009668 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009669ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009670{
9671 char_u hash[UNDO_HASH_SIZE];
9672
9673 u_compute_hash(hash);
9674 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9675}
9676
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009677 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009678ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009679{
9680 char_u hash[UNDO_HASH_SIZE];
9681
9682 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009683 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009684}
9685#endif
9686
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687/*
9688 * ":redo".
9689 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009691ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692{
9693 u_redo(1);
9694}
9695
9696/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009697 * ":earlier" and ":later".
9698 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009699 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009700ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009701{
9702 long count = 0;
9703 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009704 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009705 char_u *p = eap->arg;
9706
9707 if (*p == NUL)
9708 count = 1;
9709 else if (isdigit(*p))
9710 {
9711 count = getdigits(&p);
9712 switch (*p)
9713 {
9714 case 's': ++p; sec = TRUE; break;
9715 case 'm': ++p; sec = TRUE; count *= 60; break;
9716 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009717 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9718 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009719 }
9720 }
9721
9722 if (*p != NUL)
9723 EMSG2(_(e_invarg2), eap->arg);
9724 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009725 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9726 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009727}
9728
9729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 * ":redir": start/stop redirection.
9731 */
9732 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009733ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734{
9735 char *mode;
9736 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009737 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738
Bram Moolenaarba768492016-07-08 15:32:54 +02009739#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009740 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009741 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009742 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009743 return;
9744 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009745#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009746
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 if (STRICMP(eap->arg, "END") == 0)
9748 close_redir();
9749 else
9750 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009751 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009753 ++arg;
9754 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009756 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 mode = "a";
9758 }
9759 else
9760 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009761 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762
9763 close_redir();
9764
9765 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009766 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 if (fname == NULL)
9768 return;
9769#ifdef FEAT_BROWSE
9770 if (cmdmod.browse)
9771 {
9772 char_u *browseFile;
9773
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009774 browseFile = do_browse(BROWSE_SAVE,
9775 (char_u *)_("Save Redirection"),
9776 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 if (browseFile == NULL)
9778 return; /* operation cancelled */
9779 vim_free(fname);
9780 fname = browseFile;
9781 eap->forceit = TRUE; /* since dialog already asked */
9782 }
9783#endif
9784
9785 redir_fd = open_exfile(fname, eap->forceit, mode);
9786 vim_free(fname);
9787 }
9788#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009789 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 {
9791 /* redirect to a register a-z (resp. A-Z for appending) */
9792 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009793 ++arg;
9794 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009796 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009797 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009799 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009801 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009802 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009803 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009804 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009806 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009807 if (*arg == '>')
9808 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009809 /* Make register empty when not using @A-@Z and the
9810 * command is valid. */
9811 if (*arg == NUL && !isupper(redir_reg))
9812 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009814 }
9815 if (*arg != NUL)
9816 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009817 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009818 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009819 }
9820 }
9821 else if (*arg == '=' && arg[1] == '>')
9822 {
9823 int append;
9824
9825 /* redirect to a variable */
9826 close_redir();
9827 arg += 2;
9828
9829 if (*arg == '>')
9830 {
9831 ++arg;
9832 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 }
9834 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009835 append = FALSE;
9836
9837 if (var_redir_start(skipwhite(arg), append) == OK)
9838 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 }
9840#endif
9841
9842 /* TODO: redirect to a buffer */
9843
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009845 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009847
9848 /* Make sure redirection is not off. Can happen for cmdline completion
9849 * that indirectly invokes a command to catch its output. */
9850 if (redir_fd != NULL
9851#ifdef FEAT_EVAL
9852 || redir_reg || redir_vname
9853#endif
9854 )
9855 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856}
9857
9858/*
9859 * ":redraw": force redraw
9860 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009861 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009862ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863{
9864 int r = RedrawingDisabled;
9865 int p = p_lz;
9866
9867 RedrawingDisabled = 0;
9868 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009869 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009871 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872#ifdef FEAT_TITLE
9873 if (need_maketitle)
9874 maketitle();
9875#endif
9876 RedrawingDisabled = r;
9877 p_lz = p;
9878
9879 /* Reset msg_didout, so that a message that's there is overwritten. */
9880 msg_didout = FALSE;
9881 msg_col = 0;
9882
Bram Moolenaar56667a52013-07-17 11:54:28 +02009883 /* No need to wait after an intentional redraw. */
9884 need_wait_return = FALSE;
9885
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 out_flush();
9887}
9888
9889/*
9890 * ":redrawstatus": force redraw of status line(s)
9891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009893ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894{
9895#if defined(FEAT_WINDOWS)
9896 int r = RedrawingDisabled;
9897 int p = p_lz;
9898
9899 RedrawingDisabled = 0;
9900 p_lz = FALSE;
9901 if (eap->forceit)
9902 status_redraw_all();
9903 else
9904 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009905 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 RedrawingDisabled = r;
9907 p_lz = p;
9908 out_flush();
9909#endif
9910}
9911
9912 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009913close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914{
9915 if (redir_fd != NULL)
9916 {
9917 fclose(redir_fd);
9918 redir_fd = NULL;
9919 }
9920#ifdef FEAT_EVAL
9921 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009922 if (redir_vname)
9923 {
9924 var_redir_stop();
9925 redir_vname = 0;
9926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927#endif
9928}
9929
9930#if defined(FEAT_SESSION) && defined(USE_CRNL)
9931# define MKSESSION_NL
9932static int mksession_nl = FALSE; /* use NL only in put_eol() */
9933#endif
9934
9935/*
9936 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9937 */
9938 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009939ex_mkrc(
9940 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941{
9942 FILE *fd;
9943 int failed = FALSE;
9944 char_u *fname;
9945#ifdef FEAT_BROWSE
9946 char_u *browseFile = NULL;
9947#endif
9948#ifdef FEAT_SESSION
9949 int view_session = FALSE;
9950 int using_vdir = FALSE; /* using 'viewdir'? */
9951 char_u *viewFile = NULL;
9952 unsigned *flagp;
9953#endif
9954
9955 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9956 {
9957#ifdef FEAT_SESSION
9958 view_session = TRUE;
9959#else
9960 ex_ni(eap);
9961 return;
9962#endif
9963 }
9964
9965#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009966 /* Use the short file name until ":lcd" is used. We also don't use the
9967 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009968 did_lcd = FALSE;
9969
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9971 if (eap->cmdidx == CMD_mkview
9972 && (*eap->arg == NUL
9973 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9974 {
9975 eap->forceit = TRUE;
9976 fname = get_view_file(*eap->arg);
9977 if (fname == NULL)
9978 return;
9979 viewFile = fname;
9980 using_vdir = TRUE;
9981 }
9982 else
9983#endif
9984 if (*eap->arg != NUL)
9985 fname = eap->arg;
9986 else if (eap->cmdidx == CMD_mkvimrc)
9987 fname = (char_u *)VIMRC_FILE;
9988#ifdef FEAT_SESSION
9989 else if (eap->cmdidx == CMD_mksession)
9990 fname = (char_u *)SESSION_FILE;
9991#endif
9992 else
9993 fname = (char_u *)EXRC_FILE;
9994
9995#ifdef FEAT_BROWSE
9996 if (cmdmod.browse)
9997 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009998 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999# ifdef FEAT_SESSION
10000 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
10001 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
10002# endif
10003 (char_u *)_("Save Setup"),
10004 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
10005 if (browseFile == NULL)
10006 goto theend;
10007 fname = browseFile;
10008 eap->forceit = TRUE; /* since dialog already asked */
10009 }
10010#endif
10011
10012#if defined(FEAT_SESSION) && defined(vim_mkdir)
10013 /* When using 'viewdir' may have to create the directory. */
10014 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +000010015 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016#endif
10017
10018 fd = open_exfile(fname, eap->forceit, WRITEBIN);
10019 if (fd != NULL)
10020 {
10021#ifdef FEAT_SESSION
10022 if (eap->cmdidx == CMD_mkview)
10023 flagp = &vop_flags;
10024 else
10025 flagp = &ssop_flags;
10026#endif
10027
10028#ifdef MKSESSION_NL
10029 /* "unix" in 'sessionoptions': use NL line separator */
10030 if (view_session && (*flagp & SSOP_UNIX))
10031 mksession_nl = TRUE;
10032#endif
10033
10034 /* Write the version command for :mkvimrc */
10035 if (eap->cmdidx == CMD_mkvimrc)
10036 (void)put_line(fd, "version 6.0");
10037
10038#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010039 if (eap->cmdidx == CMD_mksession)
10040 {
10041 if (put_line(fd, "let SessionLoad = 1") == FAIL)
10042 failed = TRUE;
10043 }
10044
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 if (eap->cmdidx != CMD_mkview)
10046#endif
10047 {
10048 /* Write setting 'compatible' first, because it has side effects.
10049 * For that same reason only do it when needed. */
10050 if (p_cp)
10051 (void)put_line(fd, "if !&cp | set cp | endif");
10052 else
10053 (void)put_line(fd, "if &cp | set nocp | endif");
10054 }
10055
10056#ifdef FEAT_SESSION
10057 if (!view_session
10058 || (eap->cmdidx == CMD_mksession
10059 && (*flagp & SSOP_OPTIONS)))
10060#endif
10061 failed |= (makemap(fd, NULL) == FAIL
10062 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10063
10064#ifdef FEAT_SESSION
10065 if (!failed && view_session)
10066 {
10067 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10068 failed = TRUE;
10069 if (eap->cmdidx == CMD_mksession)
10070 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010071 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072
Bram Moolenaard9462e32011-04-11 21:35:11 +020010073 dirnow = alloc(MAXPATHL);
10074 if (dirnow == NULL)
10075 failed = TRUE;
10076 else
10077 {
10078 /*
10079 * Change to session file's dir.
10080 */
10081 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010083 *dirnow = NUL;
10084 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10085 {
10086 if (vim_chdirfile(fname) == OK)
10087 shorten_fnames(TRUE);
10088 }
10089 else if (*dirnow != NUL
10090 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10091 {
10092 if (mch_chdir((char *)globaldir) == 0)
10093 shorten_fnames(TRUE);
10094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095
Bram Moolenaard9462e32011-04-11 21:35:11 +020010096 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097
Bram Moolenaard9462e32011-04-11 21:35:11 +020010098 /* restore original dir */
10099 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010101 {
10102 if (mch_chdir((char *)dirnow) != 0)
10103 EMSG(_(e_prev_dir));
10104 shorten_fnames(TRUE);
10105 }
10106 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 }
10108 }
10109 else
10110 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010111 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10112 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 }
10114 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10115 == FAIL)
10116 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010117 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10118 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010119 if (eap->cmdidx == CMD_mksession)
10120 {
10121 if (put_line(fd, "unlet SessionLoad") == FAIL)
10122 failed = TRUE;
10123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 }
10125#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010126 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10127 failed = TRUE;
10128
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 failed |= fclose(fd);
10130
10131 if (failed)
10132 EMSG(_(e_write));
10133#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10134 else if (eap->cmdidx == CMD_mksession)
10135 {
10136 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010137 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138
Bram Moolenaard9462e32011-04-11 21:35:11 +020010139 tbuf = alloc(MAXPATHL);
10140 if (tbuf != NULL)
10141 {
10142 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10143 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10144 vim_free(tbuf);
10145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 }
10147#endif
10148#ifdef MKSESSION_NL
10149 mksession_nl = FALSE;
10150#endif
10151 }
10152
10153#ifdef FEAT_BROWSE
10154theend:
10155 vim_free(browseFile);
10156#endif
10157#ifdef FEAT_SESSION
10158 vim_free(viewFile);
10159#endif
10160}
10161
Bram Moolenaardf177f62005-02-22 08:39:57 +000010162#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10163 || defined(PROTO)
10164 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010165vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010166{
10167 if (vim_mkdir(name, prot) != 0)
10168 {
10169 EMSG2(_("E739: Cannot create directory: %s"), name);
10170 return FAIL;
10171 }
10172 return OK;
10173}
10174#endif
10175
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176/*
10177 * Open a file for writing for an Ex command, with some checks.
10178 * Return file descriptor, or NULL on failure.
10179 */
10180 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010181open_exfile(
10182 char_u *fname,
10183 int forceit,
10184 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185{
10186 FILE *fd;
10187
10188#ifdef UNIX
10189 /* with Unix it is possible to open a directory */
10190 if (mch_isdir(fname))
10191 {
10192 EMSG2(_(e_isadir2), fname);
10193 return NULL;
10194 }
10195#endif
10196 if (!forceit && *mode != 'a' && vim_fexists(fname))
10197 {
10198 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10199 return NULL;
10200 }
10201
10202 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10203 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10204
10205 return fd;
10206}
10207
10208/*
10209 * ":mark" and ":k".
10210 */
10211 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010212ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213{
10214 pos_T pos;
10215
10216 if (*eap->arg == NUL) /* No argument? */
10217 EMSG(_(e_argreq));
10218 else if (eap->arg[1] != NUL) /* more than one character? */
10219 EMSG(_(e_trailing));
10220 else
10221 {
10222 pos = curwin->w_cursor; /* save curwin->w_cursor */
10223 curwin->w_cursor.lnum = eap->line2;
10224 beginline(BL_WHITE | BL_FIX);
10225 if (setmark(*eap->arg) == FAIL) /* set mark */
10226 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10227 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10228 }
10229}
10230
10231/*
10232 * Update w_topline, w_leftcol and the cursor position.
10233 */
10234 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010235update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236{
10237 check_cursor(); /* put cursor on valid line */
10238 update_topline();
10239 if (!curwin->w_p_wrap)
10240 validate_cursor();
10241 update_curswant();
10242}
10243
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244/*
10245 * ":normal[!] {commands}": Execute normal mode commands.
10246 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010247 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010248ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 int save_msg_scroll = msg_scroll;
10251 int save_restart_edit = restart_edit;
10252 int save_msg_didout = msg_didout;
10253 int save_State = State;
10254 tasave_T tabuf;
10255 int save_insertmode = p_im;
10256 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010257 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258#ifdef FEAT_MBYTE
10259 char_u *arg = NULL;
10260 int l;
10261 char_u *p;
10262#endif
10263
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010264 if (ex_normal_lock > 0)
10265 {
10266 EMSG(_(e_secure));
10267 return;
10268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 if (ex_normal_busy >= p_mmd)
10270 {
10271 EMSG(_("E192: Recursive use of :normal too deep"));
10272 return;
10273 }
10274 ++ex_normal_busy;
10275
10276 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10277 restart_edit = 0; /* don't go to Insert mode */
10278 p_im = FALSE; /* don't use 'insertmode' */
10279
10280#ifdef FEAT_MBYTE
10281 /*
10282 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10283 * this for the K_SPECIAL leading byte, otherwise special keys will not
10284 * work.
10285 */
10286 if (has_mbyte)
10287 {
10288 int len = 0;
10289
10290 /* Count the number of characters to be escaped. */
10291 for (p = eap->arg; *p != NUL; ++p)
10292 {
10293# ifdef FEAT_GUI
10294 if (*p == CSI) /* leadbyte CSI */
10295 len += 2;
10296# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010297 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10299# ifdef FEAT_GUI
10300 || *p == CSI
10301# endif
10302 )
10303 len += 2;
10304 }
10305 if (len > 0)
10306 {
10307 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10308 if (arg != NULL)
10309 {
10310 len = 0;
10311 for (p = eap->arg; *p != NUL; ++p)
10312 {
10313 arg[len++] = *p;
10314# ifdef FEAT_GUI
10315 if (*p == CSI)
10316 {
10317 arg[len++] = KS_EXTRA;
10318 arg[len++] = (int)KE_CSI;
10319 }
10320# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010321 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 {
10323 arg[len++] = *++p;
10324 if (*p == K_SPECIAL)
10325 {
10326 arg[len++] = KS_SPECIAL;
10327 arg[len++] = KE_FILLER;
10328 }
10329# ifdef FEAT_GUI
10330 else if (*p == CSI)
10331 {
10332 arg[len++] = KS_EXTRA;
10333 arg[len++] = (int)KE_CSI;
10334 }
10335# endif
10336 }
10337 arg[len] = NUL;
10338 }
10339 }
10340 }
10341 }
10342#endif
10343
10344 /*
10345 * Save the current typeahead. This is required to allow using ":normal"
10346 * from an event handler and makes sure we don't hang when the argument
10347 * ends with half a command.
10348 */
10349 save_typeahead(&tabuf);
10350 if (tabuf.typebuf_valid)
10351 {
10352 /*
10353 * Repeat the :normal command for each line in the range. When no
10354 * range given, execute it just once, without positioning the cursor
10355 * first.
10356 */
10357 do
10358 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 if (eap->addr_count != 0)
10360 {
10361 curwin->w_cursor.lnum = eap->line1++;
10362 curwin->w_cursor.col = 0;
10363 }
10364
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010365 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366#ifdef FEAT_MBYTE
10367 arg != NULL ? arg :
10368#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010369 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 }
10371 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10372 }
10373
10374 /* Might not return to the main loop when in an event handler. */
10375 update_topline_cursor();
10376
10377 /* Restore the previous typeahead. */
10378 restore_typeahead(&tabuf);
10379
10380 --ex_normal_busy;
10381 msg_scroll = save_msg_scroll;
10382 restart_edit = save_restart_edit;
10383 p_im = save_insertmode;
10384 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010385 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10387
10388 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010389 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010391#ifdef FEAT_MOUSE
10392 setmouse();
10393#endif
10394#ifdef CURSOR_SHAPE
10395 ui_cursor_shape(); /* may show different cursor shape */
10396#endif
10397
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398#ifdef FEAT_MBYTE
10399 vim_free(arg);
10400#endif
10401}
10402
10403/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010404 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 */
10406 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010407ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010409 if (eap->forceit)
10410 {
10411 coladvance((colnr_T)MAXCOL);
10412 curwin->w_curswant = MAXCOL;
10413 curwin->w_set_curswant = FALSE;
10414 }
10415
Bram Moolenaara40c5002005-01-09 21:16:21 +000010416 /* Ignore the command when already in Insert mode. Inserting an
10417 * expression register that invokes a function can do this. */
10418 if (State & INSERT)
10419 return;
10420
Bram Moolenaar64969662005-12-14 21:59:55 +000010421 if (eap->cmdidx == CMD_startinsert)
10422 restart_edit = 'a';
10423 else if (eap->cmdidx == CMD_startreplace)
10424 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010426 restart_edit = 'V';
10427
10428 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010430 if (eap->cmdidx == CMD_startinsert)
10431 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 curwin->w_curswant = 0; /* avoid MAXCOL */
10433 }
10434}
10435
10436/*
10437 * ":stopinsert"
10438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010440ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441{
10442 restart_edit = 0;
10443 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010444 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010447/*
10448 * Execute normal mode command "cmd".
10449 * "remap" can be REMAP_NONE or REMAP_YES.
10450 */
10451 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010452exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010453{
Bram Moolenaar25281632016-01-21 23:32:32 +010010454 /* Stuff the argument into the typeahead buffer. */
10455 ins_typebuf(cmd, remap, 0, TRUE, silent);
10456 exec_normal(FALSE);
10457}
Bram Moolenaar25281632016-01-21 23:32:32 +010010458
Bram Moolenaar25281632016-01-21 23:32:32 +010010459/*
10460 * Execute normal_cmd() until there is no typeahead left.
10461 */
10462 void
10463exec_normal(int was_typed)
10464{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010465 oparg_T oa;
10466
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010467 clear_oparg(&oa);
10468 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010469 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10470 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010471 {
10472 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010473 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010474 }
10475}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010476
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477#ifdef FEAT_FIND_ID
10478 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010479ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480{
10481 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10482 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10483 (linenr_T)1, (linenr_T)MAXLNUM);
10484}
10485
10486#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10487/*
10488 * ":psearch"
10489 */
10490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010491ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492{
10493 g_do_tagpreview = p_pvh;
10494 ex_findpat(eap);
10495 g_do_tagpreview = 0;
10496}
10497#endif
10498
10499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010500ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501{
10502 int whole = TRUE;
10503 long n;
10504 char_u *p;
10505 int action;
10506
10507 switch (cmdnames[eap->cmdidx].cmd_name[2])
10508 {
10509 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10510 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10511 action = ACTION_GOTO;
10512 else
10513 action = ACTION_SHOW;
10514 break;
10515 case 'i': /* ":ilist" and ":dlist" */
10516 action = ACTION_SHOW_ALL;
10517 break;
10518 case 'u': /* ":ijump" and ":djump" */
10519 action = ACTION_GOTO;
10520 break;
10521 default: /* ":isplit" and ":dsplit" */
10522 action = ACTION_SPLIT;
10523 break;
10524 }
10525
10526 n = 1;
10527 if (vim_isdigit(*eap->arg)) /* get count */
10528 {
10529 n = getdigits(&eap->arg);
10530 eap->arg = skipwhite(eap->arg);
10531 }
10532 if (*eap->arg == '/') /* Match regexp, not just whole words */
10533 {
10534 whole = FALSE;
10535 ++eap->arg;
10536 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10537 if (*p)
10538 {
10539 *p++ = NUL;
10540 p = skipwhite(p);
10541
10542 /* Check for trailing illegal characters */
10543 if (!ends_excmd(*p))
10544 eap->errmsg = e_trailing;
10545 else
10546 eap->nextcmd = check_nextcmd(p);
10547 }
10548 }
10549 if (!eap->skip)
10550 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10551 whole, !eap->forceit,
10552 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10553 n, action, eap->line1, eap->line2);
10554}
10555#endif
10556
10557#ifdef FEAT_WINDOWS
10558
10559# ifdef FEAT_QUICKFIX
10560/*
10561 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10562 */
10563 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010564ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010566 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10568}
10569
10570/*
10571 * ":pedit"
10572 */
10573 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010574ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575{
10576 win_T *curwin_save = curwin;
10577
10578 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010579 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 keep_help_flag = curwin_save->w_buffer->b_help;
10581 do_exedit(eap, NULL);
10582 keep_help_flag = FALSE;
10583 if (curwin != curwin_save && win_valid(curwin_save))
10584 {
10585 /* Return cursor to where we were */
10586 validate_cursor();
10587 redraw_later(VALID);
10588 win_enter(curwin_save, TRUE);
10589 }
10590 g_do_tagpreview = 0;
10591}
10592# endif
10593
10594/*
10595 * ":stag", ":stselect" and ":stjump".
10596 */
10597 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010598ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599{
10600 postponed_split = -1;
10601 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010602 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10604 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010605 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606}
10607#endif
10608
10609/*
10610 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10611 */
10612 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010613ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614{
10615 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10616}
10617
10618 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010619ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620{
10621 int cmd;
10622
10623 switch (name[1])
10624 {
10625 case 'j': cmd = DT_JUMP; /* ":tjump" */
10626 break;
10627 case 's': cmd = DT_SELECT; /* ":tselect" */
10628 break;
10629 case 'p': cmd = DT_PREV; /* ":tprevious" */
10630 break;
10631 case 'N': cmd = DT_PREV; /* ":tNext" */
10632 break;
10633 case 'n': cmd = DT_NEXT; /* ":tnext" */
10634 break;
10635 case 'o': cmd = DT_POP; /* ":pop" */
10636 break;
10637 case 'f': /* ":tfirst" */
10638 case 'r': cmd = DT_FIRST; /* ":trewind" */
10639 break;
10640 case 'l': cmd = DT_LAST; /* ":tlast" */
10641 break;
10642 default: /* ":tag" */
10643#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010644 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010646 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 return;
10648 }
10649#endif
10650 cmd = DT_TAG;
10651 break;
10652 }
10653
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010654 if (name[0] == 'l')
10655 {
10656#ifndef FEAT_QUICKFIX
10657 ex_ni(eap);
10658 return;
10659#else
10660 cmd = DT_LTAG;
10661#endif
10662 }
10663
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10665 eap->forceit, TRUE);
10666}
10667
10668/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010669 * Check "str" for starting with a special cmdline variable.
10670 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10671 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10672 */
10673 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010674find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010675{
10676 int len;
10677 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010678 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010679 "%",
10680#define SPEC_PERC 0
10681 "#",
10682#define SPEC_HASH 1
10683 "<cword>", /* cursor word */
10684#define SPEC_CWORD 2
10685 "<cWORD>", /* cursor WORD */
10686#define SPEC_CCWORD 3
10687 "<cfile>", /* cursor path name */
10688#define SPEC_CFILE 4
10689 "<sfile>", /* ":so" file name */
10690#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010691 "<slnum>", /* ":so" file line number */
10692#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010693#ifdef FEAT_AUTOCMD
10694 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010695# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010696 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010697# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010698 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010699# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010700#endif
10701#ifdef FEAT_CLIENTSERVER
10702 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010703# ifdef FEAT_AUTOCMD
10704# define SPEC_CLIENT 10
10705# else
10706# define SPEC_CLIENT 7
10707# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010708#endif
10709 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010710
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010711 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010712 {
10713 len = (int)STRLEN(spec_str[i]);
10714 if (STRNCMP(src, spec_str[i], len) == 0)
10715 {
10716 *usedlen = len;
10717 return i;
10718 }
10719 }
10720 return -1;
10721}
10722
10723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 * Evaluate cmdline variables.
10725 *
10726 * change '%' to curbuf->b_ffname
10727 * '#' to curwin->w_altfile
10728 * '<cword>' to word under the cursor
10729 * '<cWORD>' to WORD under the cursor
10730 * '<cfile>' to path name under the cursor
10731 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010732 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 * '<afile>' to file name for autocommand
10734 * '<abuf>' to buffer number for autocommand
10735 * '<amatch>' to matching name for autocommand
10736 *
10737 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10738 * "" for error without a message) and NULL is returned.
10739 * Returns an allocated string if a valid match was found.
10740 * Returns NULL if no match was found. "usedlen" then still contains the
10741 * number of characters to skip.
10742 */
10743 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010744eval_vars(
10745 char_u *src, /* pointer into commandline */
10746 char_u *srcstart, /* beginning of valid memory for src */
10747 int *usedlen, /* characters after src that are used */
10748 linenr_T *lnump, /* line number for :e command, or NULL */
10749 char_u **errormsg, /* pointer to error message */
10750 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010751 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752{
10753 int i;
10754 char_u *s;
10755 char_u *result;
10756 char_u *resultbuf = NULL;
10757 int resultlen;
10758 buf_T *buf;
10759 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10760 int spec_idx;
10761#ifdef FEAT_MODIFY_FNAME
10762 int skip_mod = FALSE;
10763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765
10766 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010767 if (escaped != NULL)
10768 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769
10770 /*
10771 * Check if there is something to do.
10772 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010773 spec_idx = find_cmdline_var(src, usedlen);
10774 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 {
10776 *usedlen = 1;
10777 return NULL;
10778 }
10779
10780 /*
10781 * Skip when preceded with a backslash "\%" and "\#".
10782 * Note: In "\\%" the % is also not recognized!
10783 */
10784 if (src > srcstart && src[-1] == '\\')
10785 {
10786 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010787 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 return NULL;
10789 }
10790
10791 /*
10792 * word or WORD under cursor
10793 */
10794 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10795 {
10796 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10797 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10798 if (resultlen == 0)
10799 {
10800 *errormsg = (char_u *)"";
10801 return NULL;
10802 }
10803 }
10804
10805 /*
10806 * '#': Alternate file name
10807 * '%': Current file name
10808 * File name under the cursor
10809 * File name for autocommand
10810 * and following modifiers
10811 */
10812 else
10813 {
10814 switch (spec_idx)
10815 {
10816 case SPEC_PERC: /* '%': current file */
10817 if (curbuf->b_fname == NULL)
10818 {
10819 result = (char_u *)"";
10820 valid = 0; /* Must have ":p:h" to be valid */
10821 }
10822 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 break;
10825
10826 case SPEC_HASH: /* '#' or "#99": alternate file */
10827 if (src[1] == '#') /* "##": the argument list */
10828 {
10829 result = arg_all();
10830 resultbuf = result;
10831 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010832 if (escaped != NULL)
10833 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834#ifdef FEAT_MODIFY_FNAME
10835 skip_mod = TRUE;
10836#endif
10837 break;
10838 }
10839 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010840 if (*s == '<') /* "#<99" uses v:oldfiles */
10841 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 i = (int)getdigits(&s);
10843 *usedlen = (int)(s - src); /* length of what we expand */
10844
Bram Moolenaard812df62008-11-09 12:46:09 +000010845 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010847 if (*usedlen < 2)
10848 {
10849 /* Should we give an error message for #<text? */
10850 *usedlen = 1;
10851 return NULL;
10852 }
10853#ifdef FEAT_EVAL
10854 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10855 (long)i);
10856 if (result == NULL)
10857 {
10858 *errormsg = (char_u *)"";
10859 return NULL;
10860 }
10861#else
10862 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 }
10866 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010867 {
10868 buf = buflist_findnr(i);
10869 if (buf == NULL)
10870 {
10871 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10872 return NULL;
10873 }
10874 if (lnump != NULL)
10875 *lnump = ECMD_LAST;
10876 if (buf->b_fname == NULL)
10877 {
10878 result = (char_u *)"";
10879 valid = 0; /* Must have ":p:h" to be valid */
10880 }
10881 else
10882 result = buf->b_fname;
10883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 break;
10885
10886#ifdef FEAT_SEARCHPATH
10887 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010888 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 if (result == NULL)
10890 {
10891 *errormsg = (char_u *)"";
10892 return NULL;
10893 }
10894 resultbuf = result; /* remember allocated string */
10895 break;
10896#endif
10897
10898#ifdef FEAT_AUTOCMD
10899 case SPEC_AFILE: /* file name for autocommand */
10900 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010901 if (result != NULL && !autocmd_fname_full)
10902 {
10903 /* Still need to turn the fname into a full path. It is
10904 * postponed to avoid a delay when <afile> is not used. */
10905 autocmd_fname_full = TRUE;
10906 result = FullName_save(autocmd_fname, FALSE);
10907 vim_free(autocmd_fname);
10908 autocmd_fname = result;
10909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 if (result == NULL)
10911 {
10912 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10913 return NULL;
10914 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010915 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 break;
10917
10918 case SPEC_ABUF: /* buffer number for autocommand */
10919 if (autocmd_bufnr <= 0)
10920 {
10921 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10922 return NULL;
10923 }
10924 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10925 result = strbuf;
10926 break;
10927
10928 case SPEC_AMATCH: /* match name for autocommand */
10929 result = autocmd_match;
10930 if (result == NULL)
10931 {
10932 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10933 return NULL;
10934 }
10935 break;
10936
10937#endif
10938 case SPEC_SFILE: /* file name for ":so" command */
10939 result = sourcing_name;
10940 if (result == NULL)
10941 {
10942 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10943 return NULL;
10944 }
10945 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010946 case SPEC_SLNUM: /* line in file for ":so" command */
10947 if (sourcing_name == NULL || sourcing_lnum == 0)
10948 {
10949 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10950 return NULL;
10951 }
10952 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10953 result = strbuf;
10954 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955#if defined(FEAT_CLIENTSERVER)
10956 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010957 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10958 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 result = strbuf;
10960 break;
10961#endif
10962 }
10963
10964 resultlen = (int)STRLEN(result); /* length of new string */
10965 if (src[*usedlen] == '<') /* remove the file name extension */
10966 {
10967 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 resultlen = (int)(s - result);
10970 }
10971#ifdef FEAT_MODIFY_FNAME
10972 else if (!skip_mod)
10973 {
10974 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10975 &resultlen);
10976 if (result == NULL)
10977 {
10978 *errormsg = (char_u *)"";
10979 return NULL;
10980 }
10981 }
10982#endif
10983 }
10984
10985 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10986 {
10987 if (valid != VALID_HEAD + VALID_PATH)
10988 /* xgettext:no-c-format */
10989 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10990 else
10991 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10992 result = NULL;
10993 }
10994 else
10995 result = vim_strnsave(result, resultlen);
10996 vim_free(resultbuf);
10997 return result;
10998}
10999
11000/*
11001 * Concatenate all files in the argument list, separated by spaces, and return
11002 * it in one allocated string.
11003 * Spaces and backslashes in the file names are escaped with a backslash.
11004 * Returns NULL when out of memory.
11005 */
11006 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011007arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008{
11009 int len;
11010 int idx;
11011 char_u *retval = NULL;
11012 char_u *p;
11013
11014 /*
11015 * Do this loop two times:
11016 * first time: compute the total length
11017 * second time: concatenate the names
11018 */
11019 for (;;)
11020 {
11021 len = 0;
11022 for (idx = 0; idx < ARGCOUNT; ++idx)
11023 {
11024 p = alist_name(&ARGLIST[idx]);
11025 if (p != NULL)
11026 {
11027 if (len > 0)
11028 {
11029 /* insert a space in between names */
11030 if (retval != NULL)
11031 retval[len] = ' ';
11032 ++len;
11033 }
11034 for ( ; *p != NUL; ++p)
11035 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011036 if (*p == ' '
11037#ifndef BACKSLASH_IN_FILENAME
11038 || *p == '\\'
11039#endif
11040 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 {
11042 /* insert a backslash */
11043 if (retval != NULL)
11044 retval[len] = '\\';
11045 ++len;
11046 }
11047 if (retval != NULL)
11048 retval[len] = *p;
11049 ++len;
11050 }
11051 }
11052 }
11053
11054 /* second time: break here */
11055 if (retval != NULL)
11056 {
11057 retval[len] = NUL;
11058 break;
11059 }
11060
11061 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011062 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063 if (retval == NULL)
11064 break;
11065 }
11066
11067 return retval;
11068}
11069
11070#if defined(FEAT_AUTOCMD) || defined(PROTO)
11071/*
11072 * Expand the <sfile> string in "arg".
11073 *
11074 * Returns an allocated string, or NULL for any error.
11075 */
11076 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011077expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078{
11079 char_u *errormsg;
11080 int len;
11081 char_u *result;
11082 char_u *newres;
11083 char_u *repl;
11084 int srclen;
11085 char_u *p;
11086
11087 result = vim_strsave(arg);
11088 if (result == NULL)
11089 return NULL;
11090
11091 for (p = result; *p; )
11092 {
11093 if (STRNCMP(p, "<sfile>", 7) != 0)
11094 ++p;
11095 else
11096 {
11097 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011098 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 if (errormsg != NULL)
11100 {
11101 if (*errormsg)
11102 emsg(errormsg);
11103 vim_free(result);
11104 return NULL;
11105 }
11106 if (repl == NULL) /* no match (cannot happen) */
11107 {
11108 p += srclen;
11109 continue;
11110 }
11111 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11112 newres = alloc(len);
11113 if (newres == NULL)
11114 {
11115 vim_free(repl);
11116 vim_free(result);
11117 return NULL;
11118 }
11119 mch_memmove(newres, result, (size_t)(p - result));
11120 STRCPY(newres + (p - result), repl);
11121 len = (int)STRLEN(newres);
11122 STRCAT(newres, p + srclen);
11123 vim_free(repl);
11124 vim_free(result);
11125 result = newres;
11126 p = newres + len; /* continue after the match */
11127 }
11128 }
11129
11130 return result;
11131}
11132#endif
11133
11134#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011135static int ses_winsizes(FILE *fd, int restore_size,
11136 win_T *tab_firstwin);
11137static int ses_win_rec(FILE *fd, frame_T *fr);
11138static frame_T *ses_skipframe(frame_T *fr);
11139static int ses_do_frame(frame_T *fr);
11140static int ses_do_win(win_T *wp);
11141static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11142static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
11143static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144
11145/*
11146 * Write openfile commands for the current buffers to an .exrc file.
11147 * Return FAIL on error, OK otherwise.
11148 */
11149 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011150makeopens(
11151 FILE *fd,
11152 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153{
11154 buf_T *buf;
11155 int only_save_windows = TRUE;
11156 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157 int restore_size = TRUE;
11158 win_T *wp;
11159 char_u *sname;
11160 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011161 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011162 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011163 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011164 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011165 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011166 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167
11168 if (ssop_flags & SSOP_BUFFERS)
11169 only_save_windows = FALSE; /* Save ALL buffers */
11170
11171 /*
11172 * Begin by setting the this_session variable, and then other
11173 * sessionable variables.
11174 */
11175#ifdef FEAT_EVAL
11176 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11177 return FAIL;
11178 if (ssop_flags & SSOP_GLOBALS)
11179 if (store_session_globals(fd) == FAIL)
11180 return FAIL;
11181#endif
11182
11183 /*
11184 * Close all windows but one.
11185 */
11186 if (put_line(fd, "silent only") == FAIL)
11187 return FAIL;
11188
11189 /*
11190 * Now a :cd command to the session directory or the current directory
11191 */
11192 if (ssop_flags & SSOP_SESDIR)
11193 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011194 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11195 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196 return FAIL;
11197 }
11198 else if (ssop_flags & SSOP_CURDIR)
11199 {
11200 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11201 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011202 || fputs("cd ", fd) < 0
11203 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11204 || put_eol(fd) == FAIL)
11205 {
11206 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209 vim_free(sname);
11210 }
11211
11212 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011213 * If there is an empty, unnamed buffer we will wipe it out later.
11214 * Remember the buffer number.
11215 */
11216 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11217 return FAIL;
11218 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11219 return FAIL;
11220 if (put_line(fd, "endif") == FAIL)
11221 return FAIL;
11222
11223 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 * Now save the current files, current buffer first.
11225 */
11226 if (put_line(fd, "set shortmess=aoO") == FAIL)
11227 return FAIL;
11228
11229 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011230 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231 {
11232 if (!(only_save_windows && buf->b_nwindows == 0)
11233 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11234 && buf->b_fname != NULL
11235 && buf->b_p_bl)
11236 {
11237 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11238 : buf->b_wininfo->wi_fpos.lnum) < 0
11239 || ses_fname(fd, buf, &ssop_flags) == FAIL)
11240 return FAIL;
11241 }
11242 }
11243
11244 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011245 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11247 return FAIL;
11248
11249 if (ssop_flags & SSOP_RESIZE)
11250 {
11251 /* Note: after the restore we still check it worked!*/
11252 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11253 || put_eol(fd) == FAIL)
11254 return FAIL;
11255 }
11256
11257#ifdef FEAT_GUI
11258 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11259 {
11260 int x, y;
11261
11262 if (gui_mch_get_winpos(&x, &y) == OK)
11263 {
11264 /* Note: after the restore we still check it worked!*/
11265 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11266 return FAIL;
11267 }
11268 }
11269#endif
11270
11271 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011272 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11273 * will be displayed when creating the next tab. That resizes the windows
11274 * in the first tab, which may cause problems. Set 'showtabline' to 2
11275 * temporarily to avoid that.
11276 */
11277 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11278 {
11279 if (put_line(fd, "set stal=2") == FAIL)
11280 return FAIL;
11281 restore_stal = TRUE;
11282 }
11283
11284 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011285 * May repeat putting Windows for each tab, when "tabpages" is in
11286 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011287 * Don't use goto_tabpage(), it may change directory and trigger
11288 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011290 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011291 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011292 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011294 int need_tabnew = FALSE;
11295 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011296
Bram Moolenaar18144c82006-04-12 21:52:12 +000011297 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011299 tabpage_T *tp = find_tabpage(tabnr);
11300
11301 if (tp == NULL)
11302 break; /* done all tab pages */
11303 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011304 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011305 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011306 tab_topframe = topframe;
11307 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011308 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011309 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011310 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011311 tab_topframe = tp->tp_topframe;
11312 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011313 if (tabnr > 1)
11314 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316
Bram Moolenaar18144c82006-04-12 21:52:12 +000011317 /*
11318 * Before creating the window layout, try loading one file. If this
11319 * is aborted we don't end up with a number of useless windows.
11320 * This may have side effects! (e.g., compressed or network file).
11321 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011322 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011323 {
11324 if (ses_do_win(wp)
11325 && wp->w_buffer->b_ffname != NULL
11326 && !wp->w_buffer->b_help
11327#ifdef FEAT_QUICKFIX
11328 && !bt_nofile(wp->w_buffer)
11329#endif
11330 )
11331 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011332 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011333 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11334 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011335 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011336 if (!wp->w_arg_idx_invalid)
11337 edited_win = wp;
11338 break;
11339 }
11340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011342 /* If no file got edited create an empty tab page. */
11343 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11344 return FAIL;
11345
Bram Moolenaar18144c82006-04-12 21:52:12 +000011346 /*
11347 * Save current window layout.
11348 */
11349 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011351 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011353 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11354 return FAIL;
11355 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11356 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357
Bram Moolenaar18144c82006-04-12 21:52:12 +000011358 /*
11359 * Check if window sizes can be restored (no windows omitted).
11360 * Remember the window number of the current window after restoring.
11361 */
11362 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011363 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011364 {
11365 if (ses_do_win(wp))
11366 ++nr;
11367 else
11368 restore_size = FALSE;
11369 if (curwin == wp)
11370 cnr = nr;
11371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372
Bram Moolenaar18144c82006-04-12 21:52:12 +000011373 /* Go to the first window. */
11374 if (put_line(fd, "wincmd t") == FAIL)
11375 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011376
Bram Moolenaar18144c82006-04-12 21:52:12 +000011377 /*
11378 * If more than one window, see if sizes can be restored.
11379 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11380 * resized when moving between windows.
11381 * Do this before restoring the view, so that the topline and the
11382 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011383 * winminheight and winminwidth need to be set to avoid an error if the
11384 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011385 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011386 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011387 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011388 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011389 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390
Bram Moolenaar18144c82006-04-12 21:52:12 +000011391 /*
11392 * Restore the view of the window (options, file, cursor, etc.).
11393 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011394 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011395 {
11396 if (!ses_do_win(wp))
11397 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011398 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11399 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011400 return FAIL;
11401 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11402 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011403 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011404 }
11405
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011406 /* The argument index in the first tab page is zero, need to set it in
11407 * each window. For further tab pages it's the window where we do
11408 * "tabedit". */
11409 cur_arg_idx = next_arg_idx;
11410
Bram Moolenaar18144c82006-04-12 21:52:12 +000011411 /*
11412 * Restore cursor to the current window if it's not the first one.
11413 */
11414 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11415 || put_eol(fd) == FAIL))
11416 return FAIL;
11417
11418 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011419 * Restore window sizes again after jumping around in windows, because
11420 * the current window has a minimum size while others may not.
11421 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011422 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011423 return FAIL;
11424
Bram Moolenaar18144c82006-04-12 21:52:12 +000011425 /* Don't continue in another tab page when doing only the current one
11426 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011427 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011428 break;
11429 }
11430
11431 if (ssop_flags & SSOP_TABPAGES)
11432 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011433 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11434 || put_eol(fd) == FAIL)
11435 return FAIL;
11436 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011437 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11438 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011439
Bram Moolenaar9c102382006-05-03 21:26:49 +000011440 /*
11441 * Wipe out an empty unnamed buffer we started in.
11442 */
11443 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11444 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011445 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011446 return FAIL;
11447 if (put_line(fd, "endif") == FAIL)
11448 return FAIL;
11449 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11450 return FAIL;
11451
11452 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11453 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11454 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11455 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011456 /* Re-apply 'winminheight' and 'winminwidth'. */
11457 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11458 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11459 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460
11461 /*
11462 * Lastly, execute the x.vim file if it exists.
11463 */
11464 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11465 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011466 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467 || put_line(fd, "endif") == FAIL)
11468 return FAIL;
11469
11470 return OK;
11471}
11472
11473 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011474ses_winsizes(
11475 FILE *fd,
11476 int restore_size,
11477 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478{
11479 int n = 0;
11480 win_T *wp;
11481
11482 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11483 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011484 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485 {
11486 if (!ses_do_win(wp))
11487 continue;
11488 ++n;
11489
11490 /* restore height when not full height */
11491 if (wp->w_height + wp->w_status_height < topframe->fr_height
11492 && (fprintf(fd,
11493 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11494 n, (long)wp->w_height, Rows / 2, Rows) < 0
11495 || put_eol(fd) == FAIL))
11496 return FAIL;
11497
11498 /* restore width when not full width */
11499 if (wp->w_width < Columns && (fprintf(fd,
11500 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11501 n, (long)wp->w_width, Columns / 2, Columns) < 0
11502 || put_eol(fd) == FAIL))
11503 return FAIL;
11504 }
11505 }
11506 else
11507 {
11508 /* Just equalise window sizes */
11509 if (put_line(fd, "wincmd =") == FAIL)
11510 return FAIL;
11511 }
11512 return OK;
11513}
11514
11515/*
11516 * Write commands to "fd" to recursively create windows for frame "fr",
11517 * horizontally and vertically split.
11518 * After the commands the last window in the frame is the current window.
11519 * Returns FAIL when writing the commands to "fd" fails.
11520 */
11521 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011522ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523{
11524 frame_T *frc;
11525 int count = 0;
11526
11527 if (fr->fr_layout != FR_LEAF)
11528 {
11529 /* Find first frame that's not skipped and then create a window for
11530 * each following one (first frame is already there). */
11531 frc = ses_skipframe(fr->fr_child);
11532 if (frc != NULL)
11533 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11534 {
11535 /* Make window as big as possible so that we have lots of room
11536 * to split. */
11537 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11538 || put_line(fd, fr->fr_layout == FR_COL
11539 ? "split" : "vsplit") == FAIL)
11540 return FAIL;
11541 ++count;
11542 }
11543
11544 /* Go back to the first window. */
11545 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11546 ? "%dwincmd k" : "%dwincmd h", count) < 0
11547 || put_eol(fd) == FAIL))
11548 return FAIL;
11549
11550 /* Recursively create frames/windows in each window of this column or
11551 * row. */
11552 frc = ses_skipframe(fr->fr_child);
11553 while (frc != NULL)
11554 {
11555 ses_win_rec(fd, frc);
11556 frc = ses_skipframe(frc->fr_next);
11557 /* Go to next window. */
11558 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11559 return FAIL;
11560 }
11561 }
11562 return OK;
11563}
11564
11565/*
11566 * Skip frames that don't contain windows we want to save in the Session.
11567 * Returns NULL when there none.
11568 */
11569 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011570ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571{
11572 frame_T *frc;
11573
11574 for (frc = fr; frc != NULL; frc = frc->fr_next)
11575 if (ses_do_frame(frc))
11576 break;
11577 return frc;
11578}
11579
11580/*
11581 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11582 * the Session.
11583 */
11584 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011585ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586{
11587 frame_T *frc;
11588
11589 if (fr->fr_layout == FR_LEAF)
11590 return ses_do_win(fr->fr_win);
11591 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11592 if (ses_do_frame(frc))
11593 return TRUE;
11594 return FALSE;
11595}
11596
11597/*
11598 * Return non-zero if window "wp" is to be stored in the Session.
11599 */
11600 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011601ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602{
11603 if (wp->w_buffer->b_fname == NULL
11604#ifdef FEAT_QUICKFIX
11605 /* When 'buftype' is "nofile" can't restore the window contents. */
11606 || bt_nofile(wp->w_buffer)
11607#endif
11608 )
11609 return (ssop_flags & SSOP_BLANK);
11610 if (wp->w_buffer->b_help)
11611 return (ssop_flags & SSOP_HELP);
11612 return TRUE;
11613}
11614
11615/*
11616 * Write commands to "fd" to restore the view of a window.
11617 * Caller must make sure 'scrolloff' is zero.
11618 */
11619 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011620put_view(
11621 FILE *fd,
11622 win_T *wp,
11623 int add_edit, /* add ":edit" command to view */
11624 unsigned *flagp, /* vop_flags or ssop_flags */
11625 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011626 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627{
11628 win_T *save_curwin;
11629 int f;
11630 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011631 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632
11633 /* Always restore cursor position for ":mksession". For ":mkview" only
11634 * when 'viewoptions' contains "cursor". */
11635 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11636
11637 /*
11638 * Local argument list.
11639 */
11640 if (wp->w_alist == &global_alist)
11641 {
11642 if (put_line(fd, "argglobal") == FAIL)
11643 return FAIL;
11644 }
11645 else
11646 {
11647 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11648 flagp == &vop_flags
11649 || !(*flagp & SSOP_CURDIR)
11650 || wp->w_localdir != NULL, flagp) == FAIL)
11651 return FAIL;
11652 }
11653
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011654 /* Only when part of a session: restore the argument index. Some
11655 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011656 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011657 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011659 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 || put_eol(fd) == FAIL)
11661 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011662 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663 }
11664
11665 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011666 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667 {
11668 /*
11669 * Load the file.
11670 */
11671 if (wp->w_buffer->b_ffname != NULL
11672#ifdef FEAT_QUICKFIX
11673 && !bt_nofile(wp->w_buffer)
11674#endif
11675 )
11676 {
11677 /*
11678 * Editing a file in this buffer: use ":edit file".
11679 * This may have side effects! (e.g., compressed or network file).
11680 */
11681 if (fputs("edit ", fd) < 0
11682 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11683 return FAIL;
11684 }
11685 else
11686 {
11687 /* No file in this buffer, just make it empty. */
11688 if (put_line(fd, "enew") == FAIL)
11689 return FAIL;
11690#ifdef FEAT_QUICKFIX
11691 if (wp->w_buffer->b_ffname != NULL)
11692 {
11693 /* The buffer does have a name, but it's not a file name. */
11694 if (fputs("file ", fd) < 0
11695 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11696 return FAIL;
11697 }
11698#endif
11699 do_cursor = FALSE;
11700 }
11701 }
11702
11703 /*
11704 * Local mappings and abbreviations.
11705 */
11706 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11707 && makemap(fd, wp->w_buffer) == FAIL)
11708 return FAIL;
11709
11710 /*
11711 * Local options. Need to go to the window temporarily.
11712 * Store only local values when using ":mkview" and when ":mksession" is
11713 * used and 'sessionoptions' doesn't include "options".
11714 * Some folding options are always stored when "folds" is included,
11715 * otherwise the folds would not be restored correctly.
11716 */
11717 save_curwin = curwin;
11718 curwin = wp;
11719 curbuf = curwin->w_buffer;
11720 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11721 f = makeset(fd, OPT_LOCAL,
11722 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11723#ifdef FEAT_FOLDING
11724 else if (*flagp & SSOP_FOLDS)
11725 f = makefoldset(fd);
11726#endif
11727 else
11728 f = OK;
11729 curwin = save_curwin;
11730 curbuf = curwin->w_buffer;
11731 if (f == FAIL)
11732 return FAIL;
11733
11734#ifdef FEAT_FOLDING
11735 /*
11736 * Save Folds when 'buftype' is empty and for help files.
11737 */
11738 if ((*flagp & SSOP_FOLDS)
11739 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011740# ifdef FEAT_QUICKFIX
11741 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11742# endif
11743 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744 {
11745 if (put_folds(fd, wp) == FAIL)
11746 return FAIL;
11747 }
11748#endif
11749
11750 /*
11751 * Set the cursor after creating folds, since that moves the cursor.
11752 */
11753 if (do_cursor)
11754 {
11755
11756 /* Restore the cursor line in the file and relatively in the
11757 * window. Don't use "G", it changes the jumplist. */
11758 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11759 (long)wp->w_cursor.lnum,
11760 (long)(wp->w_cursor.lnum - wp->w_topline),
11761 (long)wp->w_height / 2, (long)wp->w_height) < 0
11762 || put_eol(fd) == FAIL
11763 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11764 || put_line(fd, "exe s:l") == FAIL
11765 || put_line(fd, "normal! zt") == FAIL
11766 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11767 || put_eol(fd) == FAIL)
11768 return FAIL;
11769 /* Restore the cursor column and left offset when not wrapping. */
11770 if (wp->w_cursor.col == 0)
11771 {
11772 if (put_line(fd, "normal! 0") == FAIL)
11773 return FAIL;
11774 }
11775 else
11776 {
11777 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11778 {
11779 if (fprintf(fd,
11780 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011781 (long)wp->w_virtcol + 1,
11782 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783 (long)wp->w_width / 2, (long)wp->w_width) < 0
11784 || put_eol(fd) == FAIL
11785 || put_line(fd, "if s:c > 0") == FAIL
11786 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011787 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11788 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789 || put_eol(fd) == FAIL
11790 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011791 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792 || put_eol(fd) == FAIL
11793 || put_line(fd, "endif") == FAIL)
11794 return FAIL;
11795 }
11796 else
11797 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011798 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799 || put_eol(fd) == FAIL)
11800 return FAIL;
11801 }
11802 }
11803 }
11804
11805 /*
11806 * Local directory.
11807 */
11808 if (wp->w_localdir != NULL)
11809 {
11810 if (fputs("lcd ", fd) < 0
11811 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11812 || put_eol(fd) == FAIL)
11813 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011814 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815 }
11816
11817 return OK;
11818}
11819
11820/*
11821 * Write an argument list to the session file.
11822 * Returns FAIL if writing fails.
11823 */
11824 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011825ses_arglist(
11826 FILE *fd,
11827 char *cmd,
11828 garray_T *gap,
11829 int fullname, /* TRUE: use full path name */
11830 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831{
11832 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011833 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834 char_u *s;
11835
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011836 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11837 return FAIL;
11838 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839 return FAIL;
11840 for (i = 0; i < gap->ga_len; ++i)
11841 {
11842 /* NULL file names are skipped (only happens when out of memory). */
11843 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11844 if (s != NULL)
11845 {
11846 if (fullname)
11847 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011848 buf = alloc(MAXPATHL);
11849 if (buf != NULL)
11850 {
11851 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11852 s = buf;
11853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011855 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011856 || ses_put_fname(fd, s, flagp) == FAIL
11857 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011858 {
11859 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011861 }
11862 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863 }
11864 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011865 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866}
11867
11868/*
11869 * Write a buffer name to the session file.
11870 * Also ends the line.
11871 * Returns FAIL if writing fails.
11872 */
11873 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011874ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875{
11876 char_u *name;
11877
11878 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011879 * the session file will be sourced.
11880 * Don't do this for ":mkview", we don't know the current directory.
11881 * Don't do this after ":lcd", we don't keep track of what the current
11882 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011883 if (buf->b_sfname != NULL
11884 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011885 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011886#ifdef FEAT_AUTOCHDIR
11887 && !p_acd
11888#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011889 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011890 name = buf->b_sfname;
11891 else
11892 name = buf->b_ffname;
11893 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11894 return FAIL;
11895 return OK;
11896}
11897
11898/*
11899 * Write a file name to the session file.
11900 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11901 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011902 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903 */
11904 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011905ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906{
11907 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011908 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910
11911 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011912 if (sname == NULL)
11913 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011915 if (*flagp & SSOP_SLASH)
11916 {
11917 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011918 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011919 if (*p == '\\')
11920 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011922
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011923 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011924 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011926 if (p == NULL)
11927 return FAIL;
11928
11929 /* write the result */
11930 if (fputs((char *)p, fd) < 0)
11931 retval = FAIL;
11932
11933 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934 return retval;
11935}
11936
11937/*
11938 * ":loadview [nr]"
11939 */
11940 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011941ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942{
11943 char_u *fname;
11944
11945 fname = get_view_file(*eap->arg);
11946 if (fname != NULL)
11947 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011948 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949 vim_free(fname);
11950 }
11951}
11952
11953/*
11954 * Get the name of the view file for the current buffer.
11955 */
11956 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011957get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958{
11959 int len = 0;
11960 char_u *p, *s;
11961 char_u *retval;
11962 char_u *sname;
11963
11964 if (curbuf->b_ffname == NULL)
11965 {
11966 EMSG(_(e_noname));
11967 return NULL;
11968 }
11969 sname = home_replace_save(NULL, curbuf->b_ffname);
11970 if (sname == NULL)
11971 return NULL;
11972
11973 /*
11974 * We want a file name without separators, because we're not going to make
11975 * a directory.
11976 * "normal" path separator -> "=+"
11977 * "=" -> "=="
11978 * ":" path separator -> "=-"
11979 */
11980 for (p = sname; *p; ++p)
11981 if (*p == '=' || vim_ispathsep(*p))
11982 ++len;
11983 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11984 if (retval != NULL)
11985 {
11986 STRCPY(retval, p_vdir);
11987 add_pathsep(retval);
11988 s = retval + STRLEN(retval);
11989 for (p = sname; *p; ++p)
11990 {
11991 if (*p == '=')
11992 {
11993 *s++ = '=';
11994 *s++ = '=';
11995 }
11996 else if (vim_ispathsep(*p))
11997 {
11998 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011999#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000 if (*p == ':')
12001 *s++ = '-';
12002 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012004 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005 }
12006 else
12007 *s++ = *p;
12008 }
12009 *s++ = '=';
12010 *s++ = c;
12011 STRCPY(s, ".vim");
12012 }
12013
12014 vim_free(sname);
12015 return retval;
12016}
12017
12018#endif /* FEAT_SESSION */
12019
12020/*
12021 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12022 * Return FAIL for a write error.
12023 */
12024 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012025put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026{
12027 if (
12028#ifdef USE_CRNL
12029 (
12030# ifdef MKSESSION_NL
12031 !mksession_nl &&
12032# endif
12033 (putc('\r', fd) < 0)) ||
12034#endif
12035 (putc('\n', fd) < 0))
12036 return FAIL;
12037 return OK;
12038}
12039
12040/*
12041 * Write a line to "fd".
12042 * Return FAIL for a write error.
12043 */
12044 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012045put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046{
12047 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12048 return FAIL;
12049 return OK;
12050}
12051
12052#ifdef FEAT_VIMINFO
12053/*
12054 * ":rviminfo" and ":wviminfo".
12055 */
12056 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012057ex_viminfo(
12058 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059{
12060 char_u *save_viminfo;
12061
12062 save_viminfo = p_viminfo;
12063 if (*p_viminfo == NUL)
12064 p_viminfo = (char_u *)"'100";
12065 if (eap->cmdidx == CMD_rviminfo)
12066 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012067 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12068 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069 EMSG(_("E195: Cannot open viminfo file for reading"));
12070 }
12071 else
12072 write_viminfo(eap->arg, eap->forceit);
12073 p_viminfo = save_viminfo;
12074}
12075#endif
12076
12077#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012078/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012079 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012080 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012083dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085 if (fname == NULL)
12086 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012087 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088}
12089#endif
12090
12091/*
12092 * ":behave {mswin,xterm}"
12093 */
12094 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012095ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096{
12097 if (STRCMP(eap->arg, "mswin") == 0)
12098 {
12099 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12100 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12101 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12102 set_option_value((char_u *)"keymodel", 0L,
12103 (char_u *)"startsel,stopsel", 0);
12104 }
12105 else if (STRCMP(eap->arg, "xterm") == 0)
12106 {
12107 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12108 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12109 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12110 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12111 }
12112 else
12113 EMSG2(_(e_invarg2), eap->arg);
12114}
12115
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012116#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12117/*
12118 * Function given to ExpandGeneric() to obtain the possible arguments of the
12119 * ":behave {mswin,xterm}" command.
12120 */
12121 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012122get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012123{
12124 if (idx == 0)
12125 return (char_u *)"mswin";
12126 if (idx == 1)
12127 return (char_u *)"xterm";
12128 return NULL;
12129}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012130
12131/*
12132 * Function given to ExpandGeneric() to obtain the possible arguments of the
12133 * ":messages {clear}" command.
12134 */
12135 char_u *
12136get_messages_arg(expand_T *xp UNUSED, int idx)
12137{
12138 if (idx == 0)
12139 return (char_u *)"clear";
12140 return NULL;
12141}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012142#endif
12143
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144#ifdef FEAT_AUTOCMD
12145static int filetype_detect = FALSE;
12146static int filetype_plugin = FALSE;
12147static int filetype_indent = FALSE;
12148
12149/*
12150 * ":filetype [plugin] [indent] {on,off,detect}"
12151 * on: Load the filetype.vim file to install autocommands for file types.
12152 * off: Load the ftoff.vim file to remove all autocommands for file types.
12153 * plugin on: load filetype.vim and ftplugin.vim
12154 * plugin off: load ftplugof.vim
12155 * indent on: load filetype.vim and indent.vim
12156 * indent off: load indoff.vim
12157 */
12158 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012159ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160{
12161 char_u *arg = eap->arg;
12162 int plugin = FALSE;
12163 int indent = FALSE;
12164
12165 if (*eap->arg == NUL)
12166 {
12167 /* Print current status. */
12168 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12169 filetype_detect ? "ON" : "OFF",
12170 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12171 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12172 return;
12173 }
12174
12175 /* Accept "plugin" and "indent" in any order. */
12176 for (;;)
12177 {
12178 if (STRNCMP(arg, "plugin", 6) == 0)
12179 {
12180 plugin = TRUE;
12181 arg = skipwhite(arg + 6);
12182 continue;
12183 }
12184 if (STRNCMP(arg, "indent", 6) == 0)
12185 {
12186 indent = TRUE;
12187 arg = skipwhite(arg + 6);
12188 continue;
12189 }
12190 break;
12191 }
12192 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12193 {
12194 if (*arg == 'o' || !filetype_detect)
12195 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012196 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197 filetype_detect = TRUE;
12198 if (plugin)
12199 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012200 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201 filetype_plugin = TRUE;
12202 }
12203 if (indent)
12204 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012205 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206 filetype_indent = TRUE;
12207 }
12208 }
12209 if (*arg == 'd')
12210 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012211 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012212 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213 }
12214 }
12215 else if (STRCMP(arg, "off") == 0)
12216 {
12217 if (plugin || indent)
12218 {
12219 if (plugin)
12220 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012221 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222 filetype_plugin = FALSE;
12223 }
12224 if (indent)
12225 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012226 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227 filetype_indent = FALSE;
12228 }
12229 }
12230 else
12231 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012232 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233 filetype_detect = FALSE;
12234 }
12235 }
12236 else
12237 EMSG2(_(e_invarg2), arg);
12238}
12239
12240/*
12241 * ":setfiletype {name}"
12242 */
12243 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012244ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245{
12246 if (!did_filetype)
12247 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
12248}
12249#endif
12250
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012252ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253{
12254#ifdef FEAT_DIGRAPHS
12255 if (*eap->arg != NUL)
12256 putdigraph(eap->arg);
12257 else
12258 listdigraphs();
12259#else
12260 EMSG(_("E196: No digraphs in this version"));
12261#endif
12262}
12263
12264 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012265ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266{
12267 int flags = 0;
12268
12269 if (eap->cmdidx == CMD_setlocal)
12270 flags = OPT_LOCAL;
12271 else if (eap->cmdidx == CMD_setglobal)
12272 flags = OPT_GLOBAL;
12273#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12274 if (cmdmod.browse && flags == 0)
12275 ex_options(eap);
12276 else
12277#endif
12278 (void)do_set(eap->arg, flags);
12279}
12280
12281#ifdef FEAT_SEARCH_EXTRA
12282/*
12283 * ":nohlsearch"
12284 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012286ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012288 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012289 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290}
12291
12292/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012293 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294 * Sets nextcmd to the start of the next command, if any. Also called when
12295 * skipping commands to find the next command.
12296 */
12297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012298ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299{
12300 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012301 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302 char_u *end;
12303 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012304 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012305
12306 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012307 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012308 else
12309 {
12310 EMSG(e_invcmd);
12311 return;
12312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313
12314 /* First clear any old pattern. */
12315 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012316 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012317
12318 if (ends_excmd(*eap->arg))
12319 end = eap->arg;
12320 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012321 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322 end = eap->arg + 4;
12323 else
12324 {
12325 p = skiptowhite(eap->arg);
12326 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012327 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328 p = skipwhite(p);
12329 if (*p == NUL)
12330 {
12331 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012332 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333 EMSG2(_(e_invarg2), eap->arg);
12334 return;
12335 }
12336 end = skip_regexp(p + 1, *p, TRUE, NULL);
12337 if (!eap->skip)
12338 {
12339 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12340 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012341 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342 eap->errmsg = e_trailing;
12343 return;
12344 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012345 if (*end != *p)
12346 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012347 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012348 EMSG2(_(e_invarg2), p);
12349 return;
12350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351
12352 c = *end;
12353 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012354 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012355 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012356 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357 }
12358 }
12359 eap->nextcmd = find_nextcmd(end);
12360}
12361#endif
12362
12363#ifdef FEAT_CRYPT
12364/*
12365 * ":X": Get crypt key
12366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012368ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012370 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012371 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372}
12373#endif
12374
12375#ifdef FEAT_FOLDING
12376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012377ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378{
12379 if (foldManualAllowed(TRUE))
12380 foldCreate(eap->line1, eap->line2);
12381}
12382
12383 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012384ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385{
12386 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12387 eap->forceit, FALSE);
12388}
12389
12390 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012391ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392{
12393 linenr_T lnum;
12394
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012395#ifdef FEAT_CLIPBOARD
12396 start_global_changes();
12397#endif
12398
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399 /* First set the marks for all lines closed/open. */
12400 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12401 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12402 ml_setmarked(lnum);
12403
12404 /* Execute the command on the marked lines. */
12405 global_exe(eap->arg);
12406 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012407#ifdef FEAT_CLIPBOARD
12408 end_global_changes();
12409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410}
12411#endif