blob: bdd152dfd7b8c037d986cda2fd767ea7c2480d14 [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"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200497#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100498
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499static char_u dollar_command[2] = {'$', 0};
500
501
502#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000503/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504typedef struct
505{
506 char_u *line; /* command line */
507 linenr_T lnum; /* sourcing_lnum of the line */
508} wcmd_T;
509
510/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000511 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000513 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000515struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516{
517 garray_T *lines_gap; /* growarray with line info */
518 int current_line; /* last read line from growarray */
519 int repeating; /* TRUE when looping a second time */
520 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100521 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 void *cookie;
523};
524
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100525static char_u *get_loop_line(int c, void *cookie, int indent);
526static int store_loop_line(garray_T *gap, char_u *line);
527static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000528
529/* Struct to save a few things while debugging. Used in do_cmdline() only. */
530struct dbg_stuff
531{
532 int trylevel;
533 int force_abort;
534 except_T *caught_stack;
535 char_u *vv_exception;
536 char_u *vv_throwpoint;
537 int did_emsg;
538 int got_int;
539 int did_throw;
540 int need_rethrow;
541 int check_cstack;
542 except_T *current_exception;
543};
544
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100545static void save_dbg_stuff(struct dbg_stuff *dsp);
546static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000547
548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100549save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000550{
551 dsp->trylevel = trylevel; trylevel = 0;
552 dsp->force_abort = force_abort; force_abort = FALSE;
553 dsp->caught_stack = caught_stack; caught_stack = NULL;
554 dsp->vv_exception = v_exception(NULL);
555 dsp->vv_throwpoint = v_throwpoint(NULL);
556
557 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
558 dsp->did_emsg = did_emsg; did_emsg = FALSE;
559 dsp->got_int = got_int; got_int = FALSE;
560 dsp->did_throw = did_throw; did_throw = FALSE;
561 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
562 dsp->check_cstack = check_cstack; check_cstack = FALSE;
563 dsp->current_exception = current_exception; current_exception = NULL;
564}
565
566 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100567restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000568{
569 suppress_errthrow = FALSE;
570 trylevel = dsp->trylevel;
571 force_abort = dsp->force_abort;
572 caught_stack = dsp->caught_stack;
573 (void)v_exception(dsp->vv_exception);
574 (void)v_throwpoint(dsp->vv_throwpoint);
575 did_emsg = dsp->did_emsg;
576 got_int = dsp->got_int;
577 did_throw = dsp->did_throw;
578 need_rethrow = dsp->need_rethrow;
579 check_cstack = dsp->check_cstack;
580 current_exception = dsp->current_exception;
581}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582#endif
583
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584/*
585 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
586 * command is given.
587 */
588 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100589do_exmode(
590 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591{
592 int save_msg_scroll;
593 int prev_msg_row;
594 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100595 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000596
597 if (improved)
598 exmode_active = EXMODE_VIM;
599 else
600 exmode_active = EXMODE_NORMAL;
601 State = NORMAL;
602
603 /* When using ":global /pat/ visual" and then "Q" we return to continue
604 * the :global command. */
605 if (global_busy)
606 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608 save_msg_scroll = msg_scroll;
609 ++RedrawingDisabled; /* don't redisplay the window */
610 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611#ifdef FEAT_GUI
612 /* Ignore scrollbar and mouse events in Ex mode */
613 ++hold_gui_events;
614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615
616 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
617 while (exmode_active)
618 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000619 /* Check for a ":normal" command and no more characters left. */
620 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
621 {
622 exmode_active = FALSE;
623 break;
624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 msg_scroll = TRUE;
626 need_wait_return = FALSE;
627 ex_pressedreturn = FALSE;
628 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100629 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 prev_msg_row = msg_row;
631 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 if (improved)
633 {
634 cmdline_row = msg_row;
635 do_cmdline(NULL, getexline, NULL, 0);
636 }
637 else
638 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
639 lines_left = Rows - 1;
640
Bram Moolenaardf177f62005-02-22 08:39:57 +0000641 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100642 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000644 if (curbuf->b_ml.ml_flags & ML_EMPTY)
645 EMSG(_(e_emptybuf));
646 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000648 if (ex_pressedreturn)
649 {
650 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000651 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000652 msg_row = prev_msg_row;
653 if (prev_msg_row == Rows - 1)
654 msg_row--;
655 }
656 msg_col = 0;
657 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
658 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000661 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
662 {
663 if (curbuf->b_ml.ml_flags & ML_EMPTY)
664 EMSG(_(e_emptybuf));
665 else
666 EMSG(_("E501: At end-of-file"));
667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 }
669
670#ifdef FEAT_GUI
671 --hold_gui_events;
672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 --RedrawingDisabled;
674 --no_wait_return;
675 update_screen(CLEAR);
676 need_wait_return = FALSE;
677 msg_scroll = save_msg_scroll;
678}
679
680/*
681 * Execute a simple command line. Used for translated commands like "*".
682 */
683 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100684do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685{
686 return do_cmdline(cmd, NULL, NULL,
687 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
688}
689
690/*
691 * do_cmdline(): execute one Ex command line
692 *
693 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100694 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 * 2. Split up in parts separated with '|'.
696 *
697 * This function can be called recursively!
698 *
699 * flags:
700 * DOCMD_VERBOSE - The command will be included in the error message.
701 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100702 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 * DOCMD_KEYTYPED - Don't reset KeyTyped.
704 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
705 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
706 *
707 * return FAIL if cmdline could not be executed, OK otherwise
708 */
709 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100710do_cmdline(
711 char_u *cmdline,
712 char_u *(*fgetline)(int, void *, int),
713 void *cookie, /* argument for fgetline() */
714 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715{
716 char_u *next_cmdline; /* next cmd to execute */
717 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100718 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 static int recursive = 0; /* recursive depth */
720 int msg_didout_before_start = 0;
721 int count = 0; /* line number count */
722 int did_inc = FALSE; /* incremented RedrawingDisabled */
723 int retval = OK;
724#ifdef FEAT_EVAL
725 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000726 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 int current_line = 0; /* active line in lines_ga */
728 char_u *fname = NULL; /* function or script name */
729 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
730 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000731 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 int initial_trylevel;
733 struct msglist **saved_msg_list = NULL;
734 struct msglist *private_msg_list;
735
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100736 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100737 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000739 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000741 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100743# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744# define cmd_cookie cookie
745#endif
746 static int call_depth = 0; /* recursiveness */
747
748#ifdef FEAT_EVAL
749 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
750 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000751 * This ensures that the do_errthrow() call in do_one_cmd() does not
752 * combine the messages stored by an earlier invocation of do_one_cmd()
753 * with the command name of the later one. This would happen when
754 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 saved_msg_list = msg_list;
756 msg_list = &private_msg_list;
757 private_msg_list = NULL;
758#endif
759
760 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100761 * here. The value of 200 allows nested function calls, ":source", etc.
762 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100763 if (call_depth >= 200
764#ifdef FEAT_EVAL
765 && call_depth >= p_mfd
766#endif
767 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 {
769 EMSG(_("E169: Command too recursive"));
770#ifdef FEAT_EVAL
771 /* When converting to an exception, we do not include the command name
772 * since this is not an error of the specific command. */
773 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
774 msg_list = saved_msg_list;
775#endif
776 return FAIL;
777 }
778 ++call_depth;
779
780#ifdef FEAT_EVAL
781 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000782 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 cstack.cs_trylevel = 0;
784 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000785 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
787
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100788 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
790 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100791 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000792 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 ++ex_nesting_level;
794
795 /* Get the function or script name and the address where the next breakpoint
796 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000797 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 {
799 fname = func_name(real_cookie);
800 breakpoint = func_breakpoint(real_cookie);
801 dbg_tick = func_dbg_tick(real_cookie);
802 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100803 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 {
805 fname = sourcing_name;
806 breakpoint = source_breakpoint(real_cookie);
807 dbg_tick = source_dbg_tick(real_cookie);
808 }
809
810 /*
811 * Initialize "force_abort" and "suppress_errthrow" at the top level.
812 */
813 if (!recursive)
814 {
815 force_abort = FALSE;
816 suppress_errthrow = FALSE;
817 }
818
819 /*
820 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000821 * exception handling (used when debugging). Otherwise clear it to avoid
822 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000824 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000825 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000826 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200827 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828
829 initial_trylevel = trylevel;
830
831 /*
832 * "did_throw" will be set to TRUE when an exception is being thrown.
833 */
834 did_throw = FALSE;
835#endif
836 /*
837 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000838 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 * If force_abort is set, we cancel everything.
840 */
841 did_emsg = FALSE;
842
843 /*
844 * KeyTyped is only set when calling vgetc(). Reset it here when not
845 * calling vgetc() (sourced command lines).
846 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100847 if (!(flags & DOCMD_KEYTYPED)
848 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 KeyTyped = FALSE;
850
851 /*
852 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000853 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 * - for multiple commands on one line, separated with '|'
855 * - when repeating until there are no more lines (for ":source")
856 */
857 next_cmdline = cmdline;
858 do
859 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000860#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100861 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000862#endif
863
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000864 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 if (next_cmdline == NULL
866#ifdef FEAT_EVAL
867 && !force_abort
868 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000869 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870#endif
871 )
872 did_emsg = FALSE;
873
874 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000875 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100876 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 * 3. If a line is given: Make a copy, so we can mess with it.
878 */
879
880#ifdef FEAT_EVAL
881 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000882 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 {
884 /* Each '|' separated command is stored separately in lines_ga, to
885 * be able to jump to it. Don't use next_cmdline now. */
886 vim_free(cmdline_copy);
887 cmdline_copy = NULL;
888
889 /* Check if a function has returned or, unless it has an unclosed
890 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000891 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000893# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000894 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000895 func_line_end(real_cookie);
896# endif
897 if (func_has_ended(real_cookie))
898 {
899 retval = FAIL;
900 break;
901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000903#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000904 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100905 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000906 script_line_end();
907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908
909 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100910 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 {
912 retval = FAIL;
913 break;
914 }
915
916 /* If breakpoints have been added/deleted need to check for it. */
917 if (breakpoint != NULL && dbg_tick != NULL
918 && *dbg_tick != debug_tick)
919 {
920 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100921 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 fname, sourcing_lnum);
923 *dbg_tick = debug_tick;
924 }
925
926 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
927 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
928
929 /* Did we encounter a breakpoint? */
930 if (breakpoint != NULL && *breakpoint != 0
931 && *breakpoint <= sourcing_lnum)
932 {
933 dbg_breakpoint(fname, sourcing_lnum);
934 /* Find next breakpoint. */
935 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100936 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 fname, sourcing_lnum);
938 *dbg_tick = debug_tick;
939 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000940# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000941 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000942 {
943 if (getline_is_func)
944 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100945 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000946 script_line_start();
947 }
948# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 }
950
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000951 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000953 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100954 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 * below, so that it stores lines in or reads them from
956 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000957 * while/for loop. */
958 cmd_getline = get_loop_line;
959 cmd_cookie = (void *)&cmd_loop_cookie;
960 cmd_loop_cookie.lines_gap = &lines_ga;
961 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100962 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000963 cmd_loop_cookie.cookie = cookie;
964 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 }
966 else
967 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100968 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 cmd_cookie = cookie;
970 }
971#endif
972
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100973 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 if (next_cmdline == NULL)
975 {
976 /*
977 * Need to set msg_didout for the first line after an ":if",
978 * otherwise the ":if" will be overwritten.
979 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100980 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100982 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983#ifdef FEAT_EVAL
984 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
985#else
986 0
987#endif
988 )) == NULL)
989 {
990 /* Don't call wait_return for aborted command line. The NULL
991 * returned for the end of a sourced file or executed function
992 * doesn't do this. */
993 if (KeyTyped && !(flags & DOCMD_REPEAT))
994 need_wait_return = FALSE;
995 retval = FAIL;
996 break;
997 }
998 used_getline = TRUE;
999
1000 /*
1001 * Keep the first typed line. Clear it when more lines are typed.
1002 */
1003 if (flags & DOCMD_KEEPLINE)
1004 {
1005 vim_free(repeat_cmdline);
1006 if (count == 0)
1007 repeat_cmdline = vim_strsave(next_cmdline);
1008 else
1009 repeat_cmdline = NULL;
1010 }
1011 }
1012
1013 /* 3. Make a copy of the command so we can mess with it. */
1014 else if (cmdline_copy == NULL)
1015 {
1016 next_cmdline = vim_strsave(next_cmdline);
1017 if (next_cmdline == NULL)
1018 {
1019 EMSG(_(e_outofmem));
1020 retval = FAIL;
1021 break;
1022 }
1023 }
1024 cmdline_copy = next_cmdline;
1025
1026#ifdef FEAT_EVAL
1027 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001028 * Save the current line when inside a ":while" or ":for", and when
1029 * the command looks like a ":while" or ":for", because we may need it
1030 * later. When there is a '|' and another command, it is stored
1031 * separately, because we need to be able to jump back to it from an
1032 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001034 if (current_line == lines_ga.ga_len
1035 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001037 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 {
1039 retval = FAIL;
1040 break;
1041 }
1042 }
1043 did_endif = FALSE;
1044#endif
1045
1046 if (count++ == 0)
1047 {
1048 /*
1049 * All output from the commands is put below each other, without
1050 * waiting for a return. Don't do this when executing commands
1051 * from a script or when being called recursive (e.g. for ":e
1052 * +command file").
1053 */
1054 if (!(flags & DOCMD_NOWAIT) && !recursive)
1055 {
1056 msg_didout_before_start = msg_didout;
1057 msg_didany = FALSE; /* no output yet */
1058 msg_start();
1059 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001060 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 ++RedrawingDisabled;
1062 did_inc = TRUE;
1063 }
1064 }
1065
1066 if (p_verbose >= 15 && sourcing_name != NULL)
1067 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001069 verbose_enter_scroll();
1070
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 smsg((char_u *)_("line %ld: %s"),
1072 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001073 if (msg_silent == 0)
1074 msg_puts((char_u *)"\n"); /* don't overwrite this */
1075
1076 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 --no_wait_return;
1078 }
1079
1080 /*
1081 * 2. Execute one '|' separated command.
1082 * do_one_cmd() will return NULL if there is no trailing '|'.
1083 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1084 */
1085 ++recursive;
1086 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1087#ifdef FEAT_EVAL
1088 &cstack,
1089#endif
1090 cmd_getline, cmd_cookie);
1091 --recursive;
1092
1093#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 if (cmd_cookie == (void *)&cmd_loop_cookie)
1095 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098#endif
1099
1100 if (next_cmdline == NULL)
1101 {
1102 vim_free(cmdline_copy);
1103 cmdline_copy = NULL;
1104#ifdef FEAT_CMDHIST
1105 /*
1106 * If the command was typed, remember it for the ':' register.
1107 * Do this AFTER executing the command to make :@: work.
1108 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001109 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 && new_last_cmdline != NULL)
1111 {
1112 vim_free(last_cmdline);
1113 last_cmdline = new_last_cmdline;
1114 new_last_cmdline = NULL;
1115 }
1116#endif
1117 }
1118 else
1119 {
1120 /* need to copy the command after the '|' to cmdline_copy, for the
1121 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001122 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 next_cmdline = cmdline_copy;
1124 }
1125
1126
1127#ifdef FEAT_EVAL
1128 /* reset did_emsg for a function that is not aborted by an error */
1129 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001130 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 && !func_has_abort(real_cookie))
1132 did_emsg = FALSE;
1133
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001134 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 {
1136 ++current_line;
1137
1138 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001139 * An ":endwhile", ":endfor" and ":continue" is handled here.
1140 * If we were executing commands, jump back to the ":while" or
1141 * ":for".
1142 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001144 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001146 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001148 /* Jump back to the matching ":while" or ":for". Be careful
1149 * not to use a cs_line[] from an entry that isn't a ":while"
1150 * or ":for": It would make "current_line" invalid and can
1151 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 if (!did_emsg && !got_int && !did_throw
1153 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001154 && (cstack.cs_flags[cstack.cs_idx]
1155 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 && cstack.cs_line[cstack.cs_idx] >= 0
1157 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1158 {
1159 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001160 /* remember we jumped there */
1161 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 line_breakcheck(); /* check if CTRL-C typed */
1163
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001164 /* Check for the next breakpoint at or after the ":while"
1165 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 if (breakpoint != NULL)
1167 {
1168 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001169 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 fname,
1171 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1172 *dbg_tick = debug_tick;
1173 }
1174 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001177 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001179 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1180 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 }
1182 }
1183
1184 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001185 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001187 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001189 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1191 }
1192 }
1193
1194 /*
1195 * When not inside any ":while" loop, clear remembered lines.
1196 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001197 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 {
1199 if (lines_ga.ga_len > 0)
1200 {
1201 sourcing_lnum =
1202 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1203 free_cmdlines(&lines_ga);
1204 }
1205 current_line = 0;
1206 }
1207
1208 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001209 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1210 * being restored at the ":endtry". Reset them here and set the
1211 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1212 * This includes the case where a missing ":endif", ":endwhile" or
1213 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001215 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001217 cstack.cs_lflags &= ~CSL_HAD_FINA;
1218 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1219 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 did_throw ? (void *)current_exception : NULL);
1221 did_emsg = got_int = did_throw = FALSE;
1222 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1223 }
1224
1225 /* Update global "trylevel" for recursive calls to do_cmdline() from
1226 * within this loop. */
1227 trylevel = initial_trylevel + cstack.cs_trylevel;
1228
1229 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001230 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 * files) is aborted because of an error, an interrupt, or an uncaught
1232 * exception, cancel everything. If it is left normally, reset
1233 * force_abort to get the non-EH compatible abortion behavior for
1234 * the rest of the script.
1235 */
1236 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1237 force_abort = FALSE;
1238
1239 /* Convert an interrupt to an exception if appropriate. */
1240 (void)do_intthrow(&cstack);
1241#endif /* FEAT_EVAL */
1242
1243 }
1244 /*
1245 * Continue executing command lines when:
1246 * - no CTRL-C typed, no aborting error, no exception thrown or try
1247 * conditionals need to be checked for executing finally clauses or
1248 * catching an interrupt exception
1249 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001250 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 * looping for ":source" command or function call.
1252 */
1253 while (!((got_int
1254#ifdef FEAT_EVAL
1255 || (did_emsg && force_abort) || did_throw
1256#endif
1257 )
1258#ifdef FEAT_EVAL
1259 && cstack.cs_trylevel == 0
1260#endif
1261 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001262 && !(did_emsg
1263#ifdef FEAT_EVAL
1264 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001265 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001266 * the :endtry to be missed. */
1267 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1268#endif
1269 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001270 && (getline_equal(fgetline, cookie, getexmodeline)
1271 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 && (next_cmdline != NULL
1273#ifdef FEAT_EVAL
1274 || cstack.cs_idx >= 0
1275#endif
1276 || (flags & DOCMD_REPEAT)));
1277
1278 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001279 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280#ifdef FEAT_EVAL
1281 free_cmdlines(&lines_ga);
1282 ga_clear(&lines_ga);
1283
1284 if (cstack.cs_idx >= 0)
1285 {
1286 /*
1287 * If a sourced file or executed function ran to its end, report the
1288 * unclosed conditional.
1289 */
1290 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001291 && ((getline_equal(fgetline, cookie, getsourceline)
1292 && !source_finished(fgetline, cookie))
1293 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 && !func_has_ended(real_cookie))))
1295 {
1296 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1297 EMSG(_(e_endtry));
1298 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1299 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001300 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1301 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 else
1303 EMSG(_(e_endif));
1304 }
1305
1306 /*
1307 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1308 * ":endtry" in a sourced file or executed function. If the try
1309 * conditional is in its finally clause, ignore anything pending.
1310 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001311 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 */
1313 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001314 {
1315 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1316
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001317 if (idx >= 0)
1318 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001319 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1320 &cstack.cs_looplevel);
1321 }
1322 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 trylevel = initial_trylevel;
1324 }
1325
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001326 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1327 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001329 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 ? (char_u *)"endfunction" : (char_u *)NULL);
1331
1332 if (trylevel == 0)
1333 {
1334 /*
1335 * When an exception is being thrown out of the outermost try
1336 * conditional, discard the uncaught exception, disable the conversion
1337 * of interrupts or errors to exceptions, and ensure that no more
1338 * commands are executed.
1339 */
1340 if (did_throw)
1341 {
1342 void *p = NULL;
1343 char_u *saved_sourcing_name;
1344 int saved_sourcing_lnum;
1345 struct msglist *messages = NULL, *next;
1346
1347 /*
1348 * If the uncaught exception is a user exception, report it as an
1349 * error. If it is an error exception, display the saved error
1350 * message now. For an interrupt exception, do nothing; the
1351 * interrupt message is given elsewhere.
1352 */
1353 switch (current_exception->type)
1354 {
1355 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001356 vim_snprintf((char *)IObuff, IOSIZE,
1357 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 current_exception->value);
1359 p = vim_strsave(IObuff);
1360 break;
1361 case ET_ERROR:
1362 messages = current_exception->messages;
1363 current_exception->messages = NULL;
1364 break;
1365 case ET_INTERRUPT:
1366 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 }
1368
1369 saved_sourcing_name = sourcing_name;
1370 saved_sourcing_lnum = sourcing_lnum;
1371 sourcing_name = current_exception->throw_name;
1372 sourcing_lnum = current_exception->throw_lnum;
1373 current_exception->throw_name = NULL;
1374
1375 discard_current_exception(); /* uses IObuff if 'verbose' */
1376 suppress_errthrow = TRUE;
1377 force_abort = TRUE;
1378
1379 if (messages != NULL)
1380 {
1381 do
1382 {
1383 next = messages->next;
1384 emsg(messages->msg);
1385 vim_free(messages->msg);
1386 vim_free(messages);
1387 messages = next;
1388 }
1389 while (messages != NULL);
1390 }
1391 else if (p != NULL)
1392 {
1393 emsg(p);
1394 vim_free(p);
1395 }
1396 vim_free(sourcing_name);
1397 sourcing_name = saved_sourcing_name;
1398 sourcing_lnum = saved_sourcing_lnum;
1399 }
1400
1401 /*
1402 * On an interrupt or an aborting error not converted to an exception,
1403 * disable the conversion of errors to exceptions. (Interrupts are not
1404 * converted any more, here.) This enables also the interrupt message
1405 * when force_abort is set and did_emsg unset in case of an interrupt
1406 * from a finally clause after an error.
1407 */
1408 else if (got_int || (did_emsg && force_abort))
1409 suppress_errthrow = TRUE;
1410 }
1411
1412 /*
1413 * The current cstack will be freed when do_cmdline() returns. An uncaught
1414 * exception will have to be rethrown in the previous cstack. If a function
1415 * has just returned or a script file was just finished and the previous
1416 * cstack belongs to the same function or, respectively, script file, it
1417 * will have to be checked for finally clauses to be executed due to the
1418 * ":return" or ":finish". This is done in do_one_cmd().
1419 */
1420 if (did_throw)
1421 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001422 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001424 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 && ex_nesting_level > func_level(real_cookie) + 1))
1426 {
1427 if (!did_throw)
1428 check_cstack = TRUE;
1429 }
1430 else
1431 {
1432 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001433 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 --ex_nesting_level;
1435 /*
1436 * Go to debug mode when returning from a function in which we are
1437 * single-stepping.
1438 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001439 if ((getline_equal(fgetline, cookie, getsourceline)
1440 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001442 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 ? (char_u *)_("End of sourced file")
1444 : (char_u *)_("End of function"));
1445 }
1446
1447 /*
1448 * Restore the exception environment (done after returning from the
1449 * debugger).
1450 */
1451 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001452 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453
1454 msg_list = saved_msg_list;
1455#endif /* FEAT_EVAL */
1456
1457 /*
1458 * If there was too much output to fit on the command line, ask the user to
1459 * hit return before redrawing the screen. With the ":global" command we do
1460 * this only once after the command is finished.
1461 */
1462 if (did_inc)
1463 {
1464 --RedrawingDisabled;
1465 --no_wait_return;
1466 msg_scroll = FALSE;
1467
1468 /*
1469 * When just finished an ":if"-":else" which was typed, no need to
1470 * wait for hit-return. Also for an error situation.
1471 */
1472 if (retval == FAIL
1473#ifdef FEAT_EVAL
1474 || (did_endif && KeyTyped && !did_emsg)
1475#endif
1476 )
1477 {
1478 need_wait_return = FALSE;
1479 msg_didany = FALSE; /* don't wait when restarting edit */
1480 }
1481 else if (need_wait_return)
1482 {
1483 /*
1484 * The msg_start() above clears msg_didout. The wait_return we do
1485 * here should not overwrite the command that may be shown before
1486 * doing that.
1487 */
1488 msg_didout |= msg_didout_before_start;
1489 wait_return(FALSE);
1490 }
1491 }
1492
Bram Moolenaar2a942252012-11-28 23:03:07 +01001493#ifdef FEAT_EVAL
1494 did_endif = FALSE; /* in case do_cmdline used recursively */
1495#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 /*
1497 * Reset if_level, in case a sourced script file contains more ":if" than
1498 * ":endif" (could be ":if x | foo | endif").
1499 */
1500 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001501#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001502
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 --call_depth;
1504 return retval;
1505}
1506
1507#ifdef FEAT_EVAL
1508/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001509 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 */
1511 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001512get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001514 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 wcmd_T *wp;
1516 char_u *line;
1517
1518 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1519 {
1520 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001521 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001523 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if (cp->getline == NULL)
1525 line = getcmdline(c, 0L, indent);
1526 else
1527 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001528 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 ++cp->current_line;
1530
1531 return line;
1532 }
1533
1534 KeyTyped = FALSE;
1535 ++cp->current_line;
1536 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1537 sourcing_lnum = wp->lnum;
1538 return vim_strsave(wp->line);
1539}
1540
1541/*
1542 * Store a line in "gap" so that a ":while" loop can execute it again.
1543 */
1544 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001545store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546{
1547 if (ga_grow(gap, 1) == FAIL)
1548 return FAIL;
1549 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1550 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1551 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 return OK;
1553}
1554
1555/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001556 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 */
1558 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001559free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
1561 while (gap->ga_len > 0)
1562 {
1563 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1564 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 }
1566}
1567#endif
1568
1569/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001570 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1571 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001574getline_equal(
1575 char_u *(*fgetline)(int, void *, int),
1576 void *cookie UNUSED, /* argument for fgetline() */
1577 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
1579#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001580 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001581 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
Bram Moolenaar89d40322006-08-29 15:30:07 +00001583 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001584 * function that's originally used to obtain the lines. This may be
1585 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001586 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001587 cp = (struct loop_cookie *)cookie;
1588 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 {
1590 gp = cp->getline;
1591 cp = cp->cookie;
1592 }
1593 return gp == func;
1594#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001595 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596#endif
1597}
1598
1599#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1600/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001601 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 * getline function. Otherwise return "cookie".
1603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001605getline_cookie(
1606 char_u *(*fgetline)(int, void *, int) UNUSED,
1607 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608{
1609# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001610 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001611 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
Bram Moolenaar89d40322006-08-29 15:30:07 +00001613 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001614 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001616 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001617 cp = (struct loop_cookie *)cookie;
1618 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 {
1620 gp = cp->getline;
1621 cp = cp->cookie;
1622 }
1623 return cp;
1624# else
1625 return cookie;
1626# endif
1627}
1628#endif
1629
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001630
1631/*
1632 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1633 * ":bwipeout", etc.
1634 * Returns the buffer number.
1635 */
1636 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001637compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001638{
1639 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001640 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001641 int count = offset;
1642
1643 buf = firstbuf;
1644 while (buf->b_next != NULL && buf->b_fnum < lnum)
1645 buf = buf->b_next;
1646 while (count != 0)
1647 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001648 count += (offset < 0) ? 1 : -1;
1649 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1650 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001651 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001652 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001653 if (addr_type == ADDR_LOADED_BUFFERS)
1654 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001655 while (buf->b_ml.ml_mfp == NULL)
1656 {
1657 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1658 if (nextbuf == NULL)
1659 break;
1660 buf = nextbuf;
1661 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001662 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001663 /* we might have gone too far, last buffer is not loadedd */
1664 if (addr_type == ADDR_LOADED_BUFFERS)
1665 while (buf->b_ml.ml_mfp == NULL)
1666 {
1667 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1668 if (nextbuf == NULL)
1669 break;
1670 buf = nextbuf;
1671 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001672 return buf->b_fnum;
1673}
1674
Bram Moolenaarf240e182014-11-27 18:33:02 +01001675#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001676static int current_win_nr(win_T *win);
1677static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001678
1679 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001680current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001681{
1682 win_T *wp;
1683 int nr = 0;
1684
Bram Moolenaar29323592016-07-24 22:04:11 +02001685 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001686 {
1687 ++nr;
1688 if (wp == win)
1689 break;
1690 }
1691 return nr;
1692}
1693
1694 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001695current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001696{
1697 tabpage_T *tp;
1698 int nr = 0;
1699
Bram Moolenaar29323592016-07-24 22:04:11 +02001700 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001701 {
1702 ++nr;
1703 if (tp == tab)
1704 break;
1705 }
1706 return nr;
1707}
1708
1709# define CURRENT_WIN_NR current_win_nr(curwin)
1710# define LAST_WIN_NR current_win_nr(NULL)
1711# define CURRENT_TAB_NR current_tab_nr(curtab)
1712# define LAST_TAB_NR current_tab_nr(NULL)
1713#else
1714# define CURRENT_WIN_NR 1
1715# define LAST_WIN_NR 1
1716# define CURRENT_TAB_NR 1
1717# define LAST_TAB_NR 1
1718#endif
1719
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721/*
1722 * Execute one Ex command.
1723 *
1724 * If 'sourcing' is TRUE, the command will be included in the error message.
1725 *
1726 * 1. skip comment lines and leading space
1727 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001728 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001729 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001730 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001731 * 6. parse arguments
1732 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001734 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 *
1736 * This function may be called recursively!
1737 */
1738#if (_MSC_VER == 1200)
1739/*
Bram Moolenaared203462004-06-16 11:19:22 +00001740 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001742 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743#endif
1744 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001745do_one_cmd(
1746 char_u **cmdlinep,
1747 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001749 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001751 char_u *(*fgetline)(int, void *, int),
1752 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
1754 char_u *p;
1755 linenr_T lnum;
1756 long n;
1757 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001758 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 exarg_T ea; /* Ex command arguments */
1760 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001761 int save_msg_scroll = msg_scroll;
1762 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001764#ifdef HAVE_SANDBOX
1765 int did_sandbox = FALSE;
1766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 cmdmod_T save_cmdmod;
1768 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001769 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001770 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
1772 vim_memset(&ea, 0, sizeof(ea));
1773 ea.line1 = 1;
1774 ea.line2 = 1;
1775#ifdef FEAT_EVAL
1776 ++ex_nesting_level;
1777#endif
1778
Bram Moolenaar21691f82012-12-05 19:13:18 +01001779 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 if (quitmore
1781#ifdef FEAT_EVAL
1782 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001783 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001784#endif
1785#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001786 /* avoid that an autocommand, e.g. QuitPre, does this */
1787 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#endif
1789 )
1790 --quitmore;
1791
1792 /*
1793 * Reset browse, confirm, etc.. They are restored when returning, for
1794 * recursive calls.
1795 */
1796 save_cmdmod = cmdmod;
1797 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1798
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001799 /* "#!anything" is handled like a comment. */
1800 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1801 goto doend;
1802
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 /*
1804 * Repeat until no more command modifiers are found.
1805 */
1806 ea.cmd = *cmdlinep;
1807 for (;;)
1808 {
1809/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001810 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 */
1812 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1813 ++ea.cmd;
1814
1815 /* in ex mode, an empty line works like :+ */
1816 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001817 && (getline_equal(fgetline, cookie, getexmodeline)
1818 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001819 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 {
1821 ea.cmd = (char_u *)"+";
1822 ex_pressedreturn = TRUE;
1823 }
1824
1825 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001826 if (*ea.cmd == '"')
1827 goto doend;
1828 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001829 {
1830 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
1834/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001835 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001837 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 switch (*p)
1839 {
1840 /* When adding an entry, also modify cmd_exists(). */
1841 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1842 break;
1843#ifdef FEAT_WINDOWS
1844 cmdmod.split |= WSP_ABOVE;
1845#endif
1846 continue;
1847
1848 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1849 {
1850#ifdef FEAT_WINDOWS
1851 cmdmod.split |= WSP_BELOW;
1852#endif
1853 continue;
1854 }
1855 if (checkforcmd(&ea.cmd, "browse", 3))
1856 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001857#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 cmdmod.browse = TRUE;
1859#endif
1860 continue;
1861 }
1862 if (!checkforcmd(&ea.cmd, "botright", 2))
1863 break;
1864#ifdef FEAT_WINDOWS
1865 cmdmod.split |= WSP_BOT;
1866#endif
1867 continue;
1868
1869 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1870 break;
1871#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1872 cmdmod.confirm = TRUE;
1873#endif
1874 continue;
1875
1876 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1877 {
1878 cmdmod.keepmarks = TRUE;
1879 continue;
1880 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001881 if (checkforcmd(&ea.cmd, "keepalt", 5))
1882 {
1883 cmdmod.keepalt = TRUE;
1884 continue;
1885 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001886 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1887 {
1888 cmdmod.keeppatterns = TRUE;
1889 continue;
1890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1892 break;
1893 cmdmod.keepjumps = TRUE;
1894 continue;
1895
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001896 case 'f': /* only accept ":filter {pat} cmd" */
1897 {
1898 char_u *reg_pat;
1899
1900 if (!checkforcmd(&p, "filter", 4)
1901 || *p == NUL || ends_excmd(*p))
1902 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001903 if (*p == '!')
1904 {
1905 cmdmod.filter_force = TRUE;
1906 p = skipwhite(p + 1);
1907 if (*p == NUL || ends_excmd(*p))
1908 break;
1909 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001910 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1911 if (p == NULL || *p == NUL)
1912 break;
1913 cmdmod.filter_regmatch.regprog =
1914 vim_regcomp(reg_pat, RE_MAGIC);
1915 if (cmdmod.filter_regmatch.regprog == NULL)
1916 break;
1917 ea.cmd = p;
1918 continue;
1919 }
1920
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 /* ":hide" and ":hide | cmd" are not modifiers */
1922 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1923 || *p == NUL || ends_excmd(*p))
1924 break;
1925 ea.cmd = p;
1926 cmdmod.hide = TRUE;
1927 continue;
1928
1929 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1930 {
1931 cmdmod.lockmarks = TRUE;
1932 continue;
1933 }
1934
1935 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1936 break;
1937#ifdef FEAT_WINDOWS
1938 cmdmod.split |= WSP_ABOVE;
1939#endif
1940 continue;
1941
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001942 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001943 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001944#ifdef FEAT_AUTOCMD
1945 if (cmdmod.save_ei == NULL)
1946 {
1947 /* Set 'eventignore' to "all". Restore the
1948 * existing option value later. */
1949 cmdmod.save_ei = vim_strsave(p_ei);
1950 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001951 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001952 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001953#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001954 continue;
1955 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001956 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001957 break;
1958 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001959 continue;
1960
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1962 break;
1963#ifdef FEAT_WINDOWS
1964 cmdmod.split |= WSP_BELOW;
1965#endif
1966 continue;
1967
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001968 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1969 {
1970#ifdef HAVE_SANDBOX
1971 if (!did_sandbox)
1972 ++sandbox;
1973 did_sandbox = TRUE;
1974#endif
1975 continue;
1976 }
1977 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001979 if (save_msg_silent == -1)
1980 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 ++msg_silent;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001982 if (*ea.cmd == '!' && !VIM_ISWHITE(ea.cmd[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
1984 /* ":silent!", but not "silent !cmd" */
1985 ea.cmd = skipwhite(ea.cmd + 1);
1986 ++emsg_silent;
1987 ++did_esilent;
1988 }
1989 continue;
1990
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001991 case 't': if (checkforcmd(&p, "tab", 3))
1992 {
1993#ifdef FEAT_WINDOWS
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001994 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01001995 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001996 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001997 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001998 else
1999 {
2000 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2001 {
2002 errormsg = (char_u *)_(e_invrange);
2003 goto doend;
2004 }
2005 cmdmod.tab = tabnr + 1;
2006 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002007 ea.cmd = p;
2008#endif
2009 continue;
2010 }
2011 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 break;
2013#ifdef FEAT_WINDOWS
2014 cmdmod.split |= WSP_TOP;
2015#endif
2016 continue;
2017
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002018 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2019 break;
2020 if (save_msg_silent == -1)
2021 save_msg_silent = msg_silent;
2022 msg_silent = 0;
2023 continue;
2024
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2026 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002027#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 cmdmod.split |= WSP_VERT;
2029#endif
2030 continue;
2031 }
2032 if (!checkforcmd(&p, "verbose", 4))
2033 break;
2034 if (verbose_save < 0)
2035 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002036 if (vim_isdigit(*ea.cmd))
2037 p_verbose = atoi((char *)ea.cmd);
2038 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 p_verbose = 1;
2040 ea.cmd = p;
2041 continue;
2042 }
2043 break;
2044 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002045 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
2047#ifdef FEAT_EVAL
2048 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2049 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2050#else
2051 ea.skip = (if_level > 0);
2052#endif
2053
2054#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002055# ifdef FEAT_PROFILE
2056 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002057 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002058 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002059 if (getline_equal(fgetline, cookie, get_func_line))
2060 func_line_exec(getline_cookie(fgetline, cookie));
2061 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002062 script_line_exec();
2063 }
2064#endif
2065
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 /* May go to debug mode. If this happens and the ">quit" debug command is
2067 * used, throw an interrupt exception and skip the next command. */
2068 dbg_check_breakpoint(&ea);
2069 if (!ea.skip && got_int)
2070 {
2071 ea.skip = TRUE;
2072 (void)do_intthrow(cstack);
2073 }
2074#endif
2075
2076/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002077 * 3. Skip over the range to find the command. Let "p" point to after it.
2078 *
2079 * We need the command to know what kind of range it uses.
2080 */
2081 cmd = ea.cmd;
2082 ea.cmd = skip_range(ea.cmd, NULL);
2083 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2084 ea.cmd = skipwhite(ea.cmd + 1);
2085 p = find_command(&ea, NULL);
2086
2087/*
2088 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 *
2090 * where 'addr' is:
2091 *
2092 * % (entire file)
2093 * $ [+-NUM]
2094 * 'x [+-NUM] (where x denotes a currently defined mark)
2095 * . [+-NUM]
2096 * [+-NUM]..
2097 * NUM
2098 *
2099 * The ea.cmd pointer is updated to point to the first character following the
2100 * range spec. If an initial address is found, but no second, the upper bound
2101 * is equal to the lower.
2102 */
2103
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002104 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002105 if (!IS_USER_CMDIDX(ea.cmdidx))
2106 {
2107 if (ea.cmdidx != CMD_SIZE)
2108 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2109 else
2110 ea.addr_type = ADDR_LINES;
2111
2112#ifdef FEAT_WINDOWS
2113 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002114 if (ea.cmdidx == CMD_wincmd && p != NULL)
2115 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002116#endif
2117 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002118
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002120 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 for (;;)
2122 {
2123 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002124 switch (ea.addr_type)
2125 {
2126 case ADDR_LINES:
2127 /* default is current line number */
2128 ea.line2 = curwin->w_cursor.lnum;
2129 break;
2130 case ADDR_WINDOWS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002131 ea.line2 = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002132 break;
2133 case ADDR_ARGUMENTS:
2134 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002135 if (ea.line2 > ARGCOUNT)
2136 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002137 break;
2138 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002139 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002140 ea.line2 = curbuf->b_fnum;
2141 break;
2142 case ADDR_TABS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002143 ea.line2 = CURRENT_TAB_NR;
2144 break;
2145 case ADDR_TABS_RELATIVE:
2146 ea.line2 = 1;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002147 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002148#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002149 case ADDR_QUICKFIX:
2150 ea.line2 = qf_get_cur_valid_idx(&ea);
2151 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002152#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002155 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002156 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 if (ea.cmd == NULL) /* error detected */
2158 goto doend;
2159 if (lnum == MAXLNUM)
2160 {
2161 if (*ea.cmd == '%') /* '%' - all lines */
2162 {
2163 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002164 switch (ea.addr_type)
2165 {
2166 case ADDR_LINES:
2167 ea.line1 = 1;
2168 ea.line2 = curbuf->b_ml.ml_line_count;
2169 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002170 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002171 {
2172 buf_T *buf = firstbuf;
2173
2174 while (buf->b_next != NULL
2175 && buf->b_ml.ml_mfp == NULL)
2176 buf = buf->b_next;
2177 ea.line1 = buf->b_fnum;
2178 buf = lastbuf;
2179 while (buf->b_prev != NULL
2180 && buf->b_ml.ml_mfp == NULL)
2181 buf = buf->b_prev;
2182 ea.line2 = buf->b_fnum;
2183 break;
2184 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002185 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002186 ea.line1 = firstbuf->b_fnum;
2187 ea.line2 = lastbuf->b_fnum;
2188 break;
2189 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002190 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002191 if (IS_USER_CMDIDX(ea.cmdidx))
2192 {
2193 ea.line1 = 1;
2194 ea.line2 = ea.addr_type == ADDR_WINDOWS
2195 ? LAST_WIN_NR : LAST_TAB_NR;
2196 }
2197 else
2198 {
2199 /* there is no Vim command which uses '%' and
2200 * ADDR_WINDOWS or ADDR_TABS */
2201 errormsg = (char_u *)_(e_invrange);
2202 goto doend;
2203 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002204 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002205 case ADDR_TABS_RELATIVE:
2206 errormsg = (char_u *)_(e_invrange);
2207 goto doend;
2208 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002209 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002210 if (ARGCOUNT == 0)
2211 ea.line1 = ea.line2 = 0;
2212 else
2213 {
2214 ea.line1 = 1;
2215 ea.line2 = ARGCOUNT;
2216 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002217 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002218#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002219 case ADDR_QUICKFIX:
2220 ea.line1 = 1;
2221 ea.line2 = qf_get_size(&ea);
2222 if (ea.line2 == 0)
2223 ea.line2 = 1;
2224 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002225#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 ++ea.addr_count;
2228 }
2229 /* '*' - visual area */
2230 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2231 {
2232 pos_T *fp;
2233
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002234 if (ea.addr_type != ADDR_LINES)
2235 {
2236 errormsg = (char_u *)_(e_invrange);
2237 goto doend;
2238 }
2239
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 ++ea.cmd;
2241 if (!ea.skip)
2242 {
2243 fp = getmark('<', FALSE);
2244 if (check_mark(fp) == FAIL)
2245 goto doend;
2246 ea.line1 = fp->lnum;
2247 fp = getmark('>', FALSE);
2248 if (check_mark(fp) == FAIL)
2249 goto doend;
2250 ea.line2 = fp->lnum;
2251 ++ea.addr_count;
2252 }
2253 }
2254 }
2255 else
2256 ea.line2 = lnum;
2257 ea.addr_count++;
2258
2259 if (*ea.cmd == ';')
2260 {
2261 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002262 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +01002264 /* don't leave the cursor on an illegal line or column */
2265 check_cursor();
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 }
2268 else if (*ea.cmd != ',')
2269 break;
2270 ++ea.cmd;
2271 }
2272
2273 /* One address given: set start and end lines */
2274 if (ea.addr_count == 1)
2275 {
2276 ea.line1 = ea.line2;
2277 /* ... but only implicit: really no address given */
2278 if (lnum == MAXLNUM)
2279 ea.addr_count = 0;
2280 }
2281
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002283 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 */
2285
2286 /*
2287 * Skip ':' and any white space
2288 */
2289 ea.cmd = skipwhite(ea.cmd);
2290 while (*ea.cmd == ':')
2291 ea.cmd = skipwhite(ea.cmd + 1);
2292
2293 /*
2294 * If we got a line, but no command, then go to the line.
2295 * If we find a '|' or '\n' we set ea.nextcmd.
2296 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002297 if (*ea.cmd == NUL || *ea.cmd == '"'
2298 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
2300 /*
2301 * strange vi behaviour:
2302 * ":3" jumps to line 3
2303 * ":3|..." prints line 3
2304 * ":|" prints current line
2305 */
2306 if (ea.skip) /* skip this if inside :if */
2307 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002308 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
2310 ea.cmdidx = CMD_print;
2311 ea.argt = RANGE+COUNT+TRLBAR;
2312 if ((errormsg = invalid_range(&ea)) == NULL)
2313 {
2314 correct_range(&ea);
2315 ex_print(&ea);
2316 }
2317 }
2318 else if (ea.addr_count != 0)
2319 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002320 if (ea.line2 > curbuf->b_ml.ml_line_count)
2321 {
2322 /* With '-' in 'cpoptions' a line number past the file is an
2323 * error, otherwise put it at the end of the file. */
2324 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2325 ea.line2 = -1;
2326 else
2327 ea.line2 = curbuf->b_ml.ml_line_count;
2328 }
2329
2330 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002331 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 else
2333 {
2334 if (ea.line2 == 0)
2335 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 else
2337 curwin->w_cursor.lnum = ea.line2;
2338 beginline(BL_SOL | BL_FIX);
2339 }
2340 }
2341 goto doend;
2342 }
2343
Bram Moolenaard5005162014-08-22 23:05:54 +02002344#ifdef FEAT_AUTOCMD
2345 /* If this looks like an undefined user command and there are CmdUndefined
2346 * autocommands defined, trigger the matching autocommands. */
2347 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2348 && ASCII_ISUPPER(*ea.cmd)
2349 && has_cmdundefined())
2350 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002351 int ret;
2352
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002353 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002354 while (ASCII_ISALNUM(*p))
2355 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002356 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002357 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2358 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002359 /* If the autocommands did something and didn't cause an error, try
2360 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002361 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002362 }
2363#endif
2364
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#ifdef FEAT_USR_CMDS
2366 if (p == NULL)
2367 {
2368 if (!ea.skip)
2369 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2370 goto doend;
2371 }
2372 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002373 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2374 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 {
2376 errormsg = uc_fun_cmd();
2377 goto doend;
2378 }
2379#endif
2380 if (ea.cmdidx == CMD_SIZE)
2381 {
2382 if (!ea.skip)
2383 {
2384 STRCPY(IObuff, _("E492: Not an editor command"));
2385 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002386 {
2387 /* If the modifier was parsed OK the error must be in the
2388 * following command */
2389 if (after_modifier != NULL)
2390 append_command(after_modifier);
2391 else
2392 append_command(*cmdlinep);
2393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002395 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
2397 goto doend;
2398 }
2399
Bram Moolenaar958636c2014-10-21 20:01:58 +02002400 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2401 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002402#ifdef HAVE_EX_SCRIPT_NI
2403 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2404#endif
2405 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
2407#ifndef FEAT_EVAL
2408 /*
2409 * When the expression evaluation is disabled, recognize the ":if" and
2410 * ":endif" commands and ignore everything in between it.
2411 */
2412 if (ea.cmdidx == CMD_if)
2413 ++if_level;
2414 if (if_level)
2415 {
2416 if (ea.cmdidx == CMD_endif)
2417 --if_level;
2418 goto doend;
2419 }
2420
2421#endif
2422
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002423 /* forced commands */
2424 if (*p == '!' && ea.cmdidx != CMD_substitute
2425 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
2427 ++p;
2428 ea.forceit = TRUE;
2429 }
2430 else
2431 ea.forceit = FALSE;
2432
2433/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002434 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002436 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002437 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438
2439 if (!ea.skip)
2440 {
2441#ifdef HAVE_SANDBOX
2442 if (sandbox != 0 && !(ea.argt & SBOXOK))
2443 {
2444 /* Command not allowed in sandbox. */
2445 errormsg = (char_u *)_(e_sandbox);
2446 goto doend;
2447 }
2448#endif
2449 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2450 {
2451 /* Command not allowed in non-'modifiable' buffer */
2452 errormsg = (char_u *)_(e_modifiable);
2453 goto doend;
2454 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002455
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002456 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002457 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002459 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002460 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 goto doend;
2462 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002463#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002464 /* Disallow editing another buffer when "curbuf_lock" is set.
2465 * Do allow ":edit" (check for argument later).
2466 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002467 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002468 && ea.cmdidx != CMD_edit
2469 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002470 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002471 && curbuf_locked())
2472 goto doend;
2473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474
2475 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2476 {
2477 /* no range allowed */
2478 errormsg = (char_u *)_(e_norange);
2479 goto doend;
2480 }
2481 }
2482
2483 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2484 {
2485 errormsg = (char_u *)_(e_nobang);
2486 goto doend;
2487 }
2488
2489 /*
2490 * Don't complain about the range if it is not used
2491 * (could happen if line_count is accidentally set to 0).
2492 */
2493 if (!ea.skip && !ni)
2494 {
2495 /*
2496 * If the range is backwards, ask for confirmation and, if given, swap
2497 * ea.line1 & ea.line2 so it's forwards again.
2498 * When global command is busy, don't ask, will fail below.
2499 */
2500 if (!global_busy && ea.line1 > ea.line2)
2501 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002502 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002504 if (sourcing || exmode_active)
2505 {
2506 errormsg = (char_u *)_("E493: Backwards range given");
2507 goto doend;
2508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 if (ask_yesno((char_u *)
2510 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002511 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 }
2513 lnum = ea.line1;
2514 ea.line1 = ea.line2;
2515 ea.line2 = lnum;
2516 }
2517 if ((errormsg = invalid_range(&ea)) != NULL)
2518 goto doend;
2519 }
2520
2521 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2522 ea.line2 = 1;
2523
2524 correct_range(&ea);
2525
2526#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002527 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2528 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
2530 /* Put the first line at the start of a closed fold, put the last line
2531 * at the end of a closed fold. */
2532 (void)hasFolding(ea.line1, &ea.line1, NULL);
2533 (void)hasFolding(ea.line2, NULL, &ea.line2);
2534 }
2535#endif
2536
2537#ifdef FEAT_QUICKFIX
2538 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002539 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 * option here, so things like % get expanded.
2541 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002542 p = replace_makeprg(&ea, p, cmdlinep);
2543 if (p == NULL)
2544 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545#endif
2546
2547 /*
2548 * Skip to start of argument.
2549 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2550 */
2551 if (ea.cmdidx == CMD_bang)
2552 ea.arg = p;
2553 else
2554 ea.arg = skipwhite(p);
2555
2556 /*
2557 * Check for "++opt=val" argument.
2558 * Must be first, allow ":w ++enc=utf8 !cmd"
2559 */
2560 if (ea.argt & ARGOPT)
2561 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2562 if (getargopt(&ea) == FAIL && !ni)
2563 {
2564 errormsg = (char_u *)_(e_invarg);
2565 goto doend;
2566 }
2567
2568 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2569 {
2570 if (*ea.arg == '>') /* append */
2571 {
2572 if (*++ea.arg != '>') /* typed wrong */
2573 {
2574 errormsg = (char_u *)_("E494: Use w or w>>");
2575 goto doend;
2576 }
2577 ea.arg = skipwhite(ea.arg + 1);
2578 ea.append = TRUE;
2579 }
2580 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2581 {
2582 ++ea.arg;
2583 ea.usefilter = TRUE;
2584 }
2585 }
2586
2587 if (ea.cmdidx == CMD_read)
2588 {
2589 if (ea.forceit)
2590 {
2591 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2592 ea.forceit = FALSE;
2593 }
2594 else if (*ea.arg == '!') /* :r !filter */
2595 {
2596 ++ea.arg;
2597 ea.usefilter = TRUE;
2598 }
2599 }
2600
2601 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2602 {
2603 ea.amount = 1;
2604 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2605 {
2606 ++ea.arg;
2607 ++ea.amount;
2608 }
2609 ea.arg = skipwhite(ea.arg);
2610 }
2611
2612 /*
2613 * Check for "+command" argument, before checking for next command.
2614 * Don't do this for ":read !cmd" and ":write !cmd".
2615 */
2616 if ((ea.argt & EDITCMD) && !ea.usefilter)
2617 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2618
2619 /*
2620 * Check for '|' to separate commands and '"' to start comments.
2621 * Don't do this for ":read !cmd" and ":write !cmd".
2622 */
2623 if ((ea.argt & TRLBAR) && !ea.usefilter)
2624 separate_nextcmd(&ea);
2625
2626 /*
2627 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002628 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2629 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002631 else if (ea.cmdidx == CMD_bang
2632 || ea.cmdidx == CMD_global
2633 || ea.cmdidx == CMD_vglobal
2634 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {
2636 for (p = ea.arg; *p; ++p)
2637 {
2638 /* Remove one backslash before a newline, so that it's possible to
2639 * pass a newline to the shell and also a newline that is preceded
2640 * with a backslash. This makes it impossible to end a shell
2641 * command in a backslash, but that doesn't appear useful.
2642 * Halving the number of backslashes is incompatible with previous
2643 * versions. */
2644 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002645 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 else if (*p == '\n')
2647 {
2648 ea.nextcmd = p + 1;
2649 *p = NUL;
2650 break;
2651 }
2652 }
2653 }
2654
2655 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2656 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002657 buf_T *buf;
2658
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002660 switch (ea.addr_type)
2661 {
2662 case ADDR_LINES:
2663 ea.line2 = curbuf->b_ml.ml_line_count;
2664 break;
2665 case ADDR_LOADED_BUFFERS:
2666 buf = firstbuf;
2667 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2668 buf = buf->b_next;
2669 ea.line1 = buf->b_fnum;
2670 buf = lastbuf;
2671 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2672 buf = buf->b_prev;
2673 ea.line2 = buf->b_fnum;
2674 break;
2675 case ADDR_BUFFERS:
2676 ea.line1 = firstbuf->b_fnum;
2677 ea.line2 = lastbuf->b_fnum;
2678 break;
2679 case ADDR_WINDOWS:
2680 ea.line2 = LAST_WIN_NR;
2681 break;
2682 case ADDR_TABS:
2683 ea.line2 = LAST_TAB_NR;
2684 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002685 case ADDR_TABS_RELATIVE:
2686 ea.line2 = 1;
2687 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002688 case ADDR_ARGUMENTS:
2689 if (ARGCOUNT == 0)
2690 ea.line1 = ea.line2 = 0;
2691 else
2692 ea.line2 = ARGCOUNT;
2693 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002694#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002695 case ADDR_QUICKFIX:
2696 ea.line2 = qf_get_size(&ea);
2697 if (ea.line2 == 0)
2698 ea.line2 = 1;
2699 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002700#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 }
2703
2704 /* accept numbered register only when no count allowed (:put) */
2705 if ( (ea.argt & REGSTR)
2706 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002707 /* Do not allow register = for user commands */
2708 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2710 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002711#ifndef FEAT_CLIPBOARD
2712 /* check these explicitly for a more specific error message */
2713 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002715 errormsg = (char_u *)_(e_invalidreg);
2716 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 }
2718#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002719 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2720 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002721 {
2722 ea.regname = *ea.arg++;
2723#ifdef FEAT_EVAL
2724 /* for '=' register: accept the rest of the line as an expression */
2725 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2726 {
2727 set_expr_line(vim_strsave(ea.arg));
2728 ea.arg += STRLEN(ea.arg);
2729 }
2730#endif
2731 ea.arg = skipwhite(ea.arg);
2732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 }
2734
2735 /*
2736 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2737 * count, it's a buffer name.
2738 */
2739 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2740 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002741 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 {
2743 n = getdigits(&ea.arg);
2744 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002745 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 {
2747 errormsg = (char_u *)_(e_zerocount);
2748 goto doend;
2749 }
2750 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2751 {
2752 ea.line2 = n;
2753 if (ea.addr_count == 0)
2754 ea.addr_count = 1;
2755 }
2756 else
2757 {
2758 ea.line1 = ea.line2;
2759 ea.line2 += n - 1;
2760 ++ea.addr_count;
2761 /*
2762 * Be vi compatible: no error message for out of range.
2763 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002764 if (ea.addr_type == ADDR_LINES
2765 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 ea.line2 = curbuf->b_ml.ml_line_count;
2767 }
2768 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002769
2770 /*
2771 * Check for flags: 'l', 'p' and '#'.
2772 */
2773 if (ea.argt & EXFLAGS)
2774 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002776 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002777 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 {
2779 errormsg = (char_u *)_(e_trailing);
2780 goto doend;
2781 }
2782
2783 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2784 {
2785 errormsg = (char_u *)_(e_argreq);
2786 goto doend;
2787 }
2788
2789#ifdef FEAT_EVAL
2790 /*
2791 * Skip the command when it's not going to be executed.
2792 * The commands like :if, :endif, etc. always need to be executed.
2793 * Also make an exception for commands that handle a trailing command
2794 * themselves.
2795 */
2796 if (ea.skip)
2797 {
2798 switch (ea.cmdidx)
2799 {
2800 /* commands that need evaluation */
2801 case CMD_while:
2802 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002803 case CMD_for:
2804 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 case CMD_if:
2806 case CMD_elseif:
2807 case CMD_else:
2808 case CMD_endif:
2809 case CMD_try:
2810 case CMD_catch:
2811 case CMD_finally:
2812 case CMD_endtry:
2813 case CMD_function:
2814 break;
2815
2816 /* Commands that handle '|' themselves. Check: A command should
2817 * either have the TRLBAR flag, appear in this list or appear in
2818 * the list at ":help :bar". */
2819 case CMD_aboveleft:
2820 case CMD_and:
2821 case CMD_belowright:
2822 case CMD_botright:
2823 case CMD_browse:
2824 case CMD_call:
2825 case CMD_confirm:
2826 case CMD_delfunction:
2827 case CMD_djump:
2828 case CMD_dlist:
2829 case CMD_dsearch:
2830 case CMD_dsplit:
2831 case CMD_echo:
2832 case CMD_echoerr:
2833 case CMD_echomsg:
2834 case CMD_echon:
2835 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002836 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 case CMD_help:
2838 case CMD_hide:
2839 case CMD_ijump:
2840 case CMD_ilist:
2841 case CMD_isearch:
2842 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002843 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 case CMD_keepjumps:
2845 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002846 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 case CMD_leftabove:
2848 case CMD_let:
2849 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002850 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002852 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002853 case CMD_noautocmd:
2854 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 case CMD_perl:
2856 case CMD_psearch:
2857 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002858 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002859 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 case CMD_return:
2861 case CMD_rightbelow:
2862 case CMD_ruby:
2863 case CMD_silent:
2864 case CMD_smagic:
2865 case CMD_snomagic:
2866 case CMD_substitute:
2867 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002868 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 case CMD_tcl:
2870 case CMD_throw:
2871 case CMD_tilde:
2872 case CMD_topleft:
2873 case CMD_unlet:
2874 case CMD_verbose:
2875 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002876 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 break;
2878
2879 default: goto doend;
2880 }
2881 }
2882#endif
2883
2884 if (ea.argt & XFILE)
2885 {
2886 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2887 goto doend;
2888 }
2889
2890#ifdef FEAT_LISTCMDS
2891 /*
2892 * Accept buffer name. Cannot be used at the same time with a buffer
2893 * number. Don't do this for a user command.
2894 */
2895 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002896 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 {
2898 /*
2899 * :bdelete, :bwipeout and :bunload take several arguments, separated
2900 * by spaces: find next space (skipping over escaped characters).
2901 * The others take one argument: ignore trailing spaces.
2902 */
2903 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2904 || ea.cmdidx == CMD_bunload)
2905 p = skiptowhite_esc(ea.arg);
2906 else
2907 {
2908 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002909 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 --p;
2911 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002912 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2913 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 if (ea.line2 < 0) /* failed */
2915 goto doend;
2916 ea.addr_count = 1;
2917 ea.arg = skipwhite(p);
2918 }
2919#endif
2920
2921/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002922 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 *
2924 * The "ea" structure holds the arguments that can be used.
2925 */
2926 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002927 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 ea.cookie = cookie;
2929#ifdef FEAT_EVAL
2930 ea.cstack = cstack;
2931#endif
2932
2933#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002934 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 {
2936 /*
2937 * Execute a user-defined command.
2938 */
2939 do_ucmd(&ea);
2940 }
2941 else
2942#endif
2943 {
2944 /*
2945 * Call the function to execute the command.
2946 */
2947 ea.errmsg = NULL;
2948 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2949 if (ea.errmsg != NULL)
2950 errormsg = (char_u *)_(ea.errmsg);
2951 }
2952
2953#ifdef FEAT_EVAL
2954 /*
2955 * If the command just executed called do_cmdline(), any throw or ":return"
2956 * or ":finish" encountered there must also check the cstack of the still
2957 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2958 * exception, or reanimate a returned function or finished script file and
2959 * return or finish it again.
2960 */
2961 if (need_rethrow)
2962 do_throw(cstack);
2963 else if (check_cstack)
2964 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002965 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002967 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 && current_func_returned())
2969 do_return(&ea, TRUE, FALSE, NULL);
2970 }
2971 need_rethrow = check_cstack = FALSE;
2972#endif
2973
2974doend:
2975 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002976 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002978 curwin->w_cursor.col = 0;
2979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980
2981 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2982 {
2983 if (sourcing)
2984 {
2985 if (errormsg != IObuff)
2986 {
2987 STRCPY(IObuff, errormsg);
2988 errormsg = IObuff;
2989 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002990 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 }
2992 emsg(errormsg);
2993 }
2994#ifdef FEAT_EVAL
2995 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002996 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2997 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998#endif
2999
3000 if (verbose_save >= 0)
3001 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003002#ifdef FEAT_AUTOCMD
3003 if (cmdmod.save_ei != NULL)
3004 {
3005 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003006 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
3007 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003008 free_string_option(cmdmod.save_ei);
3009 }
3010#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003011 if (cmdmod.filter_regmatch.regprog != NULL)
3012 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013
3014 cmdmod = save_cmdmod;
3015
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003016 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 {
3018 /* messages could be enabled for a serious error, need to check if the
3019 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00003020 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003021 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 emsg_silent -= did_esilent;
3023 if (emsg_silent < 0)
3024 emsg_silent = 0;
3025 /* Restore msg_scroll, it's set by file I/O commands, even when no
3026 * message is actually displayed. */
3027 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003028
3029 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3030 * somewhere in the line. Put it back in the first column. */
3031 if (redirecting())
3032 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 }
3034
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003035#ifdef HAVE_SANDBOX
3036 if (did_sandbox)
3037 --sandbox;
3038#endif
3039
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3041 ea.nextcmd = NULL;
3042
3043#ifdef FEAT_EVAL
3044 --ex_nesting_level;
3045#endif
3046
3047 return ea.nextcmd;
3048}
3049#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003050 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051#endif
3052
3053/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003054 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 * If there is a match advance "pp" to the argument and return TRUE.
3056 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003057 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003058checkforcmd(
3059 char_u **pp, /* start of command */
3060 char *cmd, /* name of command */
3061 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
3063 int i;
3064
3065 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003066 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 break;
3068 if (i >= len && !isalpha((*pp)[i]))
3069 {
3070 *pp = skipwhite(*pp + i);
3071 return TRUE;
3072 }
3073 return FALSE;
3074}
3075
3076/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003077 * Append "cmd" to the error message in IObuff.
3078 * Takes care of limiting the length and handling 0xa0, which would be
3079 * invisible otherwise.
3080 */
3081 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003082append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003083{
3084 char_u *s = cmd;
3085 char_u *d;
3086
3087 STRCAT(IObuff, ": ");
3088 d = IObuff + STRLEN(IObuff);
3089 while (*s != NUL && d - IObuff < IOSIZE - 7)
3090 {
3091 if (
3092#ifdef FEAT_MBYTE
3093 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3094#endif
3095 *s == 0xa0)
3096 {
3097 s +=
3098#ifdef FEAT_MBYTE
3099 enc_utf8 ? 2 :
3100#endif
3101 1;
3102 STRCPY(d, "<a0>");
3103 d += 4;
3104 }
3105 else
3106 MB_COPY_CHAR(s, d);
3107 }
3108 *d = NUL;
3109}
3110
3111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003113 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003115 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 * Returns NULL for an ambiguous user command.
3117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003119find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120{
3121 int len;
3122 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003123 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124
3125 /*
3126 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003127 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 * - the 'k' command can directly be followed by any character.
3129 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003130 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003132 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 */
3134 p = eap->cmd;
3135 if (*p == 'k')
3136 {
3137 eap->cmdidx = CMD_k;
3138 ++p;
3139 }
3140 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003141 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3142 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 || p[1] == 'g'
3144 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3145 || p[1] == 'I'
3146 || (p[1] == 'r' && p[2] != 'e')))
3147 {
3148 eap->cmdidx = CMD_substitute;
3149 ++p;
3150 }
3151 else
3152 {
3153 while (ASCII_ISALPHA(*p))
3154 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003155 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003156 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003157 while (ASCII_ISALNUM(*p))
3158 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003159
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 /* check for non-alpha command */
3161 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3162 ++p;
3163 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003164 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3165 {
3166 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3167 * :delete with the 'l' flag. Same for 'p'. */
3168 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003169 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003170 break;
3171 if (i == len - 1)
3172 {
3173 --len;
3174 if (p[-1] == 'l')
3175 eap->flags |= EXFLAG_LIST;
3176 else
3177 eap->flags |= EXFLAG_PRINT;
3178 }
3179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003181 if (ASCII_ISLOWER(eap->cmd[0]))
3182 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003183 int c1 = eap->cmd[0];
3184 int c2 = eap->cmd[1];
3185
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003186 if (command_count != (int)CMD_SIZE)
3187 {
3188 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3189 getout(1);
3190 }
3191
3192 /* Use a precomputed index for fast look-up in cmdnames[]
3193 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003194 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3195 if (ASCII_ISLOWER(c2))
3196 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003199 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200
3201 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3202 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3203 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3204 (size_t)len) == 0)
3205 {
3206#ifdef FEAT_EVAL
3207 if (full != NULL
3208 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3209 *full = TRUE;
3210#endif
3211 break;
3212 }
3213
3214#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003215 /* Look for a user defined command as a last resort. Let ":Print" be
3216 * overruled by a user defined command. */
3217 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3218 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003220 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 while (ASCII_ISALNUM(*p))
3222 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003223 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 }
3225#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003226 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 eap->cmdidx = CMD_SIZE;
3228 }
3229
3230 return p;
3231}
3232
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003233#ifdef FEAT_USR_CMDS
3234/*
3235 * Search for a user command that matches "eap->cmd".
3236 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3237 * Return a pointer to just after the command.
3238 * Return NULL if there is no matching command.
3239 */
3240 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003241find_ucmd(
3242 exarg_T *eap,
3243 char_u *p, /* end of the command (possibly including count) */
3244 int *full, /* set to TRUE for a full match */
3245 expand_T *xp, /* used for completion, NULL otherwise */
3246 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003247{
3248 int len = (int)(p - eap->cmd);
3249 int j, k, matchlen = 0;
3250 ucmd_T *uc;
3251 int found = FALSE;
3252 int possible = FALSE;
3253 char_u *cp, *np; /* Point into typed cmd and test name */
3254 garray_T *gap;
3255 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3256 only full match global is accepted. */
3257
3258 /*
3259 * Look for buffer-local user commands first, then global ones.
3260 */
3261 gap = &curbuf->b_ucmds;
3262 for (;;)
3263 {
3264 for (j = 0; j < gap->ga_len; ++j)
3265 {
3266 uc = USER_CMD_GA(gap, j);
3267 cp = eap->cmd;
3268 np = uc->uc_name;
3269 k = 0;
3270 while (k < len && *np != NUL && *cp++ == *np++)
3271 k++;
3272 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3273 {
3274 /* If finding a second match, the command is ambiguous. But
3275 * not if a buffer-local command wasn't a full match and a
3276 * global command is a full match. */
3277 if (k == len && found && *np != NUL)
3278 {
3279 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003280 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003281 amb_local = TRUE;
3282 }
3283
3284 if (!found || (k == len && *np == NUL))
3285 {
3286 /* If we matched up to a digit, then there could
3287 * be another command including the digit that we
3288 * should use instead.
3289 */
3290 if (k == len)
3291 found = TRUE;
3292 else
3293 possible = TRUE;
3294
3295 if (gap == &ucmds)
3296 eap->cmdidx = CMD_USER;
3297 else
3298 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003299 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003300 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003301 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003302
3303# ifdef FEAT_CMDL_COMPL
3304 if (compl != NULL)
3305 *compl = uc->uc_compl;
3306# ifdef FEAT_EVAL
3307 if (xp != NULL)
3308 {
3309 xp->xp_arg = uc->uc_compl_arg;
3310 xp->xp_scriptID = uc->uc_scriptID;
3311 }
3312# endif
3313# endif
3314 /* Do not search for further abbreviations
3315 * if this is an exact match. */
3316 matchlen = k;
3317 if (k == len && *np == NUL)
3318 {
3319 if (full != NULL)
3320 *full = TRUE;
3321 amb_local = FALSE;
3322 break;
3323 }
3324 }
3325 }
3326 }
3327
3328 /* Stop if we found a full match or searched all. */
3329 if (j < gap->ga_len || gap == &ucmds)
3330 break;
3331 gap = &ucmds;
3332 }
3333
3334 /* Only found ambiguous matches. */
3335 if (amb_local)
3336 {
3337 if (xp != NULL)
3338 xp->xp_context = EXPAND_UNSUCCESSFUL;
3339 return NULL;
3340 }
3341
3342 /* The match we found may be followed immediately by a number. Move "p"
3343 * back to point to it. */
3344 if (found || possible)
3345 return p + (matchlen - len);
3346 return p;
3347}
3348#endif
3349
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003351static struct cmdmod
3352{
3353 char *name;
3354 int minlen;
3355 int has_count; /* :123verbose :3tab */
3356} cmdmods[] = {
3357 {"aboveleft", 3, FALSE},
3358 {"belowright", 3, FALSE},
3359 {"botright", 2, FALSE},
3360 {"browse", 3, FALSE},
3361 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003362 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003363 {"hide", 3, FALSE},
3364 {"keepalt", 5, FALSE},
3365 {"keepjumps", 5, FALSE},
3366 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003367 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003368 {"leftabove", 5, FALSE},
3369 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003370 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003371 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003372 {"rightbelow", 6, FALSE},
3373 {"sandbox", 3, FALSE},
3374 {"silent", 3, FALSE},
3375 {"tab", 3, TRUE},
3376 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003377 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003378 {"verbose", 4, TRUE},
3379 {"vertical", 4, FALSE},
3380};
3381
3382/*
3383 * Return length of a command modifier (including optional count).
3384 * Return zero when it's not a modifier.
3385 */
3386 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003387modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003388{
3389 int i, j;
3390 char_u *p = cmd;
3391
3392 if (VIM_ISDIGIT(*cmd))
3393 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003394 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003395 {
3396 for (j = 0; p[j] != NUL; ++j)
3397 if (p[j] != cmdmods[i].name[j])
3398 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003399 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003400 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003401 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003402 }
3403 return 0;
3404}
3405
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406/*
3407 * Return > 0 if an Ex command "name" exists.
3408 * Return 2 if there is an exact match.
3409 * Return 3 if there is an ambiguous match.
3410 */
3411 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003412cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413{
3414 exarg_T ea;
3415 int full = FALSE;
3416 int i;
3417 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003418 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
3420 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003421 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 {
3423 for (j = 0; name[j] != NUL; ++j)
3424 if (name[j] != cmdmods[i].name[j])
3425 break;
3426 if (name[j] == NUL && j >= cmdmods[i].minlen)
3427 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3428 }
3429
Bram Moolenaara9587612006-05-04 21:47:50 +00003430 /* Check built-in commands and user defined commands.
3431 * For ":2match" and ":3match" we need to skip the number. */
3432 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003434 p = find_command(&ea, &full);
3435 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003437 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3438 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003439 if (*skipwhite(p) != NUL)
3440 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3442}
3443#endif
3444
3445/*
3446 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3447 * we don't need/want deleted. Maybe this could be done better if we didn't
3448 * repeat all this stuff. The only problem is that they may not stay
3449 * perfectly compatible with each other, but then the command line syntax
3450 * probably won't change that much -- webb.
3451 */
3452 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003453set_one_cmd_context(
3454 expand_T *xp,
3455 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456{
3457 char_u *p;
3458 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003459 int len = 0;
3460 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3462 int compl = EXPAND_NOTHING;
3463#endif
3464#ifdef FEAT_CMDL_COMPL
3465 int delim;
3466#endif
3467 int forceit = FALSE;
3468 int usefilter = FALSE; /* filter instead of file name */
3469
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003470 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 xp->xp_pattern = buff;
3472 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003473 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
3475/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003476 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 */
3478 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3479 ;
3480 xp->xp_pattern = cmd;
3481
3482 if (*cmd == NUL)
3483 return NULL;
3484 if (*cmd == '"') /* ignore comment lines */
3485 {
3486 xp->xp_context = EXPAND_NOTHING;
3487 return NULL;
3488 }
3489
3490/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003491 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 */
3493 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 xp->xp_pattern = cmd;
3495 if (*cmd == NUL)
3496 return NULL;
3497 if (*cmd == '"')
3498 {
3499 xp->xp_context = EXPAND_NOTHING;
3500 return NULL;
3501 }
3502
3503 if (*cmd == '|' || *cmd == '\n')
3504 return cmd + 1; /* There's another command */
3505
3506 /*
3507 * Isolate the command and search for it in the command table.
3508 * Exceptions:
3509 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003510 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3512 */
3513 if (*cmd == 'k' && cmd[1] != 'e')
3514 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003515 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 p = cmd + 1;
3517 }
3518 else
3519 {
3520 p = cmd;
3521 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3522 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003523 /* a user command may contain digits */
3524 if (ASCII_ISUPPER(cmd[0]))
3525 while (ASCII_ISALNUM(*p) || *p == '*')
3526 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003527 /* for python 3.x: ":py3*" commands completion */
3528 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003529 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003530 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003531 while (ASCII_ISALPHA(*p) || *p == '*')
3532 ++p;
3533 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003534 /* check for non-alpha command */
3535 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3536 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003537 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003539 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
3541 xp->xp_context = EXPAND_UNSUCCESSFUL;
3542 return NULL;
3543 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003544 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003545 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3546 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3547 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 break;
3549
3550#ifdef FEAT_USR_CMDS
3551 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3553 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554#endif
3555 }
3556
3557 /*
3558 * If the cursor is touching the command, and it ends in an alpha-numeric
3559 * character, complete the command name.
3560 */
3561 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3562 return NULL;
3563
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003564 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
3566 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3567 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003568 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 p = cmd + 1;
3570 }
3571#ifdef FEAT_USR_CMDS
3572 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3573 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003574 ea.cmd = cmd;
3575 p = find_ucmd(&ea, p, NULL, xp,
3576# if defined(FEAT_CMDL_COMPL)
3577 &compl
3578# else
3579 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003581 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003582 if (p == NULL)
3583 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 }
3585#endif
3586 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003587 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
3589 /* Not still touching the command and it was an illegal one */
3590 xp->xp_context = EXPAND_UNSUCCESSFUL;
3591 return NULL;
3592 }
3593
3594 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3595
3596 if (*p == '!') /* forced commands */
3597 {
3598 forceit = TRUE;
3599 ++p;
3600 }
3601
3602/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003603 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003605 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003606 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607
3608 arg = skipwhite(p);
3609
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003610 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 {
3612 if (*arg == '>') /* append */
3613 {
3614 if (*++arg == '>')
3615 ++arg;
3616 arg = skipwhite(arg);
3617 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003618 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 {
3620 ++arg;
3621 usefilter = TRUE;
3622 }
3623 }
3624
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003625 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 {
3627 usefilter = forceit; /* :r! filter if forced */
3628 if (*arg == '!') /* :r !filter */
3629 {
3630 ++arg;
3631 usefilter = TRUE;
3632 }
3633 }
3634
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003635 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 {
3637 while (*arg == *cmd) /* allow any number of '>' or '<' */
3638 ++arg;
3639 arg = skipwhite(arg);
3640 }
3641
3642 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003643 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 {
3645 /* Check if we're in the +command */
3646 p = arg + 1;
3647 arg = skip_cmd_arg(arg, FALSE);
3648
3649 /* Still touching the command after '+'? */
3650 if (*arg == NUL)
3651 return p;
3652
3653 /* Skip space(s) after +command to get to the real argument */
3654 arg = skipwhite(arg);
3655 }
3656
3657 /*
3658 * Check for '|' to separate commands and '"' to start comments.
3659 * Don't do this for ":read !cmd" and ":write !cmd".
3660 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003661 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
3663 p = arg;
3664 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003665 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 p += 2;
3667 while (*p)
3668 {
3669 if (*p == Ctrl_V)
3670 {
3671 if (p[1] != NUL)
3672 ++p;
3673 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003674 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 || *p == '|' || *p == '\n')
3676 {
3677 if (*(p - 1) != '\\')
3678 {
3679 if (*p == '|' || *p == '\n')
3680 return p + 1;
3681 return NULL; /* It's a comment */
3682 }
3683 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003684 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 }
3686 }
3687
3688 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003689 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 vim_strchr((char_u *)"|\"", *arg) == NULL)
3691 return NULL;
3692
3693 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003694 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003696 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003697 while (*p && p < buff + len)
3698 {
3699 if (*p == ' ' || *p == TAB)
3700 {
3701 /* argument starts after a space */
3702 xp->xp_pattern = ++p;
3703 }
3704 else
3705 {
3706 if (*p == '\\' && *(p + 1) != NUL)
3707 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003708 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003709 }
3710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003712 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003714 int c;
3715 int in_quote = FALSE;
3716 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
3718 /*
3719 * Allow spaces within back-quotes to count as part of the argument
3720 * being expanded.
3721 */
3722 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003723 p = xp->xp_pattern;
3724 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003726#ifdef FEAT_MBYTE
3727 if (has_mbyte)
3728 c = mb_ptr2char(p);
3729 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003731 c = *p;
3732 if (c == '\\' && p[1] != NUL)
3733 ++p;
3734 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 {
3736 if (!in_quote)
3737 {
3738 xp->xp_pattern = p;
3739 bow = p + 1;
3740 }
3741 in_quote = !in_quote;
3742 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003743 /* An argument can contain just about everything, except
3744 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003745 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003746#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003747 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003748#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003749 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003750 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003751 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003752 while (*p != NUL)
3753 {
3754#ifdef FEAT_MBYTE
3755 if (has_mbyte)
3756 c = mb_ptr2char(p);
3757 else
3758#endif
3759 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003760 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003761 break;
3762#ifdef FEAT_MBYTE
3763 if (has_mbyte)
3764 len = (*mb_ptr2len)(p);
3765 else
3766#endif
3767 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003768 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003769 }
3770 if (in_quote)
3771 bow = p;
3772 else
3773 xp->xp_pattern = p;
3774 p -= len;
3775 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003776 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778
3779 /*
3780 * If we are still inside the quotes, and we passed a space, just
3781 * expand from there.
3782 */
3783 if (bow != NULL && in_quote)
3784 xp->xp_pattern = bow;
3785 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003786
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003787 /* For a shell command more chars need to be escaped. */
3788 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003789 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003790#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003791 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003792#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003793 /* When still after the command name expand executables. */
3794 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003795 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797
3798 /* Check for environment variable */
3799 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003800#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 || *xp->xp_pattern == '%'
3802#endif
3803 )
3804 {
3805 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3806 if (!vim_isIDc(*p))
3807 break;
3808 if (*p == NUL)
3809 {
3810 xp->xp_context = EXPAND_ENV_VARS;
3811 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003812#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3813 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003814 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003815 compl = EXPAND_ENV_VARS;
3816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 }
3818 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003819#if defined(FEAT_CMDL_COMPL)
3820 /* Check for user names */
3821 if (*xp->xp_pattern == '~')
3822 {
3823 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3824 ;
3825 /* Complete ~user only if it partially matches a user name.
3826 * A full match ~user<Tab> will be replaced by user's home
3827 * directory i.e. something like ~user<Tab> -> /home/user/ */
3828 if (*p == NUL && p > xp->xp_pattern + 1
3829 && match_user(xp->xp_pattern + 1) == 1)
3830 {
3831 xp->xp_context = EXPAND_USER;
3832 ++xp->xp_pattern;
3833 }
3834 }
3835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837
3838/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003839 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003841 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003843 case CMD_find:
3844 case CMD_sfind:
3845 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003846 if (xp->xp_context == EXPAND_FILES)
3847 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003848 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 case CMD_cd:
3850 case CMD_chdir:
3851 case CMD_lcd:
3852 case CMD_lchdir:
3853 if (xp->xp_context == EXPAND_FILES)
3854 xp->xp_context = EXPAND_DIRECTORIES;
3855 break;
3856 case CMD_help:
3857 xp->xp_context = EXPAND_HELP;
3858 xp->xp_pattern = arg;
3859 break;
3860
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003861 /* Command modifiers: return the argument.
3862 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003864 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 case CMD_belowright:
3866 case CMD_botright:
3867 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003868 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003869 case CMD_cdo:
3870 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003872 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 case CMD_folddoclosed:
3874 case CMD_folddoopen:
3875 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003876 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 case CMD_keepjumps:
3878 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003879 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003880 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003882 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003884 case CMD_noautocmd:
3885 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003887 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003889 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003890 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 case CMD_topleft:
3892 case CMD_verbose:
3893 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003894 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 return arg;
3896
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003897 case CMD_filter:
3898 if (*arg != NUL)
3899 arg = skip_vimgrep_pat(arg, NULL, NULL);
3900 if (arg == NULL || *arg == NUL)
3901 {
3902 xp->xp_context = EXPAND_NOTHING;
3903 return NULL;
3904 }
3905 return skipwhite(arg);
3906
Bram Moolenaar4f688582007-07-24 12:34:30 +00003907#ifdef FEAT_CMDL_COMPL
3908# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 case CMD_match:
3910 if (*arg == NUL || !ends_excmd(*arg))
3911 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003912 /* also complete "None" */
3913 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 arg = skipwhite(skiptowhite(arg));
3915 if (*arg != NUL)
3916 {
3917 xp->xp_context = EXPAND_NOTHING;
3918 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3919 }
3920 }
3921 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924/*
3925 * All completion for the +cmdline_compl feature goes here.
3926 */
3927
3928# ifdef FEAT_USR_CMDS
3929 case CMD_command:
3930 /* Check for attributes */
3931 while (*arg == '-')
3932 {
3933 arg++; /* Skip "-" */
3934 p = skiptowhite(arg);
3935 if (*p == NUL)
3936 {
3937 /* Cursor is still in the attribute */
3938 p = vim_strchr(arg, '=');
3939 if (p == NULL)
3940 {
3941 /* No "=", so complete attribute names */
3942 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3943 xp->xp_pattern = arg;
3944 return NULL;
3945 }
3946
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003947 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 * their arguments as well.
3949 */
3950 if (STRNICMP(arg, "complete", p - arg) == 0)
3951 {
3952 xp->xp_context = EXPAND_USER_COMPLETE;
3953 xp->xp_pattern = p + 1;
3954 return NULL;
3955 }
3956 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3957 {
3958 xp->xp_context = EXPAND_USER_NARGS;
3959 xp->xp_pattern = p + 1;
3960 return NULL;
3961 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003962 else if (STRNICMP(arg, "addr", p - arg) == 0)
3963 {
3964 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3965 xp->xp_pattern = p + 1;
3966 return NULL;
3967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 return NULL;
3969 }
3970 arg = skipwhite(p);
3971 }
3972
3973 /* After the attributes comes the new command name */
3974 p = skiptowhite(arg);
3975 if (*p == NUL)
3976 {
3977 xp->xp_context = EXPAND_USER_COMMANDS;
3978 xp->xp_pattern = arg;
3979 break;
3980 }
3981
3982 /* And finally comes a normal command */
3983 return skipwhite(p);
3984
3985 case CMD_delcommand:
3986 xp->xp_context = EXPAND_USER_COMMANDS;
3987 xp->xp_pattern = arg;
3988 break;
3989# endif
3990
3991 case CMD_global:
3992 case CMD_vglobal:
3993 delim = *arg; /* get the delimiter */
3994 if (delim)
3995 ++arg; /* skip delimiter if there is one */
3996
3997 while (arg[0] != NUL && arg[0] != delim)
3998 {
3999 if (arg[0] == '\\' && arg[1] != NUL)
4000 ++arg;
4001 ++arg;
4002 }
4003 if (arg[0] != NUL)
4004 return arg + 1;
4005 break;
4006 case CMD_and:
4007 case CMD_substitute:
4008 delim = *arg;
4009 if (delim)
4010 {
4011 /* skip "from" part */
4012 ++arg;
4013 arg = skip_regexp(arg, delim, p_magic, NULL);
4014 }
4015 /* skip "to" part */
4016 while (arg[0] != NUL && arg[0] != delim)
4017 {
4018 if (arg[0] == '\\' && arg[1] != NUL)
4019 ++arg;
4020 ++arg;
4021 }
4022 if (arg[0] != NUL) /* skip delimiter */
4023 ++arg;
4024 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4025 ++arg;
4026 if (arg[0] != NUL)
4027 return arg;
4028 break;
4029 case CMD_isearch:
4030 case CMD_dsearch:
4031 case CMD_ilist:
4032 case CMD_dlist:
4033 case CMD_ijump:
4034 case CMD_psearch:
4035 case CMD_djump:
4036 case CMD_isplit:
4037 case CMD_dsplit:
4038 arg = skipwhite(skipdigits(arg)); /* skip count */
4039 if (*arg == '/') /* Match regexp, not just whole words */
4040 {
4041 for (++arg; *arg && *arg != '/'; arg++)
4042 if (*arg == '\\' && arg[1] != NUL)
4043 arg++;
4044 if (*arg)
4045 {
4046 arg = skipwhite(arg + 1);
4047
4048 /* Check for trailing illegal characters */
4049 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4050 xp->xp_context = EXPAND_NOTHING;
4051 else
4052 return arg;
4053 }
4054 }
4055 break;
4056#ifdef FEAT_AUTOCMD
4057 case CMD_autocmd:
4058 return set_context_in_autocmd(xp, arg, FALSE);
4059
4060 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004061 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 return set_context_in_autocmd(xp, arg, TRUE);
4063#endif
4064 case CMD_set:
4065 set_context_in_set_cmd(xp, arg, 0);
4066 break;
4067 case CMD_setglobal:
4068 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4069 break;
4070 case CMD_setlocal:
4071 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4072 break;
4073 case CMD_tag:
4074 case CMD_stag:
4075 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004076 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 case CMD_tselect:
4078 case CMD_stselect:
4079 case CMD_ptselect:
4080 case CMD_tjump:
4081 case CMD_stjump:
4082 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004083 if (*p_wop != NUL)
4084 xp->xp_context = EXPAND_TAGS_LISTFILES;
4085 else
4086 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 xp->xp_pattern = arg;
4088 break;
4089 case CMD_augroup:
4090 xp->xp_context = EXPAND_AUGROUP;
4091 xp->xp_pattern = arg;
4092 break;
4093#ifdef FEAT_SYN_HL
4094 case CMD_syntax:
4095 set_context_in_syntax_cmd(xp, arg);
4096 break;
4097#endif
4098#ifdef FEAT_EVAL
4099 case CMD_let:
4100 case CMD_if:
4101 case CMD_elseif:
4102 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004103 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 case CMD_echo:
4105 case CMD_echon:
4106 case CMD_execute:
4107 case CMD_echomsg:
4108 case CMD_echoerr:
4109 case CMD_call:
4110 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004111 case CMD_cexpr:
4112 case CMD_caddexpr:
4113 case CMD_cgetexpr:
4114 case CMD_lexpr:
4115 case CMD_laddexpr:
4116 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004117 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 break;
4119
4120 case CMD_unlet:
4121 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4122 arg = xp->xp_pattern + 1;
4123 xp->xp_context = EXPAND_USER_VARS;
4124 xp->xp_pattern = arg;
4125 break;
4126
4127 case CMD_function:
4128 case CMD_delfunction:
4129 xp->xp_context = EXPAND_USER_FUNC;
4130 xp->xp_pattern = arg;
4131 break;
4132
4133 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004134 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 break;
4136#endif
4137 case CMD_highlight:
4138 set_context_in_highlight_cmd(xp, arg);
4139 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004140#ifdef FEAT_CSCOPE
4141 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004142 case CMD_lcscope:
4143 case CMD_scscope:
4144 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004145 break;
4146#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004147#ifdef FEAT_SIGNS
4148 case CMD_sign:
4149 set_context_in_sign_cmd(xp, arg);
4150 break;
4151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152#ifdef FEAT_LISTCMDS
4153 case CMD_bdelete:
4154 case CMD_bwipeout:
4155 case CMD_bunload:
4156 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4157 arg = xp->xp_pattern + 1;
4158 /*FALLTHROUGH*/
4159 case CMD_buffer:
4160 case CMD_sbuffer:
4161 case CMD_checktime:
4162 xp->xp_context = EXPAND_BUFFERS;
4163 xp->xp_pattern = arg;
4164 break;
4165#endif
4166#ifdef FEAT_USR_CMDS
4167 case CMD_USER:
4168 case CMD_USER_BUF:
4169 if (compl != EXPAND_NOTHING)
4170 {
4171 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004172 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 {
4174# ifdef FEAT_MENU
4175 if (compl == EXPAND_MENUS)
4176 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4177# endif
4178 if (compl == EXPAND_COMMANDS)
4179 return arg;
4180 if (compl == EXPAND_MAPPINGS)
4181 return set_context_in_map_cmd(xp, (char_u *)"map",
4182 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004183 /* Find start of last argument. */
4184 p = arg;
4185 while (*p)
4186 {
4187 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004188 /* argument starts after a space */
4189 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004190 else if (*p == '\\' && *(p + 1) != NUL)
4191 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004192 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 xp->xp_pattern = arg;
4195 }
4196 xp->xp_context = compl;
4197 }
4198 break;
4199#endif
4200 case CMD_map: case CMD_noremap:
4201 case CMD_nmap: case CMD_nnoremap:
4202 case CMD_vmap: case CMD_vnoremap:
4203 case CMD_omap: case CMD_onoremap:
4204 case CMD_imap: case CMD_inoremap:
4205 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004206 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004207 case CMD_smap: case CMD_snoremap:
4208 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004210 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 case CMD_unmap:
4212 case CMD_nunmap:
4213 case CMD_vunmap:
4214 case CMD_ounmap:
4215 case CMD_iunmap:
4216 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004217 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004218 case CMD_sunmap:
4219 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004221 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 case CMD_abbreviate: case CMD_noreabbrev:
4223 case CMD_cabbrev: case CMD_cnoreabbrev:
4224 case CMD_iabbrev: case CMD_inoreabbrev:
4225 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004226 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 case CMD_unabbreviate:
4228 case CMD_cunabbrev:
4229 case CMD_iunabbrev:
4230 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004231 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232#ifdef FEAT_MENU
4233 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4234 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4235 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4236 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4237 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4238 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4239 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4240 case CMD_tmenu: case CMD_tunmenu:
4241 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4242 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4243#endif
4244
4245 case CMD_colorscheme:
4246 xp->xp_context = EXPAND_COLORS;
4247 xp->xp_pattern = arg;
4248 break;
4249
4250 case CMD_compiler:
4251 xp->xp_context = EXPAND_COMPILER;
4252 xp->xp_pattern = arg;
4253 break;
4254
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004255 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004256 xp->xp_context = EXPAND_OWNSYNTAX;
4257 xp->xp_pattern = arg;
4258 break;
4259
4260 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004261 xp->xp_context = EXPAND_FILETYPE;
4262 xp->xp_pattern = arg;
4263 break;
4264
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004265 case CMD_packadd:
4266 xp->xp_context = EXPAND_PACKADD;
4267 xp->xp_pattern = arg;
4268 break;
4269
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4271 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4272 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004273 p = skiptowhite(arg);
4274 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 {
4276 xp->xp_context = EXPAND_LANGUAGE;
4277 xp->xp_pattern = arg;
4278 }
4279 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004280 {
4281 if ( STRNCMP(arg, "messages", p - arg) == 0
4282 || STRNCMP(arg, "ctype", p - arg) == 0
4283 || STRNCMP(arg, "time", p - arg) == 0)
4284 {
4285 xp->xp_context = EXPAND_LOCALES;
4286 xp->xp_pattern = skipwhite(p);
4287 }
4288 else
4289 xp->xp_context = EXPAND_NOTHING;
4290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 break;
4292#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004293#if defined(FEAT_PROFILE)
4294 case CMD_profile:
4295 set_context_in_profile_cmd(xp, arg);
4296 break;
4297#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004298 case CMD_behave:
4299 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004300 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004301 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004303 case CMD_messages:
4304 xp->xp_context = EXPAND_MESSAGES;
4305 xp->xp_pattern = arg;
4306 break;
4307
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004308#if defined(FEAT_CMDHIST)
4309 case CMD_history:
4310 xp->xp_context = EXPAND_HISTORY;
4311 xp->xp_pattern = arg;
4312 break;
4313#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004314#if defined(FEAT_PROFILE)
4315 case CMD_syntime:
4316 xp->xp_context = EXPAND_SYNTIME;
4317 xp->xp_pattern = arg;
4318 break;
4319#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004320
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321#endif /* FEAT_CMDL_COMPL */
4322
4323 default:
4324 break;
4325 }
4326 return NULL;
4327}
4328
4329/*
4330 * skip a range specifier of the form: addr [,addr] [;addr] ..
4331 *
4332 * Backslashed delimiters after / or ? will be skipped, and commands will
4333 * not be expanded between /'s and ?'s or after "'".
4334 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004335 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 * Returns the "cmd" pointer advanced to beyond the range.
4337 */
4338 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004339skip_range(
4340 char_u *cmd,
4341 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004343 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004345 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004347 if (*cmd == '\\')
4348 {
4349 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4350 ++cmd;
4351 else
4352 break;
4353 }
4354 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 {
4356 if (*++cmd == NUL && ctx != NULL)
4357 *ctx = EXPAND_NOTHING;
4358 }
4359 else if (*cmd == '/' || *cmd == '?')
4360 {
4361 delim = *cmd++;
4362 while (*cmd != NUL && *cmd != delim)
4363 if (*cmd++ == '\\' && *cmd != NUL)
4364 ++cmd;
4365 if (*cmd == NUL && ctx != NULL)
4366 *ctx = EXPAND_NOTHING;
4367 }
4368 if (*cmd != NUL)
4369 ++cmd;
4370 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004371
4372 /* Skip ":" and white space. */
4373 while (*cmd == ':')
4374 cmd = skipwhite(cmd + 1);
4375
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 return cmd;
4377}
4378
4379/*
4380 * get a single EX address
4381 *
4382 * Set ptr to the next character after the part that was interpreted.
4383 * Set ptr to NULL when an error is encountered.
4384 *
4385 * Return MAXLNUM when no Ex address was found.
4386 */
4387 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004388get_address(
4389 exarg_T *eap UNUSED,
4390 char_u **ptr,
4391 int addr_type, /* flag: one of ADDR_LINES, ... */
4392 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004393 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004394 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395{
4396 int c;
4397 int i;
4398 long n;
4399 char_u *cmd;
4400 pos_T pos;
4401 pos_T *fp;
4402 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004403 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
4405 cmd = skipwhite(*ptr);
4406 lnum = MAXLNUM;
4407 do
4408 {
4409 switch (*cmd)
4410 {
4411 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004412 ++cmd;
4413 switch (addr_type)
4414 {
4415 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 lnum = curwin->w_cursor.lnum;
4417 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004418 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004419 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004420 break;
4421 case ADDR_ARGUMENTS:
4422 lnum = curwin->w_arg_idx + 1;
4423 break;
4424 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004425 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004426 lnum = curbuf->b_fnum;
4427 break;
4428 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004429 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004430 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004431 case ADDR_TABS_RELATIVE:
4432 EMSG(_(e_invrange));
4433 cmd = NULL;
4434 goto error;
4435 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004436#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004437 case ADDR_QUICKFIX:
4438 lnum = qf_get_cur_valid_idx(eap);
4439 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004440#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004441 }
4442 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
4444 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004445 ++cmd;
4446 switch (addr_type)
4447 {
4448 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 lnum = curbuf->b_ml.ml_line_count;
4450 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004451 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004452 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004453 break;
4454 case ADDR_ARGUMENTS:
4455 lnum = ARGCOUNT;
4456 break;
4457 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004458 buf = lastbuf;
4459 while (buf->b_ml.ml_mfp == NULL)
4460 {
4461 if (buf->b_prev == NULL)
4462 break;
4463 buf = buf->b_prev;
4464 }
4465 lnum = buf->b_fnum;
4466 break;
4467 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004468 lnum = lastbuf->b_fnum;
4469 break;
4470 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004471 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004472 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004473 case ADDR_TABS_RELATIVE:
4474 EMSG(_(e_invrange));
4475 cmd = NULL;
4476 goto error;
4477 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004478#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004479 case ADDR_QUICKFIX:
4480 lnum = qf_get_size(eap);
4481 if (lnum == 0)
4482 lnum = 1;
4483 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004484#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004485 }
4486 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487
4488 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004489 if (*++cmd == NUL)
4490 {
4491 cmd = NULL;
4492 goto error;
4493 }
4494 if (addr_type != ADDR_LINES)
4495 {
4496 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004497 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004498 goto error;
4499 }
4500 if (skip)
4501 ++cmd;
4502 else
4503 {
4504 /* Only accept a mark in another file when it is
4505 * used by itself: ":'M". */
4506 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4507 ++cmd;
4508 if (fp == (pos_T *)-1)
4509 /* Jumped to another file. */
4510 lnum = curwin->w_cursor.lnum;
4511 else
4512 {
4513 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 {
4515 cmd = NULL;
4516 goto error;
4517 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004518 lnum = fp->lnum;
4519 }
4520 }
4521 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522
4523 case '/':
4524 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004525 c = *cmd++;
4526 if (addr_type != ADDR_LINES)
4527 {
4528 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004529 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004530 goto error;
4531 }
4532 if (skip) /* skip "/pat/" */
4533 {
4534 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4535 if (*cmd == c)
4536 ++cmd;
4537 }
4538 else
4539 {
4540 pos = curwin->w_cursor; /* save curwin->w_cursor */
4541 /*
4542 * When '/' or '?' follows another address, start
4543 * from there.
4544 */
4545 if (lnum != MAXLNUM)
4546 curwin->w_cursor.lnum = lnum;
4547 /*
4548 * Start a forward search at the end of the line.
4549 * Start a backward search at the start of the line.
4550 * This makes sure we never match in the current
4551 * line, and can match anywhere in the
4552 * next/previous line.
4553 */
4554 if (c == '/')
4555 curwin->w_cursor.col = MAXCOL;
4556 else
4557 curwin->w_cursor.col = 0;
4558 searchcmdlen = 0;
4559 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004560 SEARCH_HIS | SEARCH_MSG, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004561 {
4562 curwin->w_cursor = pos;
4563 cmd = NULL;
4564 goto error;
4565 }
4566 lnum = curwin->w_cursor.lnum;
4567 curwin->w_cursor = pos;
4568 /* adjust command string pointer */
4569 cmd += searchcmdlen;
4570 }
4571 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572
4573 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004574 ++cmd;
4575 if (addr_type != ADDR_LINES)
4576 {
4577 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004578 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004579 goto error;
4580 }
4581 if (*cmd == '&')
4582 i = RE_SUBST;
4583 else if (*cmd == '?' || *cmd == '/')
4584 i = RE_SEARCH;
4585 else
4586 {
4587 EMSG(_(e_backslash));
4588 cmd = NULL;
4589 goto error;
4590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004592 if (!skip)
4593 {
4594 /*
4595 * When search follows another address, start from
4596 * there.
4597 */
4598 if (lnum != MAXLNUM)
4599 pos.lnum = lnum;
4600 else
4601 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004603 /*
4604 * Start the search just like for the above
4605 * do_search().
4606 */
4607 if (*cmd != '?')
4608 pos.col = MAXCOL;
4609 else
4610 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004611#ifdef FEAT_VIRTUALEDIT
4612 pos.coladd = 0;
4613#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004614 if (searchit(curwin, curbuf, &pos,
4615 *cmd == '?' ? BACKWARD : FORWARD,
4616 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004617 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004618 lnum = pos.lnum;
4619 else
4620 {
4621 cmd = NULL;
4622 goto error;
4623 }
4624 }
4625 ++cmd;
4626 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627
4628 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004629 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4630 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 }
4632
4633 for (;;)
4634 {
4635 cmd = skipwhite(cmd);
4636 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4637 break;
4638
4639 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004640 {
4641 switch (addr_type)
4642 {
4643 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004644 /* "+1" is same as ".+1" */
4645 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004646 break;
4647 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004648 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004649 break;
4650 case ADDR_ARGUMENTS:
4651 lnum = curwin->w_arg_idx + 1;
4652 break;
4653 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004654 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004655 lnum = curbuf->b_fnum;
4656 break;
4657 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004658 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004659 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004660 case ADDR_TABS_RELATIVE:
4661 lnum = 1;
4662 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004663#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004664 case ADDR_QUICKFIX:
4665 lnum = qf_get_cur_valid_idx(eap);
4666 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004667#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004668 }
4669 }
4670
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 if (VIM_ISDIGIT(*cmd))
4672 i = '+'; /* "number" is same as "+number" */
4673 else
4674 i = *cmd++;
4675 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4676 n = 1;
4677 else
4678 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004679
4680 if (addr_type == ADDR_TABS_RELATIVE)
4681 {
4682 EMSG(_(e_invrange));
4683 cmd = NULL;
4684 goto error;
4685 }
4686 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004687 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004688 lnum = compute_buffer_local_count(
4689 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004691 {
4692#ifdef FEAT_FOLDING
4693 /* Relative line addressing, need to adjust for folded lines
4694 * now, but only do it after the first address. */
4695 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4696 && address_count >= 2)
4697 (void)hasFolding(lnum, NULL, &lnum);
4698#endif
4699 if (i == '-')
4700 lnum -= n;
4701 else
4702 lnum += n;
4703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
4705 } while (*cmd == '/' || *cmd == '?');
4706
4707error:
4708 *ptr = cmd;
4709 return lnum;
4710}
4711
4712/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004713 * Get flags from an Ex command argument.
4714 */
4715 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004716get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004717{
4718 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4719 {
4720 if (*eap->arg == 'l')
4721 eap->flags |= EXFLAG_LIST;
4722 else if (*eap->arg == 'p')
4723 eap->flags |= EXFLAG_PRINT;
4724 else
4725 eap->flags |= EXFLAG_NR;
4726 eap->arg = skipwhite(eap->arg + 1);
4727 }
4728}
4729
4730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 * Function called for command which is Not Implemented. NI!
4732 */
4733 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004734ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735{
4736 if (!eap->skip)
4737 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4738}
4739
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004740#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741/*
4742 * Function called for script command which is Not Implemented. NI!
4743 * Skips over ":perl <<EOF" constructs.
4744 */
4745 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004746ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747{
4748 if (!eap->skip)
4749 ex_ni(eap);
4750 else
4751 vim_free(script_get(eap, eap->arg));
4752}
4753#endif
4754
4755/*
4756 * Check range in Ex command for validity.
4757 * Return NULL when valid, error message when invalid.
4758 */
4759 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004760invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004762 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 if ( eap->line1 < 0
4764 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004765 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004767
4768 if (eap->argt & RANGE)
4769 {
4770 switch(eap->addr_type)
4771 {
4772 case ADDR_LINES:
4773 if (!(eap->argt & NOTADR)
4774 && eap->line2 > curbuf->b_ml.ml_line_count
4775#ifdef FEAT_DIFF
4776 + (eap->cmdidx == CMD_diffget)
4777#endif
4778 )
4779 return (char_u *)_(e_invrange);
4780 break;
4781 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004782 /* add 1 if ARGCOUNT is 0 */
4783 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004784 return (char_u *)_(e_invrange);
4785 break;
4786 case ADDR_BUFFERS:
4787 if (eap->line1 < firstbuf->b_fnum
4788 || eap->line2 > lastbuf->b_fnum)
4789 return (char_u *)_(e_invrange);
4790 break;
4791 case ADDR_LOADED_BUFFERS:
4792 buf = firstbuf;
4793 while (buf->b_ml.ml_mfp == NULL)
4794 {
4795 if (buf->b_next == NULL)
4796 return (char_u *)_(e_invrange);
4797 buf = buf->b_next;
4798 }
4799 if (eap->line1 < buf->b_fnum)
4800 return (char_u *)_(e_invrange);
4801 buf = lastbuf;
4802 while (buf->b_ml.ml_mfp == NULL)
4803 {
4804 if (buf->b_prev == NULL)
4805 return (char_u *)_(e_invrange);
4806 buf = buf->b_prev;
4807 }
4808 if (eap->line2 > buf->b_fnum)
4809 return (char_u *)_(e_invrange);
4810 break;
4811 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004812 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004813 return (char_u *)_(e_invrange);
4814 break;
4815 case ADDR_TABS:
4816 if (eap->line2 > LAST_TAB_NR)
4817 return (char_u *)_(e_invrange);
4818 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004819 case ADDR_TABS_RELATIVE:
4820 /* Do nothing */
4821 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004822#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004823 case ADDR_QUICKFIX:
4824 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4825 return (char_u *)_(e_invrange);
4826 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004827#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004828 }
4829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 return NULL;
4831}
4832
4833/*
4834 * Correct the range for zero line number, if required.
4835 */
4836 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004837correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838{
4839 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4840 {
4841 if (eap->line1 == 0)
4842 eap->line1 = 1;
4843 if (eap->line2 == 0)
4844 eap->line2 = 1;
4845 }
4846}
4847
Bram Moolenaar748bf032005-02-02 23:04:36 +00004848#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004849static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004850
4851/*
4852 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4853 * pattern. Otherwise return eap->arg.
4854 */
4855 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004856skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004857{
4858 char_u *p = eap->arg;
4859
Bram Moolenaara37420f2006-02-04 22:37:47 +00004860 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4861 || eap->cmdidx == CMD_vimgrepadd
4862 || eap->cmdidx == CMD_lvimgrepadd
4863 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004864 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004865 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004866 if (p == NULL)
4867 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004868 }
4869 return p;
4870}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004871
4872/*
4873 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4874 * in the command line, so that things like % get expanded.
4875 */
4876 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004877replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004878{
4879 char_u *new_cmdline;
4880 char_u *program;
4881 char_u *pos;
4882 char_u *ptr;
4883 int len;
4884 int i;
4885
4886 /*
4887 * Don't do it when ":vimgrep" is used for ":grep".
4888 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004889 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4890 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4891 || eap->cmdidx == CMD_grepadd
4892 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004893 && !grep_internal(eap->cmdidx))
4894 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004895 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4896 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004897 {
4898 if (*curbuf->b_p_gp == NUL)
4899 program = p_gp;
4900 else
4901 program = curbuf->b_p_gp;
4902 }
4903 else
4904 {
4905 if (*curbuf->b_p_mp == NUL)
4906 program = p_mp;
4907 else
4908 program = curbuf->b_p_mp;
4909 }
4910
4911 p = skipwhite(p);
4912
4913 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4914 {
4915 /* replace $* by given arguments */
4916 i = 1;
4917 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4918 ++i;
4919 len = (int)STRLEN(p);
4920 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4921 if (new_cmdline == NULL)
4922 return NULL; /* out of memory */
4923 ptr = new_cmdline;
4924 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4925 {
4926 i = (int)(pos - program);
4927 STRNCPY(ptr, program, i);
4928 STRCPY(ptr += i, p);
4929 ptr += len;
4930 program = pos + 2;
4931 }
4932 STRCPY(ptr, program);
4933 }
4934 else
4935 {
4936 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4937 if (new_cmdline == NULL)
4938 return NULL; /* out of memory */
4939 STRCPY(new_cmdline, program);
4940 STRCAT(new_cmdline, " ");
4941 STRCAT(new_cmdline, p);
4942 }
4943 msg_make(p);
4944
4945 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4946 vim_free(*cmdlinep);
4947 *cmdlinep = new_cmdline;
4948 p = new_cmdline;
4949 }
4950 return p;
4951}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004952#endif
4953
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954/*
4955 * Expand file name in Ex command argument.
4956 * Return FAIL for failure, OK otherwise.
4957 */
4958 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004959expand_filename(
4960 exarg_T *eap,
4961 char_u **cmdlinep,
4962 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963{
4964 int has_wildcards; /* need to expand wildcards */
4965 char_u *repl;
4966 int srclen;
4967 char_u *p;
4968 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004969 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970
Bram Moolenaar748bf032005-02-02 23:04:36 +00004971#ifdef FEAT_QUICKFIX
4972 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4973 p = skip_grep_pat(eap);
4974#else
4975 p = eap->arg;
4976#endif
4977
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 /*
4979 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4980 * the file name contains a wildcard it should not cause expanding.
4981 * (it will be expanded anyway if there is a wildcard before replacing).
4982 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004983 has_wildcards = mch_has_wildcard(p);
4984 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004986#ifdef FEAT_EVAL
4987 /* Skip over `=expr`, wildcards in it are not expanded. */
4988 if (p[0] == '`' && p[1] == '=')
4989 {
4990 p += 2;
4991 (void)skip_expr(&p);
4992 if (*p == '`')
4993 ++p;
4994 continue;
4995 }
4996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 /*
4998 * Quick check if this cannot be the start of a special string.
4999 * Also removes backslash before '%', '#' and '<'.
5000 */
5001 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5002 {
5003 ++p;
5004 continue;
5005 }
5006
5007 /*
5008 * Try to find a match at this position.
5009 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005010 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5011 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 if (*errormsgp != NULL) /* error detected */
5013 return FAIL;
5014 if (repl == NULL) /* no match found */
5015 {
5016 p += srclen;
5017 continue;
5018 }
5019
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005020 /* Wildcards won't be expanded below, the replacement is taken
5021 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5022 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5023 {
5024 char_u *l = repl;
5025
5026 repl = expand_env_save(repl);
5027 vim_free(l);
5028 }
5029
Bram Moolenaar63b92542007-03-27 14:57:09 +00005030 /* Need to escape white space et al. with a backslash.
5031 * Don't do this for:
5032 * - replacement that already has been escaped: "##"
5033 * - shell commands (may have to use quotes instead).
5034 * - non-unix systems when there is a single argument (spaces don't
5035 * separate arguments then).
5036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005038 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 && eap->cmdidx != CMD_bang
5040 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00005041 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00005043 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00005045 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005046 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047#ifndef UNIX
5048 && !(eap->argt & NOSPC)
5049#endif
5050 )
5051 {
5052 char_u *l;
5053#ifdef BACKSLASH_IN_FILENAME
5054 /* Don't escape a backslash here, because rem_backslash() doesn't
5055 * remove it later. */
5056 static char_u *nobslash = (char_u *)" \t\"|";
5057# define ESCAPE_CHARS nobslash
5058#else
5059# define ESCAPE_CHARS escape_chars
5060#endif
5061
5062 for (l = repl; *l; ++l)
5063 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5064 {
5065 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5066 if (l != NULL)
5067 {
5068 vim_free(repl);
5069 repl = l;
5070 }
5071 break;
5072 }
5073 }
5074
5075 /* For a shell command a '!' must be escaped. */
5076 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005077 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 {
5079 char_u *l;
5080
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005081 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 if (l != NULL)
5083 {
5084 vim_free(repl);
5085 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 }
5087 }
5088
5089 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5090 vim_free(repl);
5091 if (p == NULL)
5092 return FAIL;
5093 }
5094
5095 /*
5096 * One file argument: Expand wildcards.
5097 * Don't do this with ":r !command" or ":w !command".
5098 */
5099 if ((eap->argt & NOSPC) && !eap->usefilter)
5100 {
5101 /*
5102 * May do this twice:
5103 * 1. Replace environment variables.
5104 * 2. Replace any other wildcards, remove backslashes.
5105 */
5106 for (n = 1; n <= 2; ++n)
5107 {
5108 if (n == 2)
5109 {
5110#ifdef UNIX
5111 /*
5112 * Only for Unix we check for more than one file name.
5113 * For other systems spaces are considered to be part
5114 * of the file name.
5115 * Only check here if there is no wildcard, otherwise
5116 * ExpandOne() will check for errors. This allows
5117 * ":e `ls ve*.c`" on Unix.
5118 */
5119 if (!has_wildcards)
5120 for (p = eap->arg; *p; ++p)
5121 {
5122 /* skip escaped characters */
5123 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5124 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005125 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 {
5127 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5128 return FAIL;
5129 }
5130 }
5131#endif
5132
5133 /*
5134 * Halve the number of backslashes (this is Vi compatible).
5135 * For Unix and OS/2, when wildcards are expanded, this is
5136 * done by ExpandOne() below.
5137 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005138#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 if (!has_wildcards)
5140#endif
5141 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 }
5143
5144 if (has_wildcards)
5145 {
5146 if (n == 1)
5147 {
5148 /*
5149 * First loop: May expand environment variables. This
5150 * can be done much faster with expand_env() than with
5151 * something else (e.g., calling a shell).
5152 * After expanding environment variables, check again
5153 * if there are still wildcards present.
5154 */
5155 if (vim_strchr(eap->arg, '$') != NULL
5156 || vim_strchr(eap->arg, '~') != NULL)
5157 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005158 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005159 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 has_wildcards = mch_has_wildcard(NameBuff);
5161 p = NameBuff;
5162 }
5163 else
5164 p = NULL;
5165 }
5166 else /* n == 2 */
5167 {
5168 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005169 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170
5171 ExpandInit(&xpc);
5172 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005173 if (p_wic)
5174 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005176 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 if (p == NULL)
5178 return FAIL;
5179 }
5180 if (p != NULL)
5181 {
5182 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5183 p, cmdlinep);
5184 if (n == 2) /* p came from ExpandOne() */
5185 vim_free(p);
5186 }
5187 }
5188 }
5189 }
5190 return OK;
5191}
5192
5193/*
5194 * Replace part of the command line, keeping eap->cmd, eap->arg and
5195 * eap->nextcmd correct.
5196 * "src" points to the part that is to be replaced, of length "srclen".
5197 * "repl" is the replacement string.
5198 * Returns a pointer to the character after the replaced string.
5199 * Returns NULL for failure.
5200 */
5201 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005202repl_cmdline(
5203 exarg_T *eap,
5204 char_u *src,
5205 int srclen,
5206 char_u *repl,
5207 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208{
5209 int len;
5210 int i;
5211 char_u *new_cmdline;
5212
5213 /*
5214 * The new command line is build in new_cmdline[].
5215 * First allocate it.
5216 * Careful: a "+cmd" argument may have been NUL terminated.
5217 */
5218 len = (int)STRLEN(repl);
5219 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005220 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5222 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5223 return NULL; /* out of memory! */
5224
5225 /*
5226 * Copy the stuff before the expanded part.
5227 * Copy the expanded stuff.
5228 * Copy what came after the expanded part.
5229 * Copy the next commands, if there are any.
5230 */
5231 i = (int)(src - *cmdlinep); /* length of part before match */
5232 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005233
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 mch_memmove(new_cmdline + i, repl, (size_t)len);
5235 i += len; /* remember the end of the string */
5236 STRCPY(new_cmdline + i, src + srclen);
5237 src = new_cmdline + i; /* remember where to continue */
5238
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005239 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 {
5241 i = (int)STRLEN(new_cmdline) + 1;
5242 STRCPY(new_cmdline + i, eap->nextcmd);
5243 eap->nextcmd = new_cmdline + i;
5244 }
5245 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5246 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5247 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5248 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5249 vim_free(*cmdlinep);
5250 *cmdlinep = new_cmdline;
5251
5252 return src;
5253}
5254
5255/*
5256 * Check for '|' to separate commands and '"' to start comments.
5257 */
5258 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005259separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260{
5261 char_u *p;
5262
Bram Moolenaar86b68352004-12-27 21:59:20 +00005263#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005264 p = skip_grep_pat(eap);
5265#else
5266 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005267#endif
5268
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005269 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 {
5271 if (*p == Ctrl_V)
5272 {
5273 if (eap->argt & (USECTRLV | XFILE))
5274 ++p; /* skip CTRL-V and next char */
5275 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005276 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005277 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 if (*p == NUL) /* stop at NUL after CTRL-V */
5279 break;
5280 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005281
5282#ifdef FEAT_EVAL
5283 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005284 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005285 {
5286 p += 2;
5287 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005288 }
5289#endif
5290
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 /* Check for '"': start of comment or '|': next command */
5292 /* :@" and :*" do not start a comment!
5293 * :redir @" doesn't either. */
5294 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5295 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5296 || p != eap->arg)
5297 && (eap->cmdidx != CMD_redir
5298 || p != eap->arg + 1 || p[-1] != '@'))
5299 || *p == '|' || *p == '\n')
5300 {
5301 /*
5302 * We remove the '\' before the '|', unless USECTRLV is used
5303 * AND 'b' is present in 'cpoptions'.
5304 */
5305 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5306 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5307 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005308 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 --p;
5310 }
5311 else
5312 {
5313 eap->nextcmd = check_nextcmd(p);
5314 *p = NUL;
5315 break;
5316 }
5317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005319
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5321 del_trailing_spaces(eap->arg);
5322}
5323
5324/*
5325 * get + command from ex argument
5326 */
5327 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005328getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329{
5330 char_u *arg = *argp;
5331 char_u *command = NULL;
5332
5333 if (*arg == '+') /* +[command] */
5334 {
5335 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005336 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 command = dollar_command;
5338 else
5339 {
5340 command = arg;
5341 arg = skip_cmd_arg(command, TRUE);
5342 if (*arg != NUL)
5343 *arg++ = NUL; /* terminate command with NUL */
5344 }
5345
5346 arg = skipwhite(arg); /* skip over spaces */
5347 *argp = arg;
5348 }
5349 return command;
5350}
5351
5352/*
5353 * Find end of "+command" argument. Skip over "\ " and "\\".
5354 */
5355 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005356skip_cmd_arg(
5357 char_u *p,
5358 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359{
5360 while (*p && !vim_isspace(*p))
5361 {
5362 if (*p == '\\' && p[1] != NUL)
5363 {
5364 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005365 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 else
5367 ++p;
5368 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005369 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 }
5371 return p;
5372}
5373
5374/*
5375 * Get "++opt=arg" argument.
5376 * Return FAIL or OK.
5377 */
5378 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005379getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380{
5381 char_u *arg = eap->arg + 2;
5382 int *pp = NULL;
5383#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005384 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 char_u *p;
5386#endif
5387
5388 /* ":edit ++[no]bin[ary] file" */
5389 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5390 {
5391 if (*arg == 'n')
5392 {
5393 arg += 2;
5394 eap->force_bin = FORCE_NOBIN;
5395 }
5396 else
5397 eap->force_bin = FORCE_BIN;
5398 if (!checkforcmd(&arg, "binary", 3))
5399 return FAIL;
5400 eap->arg = skipwhite(arg);
5401 return OK;
5402 }
5403
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005404 /* ":read ++edit file" */
5405 if (STRNCMP(arg, "edit", 4) == 0)
5406 {
5407 eap->read_edit = TRUE;
5408 eap->arg = skipwhite(arg + 4);
5409 return OK;
5410 }
5411
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 if (STRNCMP(arg, "ff", 2) == 0)
5413 {
5414 arg += 2;
5415 pp = &eap->force_ff;
5416 }
5417 else if (STRNCMP(arg, "fileformat", 10) == 0)
5418 {
5419 arg += 10;
5420 pp = &eap->force_ff;
5421 }
5422#ifdef FEAT_MBYTE
5423 else if (STRNCMP(arg, "enc", 3) == 0)
5424 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005425 if (STRNCMP(arg, "encoding", 8) == 0)
5426 arg += 8;
5427 else
5428 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 pp = &eap->force_enc;
5430 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005431 else if (STRNCMP(arg, "bad", 3) == 0)
5432 {
5433 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005434 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436#endif
5437
5438 if (pp == NULL || *arg != '=')
5439 return FAIL;
5440
5441 ++arg;
5442 *pp = (int)(arg - eap->cmd);
5443 arg = skip_cmd_arg(arg, FALSE);
5444 eap->arg = skipwhite(arg);
5445 *arg = NUL;
5446
5447#ifdef FEAT_MBYTE
5448 if (pp == &eap->force_ff)
5449 {
5450#endif
5451 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5452 return FAIL;
5453#ifdef FEAT_MBYTE
5454 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005455 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 {
5457 /* Make 'fileencoding' lower case. */
5458 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5459 *p = TOLOWER_ASC(*p);
5460 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005461 else
5462 {
5463 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5464 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005465 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005466 if (STRICMP(p, "keep") == 0)
5467 eap->bad_char = BAD_KEEP;
5468 else if (STRICMP(p, "drop") == 0)
5469 eap->bad_char = BAD_DROP;
5470 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5471 eap->bad_char = *p;
5472 else
5473 return FAIL;
5474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475#endif
5476
5477 return OK;
5478}
5479
5480/*
5481 * ":abbreviate" and friends.
5482 */
5483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005484ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485{
5486 do_exmap(eap, TRUE); /* almost the same as mapping */
5487}
5488
5489/*
5490 * ":map" and friends.
5491 */
5492 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005493ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494{
5495 /*
5496 * If we are sourcing .exrc or .vimrc in current directory we
5497 * print the mappings for security reasons.
5498 */
5499 if (secure)
5500 {
5501 secure = 2;
5502 msg_outtrans(eap->cmd);
5503 msg_putchar('\n');
5504 }
5505 do_exmap(eap, FALSE);
5506}
5507
5508/*
5509 * ":unmap" and friends.
5510 */
5511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005512ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
5514 do_exmap(eap, FALSE);
5515}
5516
5517/*
5518 * ":mapclear" and friends.
5519 */
5520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005521ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522{
5523 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5524}
5525
5526/*
5527 * ":abclear" and friends.
5528 */
5529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005530ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531{
5532 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5533}
5534
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005535#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005537ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538{
5539 /*
5540 * Disallow auto commands from .exrc and .vimrc in current
5541 * directory for security reasons.
5542 */
5543 if (secure)
5544 {
5545 secure = 2;
5546 eap->errmsg = e_curdir;
5547 }
5548 else if (eap->cmdidx == CMD_autocmd)
5549 do_autocmd(eap->arg, eap->forceit);
5550 else
5551 do_augroup(eap->arg, eap->forceit);
5552}
5553
5554/*
5555 * ":doautocmd": Apply the automatic commands to the current buffer.
5556 */
5557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005558ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005560 char_u *arg = eap->arg;
5561 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005562 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005563
Bram Moolenaar1610d052016-06-09 22:53:01 +02005564 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5565 /* Only when there is no <nomodeline>. */
5566 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005567 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568}
5569#endif
5570
5571#ifdef FEAT_LISTCMDS
5572/*
5573 * :[N]bunload[!] [N] [bufname] unload buffer
5574 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5575 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5576 */
5577 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005578ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
5580 eap->errmsg = do_bufdel(
5581 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5582 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5583 : DOBUF_UNLOAD, eap->arg,
5584 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5585}
5586
5587/*
5588 * :[N]buffer [N] to buffer N
5589 * :[N]sbuffer [N] to buffer N
5590 */
5591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005592ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593{
5594 if (*eap->arg)
5595 eap->errmsg = e_trailing;
5596 else
5597 {
5598 if (eap->addr_count == 0) /* default is current buffer */
5599 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5600 else
5601 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005602 if (eap->do_ecmd_cmd != NULL)
5603 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 }
5605}
5606
5607/*
5608 * :[N]bmodified [N] to next mod. buffer
5609 * :[N]sbmodified [N] to next mod. buffer
5610 */
5611 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005612ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613{
5614 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005615 if (eap->do_ecmd_cmd != NULL)
5616 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617}
5618
5619/*
5620 * :[N]bnext [N] to next buffer
5621 * :[N]sbnext [N] split and to next buffer
5622 */
5623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005624ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625{
5626 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005627 if (eap->do_ecmd_cmd != NULL)
5628 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629}
5630
5631/*
5632 * :[N]bNext [N] to previous buffer
5633 * :[N]bprevious [N] to previous buffer
5634 * :[N]sbNext [N] split and to previous buffer
5635 * :[N]sbprevious [N] split and to previous buffer
5636 */
5637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005638ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639{
5640 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005641 if (eap->do_ecmd_cmd != NULL)
5642 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643}
5644
5645/*
5646 * :brewind to first buffer
5647 * :bfirst to first buffer
5648 * :sbrewind split and to first buffer
5649 * :sbfirst split and to first buffer
5650 */
5651 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005652ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653{
5654 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005655 if (eap->do_ecmd_cmd != NULL)
5656 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657}
5658
5659/*
5660 * :blast to last buffer
5661 * :sblast split and to last buffer
5662 */
5663 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005664ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665{
5666 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005667 if (eap->do_ecmd_cmd != NULL)
5668 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669}
5670#endif
5671
5672 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005673ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674{
5675 return (c == NUL || c == '|' || c == '"' || c == '\n');
5676}
5677
5678#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5679 || defined(PROTO)
5680/*
5681 * Return the next command, after the first '|' or '\n'.
5682 * Return NULL if not found.
5683 */
5684 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005685find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686{
5687 while (*p != '|' && *p != '\n')
5688 {
5689 if (*p == NUL)
5690 return NULL;
5691 ++p;
5692 }
5693 return (p + 1);
5694}
5695#endif
5696
5697/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005698 * Check if *p is a separator between Ex commands, skipping over white space.
5699 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 */
5701 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005702check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005704 char_u *s = skipwhite(p);
5705
5706 if (*s == '|' || *s == '\n')
5707 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 else
5709 return NULL;
5710}
5711
5712/*
5713 * - if there are more files to edit
5714 * - and this is the last window
5715 * - and forceit not used
5716 * - and not repeated twice on a row
5717 * return FAIL and give error message if 'message' TRUE
5718 * return OK otherwise
5719 */
5720 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005721check_more(
5722 int message, /* when FALSE check only, no messages */
5723 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724{
5725 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5726
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005727 if (!forceit && only_one_window()
5728 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 {
5730 if (message)
5731 {
5732#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5733 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5734 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005735 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
5737 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005738 vim_strncpy(buff,
5739 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005740 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005742 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 _("%d more files to edit. Quit anyway?"), n);
5744 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5745 return OK;
5746 return FAIL;
5747 }
5748#endif
5749 if (n == 1)
5750 EMSG(_("E173: 1 more file to edit"));
5751 else
5752 EMSGN(_("E173: %ld more files to edit"), n);
5753 quitmore = 2; /* next try to quit is allowed */
5754 }
5755 return FAIL;
5756 }
5757 return OK;
5758}
5759
5760#ifdef FEAT_CMDL_COMPL
5761/*
5762 * Function given to ExpandGeneric() to obtain the list of command names.
5763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005765get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766{
5767 if (idx >= (int)CMD_SIZE)
5768# ifdef FEAT_USR_CMDS
5769 return get_user_command_name(idx);
5770# else
5771 return NULL;
5772# endif
5773 return cmdnames[idx].cmd_name;
5774}
5775#endif
5776
5777#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005778static 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);
5779static void uc_list(char_u *name, size_t name_len);
5780static 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);
5781static char_u *uc_split_args(char_u *arg, size_t *lenp);
5782static 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 +00005783
5784 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005785uc_add_command(
5786 char_u *name,
5787 size_t name_len,
5788 char_u *rep,
5789 long argt,
5790 long def,
5791 int flags,
5792 int compl,
5793 char_u *compl_arg,
5794 int addr_type,
5795 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796{
5797 ucmd_T *cmd = NULL;
5798 char_u *p;
5799 int i;
5800 int cmp = 1;
5801 char_u *rep_buf = NULL;
5802 garray_T *gap;
5803
Bram Moolenaar9c102382006-05-03 21:26:49 +00005804 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 if (rep_buf == NULL)
5806 {
5807 /* Can't replace termcodes - try using the string as is */
5808 rep_buf = vim_strsave(rep);
5809
5810 /* Give up if out of memory */
5811 if (rep_buf == NULL)
5812 return FAIL;
5813 }
5814
5815 /* get address of growarray: global or in curbuf */
5816 if (flags & UC_BUFFER)
5817 {
5818 gap = &curbuf->b_ucmds;
5819 if (gap->ga_itemsize == 0)
5820 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5821 }
5822 else
5823 gap = &ucmds;
5824
5825 /* Search for the command in the already defined commands. */
5826 for (i = 0; i < gap->ga_len; ++i)
5827 {
5828 size_t len;
5829
5830 cmd = USER_CMD_GA(gap, i);
5831 len = STRLEN(cmd->uc_name);
5832 cmp = STRNCMP(name, cmd->uc_name, name_len);
5833 if (cmp == 0)
5834 {
5835 if (name_len < len)
5836 cmp = -1;
5837 else if (name_len > len)
5838 cmp = 1;
5839 }
5840
5841 if (cmp == 0)
5842 {
5843 if (!force)
5844 {
5845 EMSG(_("E174: Command already exists: add ! to replace it"));
5846 goto fail;
5847 }
5848
5849 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005850 cmd->uc_rep = NULL;
5851#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5852 vim_free(cmd->uc_compl_arg);
5853 cmd->uc_compl_arg = NULL;
5854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 break;
5856 }
5857
5858 /* Stop as soon as we pass the name to add */
5859 if (cmp < 0)
5860 break;
5861 }
5862
5863 /* Extend the array unless we're replacing an existing command */
5864 if (cmp != 0)
5865 {
5866 if (ga_grow(gap, 1) != OK)
5867 goto fail;
5868 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5869 goto fail;
5870
5871 cmd = USER_CMD_GA(gap, i);
5872 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5873
5874 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875
5876 cmd->uc_name = p;
5877 }
5878
5879 cmd->uc_rep = rep_buf;
5880 cmd->uc_argt = argt;
5881 cmd->uc_def = def;
5882 cmd->uc_compl = compl;
5883#ifdef FEAT_EVAL
5884 cmd->uc_scriptID = current_SID;
5885# ifdef FEAT_CMDL_COMPL
5886 cmd->uc_compl_arg = compl_arg;
5887# endif
5888#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005889 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890
5891 return OK;
5892
5893fail:
5894 vim_free(rep_buf);
5895#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5896 vim_free(compl_arg);
5897#endif
5898 return FAIL;
5899}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005902#if defined(FEAT_USR_CMDS)
5903static struct
5904{
5905 int expand;
5906 char *name;
5907} addr_type_complete[] =
5908{
5909 {ADDR_ARGUMENTS, "arguments"},
5910 {ADDR_LINES, "lines"},
5911 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5912 {ADDR_TABS, "tabs"},
5913 {ADDR_BUFFERS, "buffers"},
5914 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005915 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005916 {-1, NULL}
5917};
5918#endif
5919
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005920#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921/*
5922 * List of names for completion for ":command" with the EXPAND_ flag.
5923 * Must be alphabetical for completion.
5924 */
5925static struct
5926{
5927 int expand;
5928 char *name;
5929} command_complete[] =
5930{
5931 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005932 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005934 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005936 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005937#if defined(FEAT_CSCOPE)
5938 {EXPAND_CSCOPE, "cscope"},
5939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5941 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005942 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943#endif
5944 {EXPAND_DIRECTORIES, "dir"},
5945 {EXPAND_ENV_VARS, "environment"},
5946 {EXPAND_EVENTS, "event"},
5947 {EXPAND_EXPRESSION, "expression"},
5948 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005949 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005950 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 {EXPAND_FUNCTIONS, "function"},
5952 {EXPAND_HELP, "help"},
5953 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005954#if defined(FEAT_CMDHIST)
5955 {EXPAND_HISTORY, "history"},
5956#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005957#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005958 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005959 {EXPAND_LOCALES, "locale"},
5960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 {EXPAND_MAPPINGS, "mapping"},
5962 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005963 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005964 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005965#if defined(FEAT_PROFILE)
5966 {EXPAND_SYNTIME, "syntime"},
5967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005969 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005970 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005971#if defined(FEAT_SIGNS)
5972 {EXPAND_SIGN, "sign"},
5973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 {EXPAND_TAGS, "tag"},
5975 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005976 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 {EXPAND_USER_VARS, "var"},
5978 {0, NULL}
5979};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005982#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005984uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985{
5986 int i, j;
5987 int found = FALSE;
5988 ucmd_T *cmd;
5989 int len;
5990 long a;
5991 garray_T *gap;
5992
5993 gap = &curbuf->b_ucmds;
5994 for (;;)
5995 {
5996 for (i = 0; i < gap->ga_len; ++i)
5997 {
5998 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005999 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000
Bram Moolenaard29459b2016-08-26 22:29:11 +02006001 /* Skip commands which don't match the requested prefix and
6002 * commands filtered out. */
6003 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6004 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 continue;
6006
6007 /* Put out the title first time */
6008 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006009 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 found = TRUE;
6011 msg_putchar('\n');
6012 if (got_int)
6013 break;
6014
6015 /* Special cases */
6016 msg_putchar(a & BANG ? '!' : ' ');
6017 msg_putchar(a & REGSTR ? '"' : ' ');
6018 msg_putchar(gap != &ucmds ? 'b' : ' ');
6019 msg_putchar(' ');
6020
Bram Moolenaar8820b482017-03-16 17:23:31 +01006021 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 len = (int)STRLEN(cmd->uc_name) + 4;
6023
6024 do {
6025 msg_putchar(' ');
6026 ++len;
6027 } while (len < 16);
6028
6029 len = 0;
6030
6031 /* Arguments */
6032 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6033 {
6034 case 0: IObuff[len++] = '0'; break;
6035 case (EXTRA): IObuff[len++] = '*'; break;
6036 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6037 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6038 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6039 }
6040
6041 do {
6042 IObuff[len++] = ' ';
6043 } while (len < 5);
6044
6045 /* Range */
6046 if (a & (RANGE|COUNT))
6047 {
6048 if (a & COUNT)
6049 {
6050 /* -count=N */
6051 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6052 len += (int)STRLEN(IObuff + len);
6053 }
6054 else if (a & DFLALL)
6055 IObuff[len++] = '%';
6056 else if (cmd->uc_def >= 0)
6057 {
6058 /* -range=N */
6059 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6060 len += (int)STRLEN(IObuff + len);
6061 }
6062 else
6063 IObuff[len++] = '.';
6064 }
6065
6066 do {
6067 IObuff[len++] = ' ';
6068 } while (len < 11);
6069
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006070 /* Address Type */
6071 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6072 if (addr_type_complete[j].expand != ADDR_LINES
6073 && addr_type_complete[j].expand == cmd->uc_addr_type)
6074 {
6075 STRCPY(IObuff + len, addr_type_complete[j].name);
6076 len += (int)STRLEN(IObuff + len);
6077 break;
6078 }
6079
6080 do {
6081 IObuff[len++] = ' ';
6082 } while (len < 21);
6083
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 /* Completion */
6085 for (j = 0; command_complete[j].expand != 0; ++j)
6086 if (command_complete[j].expand == cmd->uc_compl)
6087 {
6088 STRCPY(IObuff + len, command_complete[j].name);
6089 len += (int)STRLEN(IObuff + len);
6090 break;
6091 }
6092
6093 do {
6094 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006095 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096
6097 IObuff[len] = '\0';
6098 msg_outtrans(IObuff);
6099
6100 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006101#ifdef FEAT_EVAL
6102 if (p_verbose > 0)
6103 last_set_msg(cmd->uc_scriptID);
6104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 out_flush();
6106 ui_breakcheck();
6107 if (got_int)
6108 break;
6109 }
6110 if (gap == &ucmds || i < gap->ga_len)
6111 break;
6112 gap = &ucmds;
6113 }
6114
6115 if (!found)
6116 MSG(_("No user-defined commands found"));
6117}
6118
6119 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006120uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
6122 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6123 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6124 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6125 0xb9, 0x7f, 0};
6126 int i;
6127
6128 for (i = 0; fcmd[i]; ++i)
6129 IObuff[i] = fcmd[i] - 0x40;
6130 IObuff[i] = 0;
6131 return IObuff;
6132}
6133
6134 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006135uc_scan_attr(
6136 char_u *attr,
6137 size_t len,
6138 long *argt,
6139 long *def,
6140 int *flags,
6141 int *compl,
6142 char_u **compl_arg,
6143 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144{
6145 char_u *p;
6146
6147 if (len == 0)
6148 {
6149 EMSG(_("E175: No attribute specified"));
6150 return FAIL;
6151 }
6152
6153 /* First, try the simple attributes (no arguments) */
6154 if (STRNICMP(attr, "bang", len) == 0)
6155 *argt |= BANG;
6156 else if (STRNICMP(attr, "buffer", len) == 0)
6157 *flags |= UC_BUFFER;
6158 else if (STRNICMP(attr, "register", len) == 0)
6159 *argt |= REGSTR;
6160 else if (STRNICMP(attr, "bar", len) == 0)
6161 *argt |= TRLBAR;
6162 else
6163 {
6164 int i;
6165 char_u *val = NULL;
6166 size_t vallen = 0;
6167 size_t attrlen = len;
6168
6169 /* Look for the attribute name - which is the part before any '=' */
6170 for (i = 0; i < (int)len; ++i)
6171 {
6172 if (attr[i] == '=')
6173 {
6174 val = &attr[i + 1];
6175 vallen = len - i - 1;
6176 attrlen = i;
6177 break;
6178 }
6179 }
6180
6181 if (STRNICMP(attr, "nargs", attrlen) == 0)
6182 {
6183 if (vallen == 1)
6184 {
6185 if (*val == '0')
6186 /* Do nothing - this is the default */;
6187 else if (*val == '1')
6188 *argt |= (EXTRA | NOSPC | NEEDARG);
6189 else if (*val == '*')
6190 *argt |= EXTRA;
6191 else if (*val == '?')
6192 *argt |= (EXTRA | NOSPC);
6193 else if (*val == '+')
6194 *argt |= (EXTRA | NEEDARG);
6195 else
6196 goto wrong_nargs;
6197 }
6198 else
6199 {
6200wrong_nargs:
6201 EMSG(_("E176: Invalid number of arguments"));
6202 return FAIL;
6203 }
6204 }
6205 else if (STRNICMP(attr, "range", attrlen) == 0)
6206 {
6207 *argt |= RANGE;
6208 if (vallen == 1 && *val == '%')
6209 *argt |= DFLALL;
6210 else if (val != NULL)
6211 {
6212 p = val;
6213 if (*def >= 0)
6214 {
6215two_count:
6216 EMSG(_("E177: Count cannot be specified twice"));
6217 return FAIL;
6218 }
6219
6220 *def = getdigits(&p);
6221 *argt |= (ZEROR | NOTADR);
6222
6223 if (p != val + vallen || vallen == 0)
6224 {
6225invalid_count:
6226 EMSG(_("E178: Invalid default value for count"));
6227 return FAIL;
6228 }
6229 }
6230 }
6231 else if (STRNICMP(attr, "count", attrlen) == 0)
6232 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006233 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234
6235 if (val != NULL)
6236 {
6237 p = val;
6238 if (*def >= 0)
6239 goto two_count;
6240
6241 *def = getdigits(&p);
6242
6243 if (p != val + vallen)
6244 goto invalid_count;
6245 }
6246
6247 if (*def < 0)
6248 *def = 0;
6249 }
6250 else if (STRNICMP(attr, "complete", attrlen) == 0)
6251 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 if (val == NULL)
6253 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006254 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 return FAIL;
6256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006258 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6259 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006262 else if (STRNICMP(attr, "addr", attrlen) == 0)
6263 {
6264 *argt |= RANGE;
6265 if (val == NULL)
6266 {
6267 EMSG(_("E179: argument required for -addr"));
6268 return FAIL;
6269 }
6270 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6271 == FAIL)
6272 return FAIL;
6273 if (addr_type_arg != ADDR_LINES)
6274 *argt |= (ZEROR | NOTADR) ;
6275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 else
6277 {
6278 char_u ch = attr[len];
6279 attr[len] = '\0';
6280 EMSG2(_("E181: Invalid attribute: %s"), attr);
6281 attr[len] = ch;
6282 return FAIL;
6283 }
6284 }
6285
6286 return OK;
6287}
6288
Bram Moolenaara850a712009-01-28 14:42:59 +00006289/*
6290 * ":command ..."
6291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006293ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294{
6295 char_u *name;
6296 char_u *end;
6297 char_u *p;
6298 long argt = 0;
6299 long def = -1;
6300 int flags = 0;
6301 int compl = EXPAND_NOTHING;
6302 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006303 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006305 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306
6307 p = eap->arg;
6308
6309 /* Check for attributes */
6310 while (*p == '-')
6311 {
6312 ++p;
6313 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006314 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 == FAIL)
6316 return;
6317 p = skipwhite(end);
6318 }
6319
6320 /* Get the name (if any) and skip to the following argument */
6321 name = p;
6322 if (ASCII_ISALPHA(*p))
6323 while (ASCII_ISALNUM(*p))
6324 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006325 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 {
6327 EMSG(_("E182: Invalid command name"));
6328 return;
6329 }
6330 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006331 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332
6333 /* If there is nothing after the name, and no attributes were specified,
6334 * we are listing commands
6335 */
6336 p = skipwhite(end);
6337 if (!has_attr && ends_excmd(*p))
6338 {
6339 uc_list(name, end - name);
6340 }
6341 else if (!ASCII_ISUPPER(*name))
6342 {
6343 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6344 return;
6345 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006346 else if ((name_len == 1 && *name == 'X')
6347 || (name_len <= 4
6348 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6349 {
6350 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6351 return;
6352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 else
6354 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006355 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356}
6357
6358/*
6359 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 * Clear all user commands, global and for current buffer.
6361 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006362 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006363ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364{
6365 uc_clear(&ucmds);
6366 uc_clear(&curbuf->b_ucmds);
6367}
6368
6369/*
6370 * Clear all user commands for "gap".
6371 */
6372 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006373uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374{
6375 int i;
6376 ucmd_T *cmd;
6377
6378 for (i = 0; i < gap->ga_len; ++i)
6379 {
6380 cmd = USER_CMD_GA(gap, i);
6381 vim_free(cmd->uc_name);
6382 vim_free(cmd->uc_rep);
6383# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6384 vim_free(cmd->uc_compl_arg);
6385# endif
6386 }
6387 ga_clear(gap);
6388}
6389
6390 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006391ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392{
6393 int i = 0;
6394 ucmd_T *cmd = NULL;
6395 int cmp = -1;
6396 garray_T *gap;
6397
6398 gap = &curbuf->b_ucmds;
6399 for (;;)
6400 {
6401 for (i = 0; i < gap->ga_len; ++i)
6402 {
6403 cmd = USER_CMD_GA(gap, i);
6404 cmp = STRCMP(eap->arg, cmd->uc_name);
6405 if (cmp <= 0)
6406 break;
6407 }
6408 if (gap == &ucmds || cmp == 0)
6409 break;
6410 gap = &ucmds;
6411 }
6412
6413 if (cmp != 0)
6414 {
6415 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6416 return;
6417 }
6418
6419 vim_free(cmd->uc_name);
6420 vim_free(cmd->uc_rep);
6421# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6422 vim_free(cmd->uc_compl_arg);
6423# endif
6424
6425 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426
6427 if (i < gap->ga_len)
6428 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6429}
6430
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006431/*
6432 * split and quote args for <f-args>
6433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006435uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436{
6437 char_u *buf;
6438 char_u *p;
6439 char_u *q;
6440 int len;
6441
6442 /* Precalculate length */
6443 p = arg;
6444 len = 2; /* Initial and final quotes */
6445
6446 while (*p)
6447 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006448 if (p[0] == '\\' && p[1] == '\\')
6449 {
6450 len += 2;
6451 p += 2;
6452 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006453 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 {
6455 len += 1;
6456 p += 2;
6457 }
6458 else if (*p == '\\' || *p == '"')
6459 {
6460 len += 2;
6461 p += 1;
6462 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006463 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 {
6465 p = skipwhite(p);
6466 if (*p == NUL)
6467 break;
6468 len += 3; /* "," */
6469 }
6470 else
6471 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006472#ifdef FEAT_MBYTE
6473 int charlen = (*mb_ptr2len)(p);
6474 len += charlen;
6475 p += charlen;
6476#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 ++len;
6478 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 }
6481 }
6482
6483 buf = alloc(len + 1);
6484 if (buf == NULL)
6485 {
6486 *lenp = 0;
6487 return buf;
6488 }
6489
6490 p = arg;
6491 q = buf;
6492 *q++ = '"';
6493 while (*p)
6494 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006495 if (p[0] == '\\' && p[1] == '\\')
6496 {
6497 *q++ = '\\';
6498 *q++ = '\\';
6499 p += 2;
6500 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006501 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 {
6503 *q++ = p[1];
6504 p += 2;
6505 }
6506 else if (*p == '\\' || *p == '"')
6507 {
6508 *q++ = '\\';
6509 *q++ = *p++;
6510 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006511 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 {
6513 p = skipwhite(p);
6514 if (*p == NUL)
6515 break;
6516 *q++ = '"';
6517 *q++ = ',';
6518 *q++ = '"';
6519 }
6520 else
6521 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006522 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 }
6524 }
6525 *q++ = '"';
6526 *q = 0;
6527
6528 *lenp = len;
6529 return buf;
6530}
6531
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006532 static size_t
6533add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6534{
6535 size_t result;
6536
6537 result = STRLEN(mod_str);
6538 if (*multi_mods)
6539 result += 1;
6540 if (buf != NULL)
6541 {
6542 if (*multi_mods)
6543 STRCAT(buf, " ");
6544 STRCAT(buf, mod_str);
6545 }
6546
6547 *multi_mods = 1;
6548
6549 return result;
6550}
6551
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552/*
6553 * Check for a <> code in a user command.
6554 * "code" points to the '<'. "len" the length of the <> (inclusive).
6555 * "buf" is where the result is to be added.
6556 * "split_buf" points to a buffer used for splitting, caller should free it.
6557 * "split_len" is the length of what "split_buf" contains.
6558 * Returns the length of the replacement, which has been added to "buf".
6559 * Returns -1 if there was no match, and only the "<" has been copied.
6560 */
6561 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006562uc_check_code(
6563 char_u *code,
6564 size_t len,
6565 char_u *buf,
6566 ucmd_T *cmd, /* the user command we're expanding */
6567 exarg_T *eap, /* ex arguments */
6568 char_u **split_buf,
6569 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570{
6571 size_t result = 0;
6572 char_u *p = code + 1;
6573 size_t l = len - 2;
6574 int quote = 0;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006575 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_MODS,
6576 ct_REGISTER, ct_LT, ct_NONE } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
6578 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6579 {
6580 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6581 p += 2;
6582 l -= 2;
6583 }
6584
Bram Moolenaar371d5402006-03-20 21:47:49 +00006585 ++l;
6586 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006588 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006590 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006592 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006594 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006596 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006598 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006600 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006602 else if (STRNICMP(p, "mods>", l) == 0)
6603 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604
6605 switch (type)
6606 {
6607 case ct_ARGS:
6608 /* Simple case first */
6609 if (*eap->arg == NUL)
6610 {
6611 if (quote == 1)
6612 {
6613 result = 2;
6614 if (buf != NULL)
6615 STRCPY(buf, "''");
6616 }
6617 else
6618 result = 0;
6619 break;
6620 }
6621
6622 /* When specified there is a single argument don't split it.
6623 * Works for ":Cmd %" when % is "a b c". */
6624 if ((eap->argt & NOSPC) && quote == 2)
6625 quote = 1;
6626
6627 switch (quote)
6628 {
6629 case 0: /* No quoting, no splitting */
6630 result = STRLEN(eap->arg);
6631 if (buf != NULL)
6632 STRCPY(buf, eap->arg);
6633 break;
6634 case 1: /* Quote, but don't split */
6635 result = STRLEN(eap->arg) + 2;
6636 for (p = eap->arg; *p; ++p)
6637 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006638#ifdef FEAT_MBYTE
6639 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6640 /* DBCS can contain \ in a trail byte, skip the
6641 * double-byte character. */
6642 ++p;
6643 else
6644#endif
6645 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 ++result;
6647 }
6648
6649 if (buf != NULL)
6650 {
6651 *buf++ = '"';
6652 for (p = eap->arg; *p; ++p)
6653 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006654#ifdef FEAT_MBYTE
6655 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6656 /* DBCS can contain \ in a trail byte, copy the
6657 * double-byte character to avoid escaping. */
6658 *buf++ = *p++;
6659 else
6660#endif
6661 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 *buf++ = '\\';
6663 *buf++ = *p;
6664 }
6665 *buf = '"';
6666 }
6667
6668 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006669 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 /* This is hard, so only do it once, and cache the result */
6671 if (*split_buf == NULL)
6672 *split_buf = uc_split_args(eap->arg, split_len);
6673
6674 result = *split_len;
6675 if (buf != NULL && result != 0)
6676 STRCPY(buf, *split_buf);
6677
6678 break;
6679 }
6680 break;
6681
6682 case ct_BANG:
6683 result = eap->forceit ? 1 : 0;
6684 if (quote)
6685 result += 2;
6686 if (buf != NULL)
6687 {
6688 if (quote)
6689 *buf++ = '"';
6690 if (eap->forceit)
6691 *buf++ = '!';
6692 if (quote)
6693 *buf = '"';
6694 }
6695 break;
6696
6697 case ct_LINE1:
6698 case ct_LINE2:
6699 case ct_COUNT:
6700 {
6701 char num_buf[20];
6702 long num = (type == ct_LINE1) ? eap->line1 :
6703 (type == ct_LINE2) ? eap->line2 :
6704 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6705 size_t num_len;
6706
6707 sprintf(num_buf, "%ld", num);
6708 num_len = STRLEN(num_buf);
6709 result = num_len;
6710
6711 if (quote)
6712 result += 2;
6713
6714 if (buf != NULL)
6715 {
6716 if (quote)
6717 *buf++ = '"';
6718 STRCPY(buf, num_buf);
6719 buf += num_len;
6720 if (quote)
6721 *buf = '"';
6722 }
6723
6724 break;
6725 }
6726
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006727 case ct_MODS:
6728 {
6729 int multi_mods = 0;
6730 typedef struct {
6731 int *varp;
6732 char *name;
6733 } mod_entry_T;
6734 static mod_entry_T mod_entries[] = {
6735#ifdef FEAT_BROWSE_CMD
6736 {&cmdmod.browse, "browse"},
6737#endif
6738#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6739 {&cmdmod.confirm, "confirm"},
6740#endif
6741 {&cmdmod.hide, "hide"},
6742 {&cmdmod.keepalt, "keepalt"},
6743 {&cmdmod.keepjumps, "keepjumps"},
6744 {&cmdmod.keepmarks, "keepmarks"},
6745 {&cmdmod.keeppatterns, "keeppatterns"},
6746 {&cmdmod.lockmarks, "lockmarks"},
6747 {&cmdmod.noswapfile, "noswapfile"},
6748 {NULL, NULL}
6749 };
6750 int i;
6751
6752 result = quote ? 2 : 0;
6753 if (buf != NULL)
6754 {
6755 if (quote)
6756 *buf++ = '"';
6757 *buf = '\0';
6758 }
6759
6760#ifdef FEAT_WINDOWS
6761 /* :aboveleft and :leftabove */
6762 if (cmdmod.split & WSP_ABOVE)
6763 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6764 /* :belowright and :rightbelow */
6765 if (cmdmod.split & WSP_BELOW)
6766 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6767 /* :botright */
6768 if (cmdmod.split & WSP_BOT)
6769 result += add_cmd_modifier(buf, "botright", &multi_mods);
6770#endif
6771
6772 /* the modifiers that are simple flags */
6773 for (i = 0; mod_entries[i].varp != NULL; ++i)
6774 if (*mod_entries[i].varp)
6775 result += add_cmd_modifier(buf, mod_entries[i].name,
6776 &multi_mods);
6777
6778 /* TODO: How to support :noautocmd? */
6779#ifdef HAVE_SANDBOX
6780 /* TODO: How to support :sandbox?*/
6781#endif
6782 /* :silent */
6783 if (msg_silent > 0)
6784 result += add_cmd_modifier(buf,
6785 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6786#ifdef FEAT_WINDOWS
6787 /* :tab */
6788 if (cmdmod.tab > 0)
6789 result += add_cmd_modifier(buf, "tab", &multi_mods);
6790 /* :topleft */
6791 if (cmdmod.split & WSP_TOP)
6792 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6793#endif
6794 /* TODO: How to support :unsilent?*/
6795 /* :verbose */
6796 if (p_verbose > 0)
6797 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6798#ifdef FEAT_WINDOWS
6799 /* :vertical */
6800 if (cmdmod.split & WSP_VERT)
6801 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6802#endif
6803 if (quote && buf != NULL)
6804 {
6805 buf += result - 2;
6806 *buf = '"';
6807 }
6808 break;
6809 }
6810
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 case ct_REGISTER:
6812 result = eap->regname ? 1 : 0;
6813 if (quote)
6814 result += 2;
6815 if (buf != NULL)
6816 {
6817 if (quote)
6818 *buf++ = '\'';
6819 if (eap->regname)
6820 *buf++ = eap->regname;
6821 if (quote)
6822 *buf = '\'';
6823 }
6824 break;
6825
6826 case ct_LT:
6827 result = 1;
6828 if (buf != NULL)
6829 *buf = '<';
6830 break;
6831
6832 default:
6833 /* Not recognized: just copy the '<' and return -1. */
6834 result = (size_t)-1;
6835 if (buf != NULL)
6836 *buf = '<';
6837 break;
6838 }
6839
6840 return result;
6841}
6842
6843 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006844do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845{
6846 char_u *buf;
6847 char_u *p;
6848 char_u *q;
6849
6850 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006851 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006852 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 size_t len, totlen;
6854
6855 size_t split_len = 0;
6856 char_u *split_buf = NULL;
6857 ucmd_T *cmd;
6858#ifdef FEAT_EVAL
6859 scid_T save_current_SID = current_SID;
6860#endif
6861
6862 if (eap->cmdidx == CMD_USER)
6863 cmd = USER_CMD(eap->useridx);
6864 else
6865 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6866
6867 /*
6868 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006869 * First round: "buf" is NULL, compute length, allocate "buf".
6870 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 */
6872 buf = NULL;
6873 for (;;)
6874 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006875 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006876 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006878
6879 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006881 start = vim_strchr(p, '<');
6882 if (start != NULL)
6883 end = vim_strchr(start + 1, '>');
6884 if (buf != NULL)
6885 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006886 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6887 ;
6888 if (*ksp == K_SPECIAL
6889 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006890 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6891# ifdef FEAT_GUI
6892 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6893# endif
6894 ))
6895 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006896 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006897 * KS_SPECIAL KE_FILLER, like for mappings, but
6898 * do_cmdline() doesn't handle that, so convert it back.
6899 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6900 len = ksp - p;
6901 if (len > 0)
6902 {
6903 mch_memmove(q, p, len);
6904 q += len;
6905 }
6906 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6907 p = ksp + 3;
6908 continue;
6909 }
6910 }
6911
6912 /* break if there no <item> is found */
6913 if (start == NULL || end == NULL)
6914 break;
6915
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 /* Include the '>' */
6917 ++end;
6918
6919 /* Take everything up to the '<' */
6920 len = start - p;
6921 if (buf == NULL)
6922 totlen += len;
6923 else
6924 {
6925 mch_memmove(q, p, len);
6926 q += len;
6927 }
6928
6929 len = uc_check_code(start, end - start, q, cmd, eap,
6930 &split_buf, &split_len);
6931 if (len == (size_t)-1)
6932 {
6933 /* no match, continue after '<' */
6934 p = start + 1;
6935 len = 1;
6936 }
6937 else
6938 p = end;
6939 if (buf == NULL)
6940 totlen += len;
6941 else
6942 q += len;
6943 }
6944 if (buf != NULL) /* second time here, finished */
6945 {
6946 STRCPY(q, p);
6947 break;
6948 }
6949
6950 totlen += STRLEN(p); /* Add on the trailing characters */
6951 buf = alloc((unsigned)(totlen + 1));
6952 if (buf == NULL)
6953 {
6954 vim_free(split_buf);
6955 return;
6956 }
6957 }
6958
6959#ifdef FEAT_EVAL
6960 current_SID = cmd->uc_scriptID;
6961#endif
6962 (void)do_cmdline(buf, eap->getline, eap->cookie,
6963 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6964#ifdef FEAT_EVAL
6965 current_SID = save_current_SID;
6966#endif
6967 vim_free(buf);
6968 vim_free(split_buf);
6969}
6970
6971# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6972 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006973get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974{
6975 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6976}
6977
6978/*
6979 * Function given to ExpandGeneric() to obtain the list of user command names.
6980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006982get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983{
6984 if (idx < curbuf->b_ucmds.ga_len)
6985 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6986 idx -= curbuf->b_ucmds.ga_len;
6987 if (idx < ucmds.ga_len)
6988 return USER_CMD(idx)->uc_name;
6989 return NULL;
6990}
6991
6992/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006993 * Function given to ExpandGeneric() to obtain the list of user address type names.
6994 */
6995 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006996get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006997{
6998 return (char_u *)addr_type_complete[idx].name;
6999}
7000
7001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 * Function given to ExpandGeneric() to obtain the list of user command
7003 * attributes.
7004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007006get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007{
7008 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007009 {"addr", "bang", "bar", "buffer", "complete",
7010 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007012 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 return NULL;
7014 return (char_u *)user_cmd_flags[idx];
7015}
7016
7017/*
7018 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007021get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022{
7023 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7024
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007025 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 return NULL;
7027 return (char_u *)user_cmd_nargs[idx];
7028}
7029
7030/*
7031 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007034get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035{
7036 return (char_u *)command_complete[idx].name;
7037}
7038# endif /* FEAT_CMDL_COMPL */
7039
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007040/*
7041 * Parse address type argument
7042 */
7043 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007044parse_addr_type_arg(
7045 char_u *value,
7046 int vallen,
7047 long *argt,
7048 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007049{
7050 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007051
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007052 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7053 {
7054 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7055 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7056 if (a && b)
7057 {
7058 *addr_type_arg = addr_type_complete[i].expand;
7059 break;
7060 }
7061 }
7062
7063 if (addr_type_complete[i].expand == -1)
7064 {
7065 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007066
Bram Moolenaar1c465442017-03-12 20:10:05 +01007067 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007068 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007069 err[i] = NUL;
7070 EMSG2(_("E180: Invalid address type value: %s"), err);
7071 return FAIL;
7072 }
7073
7074 if (*addr_type_arg != ADDR_LINES)
7075 *argt |= NOTADR;
7076
7077 return OK;
7078}
7079
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080#endif /* FEAT_USR_CMDS */
7081
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007082#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7083/*
7084 * Parse a completion argument "value[vallen]".
7085 * The detected completion goes in "*complp", argument type in "*argt".
7086 * When there is an argument, for function and user defined completion, it's
7087 * copied to allocated memory and stored in "*compl_arg".
7088 * Returns FAIL if something is wrong.
7089 */
7090 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007091parse_compl_arg(
7092 char_u *value,
7093 int vallen,
7094 int *complp,
7095 long *argt,
7096 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007097{
7098 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007099# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007100 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007101# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007102 int i;
7103 int valend = vallen;
7104
7105 /* Look for any argument part - which is the part after any ',' */
7106 for (i = 0; i < vallen; ++i)
7107 {
7108 if (value[i] == ',')
7109 {
7110 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007111# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007112 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007113# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007114 valend = i;
7115 break;
7116 }
7117 }
7118
7119 for (i = 0; command_complete[i].expand != 0; ++i)
7120 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007121 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007122 && STRNCMP(value, command_complete[i].name, valend) == 0)
7123 {
7124 *complp = command_complete[i].expand;
7125 if (command_complete[i].expand == EXPAND_BUFFERS)
7126 *argt |= BUFNAME;
7127 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7128 || command_complete[i].expand == EXPAND_FILES)
7129 *argt |= XFILE;
7130 break;
7131 }
7132 }
7133
7134 if (command_complete[i].expand == 0)
7135 {
7136 EMSG2(_("E180: Invalid complete value: %s"), value);
7137 return FAIL;
7138 }
7139
7140# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7141 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7142 && arg != NULL)
7143# else
7144 if (arg != NULL)
7145# endif
7146 {
7147 EMSG(_("E468: Completion argument only allowed for custom completion"));
7148 return FAIL;
7149 }
7150
7151# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7152 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7153 && arg == NULL)
7154 {
7155 EMSG(_("E467: Custom completion requires a function argument"));
7156 return FAIL;
7157 }
7158
7159 if (arg != NULL)
7160 *compl_arg = vim_strnsave(arg, (int)arglen);
7161# endif
7162 return OK;
7163}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007164
7165 int
7166cmdcomplete_str_to_type(char_u *complete_str)
7167{
7168 int i;
7169
7170 for (i = 0; command_complete[i].expand != 0; ++i)
7171 if (STRCMP(complete_str, command_complete[i].name) == 0)
7172 return command_complete[i].expand;
7173
7174 return EXPAND_NOTHING;
7175}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007176#endif
7177
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007179ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180{
Bram Moolenaare6850792010-05-14 15:28:44 +02007181 if (*eap->arg == NUL)
7182 {
7183#ifdef FEAT_EVAL
7184 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7185 char_u *p = NULL;
7186
7187 if (expr != NULL)
7188 {
7189 ++emsg_off;
7190 p = eval_to_string(expr, NULL, FALSE);
7191 --emsg_off;
7192 vim_free(expr);
7193 }
7194 if (p != NULL)
7195 {
7196 MSG(p);
7197 vim_free(p);
7198 }
7199 else
7200 MSG("default");
7201#else
7202 MSG(_("unknown"));
7203#endif
7204 }
7205 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007206 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207}
7208
7209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007210ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211{
7212 if (*eap->arg == NUL && eap->cmd[2] == '!')
7213 MSG(_("Greetings, Vim user!"));
7214 do_highlight(eap->arg, eap->forceit, FALSE);
7215}
7216
7217
7218/*
7219 * Call this function if we thought we were going to exit, but we won't
7220 * (because of an error). May need to restore the terminal mode.
7221 */
7222 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007223not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224{
7225 exiting = FALSE;
7226 settmode(TMODE_RAW);
7227}
7228
7229/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007230 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 */
7232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007233ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007235#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007236 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007237#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007238
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239#ifdef FEAT_CMDWIN
7240 if (cmdwin_type != 0)
7241 {
7242 cmdwin_result = Ctrl_C;
7243 return;
7244 }
7245#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007246 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007247 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007248 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007249 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007250 return;
7251 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007252#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007253 if (eap->addr_count > 0)
7254 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007255 int wnr = eap->line2;
7256
7257 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7258 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007259 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007260 }
7261 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007262#endif
7263#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007264 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007265#endif
7266
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007267#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007268 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007269 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007270 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007271 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007272 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007273 return;
7274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275
7276#ifdef FEAT_NETBEANS_INTG
7277 netbeansForcedQuit = eap->forceit;
7278#endif
7279
7280 /*
7281 * If there are more files or windows we won't exit.
7282 */
7283 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7284 exiting = TRUE;
7285 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007286 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7287 | (eap->forceit ? CCGD_FORCEIT : 0)
7288 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007290 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 {
7292 not_exiting();
7293 }
7294 else
7295 {
7296#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007297 /* quit last window
7298 * Note: only_one_window() returns true, even so a help window is
7299 * still open. In that case only quit, if no address has been
7300 * specified. Example:
7301 * :h|wincmd w|1q - don't quit
7302 * :h|wincmd w|q - quit
7303 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007304 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305#endif
7306 getout(0);
7307#ifdef FEAT_WINDOWS
7308# ifdef FEAT_GUI
7309 need_mouse_correct = TRUE;
7310# endif
7311 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007312 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313#endif
7314 }
7315}
7316
7317/*
7318 * ":cquit".
7319 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007321ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322{
7323 getout(1); /* this does not always pass on the exit code to the Manx
7324 compiler. why? */
7325}
7326
7327/*
7328 * ":qall": try to quit all windows
7329 */
7330 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007331ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332{
7333# ifdef FEAT_CMDWIN
7334 if (cmdwin_type != 0)
7335 {
7336 if (eap->forceit)
7337 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7338 else
7339 cmdwin_result = K_XF2;
7340 return;
7341 }
7342# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007343
7344 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007345 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007346 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007347 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007348 return;
7349 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007350#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007351 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7352 /* Refuse to quit when locked or when the buffer in the last window is
7353 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007354 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007355 return;
7356#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007357
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007359 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 getout(0);
7361 not_exiting();
7362}
7363
Bram Moolenaard9967712006-03-11 21:18:15 +00007364#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365/*
7366 * ":close": close current window, unless it is the last one
7367 */
7368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007369ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007371 win_T *win;
7372 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373# ifdef FEAT_CMDWIN
7374 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007375 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 else
7377# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007378 if (!text_locked()
7379#ifdef FEAT_AUTOCMD
7380 && !curbuf_locked()
7381#endif
7382 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007383 {
7384 if (eap->addr_count == 0)
7385 ex_win_close(eap->forceit, curwin, NULL);
7386 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007387 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007388 {
7389 winnr++;
7390 if (winnr == eap->line2)
7391 break;
7392 }
7393 if (win == NULL)
7394 win = lastwin;
7395 ex_win_close(eap->forceit, win, NULL);
7396 }
7397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398}
7399
Bram Moolenaard9967712006-03-11 21:18:15 +00007400# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007401/*
7402 * ":pclose": Close any preview window.
7403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007405ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007406{
7407 win_T *win;
7408
Bram Moolenaar29323592016-07-24 22:04:11 +02007409 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007410 if (win->w_p_pvw)
7411 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007412 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007413 break;
7414 }
7415}
Bram Moolenaard9967712006-03-11 21:18:15 +00007416# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007417
Bram Moolenaarf740b292006-02-16 22:11:02 +00007418/*
7419 * Close window "win" and take care of handling closing the last window for a
7420 * modified buffer.
7421 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007423ex_win_close(
7424 int forceit,
7425 win_T *win,
7426 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427{
7428 int need_hide;
7429 buf_T *buf = win->w_buffer;
7430
7431 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007432 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007434# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 if ((p_confirm || cmdmod.confirm) && p_write)
7436 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007437 bufref_T bufref;
7438
7439 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007441 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 return;
7443 need_hide = FALSE;
7444 }
7445 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007446# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 {
7448 EMSG(_(e_nowrtmsg));
7449 return;
7450 }
7451 }
7452
Bram Moolenaard9967712006-03-11 21:18:15 +00007453# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007455# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007456
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007458 if (tp == NULL)
7459 win_close(win, !need_hide && !P_HID(buf));
7460 else
7461 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462}
7463
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007465 * Handle the argument for a tabpage related ex command.
7466 * Returns a tabpage number.
7467 * When an error is encountered then eap->errmsg is set.
7468 */
7469 static int
7470get_tabpage_arg(exarg_T *eap)
7471{
7472 int tab_number;
7473 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7474
7475 if (eap->arg && *eap->arg != NUL)
7476 {
7477 char_u *p = eap->arg;
7478 char_u *p_save;
7479 int relative = 0; /* argument +N/-N means: go to N places to the
7480 * right/left relative to the current position. */
7481
7482 if (*p == '-')
7483 {
7484 relative = -1;
7485 p++;
7486 }
7487 else if (*p == '+')
7488 {
7489 relative = 1;
7490 p++;
7491 }
7492
7493 p_save = p;
7494 tab_number = getdigits(&p);
7495
7496 if (relative == 0)
7497 {
7498 if (STRCMP(p, "$") == 0)
7499 tab_number = LAST_TAB_NR;
7500 else if (p == p_save || *p_save == '-' || *p != NUL
7501 || tab_number > LAST_TAB_NR)
7502 {
7503 /* No numbers as argument. */
7504 eap->errmsg = e_invarg;
7505 goto theend;
7506 }
7507 }
7508 else
7509 {
7510 if (*p_save == NUL)
7511 tab_number = 1;
7512 else if (p == p_save || *p_save == '-' || *p != NUL
7513 || tab_number == 0)
7514 {
7515 /* No numbers as argument. */
7516 eap->errmsg = e_invarg;
7517 goto theend;
7518 }
7519 tab_number = tab_number * relative + tabpage_index(curtab);
7520 if (!unaccept_arg0 && relative == -1)
7521 --tab_number;
7522 }
7523 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7524 eap->errmsg = e_invarg;
7525 }
7526 else if (eap->addr_count > 0)
7527 {
7528 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007529 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007530 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007531 tab_number = 0;
7532 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007533 else
7534 {
7535 tab_number = eap->line2;
7536 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7537 {
7538 --tab_number;
7539 if (tab_number < unaccept_arg0)
7540 eap->errmsg = e_invarg;
7541 }
7542 }
7543 }
7544 else
7545 {
7546 switch (eap->cmdidx)
7547 {
7548 case CMD_tabnext:
7549 tab_number = tabpage_index(curtab) + 1;
7550 if (tab_number > LAST_TAB_NR)
7551 tab_number = 1;
7552 break;
7553 case CMD_tabmove:
7554 tab_number = LAST_TAB_NR;
7555 break;
7556 default:
7557 tab_number = tabpage_index(curtab);
7558 }
7559 }
7560
7561theend:
7562 return tab_number;
7563}
7564
7565/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007566 * ":tabclose": close current tab page, unless it is the last one.
7567 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 */
7569 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007570ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007572 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007573 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007574
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007575# ifdef FEAT_CMDWIN
7576 if (cmdwin_type != 0)
7577 cmdwin_result = K_IGNORE;
7578 else
7579# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007580 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007581 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007582 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007584 tab_number = get_tabpage_arg(eap);
7585 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007586 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007587 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007588 if (tp == NULL)
7589 {
7590 beep_flush();
7591 return;
7592 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007593 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007594 {
7595 tabpage_close_other(tp, eap->forceit);
7596 return;
7597 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007598 else if (!text_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007599#ifdef FEAT_AUTOCMD
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007600 && !curbuf_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007601#endif
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007602 )
7603 tabpage_close(eap->forceit);
7604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007606}
7607
7608/*
7609 * ":tabonly": close all tab pages except the current one
7610 */
7611 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007612ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007613{
7614 tabpage_T *tp;
7615 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007616 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007617
7618# ifdef FEAT_CMDWIN
7619 if (cmdwin_type != 0)
7620 cmdwin_result = K_IGNORE;
7621 else
7622# endif
7623 if (first_tabpage->tp_next == NULL)
7624 MSG(_("Already only one tab page"));
7625 else
7626 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007627 tab_number = get_tabpage_arg(eap);
7628 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007629 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007630 goto_tabpage(tab_number);
7631 /* Repeat this up to a 1000 times, because autocommands may
7632 * mess up the lists. */
7633 for (done = 0; done < 1000; ++done)
7634 {
7635 FOR_ALL_TABPAGES(tp)
7636 if (tp->tp_topframe != topframe)
7637 {
7638 tabpage_close_other(tp, eap->forceit);
7639 /* if we failed to close it quit */
7640 if (valid_tabpage(tp))
7641 done = 1000;
7642 /* start over, "tp" is now invalid */
7643 break;
7644 }
7645 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007646 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007647 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007648 }
7649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651
7652/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007653 * Close the current tab page.
7654 */
7655 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007656tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007657{
7658 /* First close all the windows but the current one. If that worked then
7659 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007660 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007661 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007662 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007663 ex_win_close(forceit, curwin, NULL);
7664# ifdef FEAT_GUI
7665 need_mouse_correct = TRUE;
7666# endif
7667}
7668
7669/*
7670 * Close tab page "tp", which is not the current tab page.
7671 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007672 * Also takes care of the tab pages line disappearing when closing the
7673 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007674 */
7675 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007676tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007677{
7678 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007679 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007680 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007681
7682 /* Limit to 1000 windows, autocommands may add a window while we close
7683 * one. OK, so I'm paranoid... */
7684 while (++done < 1000)
7685 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007686 wp = tp->tp_firstwin;
7687 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007688
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007689 /* Autocommands may delete the tab page under our fingers and we may
7690 * fail to close a window with a modified buffer. */
7691 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007692 break;
7693 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007694
Bram Moolenaar29323592016-07-24 22:04:11 +02007695#ifdef FEAT_AUTOCMD
7696 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7697#endif
7698
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007699 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007700 if (h != tabline_height())
7701 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007702}
7703
7704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 * ":only".
7706 */
7707 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007708ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007710 win_T *wp;
7711 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712# ifdef FEAT_GUI
7713 need_mouse_correct = TRUE;
7714# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007715 if (eap->addr_count > 0)
7716 {
7717 wnr = eap->line2;
7718 for (wp = firstwin; --wnr > 0; )
7719 {
7720 if (wp->w_next == NULL)
7721 break;
7722 else
7723 wp = wp->w_next;
7724 }
7725 win_goto(wp);
7726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 close_others(TRUE, eap->forceit);
7728}
7729
7730/*
7731 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007732 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007734 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007735ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736{
7737 if (eap->addr_count == 0)
7738 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007739 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740}
7741#endif /* FEAT_WINDOWS */
7742
7743 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007744ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007746 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747#ifdef FEAT_WINDOWS
Bram Moolenaar2256c992016-11-15 21:17:07 +01007748 if (!eap->skip)
7749 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750# ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007751 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752# endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007753 if (eap->addr_count == 0)
7754 win_close(curwin, FALSE); /* don't free buffer */
7755 else
7756 {
7757 int winnr = 0;
7758 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007759
Bram Moolenaar2256c992016-11-15 21:17:07 +01007760 FOR_ALL_WINDOWS(win)
7761 {
7762 winnr++;
7763 if (winnr == eap->line2)
7764 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007765 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007766 if (win == NULL)
7767 win = lastwin;
7768 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772}
7773
7774/*
7775 * ":stop" and ":suspend": Suspend Vim.
7776 */
7777 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007778ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
7780 /*
7781 * Disallow suspending for "rvim".
7782 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007783 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 {
7785 if (!eap->forceit)
7786 autowrite_all();
7787 windgoto((int)Rows - 1, 0);
7788 out_char('\n');
7789 out_flush();
7790 stoptermcap();
7791 out_flush(); /* needed for SUN to restore xterm buffer */
7792#ifdef FEAT_TITLE
7793 mch_restore_title(3); /* restore window titles */
7794#endif
7795 ui_suspend(); /* call machine specific function */
7796#ifdef FEAT_TITLE
7797 maketitle();
7798 resettitle(); /* force updating the title */
7799#endif
7800 starttermcap();
7801 scroll_start(); /* scroll screen before redrawing */
7802 redraw_later_clear();
7803 shell_resized(); /* may have resized window */
7804 }
7805}
7806
7807/*
7808 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7809 */
7810 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007811ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
7813#ifdef FEAT_CMDWIN
7814 if (cmdwin_type != 0)
7815 {
7816 cmdwin_result = Ctrl_C;
7817 return;
7818 }
7819#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007820 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007821 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007822 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007823 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007824 return;
7825 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007826#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007827 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7828 /* Refuse to quit when locked or when the buffer in the last window is
7829 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007830 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007831 return;
7832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833
7834 /*
7835 * if more files or windows we won't exit
7836 */
7837 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7838 exiting = TRUE;
7839 if ( ((eap->cmdidx == CMD_wq
7840 || curbufIsChanged())
7841 && do_write(eap) == FAIL)
7842 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007843 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 {
7845 not_exiting();
7846 }
7847 else
7848 {
7849#ifdef FEAT_WINDOWS
7850 if (only_one_window()) /* quit last window, exit Vim */
7851#endif
7852 getout(0);
7853#ifdef FEAT_WINDOWS
7854# ifdef FEAT_GUI
7855 need_mouse_correct = TRUE;
7856# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007857 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 win_close(curwin, !P_HID(curwin->w_buffer));
7859#endif
7860 }
7861}
7862
7863/*
7864 * ":print", ":list", ":number".
7865 */
7866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007867ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007869 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7870 EMSG(_(e_emptybuf));
7871 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007873 for ( ;!got_int; ui_breakcheck())
7874 {
7875 print_line(eap->line1,
7876 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7877 || (eap->flags & EXFLAG_NR)),
7878 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7879 if (++eap->line1 > eap->line2)
7880 break;
7881 out_flush(); /* show one line at a time */
7882 }
7883 setpcmark();
7884 /* put cursor at last line */
7885 curwin->w_cursor.lnum = eap->line2;
7886 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 }
7888
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890}
7891
7892#ifdef FEAT_BYTEOFF
7893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007894ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895{
7896 goto_byte(eap->line2);
7897}
7898#endif
7899
7900/*
7901 * ":shell".
7902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007904ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905{
7906 do_shell(NULL, 0);
7907}
7908
7909#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7910 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007911 || defined(FEAT_GUI_MSWIN) \
7912 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 || defined(PROTO)
7914
7915/*
7916 * Handle a file drop. The code is here because a drop is *nearly* like an
7917 * :args command, but not quite (we have a list of exact filenames, so we
7918 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7919 * code is very similar to :args and hence needs access to a lot of the static
7920 * functions in this file.
7921 *
7922 * The list should be allocated using alloc(), as should each item in the
7923 * list. This function takes over responsibility for freeing the list.
7924 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007925 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007926 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 */
7928 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007929handle_drop(
7930 int filec, /* the number of files dropped */
7931 char_u **filev, /* the list of files dropped */
7932 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933{
7934 exarg_T ea;
7935 int save_msg_scroll = msg_scroll;
7936
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007937 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007938 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007940#ifdef FEAT_AUTOCMD
7941 if (curbuf_locked())
7942 return;
7943#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007944 /* When the screen is being updated we should not change buffers and
7945 * windows structures, it may cause freed memory to be used. */
7946 if (updating_screen)
7947 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948
7949 /* Check whether the current buffer is changed. If so, we will need
7950 * to split the current window or data could be lost.
7951 * We don't need to check if the 'hidden' option is set, as in this
7952 * case the buffer won't be lost.
7953 */
7954 if (!P_HID(curbuf) && !split)
7955 {
7956 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007957 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 --emsg_off;
7959 }
7960 if (split)
7961 {
7962# ifdef FEAT_WINDOWS
7963 if (win_split(0, 0) == FAIL)
7964 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007965 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966
7967 /* When splitting the window, create a new alist. Otherwise the
7968 * existing one is overwritten. */
7969 alist_unlink(curwin->w_alist);
7970 alist_new();
7971# else
7972 return; /* can't split, always fail */
7973# endif
7974 }
7975
7976 /*
7977 * Set up the new argument list.
7978 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007979 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980
7981 /*
7982 * Move to the first file.
7983 */
7984 /* Fake up a minimal "next" command for do_argfile() */
7985 vim_memset(&ea, 0, sizeof(ea));
7986 ea.cmd = (char_u *)"next";
7987 do_argfile(&ea, 0);
7988
7989 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7990 * mode that is not needed here. */
7991 need_start_insertmode = FALSE;
7992
7993 /* Restore msg_scroll, otherwise a following command may cause scrolling
7994 * unexpectedly. The screen will be redrawn by the caller, thus
7995 * msg_scroll being set by displaying a message is irrelevant. */
7996 msg_scroll = save_msg_scroll;
7997}
7998#endif
7999
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000/*
8001 * Clear an argument list: free all file names and reset it to zero entries.
8002 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008003 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008004alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005{
8006 while (--al->al_ga.ga_len >= 0)
8007 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8008 ga_clear(&al->al_ga);
8009}
8010
8011/*
8012 * Init an argument list.
8013 */
8014 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008015alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016{
8017 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8018}
8019
8020#if defined(FEAT_WINDOWS) || defined(PROTO)
8021
8022/*
8023 * Remove a reference from an argument list.
8024 * Ignored when the argument list is the global one.
8025 * If the argument list is no longer used by any window, free it.
8026 */
8027 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008028alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029{
8030 if (al != &global_alist && --al->al_refcount <= 0)
8031 {
8032 alist_clear(al);
8033 vim_free(al);
8034 }
8035}
8036
8037# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
8038/*
8039 * Create a new argument list and use it for the current window.
8040 */
8041 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008042alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043{
8044 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8045 if (curwin->w_alist == NULL)
8046 {
8047 curwin->w_alist = &global_alist;
8048 ++global_alist.al_refcount;
8049 }
8050 else
8051 {
8052 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008053 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 alist_init(curwin->w_alist);
8055 }
8056}
8057# endif
8058#endif
8059
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008060#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061/*
8062 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008063 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8064 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 */
8066 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008067alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068{
8069 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008070 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 char_u **new_arg_files;
8072 int new_arg_file_count;
8073 char_u *save_p_su = p_su;
8074 int i;
8075
8076 /* Don't use 'suffixes' here. This should work like the shell did the
8077 * expansion. Also, the vimrc file isn't read yet, thus the user
8078 * can't set the options. */
8079 p_su = empty_option;
8080 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8081 if (old_arg_files != NULL)
8082 {
8083 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008084 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8085 old_arg_count = GARGCOUNT;
8086 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008088 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 && new_arg_file_count > 0)
8090 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008091 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8092 TRUE, fnum_list, fnum_len);
8093 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 }
8096 p_su = save_p_su;
8097}
8098#endif
8099
8100/*
8101 * Set the argument list for the current window.
8102 * Takes over the allocated files[] and the allocated fnames in it.
8103 */
8104 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008105alist_set(
8106 alist_T *al,
8107 int count,
8108 char_u **files,
8109 int use_curbuf,
8110 int *fnum_list,
8111 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112{
8113 int i;
8114
8115 alist_clear(al);
8116 if (ga_grow(&al->al_ga, count) == OK)
8117 {
8118 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008119 {
8120 if (got_int)
8121 {
8122 /* When adding many buffers this can take a long time. Allow
8123 * interrupting here. */
8124 while (i < count)
8125 vim_free(files[i++]);
8126 break;
8127 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008128
8129 /* May set buffer name of a buffer previously used for the
8130 * argument list, so that it's re-used by alist_add. */
8131 if (fnum_list != NULL && i < fnum_len)
8132 buf_set_name(fnum_list[i], files[i]);
8133
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008135 ui_breakcheck();
8136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 vim_free(files);
8138 }
8139 else
8140 FreeWild(count, files);
8141#ifdef FEAT_WINDOWS
8142 if (al == &global_alist)
8143#endif
8144 arg_had_last = FALSE;
8145}
8146
8147/*
8148 * Add file "fname" to argument list "al".
8149 * "fname" must have been allocated and "al" must have been checked for room.
8150 */
8151 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008152alist_add(
8153 alist_T *al,
8154 char_u *fname,
8155 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156{
8157 if (fname == NULL) /* don't add NULL file names */
8158 return;
8159#ifdef BACKSLASH_IN_FILENAME
8160 slash_adjust(fname);
8161#endif
8162 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8163 if (set_fnum > 0)
8164 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8165 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8166 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167}
8168
8169#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8170/*
8171 * Adjust slashes in file names. Called after 'shellslash' was set.
8172 */
8173 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008174alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175{
8176 int i;
8177# ifdef FEAT_WINDOWS
8178 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008179 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180# endif
8181
8182 for (i = 0; i < GARGCOUNT; ++i)
8183 if (GARGLIST[i].ae_fname != NULL)
8184 slash_adjust(GARGLIST[i].ae_fname);
8185# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00008186 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 if (wp->w_alist != &global_alist)
8188 for (i = 0; i < WARGCOUNT(wp); ++i)
8189 if (WARGLIST(wp)[i].ae_fname != NULL)
8190 slash_adjust(WARGLIST(wp)[i].ae_fname);
8191# endif
8192}
8193#endif
8194
8195/*
8196 * ":preserve".
8197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008199ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008201 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 ml_preserve(curbuf, TRUE);
8203}
8204
8205/*
8206 * ":recover".
8207 */
8208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008209ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210{
8211 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8212 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008213 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8214 | CCGD_MULTWIN
8215 | (eap->forceit ? CCGD_FORCEIT : 0)
8216 | CCGD_EXCMD)
8217
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 && (*eap->arg == NUL
8219 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8220 ml_recover();
8221 recoverymode = FALSE;
8222}
8223
8224/*
8225 * Command modifier used in a wrong way.
8226 */
8227 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008228ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229{
8230 eap->errmsg = e_invcmd;
8231}
8232
8233#ifdef FEAT_WINDOWS
8234/*
8235 * :sview [+command] file split window with new file, read-only
8236 * :split [[+command] file] split window with current or new file
8237 * :vsplit [[+command] file] split window vertically with current or new file
8238 * :new [[+command] file] split window with no or new file
8239 * :vnew [[+command] file] split vertically window with no or new file
8240 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008241 *
8242 * :tabedit open new Tab page with empty window
8243 * :tabedit [+command] file open new Tab page and edit "file"
8244 * :tabnew [[+command] file] just like :tabedit
8245 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 */
8247 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008248ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008250 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008251# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008253# endif
8254# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008258# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008262# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008264 * quickfix windows. But it's OK when doing ":tab split". */
8265 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {
8267 if (eap->cmdidx == CMD_split)
8268 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 if (eap->cmdidx == CMD_vsplit)
8270 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008272# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008274# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008275 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 {
8277 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8278 FNAME_MESS, TRUE, curbuf->b_ffname);
8279 if (fname == NULL)
8280 goto theend;
8281 eap->arg = fname;
8282 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008283# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008285# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008287# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 && eap->cmdidx != CMD_new)
8291 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008292# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008293 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008294# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008295 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008296# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008297 au_has_group((char_u *)"FileExplorer"))
8298 {
8299 /* No browsing supported but we do have the file explorer:
8300 * Edit the directory. */
8301 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8302 eap->arg = (char_u *)".";
8303 }
8304 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008305# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008306 {
8307 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008309 if (fname == NULL)
8310 goto theend;
8311 eap->arg = fname;
8312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 }
8314 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008315# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008317 /*
8318 * Either open new tab page or split the window.
8319 */
8320 if (eap->cmdidx == CMD_tabedit
8321 || eap->cmdidx == CMD_tabfind
8322 || eap->cmdidx == CMD_tabnew)
8323 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008324 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8325 : eap->addr_count == 0 ? 0
8326 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008327 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008328 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008329
8330 /* set the alternate buffer for the window we came from */
8331 if (curwin != old_curwin
8332 && win_valid(old_curwin)
8333 && old_curwin->w_buffer != curbuf
8334 && !cmdmod.keepalt)
8335 old_curwin->w_alt_fnum = curbuf->b_fnum;
8336 }
8337 }
8338 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8340 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008341# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 /* Reset 'scrollbind' when editing another file, but keep it when
8343 * doing ":split" without arguments. */
8344 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008345# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008347# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008349 {
8350 RESET_BINDING(curwin);
8351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 else
8353 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008354# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 do_exedit(eap, old_curwin);
8356 }
8357
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008358# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008360# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008362# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363theend:
8364 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008367
8368/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008369 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008370 */
8371 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008372tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008373{
8374 exarg_T ea;
8375
8376 vim_memset(&ea, 0, sizeof(ea));
8377 ea.cmdidx = CMD_tabnew;
8378 ea.cmd = (char_u *)"tabn";
8379 ea.arg = (char_u *)"";
8380 ex_splitview(&ea);
8381}
8382
8383/*
8384 * :tabnext command
8385 */
8386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008387ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008388{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008389 int tab_number;
8390
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008391 switch (eap->cmdidx)
8392 {
8393 case CMD_tabfirst:
8394 case CMD_tabrewind:
8395 goto_tabpage(1);
8396 break;
8397 case CMD_tablast:
8398 goto_tabpage(9999);
8399 break;
8400 case CMD_tabprevious:
8401 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008402 if (eap->arg && *eap->arg != NUL)
8403 {
8404 char_u *p = eap->arg;
8405 char_u *p_save = p;
8406
8407 tab_number = getdigits(&p);
8408 if (p == p_save || *p_save == '-' || *p != NUL
8409 || tab_number == 0)
8410 {
8411 /* No numbers as argument. */
8412 eap->errmsg = e_invarg;
8413 return;
8414 }
8415 }
8416 else
8417 {
8418 if (eap->addr_count == 0)
8419 tab_number = 1;
8420 else
8421 {
8422 tab_number = eap->line2;
8423 if (tab_number < 1)
8424 {
8425 eap->errmsg = e_invrange;
8426 return;
8427 }
8428 }
8429 }
8430 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008431 break;
8432 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008433 tab_number = get_tabpage_arg(eap);
8434 if (eap->errmsg == NULL)
8435 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008436 break;
8437 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008438}
8439
8440/*
8441 * :tabmove command
8442 */
8443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008444ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008445{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008446 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008447
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008448 tab_number = get_tabpage_arg(eap);
8449 if (eap->errmsg == NULL)
8450 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008451}
8452
8453/*
8454 * :tabs command: List tabs and their contents.
8455 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008457ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008458{
8459 tabpage_T *tp;
8460 win_T *wp;
8461 int tabcount = 1;
8462
8463 msg_start();
8464 msg_scroll = TRUE;
8465 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8466 {
8467 msg_putchar('\n');
8468 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008469 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008470 out_flush(); /* output one line at a time */
8471 ui_breakcheck();
8472
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008473 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008474 wp = firstwin;
8475 else
8476 wp = tp->tp_firstwin;
8477 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8478 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008479 msg_putchar('\n');
8480 msg_putchar(wp == curwin ? '>' : ' ');
8481 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008482 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8483 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008484 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008485 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008486 else
8487 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8488 IObuff, IOSIZE, TRUE);
8489 msg_outtrans(IObuff);
8490 out_flush(); /* output one line at a time */
8491 ui_breakcheck();
8492 }
8493 }
8494}
8495
8496#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497
8498/*
8499 * ":mode": Set screen mode.
8500 * If no argument given, just get the screen size and redraw.
8501 */
8502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008503ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504{
8505 if (*eap->arg == NUL)
8506 shell_resized();
8507 else
8508 mch_screenmode(eap->arg);
8509}
8510
8511#ifdef FEAT_WINDOWS
8512/*
8513 * ":resize".
8514 * set, increment or decrement current window height
8515 */
8516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008517ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518{
8519 int n;
8520 win_T *wp = curwin;
8521
8522 if (eap->addr_count > 0)
8523 {
8524 n = eap->line2;
8525 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8526 ;
8527 }
8528
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008529# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008531# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 if (cmdmod.split & WSP_VERT)
8534 {
8535 if (*eap->arg == '-' || *eap->arg == '+')
8536 n += W_WIDTH(curwin);
8537 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8538 n = 9999;
8539 win_setwidth_win((int)n, wp);
8540 }
8541 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 {
8543 if (*eap->arg == '-' || *eap->arg == '+')
8544 n += curwin->w_height;
8545 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8546 n = 9999;
8547 win_setheight_win((int)n, wp);
8548 }
8549}
8550#endif
8551
8552/*
8553 * ":find [+command] <file>" command.
8554 */
8555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008556ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557{
8558#ifdef FEAT_SEARCHPATH
8559 char_u *fname;
8560 int count;
8561
8562 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8563 TRUE, curbuf->b_ffname);
8564 if (eap->addr_count > 0)
8565 {
8566 /* Repeat finding the file "count" times. This matters when it
8567 * appears several times in the path. */
8568 count = eap->line2;
8569 while (fname != NULL && --count > 0)
8570 {
8571 vim_free(fname);
8572 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8573 FALSE, curbuf->b_ffname);
8574 }
8575 }
8576
8577 if (fname != NULL)
8578 {
8579 eap->arg = fname;
8580#endif
8581 do_exedit(eap, NULL);
8582#ifdef FEAT_SEARCHPATH
8583 vim_free(fname);
8584 }
8585#endif
8586}
8587
8588/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008589 * ":open" simulation: for now just work like ":visual".
8590 */
8591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008592ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008593{
8594 regmatch_T regmatch;
8595 char_u *p;
8596
8597 curwin->w_cursor.lnum = eap->line2;
8598 beginline(BL_SOL | BL_FIX);
8599 if (*eap->arg == '/')
8600 {
8601 /* ":open /pattern/": put cursor in column found with pattern */
8602 ++eap->arg;
8603 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8604 *p = NUL;
8605 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8606 if (regmatch.regprog != NULL)
8607 {
8608 regmatch.rm_ic = p_ic;
8609 p = ml_get_curline();
8610 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008611 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008612 else
8613 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008614 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008615 }
8616 /* Move to the NUL, ignore any other arguments. */
8617 eap->arg += STRLEN(eap->arg);
8618 }
8619 check_cursor();
8620
8621 eap->cmdidx = CMD_visual;
8622 do_exedit(eap, NULL);
8623}
8624
8625/*
8626 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 */
8628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008629ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630{
8631 do_exedit(eap, NULL);
8632}
8633
8634/*
8635 * ":edit <file>" command and alikes.
8636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008638do_exedit(
8639 exarg_T *eap,
8640 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641{
8642 int n;
8643#ifdef FEAT_WINDOWS
8644 int need_hide;
8645#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008646 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647
8648 /*
8649 * ":vi" command ends Ex mode.
8650 */
8651 if (exmode_active && (eap->cmdidx == CMD_visual
8652 || eap->cmdidx == CMD_view))
8653 {
8654 exmode_active = FALSE;
8655 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008656 {
8657 /* Special case: ":global/pat/visual\NLvi-commands" */
8658 if (global_busy)
8659 {
8660 int rd = RedrawingDisabled;
8661 int nwr = no_wait_return;
8662 int ms = msg_scroll;
8663#ifdef FEAT_GUI
8664 int he = hold_gui_events;
8665#endif
8666
8667 if (eap->nextcmd != NULL)
8668 {
8669 stuffReadbuff(eap->nextcmd);
8670 eap->nextcmd = NULL;
8671 }
8672
8673 if (exmode_was != EXMODE_VIM)
8674 settmode(TMODE_RAW);
8675 RedrawingDisabled = 0;
8676 no_wait_return = 0;
8677 need_wait_return = FALSE;
8678 msg_scroll = 0;
8679#ifdef FEAT_GUI
8680 hold_gui_events = 0;
8681#endif
8682 must_redraw = CLEAR;
8683
8684 main_loop(FALSE, TRUE);
8685
8686 RedrawingDisabled = rd;
8687 no_wait_return = nwr;
8688 msg_scroll = ms;
8689#ifdef FEAT_GUI
8690 hold_gui_events = he;
8691#endif
8692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 }
8696
8697 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008698 || eap->cmdidx == CMD_tabnew
8699 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008700#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 || eap->cmdidx == CMD_vnew
8702#endif
8703 ) && *eap->arg == NUL)
8704 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008705 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 setpcmark();
8707 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008708 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8709 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 }
8711 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008712#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 && eap->cmdidx != CMD_vsplit
8714#endif
8715 )
8716 || *eap->arg != NUL
8717#ifdef FEAT_BROWSE
8718 || cmdmod.browse
8719#endif
8720 )
8721 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008722#ifdef FEAT_AUTOCMD
8723 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8724 * can bring us here, others are stopped earlier. */
8725 if (*eap->arg != NUL && curbuf_locked())
8726 return;
8727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 n = readonlymode;
8729 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8730 readonlymode = TRUE;
8731 else if (eap->cmdidx == CMD_enew)
8732 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8733 empty buffer */
8734 setpcmark();
8735 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8736 NULL, eap,
8737 /* ":edit" goes to first line if Vi compatible */
8738 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8739 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8740 ? ECMD_ONE : eap->do_ecmd_lnum,
8741 (P_HID(curbuf) ? ECMD_HIDE : 0)
8742 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008743 /* after a split we can use an existing buffer */
8744 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745#ifdef FEAT_LISTCMDS
8746 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8747#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008748 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 {
8750 /* Editing the file failed. If the window was split, close it. */
8751#ifdef FEAT_WINDOWS
8752 if (old_curwin != NULL)
8753 {
8754 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8755 if (!need_hide || P_HID(curbuf))
8756 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008757# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8758 cleanup_T cs;
8759
8760 /* Reset the error/interrupt/exception state here so that
8761 * aborting() returns FALSE when closing a window. */
8762 enter_cleanup(&cs);
8763# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764# ifdef FEAT_GUI
8765 need_mouse_correct = TRUE;
8766# endif
8767 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008768
8769# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8770 /* Restore the error/interrupt/exception state if not
8771 * discarded by a new aborting error, interrupt, or
8772 * uncaught exception. */
8773 leave_cleanup(&cs);
8774# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 }
8776 }
8777#endif
8778 }
8779 else if (readonlymode && curbuf->b_nwindows == 1)
8780 {
8781 /* When editing an already visited buffer, 'readonly' won't be set
8782 * but the previous value is kept. With ":view" and ":sview" we
8783 * want the file to be readonly, except when another window is
8784 * editing the same buffer. */
8785 curbuf->b_p_ro = TRUE;
8786 }
8787 readonlymode = n;
8788 }
8789 else
8790 {
8791 if (eap->do_ecmd_cmd != NULL)
8792 do_cmdline_cmd(eap->do_ecmd_cmd);
8793#ifdef FEAT_TITLE
8794 n = curwin->w_arg_idx_invalid;
8795#endif
8796 check_arg_idx(curwin);
8797#ifdef FEAT_TITLE
8798 if (n != curwin->w_arg_idx_invalid)
8799 maketitle();
8800#endif
8801 }
8802
8803#ifdef FEAT_WINDOWS
8804 /*
8805 * if ":split file" worked, set alternate file name in old window to new
8806 * file
8807 */
8808 if (old_curwin != NULL
8809 && *eap->arg != NUL
8810 && curwin != old_curwin
8811 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008812 && old_curwin->w_buffer != curbuf
8813 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 old_curwin->w_alt_fnum = curbuf->b_fnum;
8815#endif
8816
8817 ex_no_reprint = TRUE;
8818}
8819
8820#ifndef FEAT_GUI
8821/*
8822 * ":gui" and ":gvim" when there is no GUI.
8823 */
8824 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008825ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826{
8827 eap->errmsg = e_nogvim;
8828}
8829#endif
8830
8831#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8832 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008833ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834{
8835 gui_make_tearoff(eap->arg);
8836}
8837#endif
8838
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008839#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008841ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008843 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844}
8845#endif
8846
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008848ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849{
8850 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8851 MSG(_("No swap file"));
8852 else
8853 msg(curbuf->b_ml.ml_mfp->mf_fname);
8854}
8855
8856/*
8857 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8858 * offset.
8859 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008862ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863{
8864#ifdef FEAT_SCROLLBIND
8865 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008866 win_T *save_curwin = curwin;
8867 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 long topline;
8869 long y;
8870 linenr_T old_linenr = curwin->w_cursor.lnum;
8871
8872 setpcmark();
8873
8874 /*
8875 * determine max topline
8876 */
8877 if (curwin->w_p_scb)
8878 {
8879 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008880 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 {
8882 if (wp->w_p_scb && wp->w_buffer)
8883 {
8884 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8885 if (topline > y)
8886 topline = y;
8887 }
8888 }
8889 if (topline < 1)
8890 topline = 1;
8891 }
8892 else
8893 {
8894 topline = 1;
8895 }
8896
8897
8898 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008899 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008901 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 {
8903 if (curwin->w_p_scb)
8904 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008905 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 y = topline - curwin->w_topline;
8907 if (y > 0)
8908 scrollup(y, TRUE);
8909 else
8910 scrolldown(-y, TRUE);
8911 curwin->w_scbind_pos = topline;
8912 redraw_later(VALID);
8913 cursor_correct();
8914#ifdef FEAT_WINDOWS
8915 curwin->w_redr_status = TRUE;
8916#endif
8917 }
8918 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008919 curwin = save_curwin;
8920 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 if (curwin->w_p_scb)
8922 {
8923 did_syncbind = TRUE;
8924 checkpcmark();
8925 if (old_linenr != curwin->w_cursor.lnum)
8926 {
8927 char_u ctrl_o[2];
8928
8929 ctrl_o[0] = Ctrl_O;
8930 ctrl_o[1] = 0;
8931 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8932 }
8933 }
8934#endif
8935}
8936
8937
8938 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008939ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008941 int i;
8942 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8943 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944
8945 if (eap->usefilter) /* :r!cmd */
8946 do_bang(1, eap, FALSE, FALSE, TRUE);
8947 else
8948 {
8949 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8950 return;
8951
8952#ifdef FEAT_BROWSE
8953 if (cmdmod.browse)
8954 {
8955 char_u *browseFile;
8956
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008957 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 NULL, NULL, NULL, curbuf);
8959 if (browseFile != NULL)
8960 {
8961 i = readfile(browseFile, NULL,
8962 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8963 vim_free(browseFile);
8964 }
8965 else
8966 i = OK;
8967 }
8968 else
8969#endif
8970 if (*eap->arg == NUL)
8971 {
8972 if (check_fname() == FAIL) /* check for no file name */
8973 return;
8974 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8975 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8976 }
8977 else
8978 {
8979 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8980 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8981 i = readfile(eap->arg, NULL,
8982 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8983
8984 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008985 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 {
8987#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8988 if (!aborting())
8989#endif
8990 EMSG2(_(e_notopen), eap->arg);
8991 }
8992 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008993 {
8994 if (empty && exmode_active)
8995 {
8996 /* Delete the empty line that remains. Historically ex does
8997 * this but vi doesn't. */
8998 if (eap->line2 == 0)
8999 lnum = curbuf->b_ml.ml_line_count;
9000 else
9001 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009002 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009003 {
9004 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009005 if (curwin->w_cursor.lnum > 1
9006 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009007 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009008 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009009 }
9010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 }
9014}
9015
Bram Moolenaarea408852005-06-25 22:49:46 +00009016static char_u *prev_dir = NULL;
9017
9018#if defined(EXITFREE) || defined(PROTO)
9019 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009020free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009021{
9022 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00009023 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00009024
9025 vim_free(globaldir);
9026 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00009027}
9028#endif
9029
Bram Moolenaarf4258302013-06-02 18:20:17 +02009030/*
9031 * Deal with the side effects of changing the current directory.
9032 * When "local" is TRUE then this was after an ":lcd" command.
9033 */
9034 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009035post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009036{
9037 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01009038 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02009039 if (local)
9040 {
9041 /* If still in global directory, need to remember current
9042 * directory as global directory. */
9043 if (globaldir == NULL && prev_dir != NULL)
9044 globaldir = vim_strsave(prev_dir);
9045 /* Remember this local directory for the window. */
9046 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9047 curwin->w_localdir = vim_strsave(NameBuff);
9048 }
9049 else
9050 {
9051 /* We are now in the global directory, no need to remember its
9052 * name. */
9053 vim_free(globaldir);
9054 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02009055 }
9056
9057 shorten_fnames(TRUE);
9058}
9059
Bram Moolenaarea408852005-06-25 22:49:46 +00009060
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061/*
9062 * ":cd", ":lcd", ":chdir" and ":lchdir".
9063 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009064 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009065ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 char_u *new_dir;
9068 char_u *tofree;
9069
9070 new_dir = eap->arg;
9071#if !defined(UNIX) && !defined(VMS)
9072 /* for non-UNIX ":cd" means: print current directory */
9073 if (*new_dir == NUL)
9074 ex_pwd(NULL);
9075 else
9076#endif
9077 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009078#ifdef FEAT_AUTOCMD
9079 if (allbuf_locked())
9080 return;
9081#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009082 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9083 && !eap->forceit)
9084 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009085 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009086 return;
9087 }
9088
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 /* ":cd -": Change to previous directory */
9090 if (STRCMP(new_dir, "-") == 0)
9091 {
9092 if (prev_dir == NULL)
9093 {
9094 EMSG(_("E186: No previous directory"));
9095 return;
9096 }
9097 new_dir = prev_dir;
9098 }
9099
9100 /* Save current directory for next ":cd -" */
9101 tofree = prev_dir;
9102 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9103 prev_dir = vim_strsave(NameBuff);
9104 else
9105 prev_dir = NULL;
9106
9107#if defined(UNIX) || defined(VMS)
9108 /* for UNIX ":cd" means: go to home directory */
9109 if (*new_dir == NUL)
9110 {
9111 /* use NameBuff for home directory name */
9112# ifdef VMS
9113 char_u *p;
9114
9115 p = mch_getenv((char_u *)"SYS$LOGIN");
9116 if (p == NULL || *p == NUL) /* empty is the same as not set */
9117 NameBuff[0] = NUL;
9118 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009119 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120# else
9121 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9122# endif
9123 new_dir = NameBuff;
9124 }
9125#endif
9126 if (new_dir == NULL || vim_chdir(new_dir))
9127 EMSG(_(e_failed));
9128 else
9129 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02009130 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131
9132 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009133 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 ex_pwd(eap);
9135 }
9136 vim_free(tofree);
9137 }
9138}
9139
9140/*
9141 * ":pwd".
9142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009144ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145{
9146 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9147 {
9148#ifdef BACKSLASH_IN_FILENAME
9149 slash_adjust(NameBuff);
9150#endif
9151 msg(NameBuff);
9152 }
9153 else
9154 EMSG(_("E187: Unknown"));
9155}
9156
9157/*
9158 * ":=".
9159 */
9160 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009161ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162{
9163 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009164 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165}
9166
9167 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009168ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009170 int n;
9171 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172
9173 if (cursor_valid())
9174 {
9175 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9176 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01009177 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009179
9180 len = eap->line2;
9181 switch (*eap->arg)
9182 {
9183 case 'm': break;
9184 case NUL: len *= 1000L; break;
9185 default: EMSG2(_(e_invarg2), eap->arg); return;
9186 }
9187 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188}
9189
9190/*
9191 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9192 */
9193 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009194do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195{
9196 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009197 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198
9199 cursor_on();
9200 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009201 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009203 wait_now = msec - done > 1000L ? 1000L : msec - done;
9204#ifdef FEAT_TIMERS
9205 {
9206 long due_time = check_due_timer();
9207
9208 if (due_time > 0 && due_time < wait_now)
9209 wait_now = due_time;
9210 }
9211#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009212#ifdef FEAT_JOB_CHANNEL
9213 if (has_any_channel() && wait_now > 100L)
9214 wait_now = 100L;
9215#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009216 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009217#ifdef FEAT_JOB_CHANNEL
9218 if (has_any_channel())
9219 ui_breakcheck_force(TRUE);
9220 else
9221#endif
9222 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009223#ifdef MESSAGE_QUEUE
9224 /* Process the netbeans and clientserver messages that may have been
9225 * received in the call to ui_breakcheck() when the GUI is in use. This
9226 * may occur when running a test case. */
9227 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 }
9230}
9231
9232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009233do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234{
9235 int mode;
9236 char_u *cmdp;
9237
9238 cmdp = eap->cmd;
9239 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9240
9241 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9242 eap->arg, mode, isabbrev))
9243 {
9244 case 1: EMSG(_(e_invarg));
9245 break;
9246 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9247 break;
9248 }
9249}
9250
9251/*
9252 * ":winsize" command (obsolete).
9253 */
9254 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009255ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256{
9257 int w, h;
9258 char_u *arg = eap->arg;
9259 char_u *p;
9260
9261 w = getdigits(&arg);
9262 arg = skipwhite(arg);
9263 p = arg;
9264 h = getdigits(&arg);
9265 if (*p != NUL && *arg == NUL)
9266 set_shellsize(w, h, TRUE);
9267 else
9268 EMSG(_("E465: :winsize requires two number arguments"));
9269}
9270
9271#ifdef FEAT_WINDOWS
9272 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009273ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274{
9275 int xchar = NUL;
9276 char_u *p;
9277
9278 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9279 {
9280 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9281 if (eap->arg[1] == NUL)
9282 {
9283 EMSG(_(e_invarg));
9284 return;
9285 }
9286 xchar = eap->arg[1];
9287 p = eap->arg + 2;
9288 }
9289 else
9290 p = eap->arg + 1;
9291
9292 eap->nextcmd = check_nextcmd(p);
9293 p = skipwhite(p);
9294 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9295 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009296 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 {
9298 /* Pass flags on for ":vertical wincmd ]". */
9299 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009300 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9302 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009303 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 }
9305}
9306#endif
9307
Bram Moolenaar843ee412004-06-30 16:16:41 +00009308#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309/*
9310 * ":winpos".
9311 */
9312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009313ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314{
9315 int x, y;
9316 char_u *arg = eap->arg;
9317 char_u *p;
9318
9319 if (*arg == NUL)
9320 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009321# if defined(FEAT_GUI) || defined(MSWIN)
9322# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009324# else
9325 if (mch_get_winpos(&x, &y) != FAIL)
9326# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 {
9328 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9329 msg(IObuff);
9330 }
9331 else
9332# endif
9333 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9334 }
9335 else
9336 {
9337 x = getdigits(&arg);
9338 arg = skipwhite(arg);
9339 p = arg;
9340 y = getdigits(&arg);
9341 if (*p == NUL || *arg != NUL)
9342 {
9343 EMSG(_("E466: :winpos requires two number arguments"));
9344 return;
9345 }
9346# ifdef FEAT_GUI
9347 if (gui.in_use)
9348 gui_mch_set_winpos(x, y);
9349 else if (gui.starting)
9350 {
9351 /* Remember the coordinates for when the window is opened. */
9352 gui_win_x = x;
9353 gui_win_y = y;
9354 }
9355# ifdef HAVE_TGETENT
9356 else
9357# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009358# else
9359# ifdef MSWIN
9360 mch_set_winpos(x, y);
9361# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362# endif
9363# ifdef HAVE_TGETENT
9364 if (*T_CWP)
9365 term_set_winpos(x, y);
9366# endif
9367 }
9368}
9369#endif
9370
9371/*
9372 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9373 */
9374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009375ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376{
9377 oparg_T oa;
9378
9379 clear_oparg(&oa);
9380 oa.regname = eap->regname;
9381 oa.start.lnum = eap->line1;
9382 oa.end.lnum = eap->line2;
9383 oa.line_count = eap->line2 - eap->line1 + 1;
9384 oa.motion_type = MLINE;
9385#ifdef FEAT_VIRTUALEDIT
9386 virtual_op = FALSE;
9387#endif
9388 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9389 {
9390 setpcmark();
9391 curwin->w_cursor.lnum = eap->line1;
9392 beginline(BL_SOL | BL_FIX);
9393 }
9394
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009395 if (VIsual_active)
9396 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009397
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 switch (eap->cmdidx)
9399 {
9400 case CMD_delete:
9401 oa.op_type = OP_DELETE;
9402 op_delete(&oa);
9403 break;
9404
9405 case CMD_yank:
9406 oa.op_type = OP_YANK;
9407 (void)op_yank(&oa, FALSE, TRUE);
9408 break;
9409
9410 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009411 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009413 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9414#else
9415 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009417 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 oa.op_type = OP_RSHIFT;
9419 else
9420 oa.op_type = OP_LSHIFT;
9421 op_shift(&oa, FALSE, eap->amount);
9422 break;
9423 }
9424#ifdef FEAT_VIRTUALEDIT
9425 virtual_op = MAYBE;
9426#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009427 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428}
9429
9430/*
9431 * ":put".
9432 */
9433 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009434ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435{
9436 /* ":0put" works like ":1put!". */
9437 if (eap->line2 == 0)
9438 {
9439 eap->line2 = 1;
9440 eap->forceit = TRUE;
9441 }
9442 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009443 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9444 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445}
9446
9447/*
9448 * Handle ":copy" and ":move".
9449 */
9450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009451ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452{
9453 long n;
9454
Bram Moolenaarded27822017-01-02 14:27:34 +01009455 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 if (eap->arg == NULL) /* error detected */
9457 {
9458 eap->nextcmd = NULL;
9459 return;
9460 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009461 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462
9463 /*
9464 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9465 */
9466 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9467 {
9468 EMSG(_(e_invaddr));
9469 return;
9470 }
9471
9472 if (eap->cmdidx == CMD_move)
9473 {
9474 if (do_move(eap->line1, eap->line2, n) == FAIL)
9475 return;
9476 }
9477 else
9478 ex_copy(eap->line1, eap->line2, n);
9479 u_clearline();
9480 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009481 ex_may_print(eap);
9482}
9483
9484/*
9485 * Print the current line if flags were given to the Ex command.
9486 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009487 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009488ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009489{
9490 if (eap->flags != 0)
9491 {
9492 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9493 (eap->flags & EXFLAG_LIST));
9494 ex_no_reprint = TRUE;
9495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496}
9497
9498/*
9499 * ":smagic" and ":snomagic".
9500 */
9501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009502ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503{
9504 int magic_save = p_magic;
9505
9506 p_magic = (eap->cmdidx == CMD_smagic);
9507 do_sub(eap);
9508 p_magic = magic_save;
9509}
9510
9511/*
9512 * ":join".
9513 */
9514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009515ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516{
9517 curwin->w_cursor.lnum = eap->line1;
9518 if (eap->line1 == eap->line2)
9519 {
9520 if (eap->addr_count >= 2) /* :2,2join does nothing */
9521 return;
9522 if (eap->line2 == curbuf->b_ml.ml_line_count)
9523 {
9524 beep_flush();
9525 return;
9526 }
9527 ++eap->line2;
9528 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009529 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009531 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532}
9533
9534/*
9535 * ":[addr]@r" or ":[addr]*r": execute register
9536 */
9537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009538ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539{
9540 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009541 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542
9543 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009544 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545
9546#ifdef USE_ON_FLY_SCROLL
9547 dont_scroll = TRUE; /* disallow scrolling here */
9548#endif
9549
9550 /* get the register name. No name means to use the previous one */
9551 c = *eap->arg;
9552 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9553 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009554 /* Put the register in the typeahead buffer with the "silent" flag. */
9555 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9556 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 else
9561 {
9562 int save_efr = exec_from_reg;
9563
9564 exec_from_reg = TRUE;
9565
9566 /*
9567 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009568 * Continue until the stuff buffer is empty and all added characters
9569 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009571 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9573
9574 exec_from_reg = save_efr;
9575 }
9576}
9577
9578/*
9579 * ":!".
9580 */
9581 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009582ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583{
9584 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9585}
9586
9587/*
9588 * ":undo".
9589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009591ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009593 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009594 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009595 else
9596 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597}
9598
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009599#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009600 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009601ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009602{
9603 char_u hash[UNDO_HASH_SIZE];
9604
9605 u_compute_hash(hash);
9606 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9607}
9608
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009609 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009610ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009611{
9612 char_u hash[UNDO_HASH_SIZE];
9613
9614 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009615 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009616}
9617#endif
9618
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619/*
9620 * ":redo".
9621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009623ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624{
9625 u_redo(1);
9626}
9627
9628/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009629 * ":earlier" and ":later".
9630 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009631 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009632ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009633{
9634 long count = 0;
9635 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009636 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009637 char_u *p = eap->arg;
9638
9639 if (*p == NUL)
9640 count = 1;
9641 else if (isdigit(*p))
9642 {
9643 count = getdigits(&p);
9644 switch (*p)
9645 {
9646 case 's': ++p; sec = TRUE; break;
9647 case 'm': ++p; sec = TRUE; count *= 60; break;
9648 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009649 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9650 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009651 }
9652 }
9653
9654 if (*p != NUL)
9655 EMSG2(_(e_invarg2), eap->arg);
9656 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009657 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9658 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009659}
9660
9661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 * ":redir": start/stop redirection.
9663 */
9664 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009665ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666{
9667 char *mode;
9668 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009669 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670
Bram Moolenaarba768492016-07-08 15:32:54 +02009671#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009672 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009673 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009674 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009675 return;
9676 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009677#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009678
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 if (STRICMP(eap->arg, "END") == 0)
9680 close_redir();
9681 else
9682 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009683 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009685 ++arg;
9686 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009688 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 mode = "a";
9690 }
9691 else
9692 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009693 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694
9695 close_redir();
9696
9697 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009698 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 if (fname == NULL)
9700 return;
9701#ifdef FEAT_BROWSE
9702 if (cmdmod.browse)
9703 {
9704 char_u *browseFile;
9705
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009706 browseFile = do_browse(BROWSE_SAVE,
9707 (char_u *)_("Save Redirection"),
9708 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 if (browseFile == NULL)
9710 return; /* operation cancelled */
9711 vim_free(fname);
9712 fname = browseFile;
9713 eap->forceit = TRUE; /* since dialog already asked */
9714 }
9715#endif
9716
9717 redir_fd = open_exfile(fname, eap->forceit, mode);
9718 vim_free(fname);
9719 }
9720#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009721 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 {
9723 /* redirect to a register a-z (resp. A-Z for appending) */
9724 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009725 ++arg;
9726 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009728 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009729 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009731 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009733 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009734 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009735 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009736 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009738 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009739 if (*arg == '>')
9740 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009741 /* Make register empty when not using @A-@Z and the
9742 * command is valid. */
9743 if (*arg == NUL && !isupper(redir_reg))
9744 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009746 }
9747 if (*arg != NUL)
9748 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009749 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009750 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009751 }
9752 }
9753 else if (*arg == '=' && arg[1] == '>')
9754 {
9755 int append;
9756
9757 /* redirect to a variable */
9758 close_redir();
9759 arg += 2;
9760
9761 if (*arg == '>')
9762 {
9763 ++arg;
9764 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 }
9766 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009767 append = FALSE;
9768
9769 if (var_redir_start(skipwhite(arg), append) == OK)
9770 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 }
9772#endif
9773
9774 /* TODO: redirect to a buffer */
9775
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009777 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009779
9780 /* Make sure redirection is not off. Can happen for cmdline completion
9781 * that indirectly invokes a command to catch its output. */
9782 if (redir_fd != NULL
9783#ifdef FEAT_EVAL
9784 || redir_reg || redir_vname
9785#endif
9786 )
9787 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788}
9789
9790/*
9791 * ":redraw": force redraw
9792 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009793 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009794ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795{
9796 int r = RedrawingDisabled;
9797 int p = p_lz;
9798
9799 RedrawingDisabled = 0;
9800 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009801 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009803 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804#ifdef FEAT_TITLE
9805 if (need_maketitle)
9806 maketitle();
9807#endif
9808 RedrawingDisabled = r;
9809 p_lz = p;
9810
9811 /* Reset msg_didout, so that a message that's there is overwritten. */
9812 msg_didout = FALSE;
9813 msg_col = 0;
9814
Bram Moolenaar56667a52013-07-17 11:54:28 +02009815 /* No need to wait after an intentional redraw. */
9816 need_wait_return = FALSE;
9817
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 out_flush();
9819}
9820
9821/*
9822 * ":redrawstatus": force redraw of status line(s)
9823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009825ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826{
9827#if defined(FEAT_WINDOWS)
9828 int r = RedrawingDisabled;
9829 int p = p_lz;
9830
9831 RedrawingDisabled = 0;
9832 p_lz = FALSE;
9833 if (eap->forceit)
9834 status_redraw_all();
9835 else
9836 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009837 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 RedrawingDisabled = r;
9839 p_lz = p;
9840 out_flush();
9841#endif
9842}
9843
9844 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009845close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846{
9847 if (redir_fd != NULL)
9848 {
9849 fclose(redir_fd);
9850 redir_fd = NULL;
9851 }
9852#ifdef FEAT_EVAL
9853 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009854 if (redir_vname)
9855 {
9856 var_redir_stop();
9857 redir_vname = 0;
9858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859#endif
9860}
9861
9862#if defined(FEAT_SESSION) && defined(USE_CRNL)
9863# define MKSESSION_NL
9864static int mksession_nl = FALSE; /* use NL only in put_eol() */
9865#endif
9866
9867/*
9868 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9869 */
9870 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009871ex_mkrc(
9872 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873{
9874 FILE *fd;
9875 int failed = FALSE;
9876 char_u *fname;
9877#ifdef FEAT_BROWSE
9878 char_u *browseFile = NULL;
9879#endif
9880#ifdef FEAT_SESSION
9881 int view_session = FALSE;
9882 int using_vdir = FALSE; /* using 'viewdir'? */
9883 char_u *viewFile = NULL;
9884 unsigned *flagp;
9885#endif
9886
9887 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9888 {
9889#ifdef FEAT_SESSION
9890 view_session = TRUE;
9891#else
9892 ex_ni(eap);
9893 return;
9894#endif
9895 }
9896
9897#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009898 /* Use the short file name until ":lcd" is used. We also don't use the
9899 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009900 did_lcd = FALSE;
9901
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9903 if (eap->cmdidx == CMD_mkview
9904 && (*eap->arg == NUL
9905 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9906 {
9907 eap->forceit = TRUE;
9908 fname = get_view_file(*eap->arg);
9909 if (fname == NULL)
9910 return;
9911 viewFile = fname;
9912 using_vdir = TRUE;
9913 }
9914 else
9915#endif
9916 if (*eap->arg != NUL)
9917 fname = eap->arg;
9918 else if (eap->cmdidx == CMD_mkvimrc)
9919 fname = (char_u *)VIMRC_FILE;
9920#ifdef FEAT_SESSION
9921 else if (eap->cmdidx == CMD_mksession)
9922 fname = (char_u *)SESSION_FILE;
9923#endif
9924 else
9925 fname = (char_u *)EXRC_FILE;
9926
9927#ifdef FEAT_BROWSE
9928 if (cmdmod.browse)
9929 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009930 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931# ifdef FEAT_SESSION
9932 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9933 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9934# endif
9935 (char_u *)_("Save Setup"),
9936 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9937 if (browseFile == NULL)
9938 goto theend;
9939 fname = browseFile;
9940 eap->forceit = TRUE; /* since dialog already asked */
9941 }
9942#endif
9943
9944#if defined(FEAT_SESSION) && defined(vim_mkdir)
9945 /* When using 'viewdir' may have to create the directory. */
9946 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009947 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948#endif
9949
9950 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9951 if (fd != NULL)
9952 {
9953#ifdef FEAT_SESSION
9954 if (eap->cmdidx == CMD_mkview)
9955 flagp = &vop_flags;
9956 else
9957 flagp = &ssop_flags;
9958#endif
9959
9960#ifdef MKSESSION_NL
9961 /* "unix" in 'sessionoptions': use NL line separator */
9962 if (view_session && (*flagp & SSOP_UNIX))
9963 mksession_nl = TRUE;
9964#endif
9965
9966 /* Write the version command for :mkvimrc */
9967 if (eap->cmdidx == CMD_mkvimrc)
9968 (void)put_line(fd, "version 6.0");
9969
9970#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009971 if (eap->cmdidx == CMD_mksession)
9972 {
9973 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9974 failed = TRUE;
9975 }
9976
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 if (eap->cmdidx != CMD_mkview)
9978#endif
9979 {
9980 /* Write setting 'compatible' first, because it has side effects.
9981 * For that same reason only do it when needed. */
9982 if (p_cp)
9983 (void)put_line(fd, "if !&cp | set cp | endif");
9984 else
9985 (void)put_line(fd, "if &cp | set nocp | endif");
9986 }
9987
9988#ifdef FEAT_SESSION
9989 if (!view_session
9990 || (eap->cmdidx == CMD_mksession
9991 && (*flagp & SSOP_OPTIONS)))
9992#endif
9993 failed |= (makemap(fd, NULL) == FAIL
9994 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9995
9996#ifdef FEAT_SESSION
9997 if (!failed && view_session)
9998 {
9999 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10000 failed = TRUE;
10001 if (eap->cmdidx == CMD_mksession)
10002 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010003 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004
Bram Moolenaard9462e32011-04-11 21:35:11 +020010005 dirnow = alloc(MAXPATHL);
10006 if (dirnow == NULL)
10007 failed = TRUE;
10008 else
10009 {
10010 /*
10011 * Change to session file's dir.
10012 */
10013 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010015 *dirnow = NUL;
10016 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10017 {
10018 if (vim_chdirfile(fname) == OK)
10019 shorten_fnames(TRUE);
10020 }
10021 else if (*dirnow != NUL
10022 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10023 {
10024 if (mch_chdir((char *)globaldir) == 0)
10025 shorten_fnames(TRUE);
10026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027
Bram Moolenaard9462e32011-04-11 21:35:11 +020010028 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029
Bram Moolenaard9462e32011-04-11 21:35:11 +020010030 /* restore original dir */
10031 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010033 {
10034 if (mch_chdir((char *)dirnow) != 0)
10035 EMSG(_(e_prev_dir));
10036 shorten_fnames(TRUE);
10037 }
10038 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 }
10040 }
10041 else
10042 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010043 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10044 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 }
10046 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10047 == FAIL)
10048 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010049 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10050 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010051 if (eap->cmdidx == CMD_mksession)
10052 {
10053 if (put_line(fd, "unlet SessionLoad") == FAIL)
10054 failed = TRUE;
10055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 }
10057#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010058 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10059 failed = TRUE;
10060
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 failed |= fclose(fd);
10062
10063 if (failed)
10064 EMSG(_(e_write));
10065#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10066 else if (eap->cmdidx == CMD_mksession)
10067 {
10068 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010069 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070
Bram Moolenaard9462e32011-04-11 21:35:11 +020010071 tbuf = alloc(MAXPATHL);
10072 if (tbuf != NULL)
10073 {
10074 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10075 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10076 vim_free(tbuf);
10077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 }
10079#endif
10080#ifdef MKSESSION_NL
10081 mksession_nl = FALSE;
10082#endif
10083 }
10084
10085#ifdef FEAT_BROWSE
10086theend:
10087 vim_free(browseFile);
10088#endif
10089#ifdef FEAT_SESSION
10090 vim_free(viewFile);
10091#endif
10092}
10093
Bram Moolenaardf177f62005-02-22 08:39:57 +000010094#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10095 || defined(PROTO)
10096 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010097vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010098{
10099 if (vim_mkdir(name, prot) != 0)
10100 {
10101 EMSG2(_("E739: Cannot create directory: %s"), name);
10102 return FAIL;
10103 }
10104 return OK;
10105}
10106#endif
10107
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108/*
10109 * Open a file for writing for an Ex command, with some checks.
10110 * Return file descriptor, or NULL on failure.
10111 */
10112 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010113open_exfile(
10114 char_u *fname,
10115 int forceit,
10116 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117{
10118 FILE *fd;
10119
10120#ifdef UNIX
10121 /* with Unix it is possible to open a directory */
10122 if (mch_isdir(fname))
10123 {
10124 EMSG2(_(e_isadir2), fname);
10125 return NULL;
10126 }
10127#endif
10128 if (!forceit && *mode != 'a' && vim_fexists(fname))
10129 {
10130 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10131 return NULL;
10132 }
10133
10134 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10135 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10136
10137 return fd;
10138}
10139
10140/*
10141 * ":mark" and ":k".
10142 */
10143 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010144ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145{
10146 pos_T pos;
10147
10148 if (*eap->arg == NUL) /* No argument? */
10149 EMSG(_(e_argreq));
10150 else if (eap->arg[1] != NUL) /* more than one character? */
10151 EMSG(_(e_trailing));
10152 else
10153 {
10154 pos = curwin->w_cursor; /* save curwin->w_cursor */
10155 curwin->w_cursor.lnum = eap->line2;
10156 beginline(BL_WHITE | BL_FIX);
10157 if (setmark(*eap->arg) == FAIL) /* set mark */
10158 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10159 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10160 }
10161}
10162
10163/*
10164 * Update w_topline, w_leftcol and the cursor position.
10165 */
10166 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010167update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168{
10169 check_cursor(); /* put cursor on valid line */
10170 update_topline();
10171 if (!curwin->w_p_wrap)
10172 validate_cursor();
10173 update_curswant();
10174}
10175
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176/*
10177 * ":normal[!] {commands}": Execute normal mode commands.
10178 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010179 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010180ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 int save_msg_scroll = msg_scroll;
10183 int save_restart_edit = restart_edit;
10184 int save_msg_didout = msg_didout;
10185 int save_State = State;
10186 tasave_T tabuf;
10187 int save_insertmode = p_im;
10188 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010189 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190#ifdef FEAT_MBYTE
10191 char_u *arg = NULL;
10192 int l;
10193 char_u *p;
10194#endif
10195
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010196 if (ex_normal_lock > 0)
10197 {
10198 EMSG(_(e_secure));
10199 return;
10200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 if (ex_normal_busy >= p_mmd)
10202 {
10203 EMSG(_("E192: Recursive use of :normal too deep"));
10204 return;
10205 }
10206 ++ex_normal_busy;
10207
10208 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10209 restart_edit = 0; /* don't go to Insert mode */
10210 p_im = FALSE; /* don't use 'insertmode' */
10211
10212#ifdef FEAT_MBYTE
10213 /*
10214 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10215 * this for the K_SPECIAL leading byte, otherwise special keys will not
10216 * work.
10217 */
10218 if (has_mbyte)
10219 {
10220 int len = 0;
10221
10222 /* Count the number of characters to be escaped. */
10223 for (p = eap->arg; *p != NUL; ++p)
10224 {
10225# ifdef FEAT_GUI
10226 if (*p == CSI) /* leadbyte CSI */
10227 len += 2;
10228# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010229 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10231# ifdef FEAT_GUI
10232 || *p == CSI
10233# endif
10234 )
10235 len += 2;
10236 }
10237 if (len > 0)
10238 {
10239 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10240 if (arg != NULL)
10241 {
10242 len = 0;
10243 for (p = eap->arg; *p != NUL; ++p)
10244 {
10245 arg[len++] = *p;
10246# ifdef FEAT_GUI
10247 if (*p == CSI)
10248 {
10249 arg[len++] = KS_EXTRA;
10250 arg[len++] = (int)KE_CSI;
10251 }
10252# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010253 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 {
10255 arg[len++] = *++p;
10256 if (*p == K_SPECIAL)
10257 {
10258 arg[len++] = KS_SPECIAL;
10259 arg[len++] = KE_FILLER;
10260 }
10261# ifdef FEAT_GUI
10262 else if (*p == CSI)
10263 {
10264 arg[len++] = KS_EXTRA;
10265 arg[len++] = (int)KE_CSI;
10266 }
10267# endif
10268 }
10269 arg[len] = NUL;
10270 }
10271 }
10272 }
10273 }
10274#endif
10275
10276 /*
10277 * Save the current typeahead. This is required to allow using ":normal"
10278 * from an event handler and makes sure we don't hang when the argument
10279 * ends with half a command.
10280 */
10281 save_typeahead(&tabuf);
10282 if (tabuf.typebuf_valid)
10283 {
10284 /*
10285 * Repeat the :normal command for each line in the range. When no
10286 * range given, execute it just once, without positioning the cursor
10287 * first.
10288 */
10289 do
10290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 if (eap->addr_count != 0)
10292 {
10293 curwin->w_cursor.lnum = eap->line1++;
10294 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010295 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 }
10297
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010298 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299#ifdef FEAT_MBYTE
10300 arg != NULL ? arg :
10301#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010302 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 }
10304 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10305 }
10306
10307 /* Might not return to the main loop when in an event handler. */
10308 update_topline_cursor();
10309
10310 /* Restore the previous typeahead. */
10311 restore_typeahead(&tabuf);
10312
10313 --ex_normal_busy;
10314 msg_scroll = save_msg_scroll;
10315 restart_edit = save_restart_edit;
10316 p_im = save_insertmode;
10317 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010318 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10320
10321 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010322 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010324#ifdef FEAT_MOUSE
10325 setmouse();
10326#endif
10327#ifdef CURSOR_SHAPE
10328 ui_cursor_shape(); /* may show different cursor shape */
10329#endif
10330
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331#ifdef FEAT_MBYTE
10332 vim_free(arg);
10333#endif
10334}
10335
10336/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010337 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 */
10339 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010340ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010342 if (eap->forceit)
10343 {
10344 coladvance((colnr_T)MAXCOL);
10345 curwin->w_curswant = MAXCOL;
10346 curwin->w_set_curswant = FALSE;
10347 }
10348
Bram Moolenaara40c5002005-01-09 21:16:21 +000010349 /* Ignore the command when already in Insert mode. Inserting an
10350 * expression register that invokes a function can do this. */
10351 if (State & INSERT)
10352 return;
10353
Bram Moolenaar64969662005-12-14 21:59:55 +000010354 if (eap->cmdidx == CMD_startinsert)
10355 restart_edit = 'a';
10356 else if (eap->cmdidx == CMD_startreplace)
10357 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010359 restart_edit = 'V';
10360
10361 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010363 if (eap->cmdidx == CMD_startinsert)
10364 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 curwin->w_curswant = 0; /* avoid MAXCOL */
10366 }
10367}
10368
10369/*
10370 * ":stopinsert"
10371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010373ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374{
10375 restart_edit = 0;
10376 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010377 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010380/*
10381 * Execute normal mode command "cmd".
10382 * "remap" can be REMAP_NONE or REMAP_YES.
10383 */
10384 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010385exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010386{
Bram Moolenaar25281632016-01-21 23:32:32 +010010387 /* Stuff the argument into the typeahead buffer. */
10388 ins_typebuf(cmd, remap, 0, TRUE, silent);
10389 exec_normal(FALSE);
10390}
Bram Moolenaar25281632016-01-21 23:32:32 +010010391
Bram Moolenaar25281632016-01-21 23:32:32 +010010392/*
10393 * Execute normal_cmd() until there is no typeahead left.
10394 */
10395 void
10396exec_normal(int was_typed)
10397{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010398 oparg_T oa;
10399
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010400 clear_oparg(&oa);
10401 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010402 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10403 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010404 {
10405 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010406 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010407 }
10408}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010409
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410#ifdef FEAT_FIND_ID
10411 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010412ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413{
10414 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10415 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10416 (linenr_T)1, (linenr_T)MAXLNUM);
10417}
10418
10419#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10420/*
10421 * ":psearch"
10422 */
10423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010424ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425{
10426 g_do_tagpreview = p_pvh;
10427 ex_findpat(eap);
10428 g_do_tagpreview = 0;
10429}
10430#endif
10431
10432 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010433ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434{
10435 int whole = TRUE;
10436 long n;
10437 char_u *p;
10438 int action;
10439
10440 switch (cmdnames[eap->cmdidx].cmd_name[2])
10441 {
10442 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10443 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10444 action = ACTION_GOTO;
10445 else
10446 action = ACTION_SHOW;
10447 break;
10448 case 'i': /* ":ilist" and ":dlist" */
10449 action = ACTION_SHOW_ALL;
10450 break;
10451 case 'u': /* ":ijump" and ":djump" */
10452 action = ACTION_GOTO;
10453 break;
10454 default: /* ":isplit" and ":dsplit" */
10455 action = ACTION_SPLIT;
10456 break;
10457 }
10458
10459 n = 1;
10460 if (vim_isdigit(*eap->arg)) /* get count */
10461 {
10462 n = getdigits(&eap->arg);
10463 eap->arg = skipwhite(eap->arg);
10464 }
10465 if (*eap->arg == '/') /* Match regexp, not just whole words */
10466 {
10467 whole = FALSE;
10468 ++eap->arg;
10469 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10470 if (*p)
10471 {
10472 *p++ = NUL;
10473 p = skipwhite(p);
10474
10475 /* Check for trailing illegal characters */
10476 if (!ends_excmd(*p))
10477 eap->errmsg = e_trailing;
10478 else
10479 eap->nextcmd = check_nextcmd(p);
10480 }
10481 }
10482 if (!eap->skip)
10483 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10484 whole, !eap->forceit,
10485 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10486 n, action, eap->line1, eap->line2);
10487}
10488#endif
10489
10490#ifdef FEAT_WINDOWS
10491
10492# ifdef FEAT_QUICKFIX
10493/*
10494 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10495 */
10496 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010497ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010499 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10501}
10502
10503/*
10504 * ":pedit"
10505 */
10506 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010507ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508{
10509 win_T *curwin_save = curwin;
10510
10511 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010512 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 keep_help_flag = curwin_save->w_buffer->b_help;
10514 do_exedit(eap, NULL);
10515 keep_help_flag = FALSE;
10516 if (curwin != curwin_save && win_valid(curwin_save))
10517 {
10518 /* Return cursor to where we were */
10519 validate_cursor();
10520 redraw_later(VALID);
10521 win_enter(curwin_save, TRUE);
10522 }
10523 g_do_tagpreview = 0;
10524}
10525# endif
10526
10527/*
10528 * ":stag", ":stselect" and ":stjump".
10529 */
10530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010531ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532{
10533 postponed_split = -1;
10534 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010535 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10537 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010538 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539}
10540#endif
10541
10542/*
10543 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10544 */
10545 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010546ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547{
10548 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10549}
10550
10551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010552ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553{
10554 int cmd;
10555
10556 switch (name[1])
10557 {
10558 case 'j': cmd = DT_JUMP; /* ":tjump" */
10559 break;
10560 case 's': cmd = DT_SELECT; /* ":tselect" */
10561 break;
10562 case 'p': cmd = DT_PREV; /* ":tprevious" */
10563 break;
10564 case 'N': cmd = DT_PREV; /* ":tNext" */
10565 break;
10566 case 'n': cmd = DT_NEXT; /* ":tnext" */
10567 break;
10568 case 'o': cmd = DT_POP; /* ":pop" */
10569 break;
10570 case 'f': /* ":tfirst" */
10571 case 'r': cmd = DT_FIRST; /* ":trewind" */
10572 break;
10573 case 'l': cmd = DT_LAST; /* ":tlast" */
10574 break;
10575 default: /* ":tag" */
10576#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010577 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010579 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 return;
10581 }
10582#endif
10583 cmd = DT_TAG;
10584 break;
10585 }
10586
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010587 if (name[0] == 'l')
10588 {
10589#ifndef FEAT_QUICKFIX
10590 ex_ni(eap);
10591 return;
10592#else
10593 cmd = DT_LTAG;
10594#endif
10595 }
10596
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10598 eap->forceit, TRUE);
10599}
10600
10601/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010602 * Check "str" for starting with a special cmdline variable.
10603 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10604 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10605 */
10606 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010607find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010608{
10609 int len;
10610 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010611 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010612 "%",
10613#define SPEC_PERC 0
10614 "#",
10615#define SPEC_HASH 1
10616 "<cword>", /* cursor word */
10617#define SPEC_CWORD 2
10618 "<cWORD>", /* cursor WORD */
10619#define SPEC_CCWORD 3
10620 "<cfile>", /* cursor path name */
10621#define SPEC_CFILE 4
10622 "<sfile>", /* ":so" file name */
10623#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010624 "<slnum>", /* ":so" file line number */
10625#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010626#ifdef FEAT_AUTOCMD
10627 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010628# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010629 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010630# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010631 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010632# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010633#endif
10634#ifdef FEAT_CLIENTSERVER
10635 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010636# ifdef FEAT_AUTOCMD
10637# define SPEC_CLIENT 10
10638# else
10639# define SPEC_CLIENT 7
10640# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010641#endif
10642 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010643
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010644 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010645 {
10646 len = (int)STRLEN(spec_str[i]);
10647 if (STRNCMP(src, spec_str[i], len) == 0)
10648 {
10649 *usedlen = len;
10650 return i;
10651 }
10652 }
10653 return -1;
10654}
10655
10656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 * Evaluate cmdline variables.
10658 *
10659 * change '%' to curbuf->b_ffname
10660 * '#' to curwin->w_altfile
10661 * '<cword>' to word under the cursor
10662 * '<cWORD>' to WORD under the cursor
10663 * '<cfile>' to path name under the cursor
10664 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010665 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 * '<afile>' to file name for autocommand
10667 * '<abuf>' to buffer number for autocommand
10668 * '<amatch>' to matching name for autocommand
10669 *
10670 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10671 * "" for error without a message) and NULL is returned.
10672 * Returns an allocated string if a valid match was found.
10673 * Returns NULL if no match was found. "usedlen" then still contains the
10674 * number of characters to skip.
10675 */
10676 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010677eval_vars(
10678 char_u *src, /* pointer into commandline */
10679 char_u *srcstart, /* beginning of valid memory for src */
10680 int *usedlen, /* characters after src that are used */
10681 linenr_T *lnump, /* line number for :e command, or NULL */
10682 char_u **errormsg, /* pointer to error message */
10683 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010684 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685{
10686 int i;
10687 char_u *s;
10688 char_u *result;
10689 char_u *resultbuf = NULL;
10690 int resultlen;
10691 buf_T *buf;
10692 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10693 int spec_idx;
10694#ifdef FEAT_MODIFY_FNAME
10695 int skip_mod = FALSE;
10696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698
10699 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010700 if (escaped != NULL)
10701 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702
10703 /*
10704 * Check if there is something to do.
10705 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010706 spec_idx = find_cmdline_var(src, usedlen);
10707 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 {
10709 *usedlen = 1;
10710 return NULL;
10711 }
10712
10713 /*
10714 * Skip when preceded with a backslash "\%" and "\#".
10715 * Note: In "\\%" the % is also not recognized!
10716 */
10717 if (src > srcstart && src[-1] == '\\')
10718 {
10719 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010720 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 return NULL;
10722 }
10723
10724 /*
10725 * word or WORD under cursor
10726 */
10727 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10728 {
10729 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10730 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10731 if (resultlen == 0)
10732 {
10733 *errormsg = (char_u *)"";
10734 return NULL;
10735 }
10736 }
10737
10738 /*
10739 * '#': Alternate file name
10740 * '%': Current file name
10741 * File name under the cursor
10742 * File name for autocommand
10743 * and following modifiers
10744 */
10745 else
10746 {
10747 switch (spec_idx)
10748 {
10749 case SPEC_PERC: /* '%': current file */
10750 if (curbuf->b_fname == NULL)
10751 {
10752 result = (char_u *)"";
10753 valid = 0; /* Must have ":p:h" to be valid */
10754 }
10755 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 break;
10758
10759 case SPEC_HASH: /* '#' or "#99": alternate file */
10760 if (src[1] == '#') /* "##": the argument list */
10761 {
10762 result = arg_all();
10763 resultbuf = result;
10764 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010765 if (escaped != NULL)
10766 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767#ifdef FEAT_MODIFY_FNAME
10768 skip_mod = TRUE;
10769#endif
10770 break;
10771 }
10772 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010773 if (*s == '<') /* "#<99" uses v:oldfiles */
10774 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 i = (int)getdigits(&s);
10776 *usedlen = (int)(s - src); /* length of what we expand */
10777
Bram Moolenaard812df62008-11-09 12:46:09 +000010778 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010780 if (*usedlen < 2)
10781 {
10782 /* Should we give an error message for #<text? */
10783 *usedlen = 1;
10784 return NULL;
10785 }
10786#ifdef FEAT_EVAL
10787 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10788 (long)i);
10789 if (result == NULL)
10790 {
10791 *errormsg = (char_u *)"";
10792 return NULL;
10793 }
10794#else
10795 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 }
10799 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010800 {
10801 buf = buflist_findnr(i);
10802 if (buf == NULL)
10803 {
10804 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10805 return NULL;
10806 }
10807 if (lnump != NULL)
10808 *lnump = ECMD_LAST;
10809 if (buf->b_fname == NULL)
10810 {
10811 result = (char_u *)"";
10812 valid = 0; /* Must have ":p:h" to be valid */
10813 }
10814 else
10815 result = buf->b_fname;
10816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 break;
10818
10819#ifdef FEAT_SEARCHPATH
10820 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010821 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 if (result == NULL)
10823 {
10824 *errormsg = (char_u *)"";
10825 return NULL;
10826 }
10827 resultbuf = result; /* remember allocated string */
10828 break;
10829#endif
10830
10831#ifdef FEAT_AUTOCMD
10832 case SPEC_AFILE: /* file name for autocommand */
10833 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010834 if (result != NULL && !autocmd_fname_full)
10835 {
10836 /* Still need to turn the fname into a full path. It is
10837 * postponed to avoid a delay when <afile> is not used. */
10838 autocmd_fname_full = TRUE;
10839 result = FullName_save(autocmd_fname, FALSE);
10840 vim_free(autocmd_fname);
10841 autocmd_fname = result;
10842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 if (result == NULL)
10844 {
10845 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10846 return NULL;
10847 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010848 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 break;
10850
10851 case SPEC_ABUF: /* buffer number for autocommand */
10852 if (autocmd_bufnr <= 0)
10853 {
10854 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10855 return NULL;
10856 }
10857 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10858 result = strbuf;
10859 break;
10860
10861 case SPEC_AMATCH: /* match name for autocommand */
10862 result = autocmd_match;
10863 if (result == NULL)
10864 {
10865 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10866 return NULL;
10867 }
10868 break;
10869
10870#endif
10871 case SPEC_SFILE: /* file name for ":so" command */
10872 result = sourcing_name;
10873 if (result == NULL)
10874 {
10875 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10876 return NULL;
10877 }
10878 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010879 case SPEC_SLNUM: /* line in file for ":so" command */
10880 if (sourcing_name == NULL || sourcing_lnum == 0)
10881 {
10882 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10883 return NULL;
10884 }
10885 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10886 result = strbuf;
10887 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888#if defined(FEAT_CLIENTSERVER)
10889 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010890 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10891 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 result = strbuf;
10893 break;
10894#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010895 default:
10896 result = (char_u *)""; /* avoid gcc warning */
10897 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 }
10899
10900 resultlen = (int)STRLEN(result); /* length of new string */
10901 if (src[*usedlen] == '<') /* remove the file name extension */
10902 {
10903 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 resultlen = (int)(s - result);
10906 }
10907#ifdef FEAT_MODIFY_FNAME
10908 else if (!skip_mod)
10909 {
10910 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10911 &resultlen);
10912 if (result == NULL)
10913 {
10914 *errormsg = (char_u *)"";
10915 return NULL;
10916 }
10917 }
10918#endif
10919 }
10920
10921 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10922 {
10923 if (valid != VALID_HEAD + VALID_PATH)
10924 /* xgettext:no-c-format */
10925 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10926 else
10927 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10928 result = NULL;
10929 }
10930 else
10931 result = vim_strnsave(result, resultlen);
10932 vim_free(resultbuf);
10933 return result;
10934}
10935
10936/*
10937 * Concatenate all files in the argument list, separated by spaces, and return
10938 * it in one allocated string.
10939 * Spaces and backslashes in the file names are escaped with a backslash.
10940 * Returns NULL when out of memory.
10941 */
10942 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010943arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944{
10945 int len;
10946 int idx;
10947 char_u *retval = NULL;
10948 char_u *p;
10949
10950 /*
10951 * Do this loop two times:
10952 * first time: compute the total length
10953 * second time: concatenate the names
10954 */
10955 for (;;)
10956 {
10957 len = 0;
10958 for (idx = 0; idx < ARGCOUNT; ++idx)
10959 {
10960 p = alist_name(&ARGLIST[idx]);
10961 if (p != NULL)
10962 {
10963 if (len > 0)
10964 {
10965 /* insert a space in between names */
10966 if (retval != NULL)
10967 retval[len] = ' ';
10968 ++len;
10969 }
10970 for ( ; *p != NUL; ++p)
10971 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010972 if (*p == ' '
10973#ifndef BACKSLASH_IN_FILENAME
10974 || *p == '\\'
10975#endif
10976 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 {
10978 /* insert a backslash */
10979 if (retval != NULL)
10980 retval[len] = '\\';
10981 ++len;
10982 }
10983 if (retval != NULL)
10984 retval[len] = *p;
10985 ++len;
10986 }
10987 }
10988 }
10989
10990 /* second time: break here */
10991 if (retval != NULL)
10992 {
10993 retval[len] = NUL;
10994 break;
10995 }
10996
10997 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010998 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 if (retval == NULL)
11000 break;
11001 }
11002
11003 return retval;
11004}
11005
11006#if defined(FEAT_AUTOCMD) || defined(PROTO)
11007/*
11008 * Expand the <sfile> string in "arg".
11009 *
11010 * Returns an allocated string, or NULL for any error.
11011 */
11012 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011013expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014{
11015 char_u *errormsg;
11016 int len;
11017 char_u *result;
11018 char_u *newres;
11019 char_u *repl;
11020 int srclen;
11021 char_u *p;
11022
11023 result = vim_strsave(arg);
11024 if (result == NULL)
11025 return NULL;
11026
11027 for (p = result; *p; )
11028 {
11029 if (STRNCMP(p, "<sfile>", 7) != 0)
11030 ++p;
11031 else
11032 {
11033 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011034 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 if (errormsg != NULL)
11036 {
11037 if (*errormsg)
11038 emsg(errormsg);
11039 vim_free(result);
11040 return NULL;
11041 }
11042 if (repl == NULL) /* no match (cannot happen) */
11043 {
11044 p += srclen;
11045 continue;
11046 }
11047 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11048 newres = alloc(len);
11049 if (newres == NULL)
11050 {
11051 vim_free(repl);
11052 vim_free(result);
11053 return NULL;
11054 }
11055 mch_memmove(newres, result, (size_t)(p - result));
11056 STRCPY(newres + (p - result), repl);
11057 len = (int)STRLEN(newres);
11058 STRCAT(newres, p + srclen);
11059 vim_free(repl);
11060 vim_free(result);
11061 result = newres;
11062 p = newres + len; /* continue after the match */
11063 }
11064 }
11065
11066 return result;
11067}
11068#endif
11069
11070#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011071static int ses_winsizes(FILE *fd, int restore_size,
11072 win_T *tab_firstwin);
11073static int ses_win_rec(FILE *fd, frame_T *fr);
11074static frame_T *ses_skipframe(frame_T *fr);
11075static int ses_do_frame(frame_T *fr);
11076static int ses_do_win(win_T *wp);
11077static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11078static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
11079static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080
11081/*
11082 * Write openfile commands for the current buffers to an .exrc file.
11083 * Return FAIL on error, OK otherwise.
11084 */
11085 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011086makeopens(
11087 FILE *fd,
11088 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089{
11090 buf_T *buf;
11091 int only_save_windows = TRUE;
11092 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093 int restore_size = TRUE;
11094 win_T *wp;
11095 char_u *sname;
11096 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011097 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011098 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011099 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011100 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011101 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011102 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103
11104 if (ssop_flags & SSOP_BUFFERS)
11105 only_save_windows = FALSE; /* Save ALL buffers */
11106
11107 /*
11108 * Begin by setting the this_session variable, and then other
11109 * sessionable variables.
11110 */
11111#ifdef FEAT_EVAL
11112 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11113 return FAIL;
11114 if (ssop_flags & SSOP_GLOBALS)
11115 if (store_session_globals(fd) == FAIL)
11116 return FAIL;
11117#endif
11118
11119 /*
11120 * Close all windows but one.
11121 */
11122 if (put_line(fd, "silent only") == FAIL)
11123 return FAIL;
11124
11125 /*
11126 * Now a :cd command to the session directory or the current directory
11127 */
11128 if (ssop_flags & SSOP_SESDIR)
11129 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011130 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11131 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132 return FAIL;
11133 }
11134 else if (ssop_flags & SSOP_CURDIR)
11135 {
11136 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11137 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011138 || fputs("cd ", fd) < 0
11139 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11140 || put_eol(fd) == FAIL)
11141 {
11142 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145 vim_free(sname);
11146 }
11147
11148 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011149 * If there is an empty, unnamed buffer we will wipe it out later.
11150 * Remember the buffer number.
11151 */
11152 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11153 return FAIL;
11154 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11155 return FAIL;
11156 if (put_line(fd, "endif") == FAIL)
11157 return FAIL;
11158
11159 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160 * Now save the current files, current buffer first.
11161 */
11162 if (put_line(fd, "set shortmess=aoO") == FAIL)
11163 return FAIL;
11164
11165 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011166 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167 {
11168 if (!(only_save_windows && buf->b_nwindows == 0)
11169 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11170 && buf->b_fname != NULL
11171 && buf->b_p_bl)
11172 {
11173 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11174 : buf->b_wininfo->wi_fpos.lnum) < 0
11175 || ses_fname(fd, buf, &ssop_flags) == FAIL)
11176 return FAIL;
11177 }
11178 }
11179
11180 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011181 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11183 return FAIL;
11184
11185 if (ssop_flags & SSOP_RESIZE)
11186 {
11187 /* Note: after the restore we still check it worked!*/
11188 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11189 || put_eol(fd) == FAIL)
11190 return FAIL;
11191 }
11192
11193#ifdef FEAT_GUI
11194 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11195 {
11196 int x, y;
11197
11198 if (gui_mch_get_winpos(&x, &y) == OK)
11199 {
11200 /* Note: after the restore we still check it worked!*/
11201 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11202 return FAIL;
11203 }
11204 }
11205#endif
11206
11207 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011208 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11209 * will be displayed when creating the next tab. That resizes the windows
11210 * in the first tab, which may cause problems. Set 'showtabline' to 2
11211 * temporarily to avoid that.
11212 */
11213 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11214 {
11215 if (put_line(fd, "set stal=2") == FAIL)
11216 return FAIL;
11217 restore_stal = TRUE;
11218 }
11219
11220 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011221 * May repeat putting Windows for each tab, when "tabpages" is in
11222 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011223 * Don't use goto_tabpage(), it may change directory and trigger
11224 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011226 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011227 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011228 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011230 int need_tabnew = FALSE;
11231 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011232
Bram Moolenaar18144c82006-04-12 21:52:12 +000011233 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011235 tabpage_T *tp = find_tabpage(tabnr);
11236
11237 if (tp == NULL)
11238 break; /* done all tab pages */
11239 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011240 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011241 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011242 tab_topframe = topframe;
11243 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011244 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011245 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011246 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011247 tab_topframe = tp->tp_topframe;
11248 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011249 if (tabnr > 1)
11250 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252
Bram Moolenaar18144c82006-04-12 21:52:12 +000011253 /*
11254 * Before creating the window layout, try loading one file. If this
11255 * is aborted we don't end up with a number of useless windows.
11256 * This may have side effects! (e.g., compressed or network file).
11257 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011258 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011259 {
11260 if (ses_do_win(wp)
11261 && wp->w_buffer->b_ffname != NULL
11262 && !wp->w_buffer->b_help
11263#ifdef FEAT_QUICKFIX
11264 && !bt_nofile(wp->w_buffer)
11265#endif
11266 )
11267 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011268 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011269 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11270 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011271 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011272 if (!wp->w_arg_idx_invalid)
11273 edited_win = wp;
11274 break;
11275 }
11276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011278 /* If no file got edited create an empty tab page. */
11279 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11280 return FAIL;
11281
Bram Moolenaar18144c82006-04-12 21:52:12 +000011282 /*
11283 * Save current window layout.
11284 */
11285 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011287 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011289 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11290 return FAIL;
11291 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11292 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293
Bram Moolenaar18144c82006-04-12 21:52:12 +000011294 /*
11295 * Check if window sizes can be restored (no windows omitted).
11296 * Remember the window number of the current window after restoring.
11297 */
11298 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011299 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011300 {
11301 if (ses_do_win(wp))
11302 ++nr;
11303 else
11304 restore_size = FALSE;
11305 if (curwin == wp)
11306 cnr = nr;
11307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308
Bram Moolenaar18144c82006-04-12 21:52:12 +000011309 /* Go to the first window. */
11310 if (put_line(fd, "wincmd t") == FAIL)
11311 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011312
Bram Moolenaar18144c82006-04-12 21:52:12 +000011313 /*
11314 * If more than one window, see if sizes can be restored.
11315 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11316 * resized when moving between windows.
11317 * Do this before restoring the view, so that the topline and the
11318 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011319 * winminheight and winminwidth need to be set to avoid an error if the
11320 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011321 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011322 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011323 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011324 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011325 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326
Bram Moolenaar18144c82006-04-12 21:52:12 +000011327 /*
11328 * Restore the view of the window (options, file, cursor, etc.).
11329 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011330 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011331 {
11332 if (!ses_do_win(wp))
11333 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011334 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11335 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011336 return FAIL;
11337 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11338 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011339 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011340 }
11341
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011342 /* The argument index in the first tab page is zero, need to set it in
11343 * each window. For further tab pages it's the window where we do
11344 * "tabedit". */
11345 cur_arg_idx = next_arg_idx;
11346
Bram Moolenaar18144c82006-04-12 21:52:12 +000011347 /*
11348 * Restore cursor to the current window if it's not the first one.
11349 */
11350 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11351 || put_eol(fd) == FAIL))
11352 return FAIL;
11353
11354 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011355 * Restore window sizes again after jumping around in windows, because
11356 * the current window has a minimum size while others may not.
11357 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011358 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011359 return FAIL;
11360
Bram Moolenaar18144c82006-04-12 21:52:12 +000011361 /* Don't continue in another tab page when doing only the current one
11362 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011363 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011364 break;
11365 }
11366
11367 if (ssop_flags & SSOP_TABPAGES)
11368 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011369 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11370 || put_eol(fd) == FAIL)
11371 return FAIL;
11372 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011373 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11374 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011375
Bram Moolenaar9c102382006-05-03 21:26:49 +000011376 /*
11377 * Wipe out an empty unnamed buffer we started in.
11378 */
11379 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11380 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011381 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011382 return FAIL;
11383 if (put_line(fd, "endif") == FAIL)
11384 return FAIL;
11385 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11386 return FAIL;
11387
11388 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11389 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11390 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11391 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011392 /* Re-apply 'winminheight' and 'winminwidth'. */
11393 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11394 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11395 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396
11397 /*
11398 * Lastly, execute the x.vim file if it exists.
11399 */
11400 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11401 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011402 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403 || put_line(fd, "endif") == FAIL)
11404 return FAIL;
11405
11406 return OK;
11407}
11408
11409 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011410ses_winsizes(
11411 FILE *fd,
11412 int restore_size,
11413 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414{
11415 int n = 0;
11416 win_T *wp;
11417
11418 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11419 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011420 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421 {
11422 if (!ses_do_win(wp))
11423 continue;
11424 ++n;
11425
11426 /* restore height when not full height */
11427 if (wp->w_height + wp->w_status_height < topframe->fr_height
11428 && (fprintf(fd,
11429 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11430 n, (long)wp->w_height, Rows / 2, Rows) < 0
11431 || put_eol(fd) == FAIL))
11432 return FAIL;
11433
11434 /* restore width when not full width */
11435 if (wp->w_width < Columns && (fprintf(fd,
11436 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11437 n, (long)wp->w_width, Columns / 2, Columns) < 0
11438 || put_eol(fd) == FAIL))
11439 return FAIL;
11440 }
11441 }
11442 else
11443 {
11444 /* Just equalise window sizes */
11445 if (put_line(fd, "wincmd =") == FAIL)
11446 return FAIL;
11447 }
11448 return OK;
11449}
11450
11451/*
11452 * Write commands to "fd" to recursively create windows for frame "fr",
11453 * horizontally and vertically split.
11454 * After the commands the last window in the frame is the current window.
11455 * Returns FAIL when writing the commands to "fd" fails.
11456 */
11457 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011458ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459{
11460 frame_T *frc;
11461 int count = 0;
11462
11463 if (fr->fr_layout != FR_LEAF)
11464 {
11465 /* Find first frame that's not skipped and then create a window for
11466 * each following one (first frame is already there). */
11467 frc = ses_skipframe(fr->fr_child);
11468 if (frc != NULL)
11469 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11470 {
11471 /* Make window as big as possible so that we have lots of room
11472 * to split. */
11473 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11474 || put_line(fd, fr->fr_layout == FR_COL
11475 ? "split" : "vsplit") == FAIL)
11476 return FAIL;
11477 ++count;
11478 }
11479
11480 /* Go back to the first window. */
11481 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11482 ? "%dwincmd k" : "%dwincmd h", count) < 0
11483 || put_eol(fd) == FAIL))
11484 return FAIL;
11485
11486 /* Recursively create frames/windows in each window of this column or
11487 * row. */
11488 frc = ses_skipframe(fr->fr_child);
11489 while (frc != NULL)
11490 {
11491 ses_win_rec(fd, frc);
11492 frc = ses_skipframe(frc->fr_next);
11493 /* Go to next window. */
11494 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11495 return FAIL;
11496 }
11497 }
11498 return OK;
11499}
11500
11501/*
11502 * Skip frames that don't contain windows we want to save in the Session.
11503 * Returns NULL when there none.
11504 */
11505 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011506ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507{
11508 frame_T *frc;
11509
11510 for (frc = fr; frc != NULL; frc = frc->fr_next)
11511 if (ses_do_frame(frc))
11512 break;
11513 return frc;
11514}
11515
11516/*
11517 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11518 * the Session.
11519 */
11520 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011521ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522{
11523 frame_T *frc;
11524
11525 if (fr->fr_layout == FR_LEAF)
11526 return ses_do_win(fr->fr_win);
11527 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11528 if (ses_do_frame(frc))
11529 return TRUE;
11530 return FALSE;
11531}
11532
11533/*
11534 * Return non-zero if window "wp" is to be stored in the Session.
11535 */
11536 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011537ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538{
11539 if (wp->w_buffer->b_fname == NULL
11540#ifdef FEAT_QUICKFIX
11541 /* When 'buftype' is "nofile" can't restore the window contents. */
11542 || bt_nofile(wp->w_buffer)
11543#endif
11544 )
11545 return (ssop_flags & SSOP_BLANK);
11546 if (wp->w_buffer->b_help)
11547 return (ssop_flags & SSOP_HELP);
11548 return TRUE;
11549}
11550
11551/*
11552 * Write commands to "fd" to restore the view of a window.
11553 * Caller must make sure 'scrolloff' is zero.
11554 */
11555 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011556put_view(
11557 FILE *fd,
11558 win_T *wp,
11559 int add_edit, /* add ":edit" command to view */
11560 unsigned *flagp, /* vop_flags or ssop_flags */
11561 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011562 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563{
11564 win_T *save_curwin;
11565 int f;
11566 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011567 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568
11569 /* Always restore cursor position for ":mksession". For ":mkview" only
11570 * when 'viewoptions' contains "cursor". */
11571 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11572
11573 /*
11574 * Local argument list.
11575 */
11576 if (wp->w_alist == &global_alist)
11577 {
11578 if (put_line(fd, "argglobal") == FAIL)
11579 return FAIL;
11580 }
11581 else
11582 {
11583 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11584 flagp == &vop_flags
11585 || !(*flagp & SSOP_CURDIR)
11586 || wp->w_localdir != NULL, flagp) == FAIL)
11587 return FAIL;
11588 }
11589
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011590 /* Only when part of a session: restore the argument index. Some
11591 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011592 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011593 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011595 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596 || put_eol(fd) == FAIL)
11597 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011598 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599 }
11600
11601 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011602 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 {
11604 /*
11605 * Load the file.
11606 */
11607 if (wp->w_buffer->b_ffname != NULL
11608#ifdef FEAT_QUICKFIX
11609 && !bt_nofile(wp->w_buffer)
11610#endif
11611 )
11612 {
11613 /*
11614 * Editing a file in this buffer: use ":edit file".
11615 * This may have side effects! (e.g., compressed or network file).
11616 */
11617 if (fputs("edit ", fd) < 0
11618 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11619 return FAIL;
11620 }
11621 else
11622 {
11623 /* No file in this buffer, just make it empty. */
11624 if (put_line(fd, "enew") == FAIL)
11625 return FAIL;
11626#ifdef FEAT_QUICKFIX
11627 if (wp->w_buffer->b_ffname != NULL)
11628 {
11629 /* The buffer does have a name, but it's not a file name. */
11630 if (fputs("file ", fd) < 0
11631 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11632 return FAIL;
11633 }
11634#endif
11635 do_cursor = FALSE;
11636 }
11637 }
11638
11639 /*
11640 * Local mappings and abbreviations.
11641 */
11642 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11643 && makemap(fd, wp->w_buffer) == FAIL)
11644 return FAIL;
11645
11646 /*
11647 * Local options. Need to go to the window temporarily.
11648 * Store only local values when using ":mkview" and when ":mksession" is
11649 * used and 'sessionoptions' doesn't include "options".
11650 * Some folding options are always stored when "folds" is included,
11651 * otherwise the folds would not be restored correctly.
11652 */
11653 save_curwin = curwin;
11654 curwin = wp;
11655 curbuf = curwin->w_buffer;
11656 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11657 f = makeset(fd, OPT_LOCAL,
11658 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11659#ifdef FEAT_FOLDING
11660 else if (*flagp & SSOP_FOLDS)
11661 f = makefoldset(fd);
11662#endif
11663 else
11664 f = OK;
11665 curwin = save_curwin;
11666 curbuf = curwin->w_buffer;
11667 if (f == FAIL)
11668 return FAIL;
11669
11670#ifdef FEAT_FOLDING
11671 /*
11672 * Save Folds when 'buftype' is empty and for help files.
11673 */
11674 if ((*flagp & SSOP_FOLDS)
11675 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011676# ifdef FEAT_QUICKFIX
11677 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11678# endif
11679 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680 {
11681 if (put_folds(fd, wp) == FAIL)
11682 return FAIL;
11683 }
11684#endif
11685
11686 /*
11687 * Set the cursor after creating folds, since that moves the cursor.
11688 */
11689 if (do_cursor)
11690 {
11691
11692 /* Restore the cursor line in the file and relatively in the
11693 * window. Don't use "G", it changes the jumplist. */
11694 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11695 (long)wp->w_cursor.lnum,
11696 (long)(wp->w_cursor.lnum - wp->w_topline),
11697 (long)wp->w_height / 2, (long)wp->w_height) < 0
11698 || put_eol(fd) == FAIL
11699 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11700 || put_line(fd, "exe s:l") == FAIL
11701 || put_line(fd, "normal! zt") == FAIL
11702 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11703 || put_eol(fd) == FAIL)
11704 return FAIL;
11705 /* Restore the cursor column and left offset when not wrapping. */
11706 if (wp->w_cursor.col == 0)
11707 {
11708 if (put_line(fd, "normal! 0") == FAIL)
11709 return FAIL;
11710 }
11711 else
11712 {
11713 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11714 {
11715 if (fprintf(fd,
11716 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011717 (long)wp->w_virtcol + 1,
11718 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 (long)wp->w_width / 2, (long)wp->w_width) < 0
11720 || put_eol(fd) == FAIL
11721 || put_line(fd, "if s:c > 0") == FAIL
11722 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011723 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11724 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011725 || put_eol(fd) == FAIL
11726 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011727 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728 || put_eol(fd) == FAIL
11729 || put_line(fd, "endif") == FAIL)
11730 return FAIL;
11731 }
11732 else
11733 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011734 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 || put_eol(fd) == FAIL)
11736 return FAIL;
11737 }
11738 }
11739 }
11740
11741 /*
11742 * Local directory.
11743 */
11744 if (wp->w_localdir != NULL)
11745 {
11746 if (fputs("lcd ", fd) < 0
11747 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11748 || put_eol(fd) == FAIL)
11749 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011750 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 }
11752
11753 return OK;
11754}
11755
11756/*
11757 * Write an argument list to the session file.
11758 * Returns FAIL if writing fails.
11759 */
11760 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011761ses_arglist(
11762 FILE *fd,
11763 char *cmd,
11764 garray_T *gap,
11765 int fullname, /* TRUE: use full path name */
11766 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767{
11768 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011769 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770 char_u *s;
11771
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011772 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11773 return FAIL;
11774 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775 return FAIL;
11776 for (i = 0; i < gap->ga_len; ++i)
11777 {
11778 /* NULL file names are skipped (only happens when out of memory). */
11779 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11780 if (s != NULL)
11781 {
11782 if (fullname)
11783 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011784 buf = alloc(MAXPATHL);
11785 if (buf != NULL)
11786 {
11787 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11788 s = buf;
11789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011791 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011792 || ses_put_fname(fd, s, flagp) == FAIL
11793 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011794 {
11795 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011797 }
11798 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799 }
11800 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011801 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802}
11803
11804/*
11805 * Write a buffer name to the session file.
11806 * Also ends the line.
11807 * Returns FAIL if writing fails.
11808 */
11809 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011810ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811{
11812 char_u *name;
11813
11814 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011815 * the session file will be sourced.
11816 * Don't do this for ":mkview", we don't know the current directory.
11817 * Don't do this after ":lcd", we don't keep track of what the current
11818 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819 if (buf->b_sfname != NULL
11820 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011821 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011822#ifdef FEAT_AUTOCHDIR
11823 && !p_acd
11824#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011825 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826 name = buf->b_sfname;
11827 else
11828 name = buf->b_ffname;
11829 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11830 return FAIL;
11831 return OK;
11832}
11833
11834/*
11835 * Write a file name to the session file.
11836 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11837 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011838 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839 */
11840 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011841ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842{
11843 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011844 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846
11847 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011848 if (sname == NULL)
11849 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011851 if (*flagp & SSOP_SLASH)
11852 {
11853 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011854 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011855 if (*p == '\\')
11856 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011858
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011859 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011860 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011862 if (p == NULL)
11863 return FAIL;
11864
11865 /* write the result */
11866 if (fputs((char *)p, fd) < 0)
11867 retval = FAIL;
11868
11869 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870 return retval;
11871}
11872
11873/*
11874 * ":loadview [nr]"
11875 */
11876 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011877ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878{
11879 char_u *fname;
11880
11881 fname = get_view_file(*eap->arg);
11882 if (fname != NULL)
11883 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011884 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885 vim_free(fname);
11886 }
11887}
11888
11889/*
11890 * Get the name of the view file for the current buffer.
11891 */
11892 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011893get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894{
11895 int len = 0;
11896 char_u *p, *s;
11897 char_u *retval;
11898 char_u *sname;
11899
11900 if (curbuf->b_ffname == NULL)
11901 {
11902 EMSG(_(e_noname));
11903 return NULL;
11904 }
11905 sname = home_replace_save(NULL, curbuf->b_ffname);
11906 if (sname == NULL)
11907 return NULL;
11908
11909 /*
11910 * We want a file name without separators, because we're not going to make
11911 * a directory.
11912 * "normal" path separator -> "=+"
11913 * "=" -> "=="
11914 * ":" path separator -> "=-"
11915 */
11916 for (p = sname; *p; ++p)
11917 if (*p == '=' || vim_ispathsep(*p))
11918 ++len;
11919 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11920 if (retval != NULL)
11921 {
11922 STRCPY(retval, p_vdir);
11923 add_pathsep(retval);
11924 s = retval + STRLEN(retval);
11925 for (p = sname; *p; ++p)
11926 {
11927 if (*p == '=')
11928 {
11929 *s++ = '=';
11930 *s++ = '=';
11931 }
11932 else if (vim_ispathsep(*p))
11933 {
11934 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011935#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 if (*p == ':')
11937 *s++ = '-';
11938 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011940 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941 }
11942 else
11943 *s++ = *p;
11944 }
11945 *s++ = '=';
11946 *s++ = c;
11947 STRCPY(s, ".vim");
11948 }
11949
11950 vim_free(sname);
11951 return retval;
11952}
11953
11954#endif /* FEAT_SESSION */
11955
11956/*
11957 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11958 * Return FAIL for a write error.
11959 */
11960 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011961put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962{
11963 if (
11964#ifdef USE_CRNL
11965 (
11966# ifdef MKSESSION_NL
11967 !mksession_nl &&
11968# endif
11969 (putc('\r', fd) < 0)) ||
11970#endif
11971 (putc('\n', fd) < 0))
11972 return FAIL;
11973 return OK;
11974}
11975
11976/*
11977 * Write a line to "fd".
11978 * Return FAIL for a write error.
11979 */
11980 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011981put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982{
11983 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11984 return FAIL;
11985 return OK;
11986}
11987
11988#ifdef FEAT_VIMINFO
11989/*
11990 * ":rviminfo" and ":wviminfo".
11991 */
11992 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011993ex_viminfo(
11994 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995{
11996 char_u *save_viminfo;
11997
11998 save_viminfo = p_viminfo;
11999 if (*p_viminfo == NUL)
12000 p_viminfo = (char_u *)"'100";
12001 if (eap->cmdidx == CMD_rviminfo)
12002 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012003 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12004 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005 EMSG(_("E195: Cannot open viminfo file for reading"));
12006 }
12007 else
12008 write_viminfo(eap->arg, eap->forceit);
12009 p_viminfo = save_viminfo;
12010}
12011#endif
12012
12013#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012014/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012015 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012016 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012019dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021 if (fname == NULL)
12022 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012023 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024}
12025#endif
12026
12027/*
12028 * ":behave {mswin,xterm}"
12029 */
12030 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012031ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032{
12033 if (STRCMP(eap->arg, "mswin") == 0)
12034 {
12035 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12036 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12037 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12038 set_option_value((char_u *)"keymodel", 0L,
12039 (char_u *)"startsel,stopsel", 0);
12040 }
12041 else if (STRCMP(eap->arg, "xterm") == 0)
12042 {
12043 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12044 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12045 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12046 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12047 }
12048 else
12049 EMSG2(_(e_invarg2), eap->arg);
12050}
12051
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012052#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12053/*
12054 * Function given to ExpandGeneric() to obtain the possible arguments of the
12055 * ":behave {mswin,xterm}" command.
12056 */
12057 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012058get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012059{
12060 if (idx == 0)
12061 return (char_u *)"mswin";
12062 if (idx == 1)
12063 return (char_u *)"xterm";
12064 return NULL;
12065}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012066
12067/*
12068 * Function given to ExpandGeneric() to obtain the possible arguments of the
12069 * ":messages {clear}" command.
12070 */
12071 char_u *
12072get_messages_arg(expand_T *xp UNUSED, int idx)
12073{
12074 if (idx == 0)
12075 return (char_u *)"clear";
12076 return NULL;
12077}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012078#endif
12079
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080#ifdef FEAT_AUTOCMD
12081static int filetype_detect = FALSE;
12082static int filetype_plugin = FALSE;
12083static int filetype_indent = FALSE;
12084
12085/*
12086 * ":filetype [plugin] [indent] {on,off,detect}"
12087 * on: Load the filetype.vim file to install autocommands for file types.
12088 * off: Load the ftoff.vim file to remove all autocommands for file types.
12089 * plugin on: load filetype.vim and ftplugin.vim
12090 * plugin off: load ftplugof.vim
12091 * indent on: load filetype.vim and indent.vim
12092 * indent off: load indoff.vim
12093 */
12094 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012095ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096{
12097 char_u *arg = eap->arg;
12098 int plugin = FALSE;
12099 int indent = FALSE;
12100
12101 if (*eap->arg == NUL)
12102 {
12103 /* Print current status. */
12104 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12105 filetype_detect ? "ON" : "OFF",
12106 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12107 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12108 return;
12109 }
12110
12111 /* Accept "plugin" and "indent" in any order. */
12112 for (;;)
12113 {
12114 if (STRNCMP(arg, "plugin", 6) == 0)
12115 {
12116 plugin = TRUE;
12117 arg = skipwhite(arg + 6);
12118 continue;
12119 }
12120 if (STRNCMP(arg, "indent", 6) == 0)
12121 {
12122 indent = TRUE;
12123 arg = skipwhite(arg + 6);
12124 continue;
12125 }
12126 break;
12127 }
12128 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12129 {
12130 if (*arg == 'o' || !filetype_detect)
12131 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012132 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012133 filetype_detect = TRUE;
12134 if (plugin)
12135 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012136 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137 filetype_plugin = TRUE;
12138 }
12139 if (indent)
12140 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012141 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142 filetype_indent = TRUE;
12143 }
12144 }
12145 if (*arg == 'd')
12146 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012147 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012148 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149 }
12150 }
12151 else if (STRCMP(arg, "off") == 0)
12152 {
12153 if (plugin || indent)
12154 {
12155 if (plugin)
12156 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012157 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158 filetype_plugin = FALSE;
12159 }
12160 if (indent)
12161 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012162 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163 filetype_indent = FALSE;
12164 }
12165 }
12166 else
12167 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012168 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169 filetype_detect = FALSE;
12170 }
12171 }
12172 else
12173 EMSG2(_(e_invarg2), arg);
12174}
12175
12176/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012177 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178 */
12179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012180ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181{
12182 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012183 {
12184 char_u *arg = eap->arg;
12185
12186 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12187 arg += 9;
12188
12189 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12190 if (arg != eap->arg)
12191 did_filetype = FALSE;
12192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193}
12194#endif
12195
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012197ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198{
12199#ifdef FEAT_DIGRAPHS
12200 if (*eap->arg != NUL)
12201 putdigraph(eap->arg);
12202 else
12203 listdigraphs();
12204#else
12205 EMSG(_("E196: No digraphs in this version"));
12206#endif
12207}
12208
12209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012210ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211{
12212 int flags = 0;
12213
12214 if (eap->cmdidx == CMD_setlocal)
12215 flags = OPT_LOCAL;
12216 else if (eap->cmdidx == CMD_setglobal)
12217 flags = OPT_GLOBAL;
12218#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12219 if (cmdmod.browse && flags == 0)
12220 ex_options(eap);
12221 else
12222#endif
12223 (void)do_set(eap->arg, flags);
12224}
12225
12226#ifdef FEAT_SEARCH_EXTRA
12227/*
12228 * ":nohlsearch"
12229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012231ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012233 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012234 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235}
12236
12237/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012238 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239 * Sets nextcmd to the start of the next command, if any. Also called when
12240 * skipping commands to find the next command.
12241 */
12242 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012243ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244{
12245 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012246 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247 char_u *end;
12248 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012249 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012250
12251 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012252 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012253 else
12254 {
12255 EMSG(e_invcmd);
12256 return;
12257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258
12259 /* First clear any old pattern. */
12260 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012261 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262
12263 if (ends_excmd(*eap->arg))
12264 end = eap->arg;
12265 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012266 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267 end = eap->arg + 4;
12268 else
12269 {
12270 p = skiptowhite(eap->arg);
12271 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012272 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273 p = skipwhite(p);
12274 if (*p == NUL)
12275 {
12276 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012277 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278 EMSG2(_(e_invarg2), eap->arg);
12279 return;
12280 }
12281 end = skip_regexp(p + 1, *p, TRUE, NULL);
12282 if (!eap->skip)
12283 {
12284 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12285 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012286 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287 eap->errmsg = e_trailing;
12288 return;
12289 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012290 if (*end != *p)
12291 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012292 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012293 EMSG2(_(e_invarg2), p);
12294 return;
12295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296
12297 c = *end;
12298 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012299 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012300 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012301 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302 }
12303 }
12304 eap->nextcmd = find_nextcmd(end);
12305}
12306#endif
12307
12308#ifdef FEAT_CRYPT
12309/*
12310 * ":X": Get crypt key
12311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012313ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012315 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012316 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012317}
12318#endif
12319
12320#ifdef FEAT_FOLDING
12321 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012322ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323{
12324 if (foldManualAllowed(TRUE))
12325 foldCreate(eap->line1, eap->line2);
12326}
12327
12328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012329ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330{
12331 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12332 eap->forceit, FALSE);
12333}
12334
12335 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012336ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337{
12338 linenr_T lnum;
12339
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012340#ifdef FEAT_CLIPBOARD
12341 start_global_changes();
12342#endif
12343
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 /* First set the marks for all lines closed/open. */
12345 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12346 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12347 ml_setmarked(lnum);
12348
12349 /* Execute the command on the marked lines. */
12350 global_exe(eap->arg);
12351 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012352#ifdef FEAT_CLIPBOARD
12353 end_global_changes();
12354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355}
12356#endif