blob: 591775f4c4c2efaef63c7731873396522e964496 [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
Bram Moolenaar4033c552017-09-16 20:54:51 +0200111#if !defined(FEAT_LISTCMDS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112# 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
Bram Moolenaar4033c552017-09-16 20:54:51 +0200129#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130# 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 Moolenaarf28dbce2016-01-29 22:03:47 +0100162static void ex_close(exarg_T *eap);
163static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
164static void ex_only(exarg_T *eap);
165static void ex_resize(exarg_T *eap);
166static void ex_stag(exarg_T *eap);
167static void ex_tabclose(exarg_T *eap);
168static void ex_tabonly(exarg_T *eap);
169static void ex_tabnext(exarg_T *eap);
170static void ex_tabmove(exarg_T *eap);
171static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200172#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100173static void ex_pclose(exarg_T *eap);
174static void ex_ptag(exarg_T *eap);
175static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#else
177# define ex_pclose ex_ni
178# define ex_ptag ex_ni
179# define ex_pedit ex_ni
180#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100181static void ex_hide(exarg_T *eap);
182static void ex_stop(exarg_T *eap);
183static void ex_exit(exarg_T *eap);
184static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100186static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#else
188# define ex_goto ex_ni
189#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static void ex_shell(exarg_T *eap);
191static void ex_preserve(exarg_T *eap);
192static void ex_recover(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193#ifndef FEAT_LISTCMDS
194# define ex_argedit ex_ni
195# define ex_argadd ex_ni
196# define ex_argdelete ex_ni
197# define ex_listdo ex_ni
198#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100199static void ex_mode(exarg_T *eap);
200static void ex_wrongmodifier(exarg_T *eap);
201static void ex_find(exarg_T *eap);
202static void ex_open(exarg_T *eap);
203static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
205# define ex_drop ex_ni
206#endif
207#ifndef FEAT_GUI
208# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100209static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210#endif
211#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100212static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#else
214# define ex_tearoff ex_ni
215#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000216#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100217static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#else
219# define ex_popup ex_ni
220#endif
221#ifndef FEAT_GUI_MSWIN
222# define ex_simalt ex_ni
223#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000224#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225# define gui_mch_find_dialog ex_ni
226# define gui_mch_replace_dialog ex_ni
227#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000228#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229# define ex_helpfind ex_ni
230#endif
231#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100232# define ex_cscope ex_ni
233# define ex_scscope ex_ni
234# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235#endif
236#ifndef FEAT_SYN_HL
237# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200238# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000239#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200240#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200241# define ex_syntime ex_ni
242#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000243#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000244# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000245# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000246# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000247# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000248# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000249#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200250#ifndef FEAT_PERSISTENT_UNDO
251# define ex_rundo ex_ni
252# define ex_wundo ex_ni
253#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200254#ifndef FEAT_LUA
255# define ex_lua ex_script_ni
256# define ex_luado ex_ni
257# define ex_luafile ex_ni
258#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000259#ifndef FEAT_MZSCHEME
260# define ex_mzscheme ex_script_ni
261# define ex_mzfile ex_ni
262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifndef FEAT_PERL
264# define ex_perl ex_script_ni
265# define ex_perldo ex_ni
266#endif
267#ifndef FEAT_PYTHON
268# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200269# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270# define ex_pyfile ex_ni
271#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200272#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200273# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200274# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200275# define ex_py3file ex_ni
276#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100277#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
278# define ex_pyx ex_script_ni
279# define ex_pyxdo ex_ni
280# define ex_pyxfile ex_ni
281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282#ifndef FEAT_TCL
283# define ex_tcl ex_script_ni
284# define ex_tcldo ex_ni
285# define ex_tclfile ex_ni
286#endif
287#ifndef FEAT_RUBY
288# define ex_ruby ex_script_ni
289# define ex_rubydo ex_ni
290# define ex_rubyfile ex_ni
291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292#ifndef FEAT_KEYMAP
293# define ex_loadkeymap ex_ni
294#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100295static void ex_swapname(exarg_T *eap);
296static void ex_syncbind(exarg_T *eap);
297static void ex_read(exarg_T *eap);
298static void ex_pwd(exarg_T *eap);
299static void ex_equal(exarg_T *eap);
300static void ex_sleep(exarg_T *eap);
301static void do_exmap(exarg_T *eap, int isabbrev);
302static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100303static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000304#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100305static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#else
307# define ex_winpos ex_ni
308#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100309static void ex_operators(exarg_T *eap);
310static void ex_put(exarg_T *eap);
311static void ex_copymove(exarg_T *eap);
312static void ex_submagic(exarg_T *eap);
313static void ex_join(exarg_T *eap);
314static void ex_at(exarg_T *eap);
315static void ex_bang(exarg_T *eap);
316static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200317#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_wundo(exarg_T *eap);
319static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200320#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100321static void ex_redo(exarg_T *eap);
322static void ex_later(exarg_T *eap);
323static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100324static void ex_redrawstatus(exarg_T *eap);
325static void close_redir(void);
326static void ex_mkrc(exarg_T *eap);
327static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static char_u *uc_fun_cmd(void);
330static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100332static void ex_startinsert(exarg_T *eap);
333static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_checkpath(exarg_T *eap);
336static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337#else
338# define ex_findpat ex_ni
339# define ex_checkpath ex_ni
340#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200341#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#else
344# define ex_psearch ex_ni
345#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100346static void ex_tag(exarg_T *eap);
347static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#ifndef FEAT_EVAL
349# define ex_scriptnames ex_ni
350# define ex_finish ex_ni
351# define ex_echo ex_ni
352# define ex_echohl ex_ni
353# define ex_execute ex_ni
354# define ex_call ex_ni
355# define ex_if ex_ni
356# define ex_endif ex_ni
357# define ex_else ex_ni
358# define ex_while ex_ni
359# define ex_continue ex_ni
360# define ex_break ex_ni
361# define ex_endwhile ex_ni
362# define ex_throw ex_ni
363# define ex_try ex_ni
364# define ex_catch ex_ni
365# define ex_finally ex_ni
366# define ex_endtry ex_ni
367# define ex_endfunction ex_ni
368# define ex_let ex_ni
369# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000370# define ex_lockvar ex_ni
371# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372# define ex_function ex_ni
373# define ex_delfunction ex_ni
374# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000375# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100377static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100379static int makeopens(FILE *fd, char_u *dirnow);
380static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
381static void ex_loadview(exarg_T *eap);
382static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000383static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384#else
385# define ex_loadview ex_ni
386#endif
387#ifndef FEAT_EVAL
388# define ex_compiler ex_ni
389#endif
390#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100391static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#else
393# define ex_viminfo ex_ni
394#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100395static void ex_behave(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100397static void ex_filetype(exarg_T *eap);
398static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399#else
400# define ex_filetype ex_ni
401# define ex_setfiletype ex_ni
402#endif
403#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000404# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405# define ex_diffpatch ex_ni
406# define ex_diffgetput ex_ni
407# define ex_diffsplit ex_ni
408# define ex_diffthis ex_ni
409# define ex_diffupdate ex_ni
410#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100411static void ex_digraphs(exarg_T *eap);
412static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
414# define ex_options ex_ni
415#endif
416#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100417static void ex_nohlsearch(exarg_T *eap);
418static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419#else
420# define ex_nohlsearch ex_ni
421# define ex_match ex_ni
422#endif
423#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100424static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425#else
426# define ex_X ex_ni
427#endif
428#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100429static void ex_fold(exarg_T *eap);
430static void ex_foldopen(exarg_T *eap);
431static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432#else
433# define ex_fold ex_ni
434# define ex_foldopen ex_ni
435# define ex_folddo ex_ni
436#endif
437#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
438 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
439# define ex_language ex_ni
440#endif
441#ifndef FEAT_SIGNS
442# define ex_sign ex_ni
443#endif
444#ifndef FEAT_SUN_WORKSHOP
445# define ex_wsverb ex_ni
446#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000447#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200448# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000449# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200450# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452
453#ifndef FEAT_EVAL
454# define ex_debug ex_ni
455# define ex_breakadd ex_ni
456# define ex_debuggreedy ex_ni
457# define ex_breakdel ex_ni
458# define ex_breaklist ex_ni
459#endif
460
461#ifndef FEAT_CMDHIST
462# define ex_history ex_ni
463#endif
464#ifndef FEAT_JUMPLIST
465# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200466# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467# define ex_changes ex_ni
468#endif
469
Bram Moolenaar05159a02005-02-26 23:04:13 +0000470#ifndef FEAT_PROFILE
471# define ex_profile ex_ni
472#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200473#ifndef FEAT_TERMINAL
474# define ex_terminal ex_ni
475#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000476
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477/*
478 * Declare cmdnames[].
479 */
480#define DO_DECLARE_EXCMD
481#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200482#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100483
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484static char_u dollar_command[2] = {'$', 0};
485
486
487#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000488/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489typedef struct
490{
491 char_u *line; /* command line */
492 linenr_T lnum; /* sourcing_lnum of the line */
493} wcmd_T;
494
495/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000496 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000498 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000500struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
502 garray_T *lines_gap; /* growarray with line info */
503 int current_line; /* last read line from growarray */
504 int repeating; /* TRUE when looping a second time */
505 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100506 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 void *cookie;
508};
509
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100510static char_u *get_loop_line(int c, void *cookie, int indent);
511static int store_loop_line(garray_T *gap, char_u *line);
512static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000513
514/* Struct to save a few things while debugging. Used in do_cmdline() only. */
515struct dbg_stuff
516{
517 int trylevel;
518 int force_abort;
519 except_T *caught_stack;
520 char_u *vv_exception;
521 char_u *vv_throwpoint;
522 int did_emsg;
523 int got_int;
524 int did_throw;
525 int need_rethrow;
526 int check_cstack;
527 except_T *current_exception;
528};
529
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100530static void save_dbg_stuff(struct dbg_stuff *dsp);
531static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000532
533 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100534save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000535{
536 dsp->trylevel = trylevel; trylevel = 0;
537 dsp->force_abort = force_abort; force_abort = FALSE;
538 dsp->caught_stack = caught_stack; caught_stack = NULL;
539 dsp->vv_exception = v_exception(NULL);
540 dsp->vv_throwpoint = v_throwpoint(NULL);
541
542 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
543 dsp->did_emsg = did_emsg; did_emsg = FALSE;
544 dsp->got_int = got_int; got_int = FALSE;
545 dsp->did_throw = did_throw; did_throw = FALSE;
546 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
547 dsp->check_cstack = check_cstack; check_cstack = FALSE;
548 dsp->current_exception = current_exception; current_exception = NULL;
549}
550
551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100552restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000553{
554 suppress_errthrow = FALSE;
555 trylevel = dsp->trylevel;
556 force_abort = dsp->force_abort;
557 caught_stack = dsp->caught_stack;
558 (void)v_exception(dsp->vv_exception);
559 (void)v_throwpoint(dsp->vv_throwpoint);
560 did_emsg = dsp->did_emsg;
561 got_int = dsp->got_int;
562 did_throw = dsp->did_throw;
563 need_rethrow = dsp->need_rethrow;
564 check_cstack = dsp->check_cstack;
565 current_exception = dsp->current_exception;
566}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567#endif
568
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569/*
570 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
571 * command is given.
572 */
573 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100574do_exmode(
575 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576{
577 int save_msg_scroll;
578 int prev_msg_row;
579 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100580 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000581
582 if (improved)
583 exmode_active = EXMODE_VIM;
584 else
585 exmode_active = EXMODE_NORMAL;
586 State = NORMAL;
587
588 /* When using ":global /pat/ visual" and then "Q" we return to continue
589 * the :global command. */
590 if (global_busy)
591 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 save_msg_scroll = msg_scroll;
594 ++RedrawingDisabled; /* don't redisplay the window */
595 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596#ifdef FEAT_GUI
597 /* Ignore scrollbar and mouse events in Ex mode */
598 ++hold_gui_events;
599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600
601 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
602 while (exmode_active)
603 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000604 /* Check for a ":normal" command and no more characters left. */
605 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
606 {
607 exmode_active = FALSE;
608 break;
609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 msg_scroll = TRUE;
611 need_wait_return = FALSE;
612 ex_pressedreturn = FALSE;
613 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100614 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 prev_msg_row = msg_row;
616 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 if (improved)
618 {
619 cmdline_row = msg_row;
620 do_cmdline(NULL, getexline, NULL, 0);
621 }
622 else
623 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
624 lines_left = Rows - 1;
625
Bram Moolenaardf177f62005-02-22 08:39:57 +0000626 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100627 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000629 if (curbuf->b_ml.ml_flags & ML_EMPTY)
630 EMSG(_(e_emptybuf));
631 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000633 if (ex_pressedreturn)
634 {
635 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000636 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000637 msg_row = prev_msg_row;
638 if (prev_msg_row == Rows - 1)
639 msg_row--;
640 }
641 msg_col = 0;
642 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
643 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000646 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
647 {
648 if (curbuf->b_ml.ml_flags & ML_EMPTY)
649 EMSG(_(e_emptybuf));
650 else
651 EMSG(_("E501: At end-of-file"));
652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 }
654
655#ifdef FEAT_GUI
656 --hold_gui_events;
657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 --RedrawingDisabled;
659 --no_wait_return;
660 update_screen(CLEAR);
661 need_wait_return = FALSE;
662 msg_scroll = save_msg_scroll;
663}
664
665/*
666 * Execute a simple command line. Used for translated commands like "*".
667 */
668 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100669do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670{
671 return do_cmdline(cmd, NULL, NULL,
672 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
673}
674
675/*
676 * do_cmdline(): execute one Ex command line
677 *
678 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100679 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 * 2. Split up in parts separated with '|'.
681 *
682 * This function can be called recursively!
683 *
684 * flags:
685 * DOCMD_VERBOSE - The command will be included in the error message.
686 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100687 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 * DOCMD_KEYTYPED - Don't reset KeyTyped.
689 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
690 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
691 *
692 * return FAIL if cmdline could not be executed, OK otherwise
693 */
694 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100695do_cmdline(
696 char_u *cmdline,
697 char_u *(*fgetline)(int, void *, int),
698 void *cookie, /* argument for fgetline() */
699 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700{
701 char_u *next_cmdline; /* next cmd to execute */
702 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100703 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 static int recursive = 0; /* recursive depth */
705 int msg_didout_before_start = 0;
706 int count = 0; /* line number count */
707 int did_inc = FALSE; /* incremented RedrawingDisabled */
708 int retval = OK;
709#ifdef FEAT_EVAL
710 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000711 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 int current_line = 0; /* active line in lines_ga */
713 char_u *fname = NULL; /* function or script name */
714 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
715 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000716 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 int initial_trylevel;
718 struct msglist **saved_msg_list = NULL;
719 struct msglist *private_msg_list;
720
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100721 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100722 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000724 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000726 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100728# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729# define cmd_cookie cookie
730#endif
731 static int call_depth = 0; /* recursiveness */
732
733#ifdef FEAT_EVAL
734 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
735 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000736 * This ensures that the do_errthrow() call in do_one_cmd() does not
737 * combine the messages stored by an earlier invocation of do_one_cmd()
738 * with the command name of the later one. This would happen when
739 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 saved_msg_list = msg_list;
741 msg_list = &private_msg_list;
742 private_msg_list = NULL;
743#endif
744
745 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100746 * here. The value of 200 allows nested function calls, ":source", etc.
747 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100748 if (call_depth >= 200
749#ifdef FEAT_EVAL
750 && call_depth >= p_mfd
751#endif
752 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 {
754 EMSG(_("E169: Command too recursive"));
755#ifdef FEAT_EVAL
756 /* When converting to an exception, we do not include the command name
757 * since this is not an error of the specific command. */
758 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
759 msg_list = saved_msg_list;
760#endif
761 return FAIL;
762 }
763 ++call_depth;
764
765#ifdef FEAT_EVAL
766 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000767 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 cstack.cs_trylevel = 0;
769 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000770 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
772
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100773 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
775 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100776 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000777 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 ++ex_nesting_level;
779
780 /* Get the function or script name and the address where the next breakpoint
781 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000782 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 {
784 fname = func_name(real_cookie);
785 breakpoint = func_breakpoint(real_cookie);
786 dbg_tick = func_dbg_tick(real_cookie);
787 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100788 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 {
790 fname = sourcing_name;
791 breakpoint = source_breakpoint(real_cookie);
792 dbg_tick = source_dbg_tick(real_cookie);
793 }
794
795 /*
796 * Initialize "force_abort" and "suppress_errthrow" at the top level.
797 */
798 if (!recursive)
799 {
800 force_abort = FALSE;
801 suppress_errthrow = FALSE;
802 }
803
804 /*
805 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000806 * exception handling (used when debugging). Otherwise clear it to avoid
807 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000809 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000810 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000811 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200812 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
814 initial_trylevel = trylevel;
815
816 /*
817 * "did_throw" will be set to TRUE when an exception is being thrown.
818 */
819 did_throw = FALSE;
820#endif
821 /*
822 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000823 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 * If force_abort is set, we cancel everything.
825 */
826 did_emsg = FALSE;
827
828 /*
829 * KeyTyped is only set when calling vgetc(). Reset it here when not
830 * calling vgetc() (sourced command lines).
831 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100832 if (!(flags & DOCMD_KEYTYPED)
833 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 KeyTyped = FALSE;
835
836 /*
837 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000838 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 * - for multiple commands on one line, separated with '|'
840 * - when repeating until there are no more lines (for ":source")
841 */
842 next_cmdline = cmdline;
843 do
844 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100846 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847#endif
848
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000849 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 if (next_cmdline == NULL
851#ifdef FEAT_EVAL
852 && !force_abort
853 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000854 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855#endif
856 )
857 did_emsg = FALSE;
858
859 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000860 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100861 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 * 3. If a line is given: Make a copy, so we can mess with it.
863 */
864
865#ifdef FEAT_EVAL
866 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000867 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 {
869 /* Each '|' separated command is stored separately in lines_ga, to
870 * be able to jump to it. Don't use next_cmdline now. */
871 vim_free(cmdline_copy);
872 cmdline_copy = NULL;
873
874 /* Check if a function has returned or, unless it has an unclosed
875 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000876 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000878# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000879 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000880 func_line_end(real_cookie);
881# endif
882 if (func_has_ended(real_cookie))
883 {
884 retval = FAIL;
885 break;
886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000888#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000889 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100890 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000891 script_line_end();
892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
894 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100895 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 {
897 retval = FAIL;
898 break;
899 }
900
901 /* If breakpoints have been added/deleted need to check for it. */
902 if (breakpoint != NULL && dbg_tick != NULL
903 && *dbg_tick != debug_tick)
904 {
905 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100906 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 fname, sourcing_lnum);
908 *dbg_tick = debug_tick;
909 }
910
911 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
912 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
913
914 /* Did we encounter a breakpoint? */
915 if (breakpoint != NULL && *breakpoint != 0
916 && *breakpoint <= sourcing_lnum)
917 {
918 dbg_breakpoint(fname, sourcing_lnum);
919 /* Find next breakpoint. */
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 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000925# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000926 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927 {
928 if (getline_is_func)
929 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100930 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000931 script_line_start();
932 }
933# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 }
935
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000936 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000938 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100939 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 * below, so that it stores lines in or reads them from
941 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000942 * while/for loop. */
943 cmd_getline = get_loop_line;
944 cmd_cookie = (void *)&cmd_loop_cookie;
945 cmd_loop_cookie.lines_gap = &lines_ga;
946 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100947 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000948 cmd_loop_cookie.cookie = cookie;
949 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 }
951 else
952 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100953 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 cmd_cookie = cookie;
955 }
956#endif
957
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100958 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 if (next_cmdline == NULL)
960 {
961 /*
962 * Need to set msg_didout for the first line after an ":if",
963 * otherwise the ":if" will be overwritten.
964 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100965 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100967 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968#ifdef FEAT_EVAL
969 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
970#else
971 0
972#endif
973 )) == NULL)
974 {
975 /* Don't call wait_return for aborted command line. The NULL
976 * returned for the end of a sourced file or executed function
977 * doesn't do this. */
978 if (KeyTyped && !(flags & DOCMD_REPEAT))
979 need_wait_return = FALSE;
980 retval = FAIL;
981 break;
982 }
983 used_getline = TRUE;
984
985 /*
986 * Keep the first typed line. Clear it when more lines are typed.
987 */
988 if (flags & DOCMD_KEEPLINE)
989 {
990 vim_free(repeat_cmdline);
991 if (count == 0)
992 repeat_cmdline = vim_strsave(next_cmdline);
993 else
994 repeat_cmdline = NULL;
995 }
996 }
997
998 /* 3. Make a copy of the command so we can mess with it. */
999 else if (cmdline_copy == NULL)
1000 {
1001 next_cmdline = vim_strsave(next_cmdline);
1002 if (next_cmdline == NULL)
1003 {
1004 EMSG(_(e_outofmem));
1005 retval = FAIL;
1006 break;
1007 }
1008 }
1009 cmdline_copy = next_cmdline;
1010
1011#ifdef FEAT_EVAL
1012 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001013 * Save the current line when inside a ":while" or ":for", and when
1014 * the command looks like a ":while" or ":for", because we may need it
1015 * later. When there is a '|' and another command, it is stored
1016 * separately, because we need to be able to jump back to it from an
1017 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001019 if (current_line == lines_ga.ga_len
1020 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001022 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 {
1024 retval = FAIL;
1025 break;
1026 }
1027 }
1028 did_endif = FALSE;
1029#endif
1030
1031 if (count++ == 0)
1032 {
1033 /*
1034 * All output from the commands is put below each other, without
1035 * waiting for a return. Don't do this when executing commands
1036 * from a script or when being called recursive (e.g. for ":e
1037 * +command file").
1038 */
1039 if (!(flags & DOCMD_NOWAIT) && !recursive)
1040 {
1041 msg_didout_before_start = msg_didout;
1042 msg_didany = FALSE; /* no output yet */
1043 msg_start();
1044 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001045 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 ++RedrawingDisabled;
1047 did_inc = TRUE;
1048 }
1049 }
1050
1051 if (p_verbose >= 15 && sourcing_name != NULL)
1052 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001054 verbose_enter_scroll();
1055
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 smsg((char_u *)_("line %ld: %s"),
1057 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001058 if (msg_silent == 0)
1059 msg_puts((char_u *)"\n"); /* don't overwrite this */
1060
1061 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 --no_wait_return;
1063 }
1064
1065 /*
1066 * 2. Execute one '|' separated command.
1067 * do_one_cmd() will return NULL if there is no trailing '|'.
1068 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1069 */
1070 ++recursive;
1071 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1072#ifdef FEAT_EVAL
1073 &cstack,
1074#endif
1075 cmd_getline, cmd_cookie);
1076 --recursive;
1077
1078#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001079 if (cmd_cookie == (void *)&cmd_loop_cookie)
1080 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083#endif
1084
1085 if (next_cmdline == NULL)
1086 {
1087 vim_free(cmdline_copy);
1088 cmdline_copy = NULL;
1089#ifdef FEAT_CMDHIST
1090 /*
1091 * If the command was typed, remember it for the ':' register.
1092 * Do this AFTER executing the command to make :@: work.
1093 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001094 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 && new_last_cmdline != NULL)
1096 {
1097 vim_free(last_cmdline);
1098 last_cmdline = new_last_cmdline;
1099 new_last_cmdline = NULL;
1100 }
1101#endif
1102 }
1103 else
1104 {
1105 /* need to copy the command after the '|' to cmdline_copy, for the
1106 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001107 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 next_cmdline = cmdline_copy;
1109 }
1110
1111
1112#ifdef FEAT_EVAL
1113 /* reset did_emsg for a function that is not aborted by an error */
1114 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001115 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 && !func_has_abort(real_cookie))
1117 did_emsg = FALSE;
1118
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001119 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 {
1121 ++current_line;
1122
1123 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001124 * An ":endwhile", ":endfor" and ":continue" is handled here.
1125 * If we were executing commands, jump back to the ":while" or
1126 * ":for".
1127 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001129 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 /* Jump back to the matching ":while" or ":for". Be careful
1134 * not to use a cs_line[] from an entry that isn't a ":while"
1135 * or ":for": It would make "current_line" invalid and can
1136 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 if (!did_emsg && !got_int && !did_throw
1138 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001139 && (cstack.cs_flags[cstack.cs_idx]
1140 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 && cstack.cs_line[cstack.cs_idx] >= 0
1142 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1143 {
1144 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001145 /* remember we jumped there */
1146 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 line_breakcheck(); /* check if CTRL-C typed */
1148
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001149 /* Check for the next breakpoint at or after the ":while"
1150 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 if (breakpoint != NULL)
1152 {
1153 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001154 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 fname,
1156 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1157 *dbg_tick = debug_tick;
1158 }
1159 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001160 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001162 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001164 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1165 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 }
1167 }
1168
1169 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001174 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1176 }
1177 }
1178
1179 /*
1180 * When not inside any ":while" loop, clear remembered lines.
1181 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {
1184 if (lines_ga.ga_len > 0)
1185 {
1186 sourcing_lnum =
1187 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1188 free_cmdlines(&lines_ga);
1189 }
1190 current_line = 0;
1191 }
1192
1193 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001194 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1195 * being restored at the ":endtry". Reset them here and set the
1196 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1197 * This includes the case where a missing ":endif", ":endwhile" or
1198 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001202 cstack.cs_lflags &= ~CSL_HAD_FINA;
1203 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1204 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 did_throw ? (void *)current_exception : NULL);
1206 did_emsg = got_int = did_throw = FALSE;
1207 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1208 }
1209
1210 /* Update global "trylevel" for recursive calls to do_cmdline() from
1211 * within this loop. */
1212 trylevel = initial_trylevel + cstack.cs_trylevel;
1213
1214 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001215 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 * files) is aborted because of an error, an interrupt, or an uncaught
1217 * exception, cancel everything. If it is left normally, reset
1218 * force_abort to get the non-EH compatible abortion behavior for
1219 * the rest of the script.
1220 */
1221 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1222 force_abort = FALSE;
1223
1224 /* Convert an interrupt to an exception if appropriate. */
1225 (void)do_intthrow(&cstack);
1226#endif /* FEAT_EVAL */
1227
1228 }
1229 /*
1230 * Continue executing command lines when:
1231 * - no CTRL-C typed, no aborting error, no exception thrown or try
1232 * conditionals need to be checked for executing finally clauses or
1233 * catching an interrupt exception
1234 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001235 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 * looping for ":source" command or function call.
1237 */
1238 while (!((got_int
1239#ifdef FEAT_EVAL
1240 || (did_emsg && force_abort) || did_throw
1241#endif
1242 )
1243#ifdef FEAT_EVAL
1244 && cstack.cs_trylevel == 0
1245#endif
1246 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001247 && !(did_emsg
1248#ifdef FEAT_EVAL
1249 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001250 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001251 * the :endtry to be missed. */
1252 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1253#endif
1254 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001255 && (getline_equal(fgetline, cookie, getexmodeline)
1256 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 && (next_cmdline != NULL
1258#ifdef FEAT_EVAL
1259 || cstack.cs_idx >= 0
1260#endif
1261 || (flags & DOCMD_REPEAT)));
1262
1263 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001264 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265#ifdef FEAT_EVAL
1266 free_cmdlines(&lines_ga);
1267 ga_clear(&lines_ga);
1268
1269 if (cstack.cs_idx >= 0)
1270 {
1271 /*
1272 * If a sourced file or executed function ran to its end, report the
1273 * unclosed conditional.
1274 */
1275 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001276 && ((getline_equal(fgetline, cookie, getsourceline)
1277 && !source_finished(fgetline, cookie))
1278 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 && !func_has_ended(real_cookie))))
1280 {
1281 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1282 EMSG(_(e_endtry));
1283 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1284 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001285 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1286 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 else
1288 EMSG(_(e_endif));
1289 }
1290
1291 /*
1292 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1293 * ":endtry" in a sourced file or executed function. If the try
1294 * conditional is in its finally clause, ignore anything pending.
1295 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001296 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 */
1298 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001299 {
1300 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1301
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001302 if (idx >= 0)
1303 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001304 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1305 &cstack.cs_looplevel);
1306 }
1307 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 trylevel = initial_trylevel;
1309 }
1310
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001311 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1312 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001314 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 ? (char_u *)"endfunction" : (char_u *)NULL);
1316
1317 if (trylevel == 0)
1318 {
1319 /*
1320 * When an exception is being thrown out of the outermost try
1321 * conditional, discard the uncaught exception, disable the conversion
1322 * of interrupts or errors to exceptions, and ensure that no more
1323 * commands are executed.
1324 */
1325 if (did_throw)
1326 {
1327 void *p = NULL;
1328 char_u *saved_sourcing_name;
1329 int saved_sourcing_lnum;
1330 struct msglist *messages = NULL, *next;
1331
1332 /*
1333 * If the uncaught exception is a user exception, report it as an
1334 * error. If it is an error exception, display the saved error
1335 * message now. For an interrupt exception, do nothing; the
1336 * interrupt message is given elsewhere.
1337 */
1338 switch (current_exception->type)
1339 {
1340 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001341 vim_snprintf((char *)IObuff, IOSIZE,
1342 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 current_exception->value);
1344 p = vim_strsave(IObuff);
1345 break;
1346 case ET_ERROR:
1347 messages = current_exception->messages;
1348 current_exception->messages = NULL;
1349 break;
1350 case ET_INTERRUPT:
1351 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353
1354 saved_sourcing_name = sourcing_name;
1355 saved_sourcing_lnum = sourcing_lnum;
1356 sourcing_name = current_exception->throw_name;
1357 sourcing_lnum = current_exception->throw_lnum;
1358 current_exception->throw_name = NULL;
1359
1360 discard_current_exception(); /* uses IObuff if 'verbose' */
1361 suppress_errthrow = TRUE;
1362 force_abort = TRUE;
1363
1364 if (messages != NULL)
1365 {
1366 do
1367 {
1368 next = messages->next;
1369 emsg(messages->msg);
1370 vim_free(messages->msg);
1371 vim_free(messages);
1372 messages = next;
1373 }
1374 while (messages != NULL);
1375 }
1376 else if (p != NULL)
1377 {
1378 emsg(p);
1379 vim_free(p);
1380 }
1381 vim_free(sourcing_name);
1382 sourcing_name = saved_sourcing_name;
1383 sourcing_lnum = saved_sourcing_lnum;
1384 }
1385
1386 /*
1387 * On an interrupt or an aborting error not converted to an exception,
1388 * disable the conversion of errors to exceptions. (Interrupts are not
1389 * converted any more, here.) This enables also the interrupt message
1390 * when force_abort is set and did_emsg unset in case of an interrupt
1391 * from a finally clause after an error.
1392 */
1393 else if (got_int || (did_emsg && force_abort))
1394 suppress_errthrow = TRUE;
1395 }
1396
1397 /*
1398 * The current cstack will be freed when do_cmdline() returns. An uncaught
1399 * exception will have to be rethrown in the previous cstack. If a function
1400 * has just returned or a script file was just finished and the previous
1401 * cstack belongs to the same function or, respectively, script file, it
1402 * will have to be checked for finally clauses to be executed due to the
1403 * ":return" or ":finish". This is done in do_one_cmd().
1404 */
1405 if (did_throw)
1406 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001407 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001409 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 && ex_nesting_level > func_level(real_cookie) + 1))
1411 {
1412 if (!did_throw)
1413 check_cstack = TRUE;
1414 }
1415 else
1416 {
1417 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001418 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 --ex_nesting_level;
1420 /*
1421 * Go to debug mode when returning from a function in which we are
1422 * single-stepping.
1423 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001424 if ((getline_equal(fgetline, cookie, getsourceline)
1425 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001427 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 ? (char_u *)_("End of sourced file")
1429 : (char_u *)_("End of function"));
1430 }
1431
1432 /*
1433 * Restore the exception environment (done after returning from the
1434 * debugger).
1435 */
1436 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001437 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438
1439 msg_list = saved_msg_list;
1440#endif /* FEAT_EVAL */
1441
1442 /*
1443 * If there was too much output to fit on the command line, ask the user to
1444 * hit return before redrawing the screen. With the ":global" command we do
1445 * this only once after the command is finished.
1446 */
1447 if (did_inc)
1448 {
1449 --RedrawingDisabled;
1450 --no_wait_return;
1451 msg_scroll = FALSE;
1452
1453 /*
1454 * When just finished an ":if"-":else" which was typed, no need to
1455 * wait for hit-return. Also for an error situation.
1456 */
1457 if (retval == FAIL
1458#ifdef FEAT_EVAL
1459 || (did_endif && KeyTyped && !did_emsg)
1460#endif
1461 )
1462 {
1463 need_wait_return = FALSE;
1464 msg_didany = FALSE; /* don't wait when restarting edit */
1465 }
1466 else if (need_wait_return)
1467 {
1468 /*
1469 * The msg_start() above clears msg_didout. The wait_return we do
1470 * here should not overwrite the command that may be shown before
1471 * doing that.
1472 */
1473 msg_didout |= msg_didout_before_start;
1474 wait_return(FALSE);
1475 }
1476 }
1477
Bram Moolenaar2a942252012-11-28 23:03:07 +01001478#ifdef FEAT_EVAL
1479 did_endif = FALSE; /* in case do_cmdline used recursively */
1480#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 /*
1482 * Reset if_level, in case a sourced script file contains more ":if" than
1483 * ":endif" (could be ":if x | foo | endif").
1484 */
1485 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001486#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 --call_depth;
1489 return retval;
1490}
1491
1492#ifdef FEAT_EVAL
1493/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001494 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 */
1496 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001497get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001499 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 wcmd_T *wp;
1501 char_u *line;
1502
1503 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1504 {
1505 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001506 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001508 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 if (cp->getline == NULL)
1510 line = getcmdline(c, 0L, indent);
1511 else
1512 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001513 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 ++cp->current_line;
1515
1516 return line;
1517 }
1518
1519 KeyTyped = FALSE;
1520 ++cp->current_line;
1521 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1522 sourcing_lnum = wp->lnum;
1523 return vim_strsave(wp->line);
1524}
1525
1526/*
1527 * Store a line in "gap" so that a ":while" loop can execute it again.
1528 */
1529 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001530store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531{
1532 if (ga_grow(gap, 1) == FAIL)
1533 return FAIL;
1534 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1535 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1536 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 return OK;
1538}
1539
1540/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001541 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 */
1543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001544free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545{
1546 while (gap->ga_len > 0)
1547 {
1548 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1549 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 }
1551}
1552#endif
1553
1554/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001555 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1556 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001559getline_equal(
1560 char_u *(*fgetline)(int, void *, int),
1561 void *cookie UNUSED, /* argument for fgetline() */
1562 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
1564#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001565 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001566 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
Bram Moolenaar89d40322006-08-29 15:30:07 +00001568 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001569 * function that's originally used to obtain the lines. This may be
1570 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001571 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001572 cp = (struct loop_cookie *)cookie;
1573 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 {
1575 gp = cp->getline;
1576 cp = cp->cookie;
1577 }
1578 return gp == func;
1579#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001580 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581#endif
1582}
1583
1584#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1585/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001586 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 * getline function. Otherwise return "cookie".
1588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001590getline_cookie(
1591 char_u *(*fgetline)(int, void *, int) UNUSED,
1592 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593{
1594# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001595 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001596 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597
Bram Moolenaar89d40322006-08-29 15:30:07 +00001598 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001599 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001601 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001602 cp = (struct loop_cookie *)cookie;
1603 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 {
1605 gp = cp->getline;
1606 cp = cp->cookie;
1607 }
1608 return cp;
1609# else
1610 return cookie;
1611# endif
1612}
1613#endif
1614
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001615
1616/*
1617 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1618 * ":bwipeout", etc.
1619 * Returns the buffer number.
1620 */
1621 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001622compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001623{
1624 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001625 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001626 int count = offset;
1627
1628 buf = firstbuf;
1629 while (buf->b_next != NULL && buf->b_fnum < lnum)
1630 buf = buf->b_next;
1631 while (count != 0)
1632 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001633 count += (offset < 0) ? 1 : -1;
1634 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1635 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001636 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001637 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001638 if (addr_type == ADDR_LOADED_BUFFERS)
1639 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001640 while (buf->b_ml.ml_mfp == NULL)
1641 {
1642 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1643 if (nextbuf == NULL)
1644 break;
1645 buf = nextbuf;
1646 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001647 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001648 /* we might have gone too far, last buffer is not loadedd */
1649 if (addr_type == ADDR_LOADED_BUFFERS)
1650 while (buf->b_ml.ml_mfp == NULL)
1651 {
1652 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1653 if (nextbuf == NULL)
1654 break;
1655 buf = nextbuf;
1656 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001657 return buf->b_fnum;
1658}
1659
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001660static int current_win_nr(win_T *win);
1661static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001662
1663 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001664current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001665{
1666 win_T *wp;
1667 int nr = 0;
1668
Bram Moolenaar29323592016-07-24 22:04:11 +02001669 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001670 {
1671 ++nr;
1672 if (wp == win)
1673 break;
1674 }
1675 return nr;
1676}
1677
1678 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001679current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001680{
1681 tabpage_T *tp;
1682 int nr = 0;
1683
Bram Moolenaar29323592016-07-24 22:04:11 +02001684 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001685 {
1686 ++nr;
1687 if (tp == tab)
1688 break;
1689 }
1690 return nr;
1691}
1692
1693# define CURRENT_WIN_NR current_win_nr(curwin)
1694# define LAST_WIN_NR current_win_nr(NULL)
1695# define CURRENT_TAB_NR current_tab_nr(curtab)
1696# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001697
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698/*
1699 * Execute one Ex command.
1700 *
1701 * If 'sourcing' is TRUE, the command will be included in the error message.
1702 *
1703 * 1. skip comment lines and leading space
1704 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001705 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001706 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001707 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001708 * 6. parse arguments
1709 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001711 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 *
1713 * This function may be called recursively!
1714 */
1715#if (_MSC_VER == 1200)
1716/*
Bram Moolenaared203462004-06-16 11:19:22 +00001717 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001719 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720#endif
1721 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001722do_one_cmd(
1723 char_u **cmdlinep,
1724 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001726 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001728 char_u *(*fgetline)(int, void *, int),
1729 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730{
1731 char_u *p;
1732 linenr_T lnum;
1733 long n;
1734 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001735 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 exarg_T ea; /* Ex command arguments */
1737 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001738 int save_msg_scroll = msg_scroll;
1739 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001741#ifdef HAVE_SANDBOX
1742 int did_sandbox = FALSE;
1743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 cmdmod_T save_cmdmod;
1745 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001746 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001747 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
1749 vim_memset(&ea, 0, sizeof(ea));
1750 ea.line1 = 1;
1751 ea.line2 = 1;
1752#ifdef FEAT_EVAL
1753 ++ex_nesting_level;
1754#endif
1755
Bram Moolenaar21691f82012-12-05 19:13:18 +01001756 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 if (quitmore
1758#ifdef FEAT_EVAL
1759 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001760 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001761#endif
1762#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001763 /* avoid that an autocommand, e.g. QuitPre, does this */
1764 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765#endif
1766 )
1767 --quitmore;
1768
1769 /*
1770 * Reset browse, confirm, etc.. They are restored when returning, for
1771 * recursive calls.
1772 */
1773 save_cmdmod = cmdmod;
1774 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1775
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001776 /* "#!anything" is handled like a comment. */
1777 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1778 goto doend;
1779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 /*
1781 * Repeat until no more command modifiers are found.
1782 */
1783 ea.cmd = *cmdlinep;
1784 for (;;)
1785 {
1786/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001787 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 */
1789 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1790 ++ea.cmd;
1791
1792 /* in ex mode, an empty line works like :+ */
1793 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001794 && (getline_equal(fgetline, cookie, getexmodeline)
1795 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001796 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 {
1798 ea.cmd = (char_u *)"+";
1799 ex_pressedreturn = TRUE;
1800 }
1801
1802 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001803 if (*ea.cmd == '"')
1804 goto doend;
1805 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001806 {
1807 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
1811/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001812 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001814 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 switch (*p)
1816 {
1817 /* When adding an entry, also modify cmd_exists(). */
1818 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1819 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 continue;
1822
1823 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1824 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 continue;
1827 }
1828 if (checkforcmd(&ea.cmd, "browse", 3))
1829 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001830#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 cmdmod.browse = TRUE;
1832#endif
1833 continue;
1834 }
1835 if (!checkforcmd(&ea.cmd, "botright", 2))
1836 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 cmdmod.split |= WSP_BOT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 continue;
1839
1840 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1841 break;
1842#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1843 cmdmod.confirm = TRUE;
1844#endif
1845 continue;
1846
1847 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1848 {
1849 cmdmod.keepmarks = TRUE;
1850 continue;
1851 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001852 if (checkforcmd(&ea.cmd, "keepalt", 5))
1853 {
1854 cmdmod.keepalt = TRUE;
1855 continue;
1856 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001857 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1858 {
1859 cmdmod.keeppatterns = TRUE;
1860 continue;
1861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1863 break;
1864 cmdmod.keepjumps = TRUE;
1865 continue;
1866
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001867 case 'f': /* only accept ":filter {pat} cmd" */
1868 {
1869 char_u *reg_pat;
1870
1871 if (!checkforcmd(&p, "filter", 4)
1872 || *p == NUL || ends_excmd(*p))
1873 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001874 if (*p == '!')
1875 {
1876 cmdmod.filter_force = TRUE;
1877 p = skipwhite(p + 1);
1878 if (*p == NUL || ends_excmd(*p))
1879 break;
1880 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001881 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1882 if (p == NULL || *p == NUL)
1883 break;
1884 cmdmod.filter_regmatch.regprog =
1885 vim_regcomp(reg_pat, RE_MAGIC);
1886 if (cmdmod.filter_regmatch.regprog == NULL)
1887 break;
1888 ea.cmd = p;
1889 continue;
1890 }
1891
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 /* ":hide" and ":hide | cmd" are not modifiers */
1893 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1894 || *p == NUL || ends_excmd(*p))
1895 break;
1896 ea.cmd = p;
1897 cmdmod.hide = TRUE;
1898 continue;
1899
1900 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1901 {
1902 cmdmod.lockmarks = TRUE;
1903 continue;
1904 }
1905
1906 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1907 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 continue;
1910
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001911 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001912 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001913#ifdef FEAT_AUTOCMD
1914 if (cmdmod.save_ei == NULL)
1915 {
1916 /* Set 'eventignore' to "all". Restore the
1917 * existing option value later. */
1918 cmdmod.save_ei = vim_strsave(p_ei);
1919 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001920 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001921 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001922#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001923 continue;
1924 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001925 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001926 break;
1927 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001928 continue;
1929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 continue;
1934
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001935 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1936 {
1937#ifdef HAVE_SANDBOX
1938 if (!did_sandbox)
1939 ++sandbox;
1940 did_sandbox = TRUE;
1941#endif
1942 continue;
1943 }
1944 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001946 if (save_msg_silent == -1)
1947 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 ++msg_silent;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001949 if (*ea.cmd == '!' && !VIM_ISWHITE(ea.cmd[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 {
1951 /* ":silent!", but not "silent !cmd" */
1952 ea.cmd = skipwhite(ea.cmd + 1);
1953 ++emsg_silent;
1954 ++did_esilent;
1955 }
1956 continue;
1957
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001958 case 't': if (checkforcmd(&p, "tab", 3))
1959 {
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001960 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01001961 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001962 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001963 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001964 else
1965 {
1966 if (tabnr < 0 || tabnr > LAST_TAB_NR)
1967 {
1968 errormsg = (char_u *)_(e_invrange);
1969 goto doend;
1970 }
1971 cmdmod.tab = tabnr + 1;
1972 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001973 ea.cmd = p;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001974 continue;
1975 }
1976 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 cmdmod.split |= WSP_TOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 continue;
1980
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001981 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1982 break;
1983 if (save_msg_silent == -1)
1984 save_msg_silent = msg_silent;
1985 msg_silent = 0;
1986 continue;
1987
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1989 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 cmdmod.split |= WSP_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 continue;
1992 }
1993 if (!checkforcmd(&p, "verbose", 4))
1994 break;
1995 if (verbose_save < 0)
1996 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001997 if (vim_isdigit(*ea.cmd))
1998 p_verbose = atoi((char *)ea.cmd);
1999 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 p_verbose = 1;
2001 ea.cmd = p;
2002 continue;
2003 }
2004 break;
2005 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002006 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007
2008#ifdef FEAT_EVAL
2009 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2010 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2011#else
2012 ea.skip = (if_level > 0);
2013#endif
2014
2015#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002016# ifdef FEAT_PROFILE
2017 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002018 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002019 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002020 if (getline_equal(fgetline, cookie, get_func_line))
2021 func_line_exec(getline_cookie(fgetline, cookie));
2022 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002023 script_line_exec();
2024 }
2025#endif
2026
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 /* May go to debug mode. If this happens and the ">quit" debug command is
2028 * used, throw an interrupt exception and skip the next command. */
2029 dbg_check_breakpoint(&ea);
2030 if (!ea.skip && got_int)
2031 {
2032 ea.skip = TRUE;
2033 (void)do_intthrow(cstack);
2034 }
2035#endif
2036
2037/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002038 * 3. Skip over the range to find the command. Let "p" point to after it.
2039 *
2040 * We need the command to know what kind of range it uses.
2041 */
2042 cmd = ea.cmd;
2043 ea.cmd = skip_range(ea.cmd, NULL);
2044 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2045 ea.cmd = skipwhite(ea.cmd + 1);
2046 p = find_command(&ea, NULL);
2047
2048/*
2049 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 *
2051 * where 'addr' is:
2052 *
2053 * % (entire file)
2054 * $ [+-NUM]
2055 * 'x [+-NUM] (where x denotes a currently defined mark)
2056 * . [+-NUM]
2057 * [+-NUM]..
2058 * NUM
2059 *
2060 * The ea.cmd pointer is updated to point to the first character following the
2061 * range spec. If an initial address is found, but no second, the upper bound
2062 * is equal to the lower.
2063 */
2064
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002065 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002066 if (!IS_USER_CMDIDX(ea.cmdidx))
2067 {
2068 if (ea.cmdidx != CMD_SIZE)
2069 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2070 else
2071 ea.addr_type = ADDR_LINES;
2072
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002073 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002074 if (ea.cmdidx == CMD_wincmd && p != NULL)
2075 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002076 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002077
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002079 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 for (;;)
2081 {
2082 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002083 switch (ea.addr_type)
2084 {
2085 case ADDR_LINES:
2086 /* default is current line number */
2087 ea.line2 = curwin->w_cursor.lnum;
2088 break;
2089 case ADDR_WINDOWS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002090 ea.line2 = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002091 break;
2092 case ADDR_ARGUMENTS:
2093 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002094 if (ea.line2 > ARGCOUNT)
2095 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002096 break;
2097 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002098 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002099 ea.line2 = curbuf->b_fnum;
2100 break;
2101 case ADDR_TABS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002102 ea.line2 = CURRENT_TAB_NR;
2103 break;
2104 case ADDR_TABS_RELATIVE:
2105 ea.line2 = 1;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002106 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002107#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002108 case ADDR_QUICKFIX:
2109 ea.line2 = qf_get_cur_valid_idx(&ea);
2110 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002111#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002114 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002115 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 if (ea.cmd == NULL) /* error detected */
2117 goto doend;
2118 if (lnum == MAXLNUM)
2119 {
2120 if (*ea.cmd == '%') /* '%' - all lines */
2121 {
2122 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002123 switch (ea.addr_type)
2124 {
2125 case ADDR_LINES:
2126 ea.line1 = 1;
2127 ea.line2 = curbuf->b_ml.ml_line_count;
2128 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002129 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002130 {
2131 buf_T *buf = firstbuf;
2132
2133 while (buf->b_next != NULL
2134 && buf->b_ml.ml_mfp == NULL)
2135 buf = buf->b_next;
2136 ea.line1 = buf->b_fnum;
2137 buf = lastbuf;
2138 while (buf->b_prev != NULL
2139 && buf->b_ml.ml_mfp == NULL)
2140 buf = buf->b_prev;
2141 ea.line2 = buf->b_fnum;
2142 break;
2143 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002144 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002145 ea.line1 = firstbuf->b_fnum;
2146 ea.line2 = lastbuf->b_fnum;
2147 break;
2148 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002149 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002150 if (IS_USER_CMDIDX(ea.cmdidx))
2151 {
2152 ea.line1 = 1;
2153 ea.line2 = ea.addr_type == ADDR_WINDOWS
2154 ? LAST_WIN_NR : LAST_TAB_NR;
2155 }
2156 else
2157 {
2158 /* there is no Vim command which uses '%' and
2159 * ADDR_WINDOWS or ADDR_TABS */
2160 errormsg = (char_u *)_(e_invrange);
2161 goto doend;
2162 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002163 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002164 case ADDR_TABS_RELATIVE:
2165 errormsg = (char_u *)_(e_invrange);
2166 goto doend;
2167 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002168 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002169 if (ARGCOUNT == 0)
2170 ea.line1 = ea.line2 = 0;
2171 else
2172 {
2173 ea.line1 = 1;
2174 ea.line2 = ARGCOUNT;
2175 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002176 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002177#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002178 case ADDR_QUICKFIX:
2179 ea.line1 = 1;
2180 ea.line2 = qf_get_size(&ea);
2181 if (ea.line2 == 0)
2182 ea.line2 = 1;
2183 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002184#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 ++ea.addr_count;
2187 }
2188 /* '*' - visual area */
2189 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2190 {
2191 pos_T *fp;
2192
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002193 if (ea.addr_type != ADDR_LINES)
2194 {
2195 errormsg = (char_u *)_(e_invrange);
2196 goto doend;
2197 }
2198
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 ++ea.cmd;
2200 if (!ea.skip)
2201 {
2202 fp = getmark('<', FALSE);
2203 if (check_mark(fp) == FAIL)
2204 goto doend;
2205 ea.line1 = fp->lnum;
2206 fp = getmark('>', FALSE);
2207 if (check_mark(fp) == FAIL)
2208 goto doend;
2209 ea.line2 = fp->lnum;
2210 ++ea.addr_count;
2211 }
2212 }
2213 }
2214 else
2215 ea.line2 = lnum;
2216 ea.addr_count++;
2217
2218 if (*ea.cmd == ';')
2219 {
2220 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002221 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +01002223 /* don't leave the cursor on an illegal line or column */
2224 check_cursor();
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 }
2227 else if (*ea.cmd != ',')
2228 break;
2229 ++ea.cmd;
2230 }
2231
2232 /* One address given: set start and end lines */
2233 if (ea.addr_count == 1)
2234 {
2235 ea.line1 = ea.line2;
2236 /* ... but only implicit: really no address given */
2237 if (lnum == MAXLNUM)
2238 ea.addr_count = 0;
2239 }
2240
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002242 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 */
2244
2245 /*
2246 * Skip ':' and any white space
2247 */
2248 ea.cmd = skipwhite(ea.cmd);
2249 while (*ea.cmd == ':')
2250 ea.cmd = skipwhite(ea.cmd + 1);
2251
2252 /*
2253 * If we got a line, but no command, then go to the line.
2254 * If we find a '|' or '\n' we set ea.nextcmd.
2255 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002256 if (*ea.cmd == NUL || *ea.cmd == '"'
2257 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
2259 /*
2260 * strange vi behaviour:
2261 * ":3" jumps to line 3
2262 * ":3|..." prints line 3
2263 * ":|" prints current line
2264 */
2265 if (ea.skip) /* skip this if inside :if */
2266 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002267 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 {
2269 ea.cmdidx = CMD_print;
2270 ea.argt = RANGE+COUNT+TRLBAR;
2271 if ((errormsg = invalid_range(&ea)) == NULL)
2272 {
2273 correct_range(&ea);
2274 ex_print(&ea);
2275 }
2276 }
2277 else if (ea.addr_count != 0)
2278 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002279 if (ea.line2 > curbuf->b_ml.ml_line_count)
2280 {
2281 /* With '-' in 'cpoptions' a line number past the file is an
2282 * error, otherwise put it at the end of the file. */
2283 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2284 ea.line2 = -1;
2285 else
2286 ea.line2 = curbuf->b_ml.ml_line_count;
2287 }
2288
2289 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002290 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 else
2292 {
2293 if (ea.line2 == 0)
2294 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 else
2296 curwin->w_cursor.lnum = ea.line2;
2297 beginline(BL_SOL | BL_FIX);
2298 }
2299 }
2300 goto doend;
2301 }
2302
Bram Moolenaard5005162014-08-22 23:05:54 +02002303#ifdef FEAT_AUTOCMD
2304 /* If this looks like an undefined user command and there are CmdUndefined
2305 * autocommands defined, trigger the matching autocommands. */
2306 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2307 && ASCII_ISUPPER(*ea.cmd)
2308 && has_cmdundefined())
2309 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002310 int ret;
2311
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002312 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002313 while (ASCII_ISALNUM(*p))
2314 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002315 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002316 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2317 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002318 /* If the autocommands did something and didn't cause an error, try
2319 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002320 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002321 }
2322#endif
2323
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324#ifdef FEAT_USR_CMDS
2325 if (p == NULL)
2326 {
2327 if (!ea.skip)
2328 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2329 goto doend;
2330 }
2331 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002332 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2333 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 {
2335 errormsg = uc_fun_cmd();
2336 goto doend;
2337 }
2338#endif
2339 if (ea.cmdidx == CMD_SIZE)
2340 {
2341 if (!ea.skip)
2342 {
2343 STRCPY(IObuff, _("E492: Not an editor command"));
2344 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002345 {
2346 /* If the modifier was parsed OK the error must be in the
2347 * following command */
2348 if (after_modifier != NULL)
2349 append_command(after_modifier);
2350 else
2351 append_command(*cmdlinep);
2352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002354 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 }
2356 goto doend;
2357 }
2358
Bram Moolenaar958636c2014-10-21 20:01:58 +02002359 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2360 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002361#ifdef HAVE_EX_SCRIPT_NI
2362 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2363#endif
2364 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
2366#ifndef FEAT_EVAL
2367 /*
2368 * When the expression evaluation is disabled, recognize the ":if" and
2369 * ":endif" commands and ignore everything in between it.
2370 */
2371 if (ea.cmdidx == CMD_if)
2372 ++if_level;
2373 if (if_level)
2374 {
2375 if (ea.cmdidx == CMD_endif)
2376 --if_level;
2377 goto doend;
2378 }
2379
2380#endif
2381
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002382 /* forced commands */
2383 if (*p == '!' && ea.cmdidx != CMD_substitute
2384 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 {
2386 ++p;
2387 ea.forceit = TRUE;
2388 }
2389 else
2390 ea.forceit = FALSE;
2391
2392/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002393 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002395 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002396 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
2398 if (!ea.skip)
2399 {
2400#ifdef HAVE_SANDBOX
2401 if (sandbox != 0 && !(ea.argt & SBOXOK))
2402 {
2403 /* Command not allowed in sandbox. */
2404 errormsg = (char_u *)_(e_sandbox);
2405 goto doend;
2406 }
2407#endif
2408 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2409 {
2410 /* Command not allowed in non-'modifiable' buffer */
2411 errormsg = (char_u *)_(e_modifiable);
2412 goto doend;
2413 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002414
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002415 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002416 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002418 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002419 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 goto doend;
2421 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002422#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002423 /* Disallow editing another buffer when "curbuf_lock" is set.
2424 * Do allow ":edit" (check for argument later).
2425 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002426 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002427 && ea.cmdidx != CMD_edit
2428 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002429 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002430 && curbuf_locked())
2431 goto doend;
2432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433
2434 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2435 {
2436 /* no range allowed */
2437 errormsg = (char_u *)_(e_norange);
2438 goto doend;
2439 }
2440 }
2441
2442 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2443 {
2444 errormsg = (char_u *)_(e_nobang);
2445 goto doend;
2446 }
2447
2448 /*
2449 * Don't complain about the range if it is not used
2450 * (could happen if line_count is accidentally set to 0).
2451 */
2452 if (!ea.skip && !ni)
2453 {
2454 /*
2455 * If the range is backwards, ask for confirmation and, if given, swap
2456 * ea.line1 & ea.line2 so it's forwards again.
2457 * When global command is busy, don't ask, will fail below.
2458 */
2459 if (!global_busy && ea.line1 > ea.line2)
2460 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002461 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002463 if (sourcing || exmode_active)
2464 {
2465 errormsg = (char_u *)_("E493: Backwards range given");
2466 goto doend;
2467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 if (ask_yesno((char_u *)
2469 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002470 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 }
2472 lnum = ea.line1;
2473 ea.line1 = ea.line2;
2474 ea.line2 = lnum;
2475 }
2476 if ((errormsg = invalid_range(&ea)) != NULL)
2477 goto doend;
2478 }
2479
2480 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2481 ea.line2 = 1;
2482
2483 correct_range(&ea);
2484
2485#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002486 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2487 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 {
2489 /* Put the first line at the start of a closed fold, put the last line
2490 * at the end of a closed fold. */
2491 (void)hasFolding(ea.line1, &ea.line1, NULL);
2492 (void)hasFolding(ea.line2, NULL, &ea.line2);
2493 }
2494#endif
2495
2496#ifdef FEAT_QUICKFIX
2497 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002498 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 * option here, so things like % get expanded.
2500 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002501 p = replace_makeprg(&ea, p, cmdlinep);
2502 if (p == NULL)
2503 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504#endif
2505
2506 /*
2507 * Skip to start of argument.
2508 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2509 */
2510 if (ea.cmdidx == CMD_bang)
2511 ea.arg = p;
2512 else
2513 ea.arg = skipwhite(p);
2514
2515 /*
2516 * Check for "++opt=val" argument.
2517 * Must be first, allow ":w ++enc=utf8 !cmd"
2518 */
2519 if (ea.argt & ARGOPT)
2520 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2521 if (getargopt(&ea) == FAIL && !ni)
2522 {
2523 errormsg = (char_u *)_(e_invarg);
2524 goto doend;
2525 }
2526
2527 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2528 {
2529 if (*ea.arg == '>') /* append */
2530 {
2531 if (*++ea.arg != '>') /* typed wrong */
2532 {
2533 errormsg = (char_u *)_("E494: Use w or w>>");
2534 goto doend;
2535 }
2536 ea.arg = skipwhite(ea.arg + 1);
2537 ea.append = TRUE;
2538 }
2539 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2540 {
2541 ++ea.arg;
2542 ea.usefilter = TRUE;
2543 }
2544 }
2545
2546 if (ea.cmdidx == CMD_read)
2547 {
2548 if (ea.forceit)
2549 {
2550 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2551 ea.forceit = FALSE;
2552 }
2553 else if (*ea.arg == '!') /* :r !filter */
2554 {
2555 ++ea.arg;
2556 ea.usefilter = TRUE;
2557 }
2558 }
2559
2560 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2561 {
2562 ea.amount = 1;
2563 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2564 {
2565 ++ea.arg;
2566 ++ea.amount;
2567 }
2568 ea.arg = skipwhite(ea.arg);
2569 }
2570
2571 /*
2572 * Check for "+command" argument, before checking for next command.
2573 * Don't do this for ":read !cmd" and ":write !cmd".
2574 */
2575 if ((ea.argt & EDITCMD) && !ea.usefilter)
2576 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2577
2578 /*
2579 * Check for '|' to separate commands and '"' to start comments.
2580 * Don't do this for ":read !cmd" and ":write !cmd".
2581 */
2582 if ((ea.argt & TRLBAR) && !ea.usefilter)
2583 separate_nextcmd(&ea);
2584
2585 /*
2586 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002587 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2588 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002590 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002591 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002592 || ea.cmdidx == CMD_global
2593 || ea.cmdidx == CMD_vglobal
2594 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 {
2596 for (p = ea.arg; *p; ++p)
2597 {
2598 /* Remove one backslash before a newline, so that it's possible to
2599 * pass a newline to the shell and also a newline that is preceded
2600 * with a backslash. This makes it impossible to end a shell
2601 * command in a backslash, but that doesn't appear useful.
2602 * Halving the number of backslashes is incompatible with previous
2603 * versions. */
2604 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002605 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 else if (*p == '\n')
2607 {
2608 ea.nextcmd = p + 1;
2609 *p = NUL;
2610 break;
2611 }
2612 }
2613 }
2614
2615 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2616 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002617 buf_T *buf;
2618
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002620 switch (ea.addr_type)
2621 {
2622 case ADDR_LINES:
2623 ea.line2 = curbuf->b_ml.ml_line_count;
2624 break;
2625 case ADDR_LOADED_BUFFERS:
2626 buf = firstbuf;
2627 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2628 buf = buf->b_next;
2629 ea.line1 = buf->b_fnum;
2630 buf = lastbuf;
2631 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2632 buf = buf->b_prev;
2633 ea.line2 = buf->b_fnum;
2634 break;
2635 case ADDR_BUFFERS:
2636 ea.line1 = firstbuf->b_fnum;
2637 ea.line2 = lastbuf->b_fnum;
2638 break;
2639 case ADDR_WINDOWS:
2640 ea.line2 = LAST_WIN_NR;
2641 break;
2642 case ADDR_TABS:
2643 ea.line2 = LAST_TAB_NR;
2644 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002645 case ADDR_TABS_RELATIVE:
2646 ea.line2 = 1;
2647 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002648 case ADDR_ARGUMENTS:
2649 if (ARGCOUNT == 0)
2650 ea.line1 = ea.line2 = 0;
2651 else
2652 ea.line2 = ARGCOUNT;
2653 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002654#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002655 case ADDR_QUICKFIX:
2656 ea.line2 = qf_get_size(&ea);
2657 if (ea.line2 == 0)
2658 ea.line2 = 1;
2659 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002660#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 }
2663
2664 /* accept numbered register only when no count allowed (:put) */
2665 if ( (ea.argt & REGSTR)
2666 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002667 /* Do not allow register = for user commands */
2668 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2670 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002671#ifndef FEAT_CLIPBOARD
2672 /* check these explicitly for a more specific error message */
2673 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002675 errormsg = (char_u *)_(e_invalidreg);
2676 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
2678#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002679 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2680 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002681 {
2682 ea.regname = *ea.arg++;
2683#ifdef FEAT_EVAL
2684 /* for '=' register: accept the rest of the line as an expression */
2685 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2686 {
2687 set_expr_line(vim_strsave(ea.arg));
2688 ea.arg += STRLEN(ea.arg);
2689 }
2690#endif
2691 ea.arg = skipwhite(ea.arg);
2692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 }
2694
2695 /*
2696 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2697 * count, it's a buffer name.
2698 */
2699 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2700 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002701 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 {
2703 n = getdigits(&ea.arg);
2704 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002705 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 {
2707 errormsg = (char_u *)_(e_zerocount);
2708 goto doend;
2709 }
2710 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2711 {
2712 ea.line2 = n;
2713 if (ea.addr_count == 0)
2714 ea.addr_count = 1;
2715 }
2716 else
2717 {
2718 ea.line1 = ea.line2;
2719 ea.line2 += n - 1;
2720 ++ea.addr_count;
2721 /*
2722 * Be vi compatible: no error message for out of range.
2723 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002724 if (ea.addr_type == ADDR_LINES
2725 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 ea.line2 = curbuf->b_ml.ml_line_count;
2727 }
2728 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002729
2730 /*
2731 * Check for flags: 'l', 'p' and '#'.
2732 */
2733 if (ea.argt & EXFLAGS)
2734 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002736 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002737 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 {
2739 errormsg = (char_u *)_(e_trailing);
2740 goto doend;
2741 }
2742
2743 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2744 {
2745 errormsg = (char_u *)_(e_argreq);
2746 goto doend;
2747 }
2748
2749#ifdef FEAT_EVAL
2750 /*
2751 * Skip the command when it's not going to be executed.
2752 * The commands like :if, :endif, etc. always need to be executed.
2753 * Also make an exception for commands that handle a trailing command
2754 * themselves.
2755 */
2756 if (ea.skip)
2757 {
2758 switch (ea.cmdidx)
2759 {
2760 /* commands that need evaluation */
2761 case CMD_while:
2762 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002763 case CMD_for:
2764 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 case CMD_if:
2766 case CMD_elseif:
2767 case CMD_else:
2768 case CMD_endif:
2769 case CMD_try:
2770 case CMD_catch:
2771 case CMD_finally:
2772 case CMD_endtry:
2773 case CMD_function:
2774 break;
2775
2776 /* Commands that handle '|' themselves. Check: A command should
2777 * either have the TRLBAR flag, appear in this list or appear in
2778 * the list at ":help :bar". */
2779 case CMD_aboveleft:
2780 case CMD_and:
2781 case CMD_belowright:
2782 case CMD_botright:
2783 case CMD_browse:
2784 case CMD_call:
2785 case CMD_confirm:
2786 case CMD_delfunction:
2787 case CMD_djump:
2788 case CMD_dlist:
2789 case CMD_dsearch:
2790 case CMD_dsplit:
2791 case CMD_echo:
2792 case CMD_echoerr:
2793 case CMD_echomsg:
2794 case CMD_echon:
2795 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002796 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 case CMD_help:
2798 case CMD_hide:
2799 case CMD_ijump:
2800 case CMD_ilist:
2801 case CMD_isearch:
2802 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002803 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 case CMD_keepjumps:
2805 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002806 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 case CMD_leftabove:
2808 case CMD_let:
2809 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002810 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002812 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002813 case CMD_noautocmd:
2814 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 case CMD_perl:
2816 case CMD_psearch:
2817 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002818 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002819 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 case CMD_return:
2821 case CMD_rightbelow:
2822 case CMD_ruby:
2823 case CMD_silent:
2824 case CMD_smagic:
2825 case CMD_snomagic:
2826 case CMD_substitute:
2827 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002828 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 case CMD_tcl:
2830 case CMD_throw:
2831 case CMD_tilde:
2832 case CMD_topleft:
2833 case CMD_unlet:
2834 case CMD_verbose:
2835 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002836 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 break;
2838
2839 default: goto doend;
2840 }
2841 }
2842#endif
2843
2844 if (ea.argt & XFILE)
2845 {
2846 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2847 goto doend;
2848 }
2849
2850#ifdef FEAT_LISTCMDS
2851 /*
2852 * Accept buffer name. Cannot be used at the same time with a buffer
2853 * number. Don't do this for a user command.
2854 */
2855 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002856 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
2858 /*
2859 * :bdelete, :bwipeout and :bunload take several arguments, separated
2860 * by spaces: find next space (skipping over escaped characters).
2861 * The others take one argument: ignore trailing spaces.
2862 */
2863 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2864 || ea.cmdidx == CMD_bunload)
2865 p = skiptowhite_esc(ea.arg);
2866 else
2867 {
2868 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002869 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 --p;
2871 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002872 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2873 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 if (ea.line2 < 0) /* failed */
2875 goto doend;
2876 ea.addr_count = 1;
2877 ea.arg = skipwhite(p);
2878 }
2879#endif
2880
2881/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002882 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 *
2884 * The "ea" structure holds the arguments that can be used.
2885 */
2886 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002887 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 ea.cookie = cookie;
2889#ifdef FEAT_EVAL
2890 ea.cstack = cstack;
2891#endif
2892
2893#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002894 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 {
2896 /*
2897 * Execute a user-defined command.
2898 */
2899 do_ucmd(&ea);
2900 }
2901 else
2902#endif
2903 {
2904 /*
2905 * Call the function to execute the command.
2906 */
2907 ea.errmsg = NULL;
2908 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2909 if (ea.errmsg != NULL)
2910 errormsg = (char_u *)_(ea.errmsg);
2911 }
2912
2913#ifdef FEAT_EVAL
2914 /*
2915 * If the command just executed called do_cmdline(), any throw or ":return"
2916 * or ":finish" encountered there must also check the cstack of the still
2917 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2918 * exception, or reanimate a returned function or finished script file and
2919 * return or finish it again.
2920 */
2921 if (need_rethrow)
2922 do_throw(cstack);
2923 else if (check_cstack)
2924 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002925 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002927 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 && current_func_returned())
2929 do_return(&ea, TRUE, FALSE, NULL);
2930 }
2931 need_rethrow = check_cstack = FALSE;
2932#endif
2933
2934doend:
2935 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002936 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002938 curwin->w_cursor.col = 0;
2939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940
2941 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2942 {
2943 if (sourcing)
2944 {
2945 if (errormsg != IObuff)
2946 {
2947 STRCPY(IObuff, errormsg);
2948 errormsg = IObuff;
2949 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002950 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 }
2952 emsg(errormsg);
2953 }
2954#ifdef FEAT_EVAL
2955 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002956 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2957 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958#endif
2959
2960 if (verbose_save >= 0)
2961 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002962#ifdef FEAT_AUTOCMD
2963 if (cmdmod.save_ei != NULL)
2964 {
2965 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002966 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2967 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002968 free_string_option(cmdmod.save_ei);
2969 }
2970#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002971 if (cmdmod.filter_regmatch.regprog != NULL)
2972 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
2974 cmdmod = save_cmdmod;
2975
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002976 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 {
2978 /* messages could be enabled for a serious error, need to check if the
2979 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002980 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002981 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 emsg_silent -= did_esilent;
2983 if (emsg_silent < 0)
2984 emsg_silent = 0;
2985 /* Restore msg_scroll, it's set by file I/O commands, even when no
2986 * message is actually displayed. */
2987 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002988
2989 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2990 * somewhere in the line. Put it back in the first column. */
2991 if (redirecting())
2992 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 }
2994
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002995#ifdef HAVE_SANDBOX
2996 if (did_sandbox)
2997 --sandbox;
2998#endif
2999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3001 ea.nextcmd = NULL;
3002
3003#ifdef FEAT_EVAL
3004 --ex_nesting_level;
3005#endif
3006
3007 return ea.nextcmd;
3008}
3009#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003010 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011#endif
3012
3013/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003014 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 * If there is a match advance "pp" to the argument and return TRUE.
3016 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003017 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003018checkforcmd(
3019 char_u **pp, /* start of command */
3020 char *cmd, /* name of command */
3021 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022{
3023 int i;
3024
3025 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003026 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 break;
3028 if (i >= len && !isalpha((*pp)[i]))
3029 {
3030 *pp = skipwhite(*pp + i);
3031 return TRUE;
3032 }
3033 return FALSE;
3034}
3035
3036/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003037 * Append "cmd" to the error message in IObuff.
3038 * Takes care of limiting the length and handling 0xa0, which would be
3039 * invisible otherwise.
3040 */
3041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003042append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003043{
3044 char_u *s = cmd;
3045 char_u *d;
3046
3047 STRCAT(IObuff, ": ");
3048 d = IObuff + STRLEN(IObuff);
3049 while (*s != NUL && d - IObuff < IOSIZE - 7)
3050 {
3051 if (
3052#ifdef FEAT_MBYTE
3053 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3054#endif
3055 *s == 0xa0)
3056 {
3057 s +=
3058#ifdef FEAT_MBYTE
3059 enc_utf8 ? 2 :
3060#endif
3061 1;
3062 STRCPY(d, "<a0>");
3063 d += 4;
3064 }
3065 else
3066 MB_COPY_CHAR(s, d);
3067 }
3068 *d = NUL;
3069}
3070
3071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003073 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003075 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 * Returns NULL for an ambiguous user command.
3077 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003079find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080{
3081 int len;
3082 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003083 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084
3085 /*
3086 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003087 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 * - the 'k' command can directly be followed by any character.
3089 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003090 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003092 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 */
3094 p = eap->cmd;
3095 if (*p == 'k')
3096 {
3097 eap->cmdidx = CMD_k;
3098 ++p;
3099 }
3100 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003101 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3102 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 || p[1] == 'g'
3104 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3105 || p[1] == 'I'
3106 || (p[1] == 'r' && p[2] != 'e')))
3107 {
3108 eap->cmdidx = CMD_substitute;
3109 ++p;
3110 }
3111 else
3112 {
3113 while (ASCII_ISALPHA(*p))
3114 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003115 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003116 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003117 while (ASCII_ISALNUM(*p))
3118 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003119
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 /* check for non-alpha command */
3121 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3122 ++p;
3123 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003124 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3125 {
3126 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3127 * :delete with the 'l' flag. Same for 'p'. */
3128 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003129 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003130 break;
3131 if (i == len - 1)
3132 {
3133 --len;
3134 if (p[-1] == 'l')
3135 eap->flags |= EXFLAG_LIST;
3136 else
3137 eap->flags |= EXFLAG_PRINT;
3138 }
3139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003141 if (ASCII_ISLOWER(eap->cmd[0]))
3142 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003143 int c1 = eap->cmd[0];
3144 int c2 = eap->cmd[1];
3145
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003146 if (command_count != (int)CMD_SIZE)
3147 {
3148 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3149 getout(1);
3150 }
3151
3152 /* Use a precomputed index for fast look-up in cmdnames[]
3153 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003154 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3155 if (ASCII_ISLOWER(c2))
3156 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003159 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160
3161 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3162 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3163 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3164 (size_t)len) == 0)
3165 {
3166#ifdef FEAT_EVAL
3167 if (full != NULL
3168 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3169 *full = TRUE;
3170#endif
3171 break;
3172 }
3173
3174#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003175 /* Look for a user defined command as a last resort. Let ":Print" be
3176 * overruled by a user defined command. */
3177 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3178 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003180 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 while (ASCII_ISALNUM(*p))
3182 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003183 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 }
3185#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003186 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 eap->cmdidx = CMD_SIZE;
3188 }
3189
3190 return p;
3191}
3192
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003193#ifdef FEAT_USR_CMDS
3194/*
3195 * Search for a user command that matches "eap->cmd".
3196 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3197 * Return a pointer to just after the command.
3198 * Return NULL if there is no matching command.
3199 */
3200 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003201find_ucmd(
3202 exarg_T *eap,
3203 char_u *p, /* end of the command (possibly including count) */
3204 int *full, /* set to TRUE for a full match */
3205 expand_T *xp, /* used for completion, NULL otherwise */
3206 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003207{
3208 int len = (int)(p - eap->cmd);
3209 int j, k, matchlen = 0;
3210 ucmd_T *uc;
3211 int found = FALSE;
3212 int possible = FALSE;
3213 char_u *cp, *np; /* Point into typed cmd and test name */
3214 garray_T *gap;
3215 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3216 only full match global is accepted. */
3217
3218 /*
3219 * Look for buffer-local user commands first, then global ones.
3220 */
3221 gap = &curbuf->b_ucmds;
3222 for (;;)
3223 {
3224 for (j = 0; j < gap->ga_len; ++j)
3225 {
3226 uc = USER_CMD_GA(gap, j);
3227 cp = eap->cmd;
3228 np = uc->uc_name;
3229 k = 0;
3230 while (k < len && *np != NUL && *cp++ == *np++)
3231 k++;
3232 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3233 {
3234 /* If finding a second match, the command is ambiguous. But
3235 * not if a buffer-local command wasn't a full match and a
3236 * global command is a full match. */
3237 if (k == len && found && *np != NUL)
3238 {
3239 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003240 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003241 amb_local = TRUE;
3242 }
3243
3244 if (!found || (k == len && *np == NUL))
3245 {
3246 /* If we matched up to a digit, then there could
3247 * be another command including the digit that we
3248 * should use instead.
3249 */
3250 if (k == len)
3251 found = TRUE;
3252 else
3253 possible = TRUE;
3254
3255 if (gap == &ucmds)
3256 eap->cmdidx = CMD_USER;
3257 else
3258 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003259 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003260 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003261 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003262
3263# ifdef FEAT_CMDL_COMPL
3264 if (compl != NULL)
3265 *compl = uc->uc_compl;
3266# ifdef FEAT_EVAL
3267 if (xp != NULL)
3268 {
3269 xp->xp_arg = uc->uc_compl_arg;
3270 xp->xp_scriptID = uc->uc_scriptID;
3271 }
3272# endif
3273# endif
3274 /* Do not search for further abbreviations
3275 * if this is an exact match. */
3276 matchlen = k;
3277 if (k == len && *np == NUL)
3278 {
3279 if (full != NULL)
3280 *full = TRUE;
3281 amb_local = FALSE;
3282 break;
3283 }
3284 }
3285 }
3286 }
3287
3288 /* Stop if we found a full match or searched all. */
3289 if (j < gap->ga_len || gap == &ucmds)
3290 break;
3291 gap = &ucmds;
3292 }
3293
3294 /* Only found ambiguous matches. */
3295 if (amb_local)
3296 {
3297 if (xp != NULL)
3298 xp->xp_context = EXPAND_UNSUCCESSFUL;
3299 return NULL;
3300 }
3301
3302 /* The match we found may be followed immediately by a number. Move "p"
3303 * back to point to it. */
3304 if (found || possible)
3305 return p + (matchlen - len);
3306 return p;
3307}
3308#endif
3309
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003311static struct cmdmod
3312{
3313 char *name;
3314 int minlen;
3315 int has_count; /* :123verbose :3tab */
3316} cmdmods[] = {
3317 {"aboveleft", 3, FALSE},
3318 {"belowright", 3, FALSE},
3319 {"botright", 2, FALSE},
3320 {"browse", 3, FALSE},
3321 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003322 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003323 {"hide", 3, FALSE},
3324 {"keepalt", 5, FALSE},
3325 {"keepjumps", 5, FALSE},
3326 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003327 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003328 {"leftabove", 5, FALSE},
3329 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003330 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003331 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003332 {"rightbelow", 6, FALSE},
3333 {"sandbox", 3, FALSE},
3334 {"silent", 3, FALSE},
3335 {"tab", 3, TRUE},
3336 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003337 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003338 {"verbose", 4, TRUE},
3339 {"vertical", 4, FALSE},
3340};
3341
3342/*
3343 * Return length of a command modifier (including optional count).
3344 * Return zero when it's not a modifier.
3345 */
3346 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003347modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003348{
3349 int i, j;
3350 char_u *p = cmd;
3351
3352 if (VIM_ISDIGIT(*cmd))
3353 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003354 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003355 {
3356 for (j = 0; p[j] != NUL; ++j)
3357 if (p[j] != cmdmods[i].name[j])
3358 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003359 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003360 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003361 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003362 }
3363 return 0;
3364}
3365
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366/*
3367 * Return > 0 if an Ex command "name" exists.
3368 * Return 2 if there is an exact match.
3369 * Return 3 if there is an ambiguous match.
3370 */
3371 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003372cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 exarg_T ea;
3375 int full = FALSE;
3376 int i;
3377 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003378 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379
3380 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003381 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
3383 for (j = 0; name[j] != NUL; ++j)
3384 if (name[j] != cmdmods[i].name[j])
3385 break;
3386 if (name[j] == NUL && j >= cmdmods[i].minlen)
3387 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3388 }
3389
Bram Moolenaara9587612006-05-04 21:47:50 +00003390 /* Check built-in commands and user defined commands.
3391 * For ":2match" and ":3match" we need to skip the number. */
3392 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003394 p = find_command(&ea, &full);
3395 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003397 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3398 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003399 if (*skipwhite(p) != NUL)
3400 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3402}
3403#endif
3404
3405/*
3406 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3407 * we don't need/want deleted. Maybe this could be done better if we didn't
3408 * repeat all this stuff. The only problem is that they may not stay
3409 * perfectly compatible with each other, but then the command line syntax
3410 * probably won't change that much -- webb.
3411 */
3412 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003413set_one_cmd_context(
3414 expand_T *xp,
3415 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416{
3417 char_u *p;
3418 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003419 int len = 0;
3420 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3422 int compl = EXPAND_NOTHING;
3423#endif
3424#ifdef FEAT_CMDL_COMPL
3425 int delim;
3426#endif
3427 int forceit = FALSE;
3428 int usefilter = FALSE; /* filter instead of file name */
3429
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003430 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 xp->xp_pattern = buff;
3432 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003433 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003436 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 */
3438 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3439 ;
3440 xp->xp_pattern = cmd;
3441
3442 if (*cmd == NUL)
3443 return NULL;
3444 if (*cmd == '"') /* ignore comment lines */
3445 {
3446 xp->xp_context = EXPAND_NOTHING;
3447 return NULL;
3448 }
3449
3450/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003451 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 */
3453 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 xp->xp_pattern = cmd;
3455 if (*cmd == NUL)
3456 return NULL;
3457 if (*cmd == '"')
3458 {
3459 xp->xp_context = EXPAND_NOTHING;
3460 return NULL;
3461 }
3462
3463 if (*cmd == '|' || *cmd == '\n')
3464 return cmd + 1; /* There's another command */
3465
3466 /*
3467 * Isolate the command and search for it in the command table.
3468 * Exceptions:
3469 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003470 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3472 */
3473 if (*cmd == 'k' && cmd[1] != 'e')
3474 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003475 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 p = cmd + 1;
3477 }
3478 else
3479 {
3480 p = cmd;
3481 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3482 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003483 /* a user command may contain digits */
3484 if (ASCII_ISUPPER(cmd[0]))
3485 while (ASCII_ISALNUM(*p) || *p == '*')
3486 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003487 /* for python 3.x: ":py3*" commands completion */
3488 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003489 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003490 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003491 while (ASCII_ISALPHA(*p) || *p == '*')
3492 ++p;
3493 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003494 /* check for non-alpha command */
3495 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3496 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003497 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003499 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 {
3501 xp->xp_context = EXPAND_UNSUCCESSFUL;
3502 return NULL;
3503 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003504 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003505 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3506 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3507 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 break;
3509
3510#ifdef FEAT_USR_CMDS
3511 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3513 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514#endif
3515 }
3516
3517 /*
3518 * If the cursor is touching the command, and it ends in an alpha-numeric
3519 * character, complete the command name.
3520 */
3521 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3522 return NULL;
3523
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003524 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
3526 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3527 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003528 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 p = cmd + 1;
3530 }
3531#ifdef FEAT_USR_CMDS
3532 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3533 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003534 ea.cmd = cmd;
3535 p = find_ucmd(&ea, p, NULL, xp,
3536# if defined(FEAT_CMDL_COMPL)
3537 &compl
3538# else
3539 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003541 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003542 if (p == NULL)
3543 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
3545#endif
3546 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003547 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 {
3549 /* Not still touching the command and it was an illegal one */
3550 xp->xp_context = EXPAND_UNSUCCESSFUL;
3551 return NULL;
3552 }
3553
3554 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3555
3556 if (*p == '!') /* forced commands */
3557 {
3558 forceit = TRUE;
3559 ++p;
3560 }
3561
3562/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003563 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003565 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003566 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567
3568 arg = skipwhite(p);
3569
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003570 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
3572 if (*arg == '>') /* append */
3573 {
3574 if (*++arg == '>')
3575 ++arg;
3576 arg = skipwhite(arg);
3577 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003578 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
3580 ++arg;
3581 usefilter = TRUE;
3582 }
3583 }
3584
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003585 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
3587 usefilter = forceit; /* :r! filter if forced */
3588 if (*arg == '!') /* :r !filter */
3589 {
3590 ++arg;
3591 usefilter = TRUE;
3592 }
3593 }
3594
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003595 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 {
3597 while (*arg == *cmd) /* allow any number of '>' or '<' */
3598 ++arg;
3599 arg = skipwhite(arg);
3600 }
3601
3602 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003603 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
3605 /* Check if we're in the +command */
3606 p = arg + 1;
3607 arg = skip_cmd_arg(arg, FALSE);
3608
3609 /* Still touching the command after '+'? */
3610 if (*arg == NUL)
3611 return p;
3612
3613 /* Skip space(s) after +command to get to the real argument */
3614 arg = skipwhite(arg);
3615 }
3616
3617 /*
3618 * Check for '|' to separate commands and '"' to start comments.
3619 * Don't do this for ":read !cmd" and ":write !cmd".
3620 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003621 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
3623 p = arg;
3624 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003625 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 p += 2;
3627 while (*p)
3628 {
3629 if (*p == Ctrl_V)
3630 {
3631 if (p[1] != NUL)
3632 ++p;
3633 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003634 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 || *p == '|' || *p == '\n')
3636 {
3637 if (*(p - 1) != '\\')
3638 {
3639 if (*p == '|' || *p == '\n')
3640 return p + 1;
3641 return NULL; /* It's a comment */
3642 }
3643 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003644 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
3646 }
3647
3648 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003649 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 vim_strchr((char_u *)"|\"", *arg) == NULL)
3651 return NULL;
3652
3653 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003654 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003656 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003657 while (*p && p < buff + len)
3658 {
3659 if (*p == ' ' || *p == TAB)
3660 {
3661 /* argument starts after a space */
3662 xp->xp_pattern = ++p;
3663 }
3664 else
3665 {
3666 if (*p == '\\' && *(p + 1) != NUL)
3667 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003668 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003669 }
3670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003672 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003674 int c;
3675 int in_quote = FALSE;
3676 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677
3678 /*
3679 * Allow spaces within back-quotes to count as part of the argument
3680 * being expanded.
3681 */
3682 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003683 p = xp->xp_pattern;
3684 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003686#ifdef FEAT_MBYTE
3687 if (has_mbyte)
3688 c = mb_ptr2char(p);
3689 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003691 c = *p;
3692 if (c == '\\' && p[1] != NUL)
3693 ++p;
3694 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 {
3696 if (!in_quote)
3697 {
3698 xp->xp_pattern = p;
3699 bow = p + 1;
3700 }
3701 in_quote = !in_quote;
3702 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003703 /* An argument can contain just about everything, except
3704 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003705 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003706#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003707 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003708#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003709 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003710 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003711 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003712 while (*p != NUL)
3713 {
3714#ifdef FEAT_MBYTE
3715 if (has_mbyte)
3716 c = mb_ptr2char(p);
3717 else
3718#endif
3719 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003720 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003721 break;
3722#ifdef FEAT_MBYTE
3723 if (has_mbyte)
3724 len = (*mb_ptr2len)(p);
3725 else
3726#endif
3727 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003728 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003729 }
3730 if (in_quote)
3731 bow = p;
3732 else
3733 xp->xp_pattern = p;
3734 p -= len;
3735 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003736 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 }
3738
3739 /*
3740 * If we are still inside the quotes, and we passed a space, just
3741 * expand from there.
3742 */
3743 if (bow != NULL && in_quote)
3744 xp->xp_pattern = bow;
3745 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003746
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003747 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003748 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003749 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003750#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003751 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003752#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003753 /* When still after the command name expand executables. */
3754 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003755 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757
3758 /* Check for environment variable */
3759 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003760#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 || *xp->xp_pattern == '%'
3762#endif
3763 )
3764 {
3765 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3766 if (!vim_isIDc(*p))
3767 break;
3768 if (*p == NUL)
3769 {
3770 xp->xp_context = EXPAND_ENV_VARS;
3771 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003772#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3773 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003774 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003775 compl = EXPAND_ENV_VARS;
3776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003779#if defined(FEAT_CMDL_COMPL)
3780 /* Check for user names */
3781 if (*xp->xp_pattern == '~')
3782 {
3783 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3784 ;
3785 /* Complete ~user only if it partially matches a user name.
3786 * A full match ~user<Tab> will be replaced by user's home
3787 * directory i.e. something like ~user<Tab> -> /home/user/ */
3788 if (*p == NUL && p > xp->xp_pattern + 1
3789 && match_user(xp->xp_pattern + 1) == 1)
3790 {
3791 xp->xp_context = EXPAND_USER;
3792 ++xp->xp_pattern;
3793 }
3794 }
3795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 }
3797
3798/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003799 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003801 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003803 case CMD_find:
3804 case CMD_sfind:
3805 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003806 if (xp->xp_context == EXPAND_FILES)
3807 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003808 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 case CMD_cd:
3810 case CMD_chdir:
3811 case CMD_lcd:
3812 case CMD_lchdir:
3813 if (xp->xp_context == EXPAND_FILES)
3814 xp->xp_context = EXPAND_DIRECTORIES;
3815 break;
3816 case CMD_help:
3817 xp->xp_context = EXPAND_HELP;
3818 xp->xp_pattern = arg;
3819 break;
3820
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003821 /* Command modifiers: return the argument.
3822 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003824 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 case CMD_belowright:
3826 case CMD_botright:
3827 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003828 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003829 case CMD_cdo:
3830 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003832 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 case CMD_folddoclosed:
3834 case CMD_folddoopen:
3835 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003836 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 case CMD_keepjumps:
3838 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003839 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003840 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003842 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003844 case CMD_noautocmd:
3845 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003847 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003849 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003850 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 case CMD_topleft:
3852 case CMD_verbose:
3853 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003854 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 return arg;
3856
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003857 case CMD_filter:
3858 if (*arg != NUL)
3859 arg = skip_vimgrep_pat(arg, NULL, NULL);
3860 if (arg == NULL || *arg == NUL)
3861 {
3862 xp->xp_context = EXPAND_NOTHING;
3863 return NULL;
3864 }
3865 return skipwhite(arg);
3866
Bram Moolenaar4f688582007-07-24 12:34:30 +00003867#ifdef FEAT_CMDL_COMPL
3868# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 case CMD_match:
3870 if (*arg == NUL || !ends_excmd(*arg))
3871 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003872 /* also complete "None" */
3873 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 arg = skipwhite(skiptowhite(arg));
3875 if (*arg != NUL)
3876 {
3877 xp->xp_context = EXPAND_NOTHING;
3878 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3879 }
3880 }
3881 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003882# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884/*
3885 * All completion for the +cmdline_compl feature goes here.
3886 */
3887
3888# ifdef FEAT_USR_CMDS
3889 case CMD_command:
3890 /* Check for attributes */
3891 while (*arg == '-')
3892 {
3893 arg++; /* Skip "-" */
3894 p = skiptowhite(arg);
3895 if (*p == NUL)
3896 {
3897 /* Cursor is still in the attribute */
3898 p = vim_strchr(arg, '=');
3899 if (p == NULL)
3900 {
3901 /* No "=", so complete attribute names */
3902 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3903 xp->xp_pattern = arg;
3904 return NULL;
3905 }
3906
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003907 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 * their arguments as well.
3909 */
3910 if (STRNICMP(arg, "complete", p - arg) == 0)
3911 {
3912 xp->xp_context = EXPAND_USER_COMPLETE;
3913 xp->xp_pattern = p + 1;
3914 return NULL;
3915 }
3916 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3917 {
3918 xp->xp_context = EXPAND_USER_NARGS;
3919 xp->xp_pattern = p + 1;
3920 return NULL;
3921 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003922 else if (STRNICMP(arg, "addr", p - arg) == 0)
3923 {
3924 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3925 xp->xp_pattern = p + 1;
3926 return NULL;
3927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 return NULL;
3929 }
3930 arg = skipwhite(p);
3931 }
3932
3933 /* After the attributes comes the new command name */
3934 p = skiptowhite(arg);
3935 if (*p == NUL)
3936 {
3937 xp->xp_context = EXPAND_USER_COMMANDS;
3938 xp->xp_pattern = arg;
3939 break;
3940 }
3941
3942 /* And finally comes a normal command */
3943 return skipwhite(p);
3944
3945 case CMD_delcommand:
3946 xp->xp_context = EXPAND_USER_COMMANDS;
3947 xp->xp_pattern = arg;
3948 break;
3949# endif
3950
3951 case CMD_global:
3952 case CMD_vglobal:
3953 delim = *arg; /* get the delimiter */
3954 if (delim)
3955 ++arg; /* skip delimiter if there is one */
3956
3957 while (arg[0] != NUL && arg[0] != delim)
3958 {
3959 if (arg[0] == '\\' && arg[1] != NUL)
3960 ++arg;
3961 ++arg;
3962 }
3963 if (arg[0] != NUL)
3964 return arg + 1;
3965 break;
3966 case CMD_and:
3967 case CMD_substitute:
3968 delim = *arg;
3969 if (delim)
3970 {
3971 /* skip "from" part */
3972 ++arg;
3973 arg = skip_regexp(arg, delim, p_magic, NULL);
3974 }
3975 /* skip "to" part */
3976 while (arg[0] != NUL && arg[0] != delim)
3977 {
3978 if (arg[0] == '\\' && arg[1] != NUL)
3979 ++arg;
3980 ++arg;
3981 }
3982 if (arg[0] != NUL) /* skip delimiter */
3983 ++arg;
3984 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3985 ++arg;
3986 if (arg[0] != NUL)
3987 return arg;
3988 break;
3989 case CMD_isearch:
3990 case CMD_dsearch:
3991 case CMD_ilist:
3992 case CMD_dlist:
3993 case CMD_ijump:
3994 case CMD_psearch:
3995 case CMD_djump:
3996 case CMD_isplit:
3997 case CMD_dsplit:
3998 arg = skipwhite(skipdigits(arg)); /* skip count */
3999 if (*arg == '/') /* Match regexp, not just whole words */
4000 {
4001 for (++arg; *arg && *arg != '/'; arg++)
4002 if (*arg == '\\' && arg[1] != NUL)
4003 arg++;
4004 if (*arg)
4005 {
4006 arg = skipwhite(arg + 1);
4007
4008 /* Check for trailing illegal characters */
4009 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4010 xp->xp_context = EXPAND_NOTHING;
4011 else
4012 return arg;
4013 }
4014 }
4015 break;
4016#ifdef FEAT_AUTOCMD
4017 case CMD_autocmd:
4018 return set_context_in_autocmd(xp, arg, FALSE);
4019
4020 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004021 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 return set_context_in_autocmd(xp, arg, TRUE);
4023#endif
4024 case CMD_set:
4025 set_context_in_set_cmd(xp, arg, 0);
4026 break;
4027 case CMD_setglobal:
4028 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4029 break;
4030 case CMD_setlocal:
4031 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4032 break;
4033 case CMD_tag:
4034 case CMD_stag:
4035 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004036 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 case CMD_tselect:
4038 case CMD_stselect:
4039 case CMD_ptselect:
4040 case CMD_tjump:
4041 case CMD_stjump:
4042 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004043 if (*p_wop != NUL)
4044 xp->xp_context = EXPAND_TAGS_LISTFILES;
4045 else
4046 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 xp->xp_pattern = arg;
4048 break;
4049 case CMD_augroup:
4050 xp->xp_context = EXPAND_AUGROUP;
4051 xp->xp_pattern = arg;
4052 break;
4053#ifdef FEAT_SYN_HL
4054 case CMD_syntax:
4055 set_context_in_syntax_cmd(xp, arg);
4056 break;
4057#endif
4058#ifdef FEAT_EVAL
4059 case CMD_let:
4060 case CMD_if:
4061 case CMD_elseif:
4062 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004063 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 case CMD_echo:
4065 case CMD_echon:
4066 case CMD_execute:
4067 case CMD_echomsg:
4068 case CMD_echoerr:
4069 case CMD_call:
4070 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004071 case CMD_cexpr:
4072 case CMD_caddexpr:
4073 case CMD_cgetexpr:
4074 case CMD_lexpr:
4075 case CMD_laddexpr:
4076 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004077 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 break;
4079
4080 case CMD_unlet:
4081 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4082 arg = xp->xp_pattern + 1;
4083 xp->xp_context = EXPAND_USER_VARS;
4084 xp->xp_pattern = arg;
4085 break;
4086
4087 case CMD_function:
4088 case CMD_delfunction:
4089 xp->xp_context = EXPAND_USER_FUNC;
4090 xp->xp_pattern = arg;
4091 break;
4092
4093 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004094 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 break;
4096#endif
4097 case CMD_highlight:
4098 set_context_in_highlight_cmd(xp, arg);
4099 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004100#ifdef FEAT_CSCOPE
4101 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004102 case CMD_lcscope:
4103 case CMD_scscope:
4104 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004105 break;
4106#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004107#ifdef FEAT_SIGNS
4108 case CMD_sign:
4109 set_context_in_sign_cmd(xp, arg);
4110 break;
4111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112#ifdef FEAT_LISTCMDS
4113 case CMD_bdelete:
4114 case CMD_bwipeout:
4115 case CMD_bunload:
4116 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4117 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004118 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 case CMD_buffer:
4120 case CMD_sbuffer:
4121 case CMD_checktime:
4122 xp->xp_context = EXPAND_BUFFERS;
4123 xp->xp_pattern = arg;
4124 break;
4125#endif
4126#ifdef FEAT_USR_CMDS
4127 case CMD_USER:
4128 case CMD_USER_BUF:
4129 if (compl != EXPAND_NOTHING)
4130 {
4131 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004132 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 {
4134# ifdef FEAT_MENU
4135 if (compl == EXPAND_MENUS)
4136 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4137# endif
4138 if (compl == EXPAND_COMMANDS)
4139 return arg;
4140 if (compl == EXPAND_MAPPINGS)
4141 return set_context_in_map_cmd(xp, (char_u *)"map",
4142 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004143 /* Find start of last argument. */
4144 p = arg;
4145 while (*p)
4146 {
4147 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004148 /* argument starts after a space */
4149 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004150 else if (*p == '\\' && *(p + 1) != NUL)
4151 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004152 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 xp->xp_pattern = arg;
4155 }
4156 xp->xp_context = compl;
4157 }
4158 break;
4159#endif
4160 case CMD_map: case CMD_noremap:
4161 case CMD_nmap: case CMD_nnoremap:
4162 case CMD_vmap: case CMD_vnoremap:
4163 case CMD_omap: case CMD_onoremap:
4164 case CMD_imap: case CMD_inoremap:
4165 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004166 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004167 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004168 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004169 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004171 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 case CMD_unmap:
4173 case CMD_nunmap:
4174 case CMD_vunmap:
4175 case CMD_ounmap:
4176 case CMD_iunmap:
4177 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004178 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004179 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004180 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004181 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004183 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004184 case CMD_mapclear:
4185 case CMD_nmapclear:
4186 case CMD_vmapclear:
4187 case CMD_omapclear:
4188 case CMD_imapclear:
4189 case CMD_cmapclear:
4190 case CMD_lmapclear:
4191 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004192 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004193 case CMD_xmapclear:
4194 xp->xp_context = EXPAND_MAPCLEAR;
4195 xp->xp_pattern = arg;
4196 break;
4197
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 case CMD_abbreviate: case CMD_noreabbrev:
4199 case CMD_cabbrev: case CMD_cnoreabbrev:
4200 case CMD_iabbrev: case CMD_inoreabbrev:
4201 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004202 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 case CMD_unabbreviate:
4204 case CMD_cunabbrev:
4205 case CMD_iunabbrev:
4206 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004207 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208#ifdef FEAT_MENU
4209 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4210 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4211 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4212 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4213 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4214 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4215 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4216 case CMD_tmenu: case CMD_tunmenu:
4217 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4218 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4219#endif
4220
4221 case CMD_colorscheme:
4222 xp->xp_context = EXPAND_COLORS;
4223 xp->xp_pattern = arg;
4224 break;
4225
4226 case CMD_compiler:
4227 xp->xp_context = EXPAND_COMPILER;
4228 xp->xp_pattern = arg;
4229 break;
4230
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004231 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004232 xp->xp_context = EXPAND_OWNSYNTAX;
4233 xp->xp_pattern = arg;
4234 break;
4235
4236 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004237 xp->xp_context = EXPAND_FILETYPE;
4238 xp->xp_pattern = arg;
4239 break;
4240
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004241 case CMD_packadd:
4242 xp->xp_context = EXPAND_PACKADD;
4243 xp->xp_pattern = arg;
4244 break;
4245
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4247 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4248 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004249 p = skiptowhite(arg);
4250 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 {
4252 xp->xp_context = EXPAND_LANGUAGE;
4253 xp->xp_pattern = arg;
4254 }
4255 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004256 {
4257 if ( STRNCMP(arg, "messages", p - arg) == 0
4258 || STRNCMP(arg, "ctype", p - arg) == 0
4259 || STRNCMP(arg, "time", p - arg) == 0)
4260 {
4261 xp->xp_context = EXPAND_LOCALES;
4262 xp->xp_pattern = skipwhite(p);
4263 }
4264 else
4265 xp->xp_context = EXPAND_NOTHING;
4266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 break;
4268#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004269#if defined(FEAT_PROFILE)
4270 case CMD_profile:
4271 set_context_in_profile_cmd(xp, arg);
4272 break;
4273#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004274 case CMD_behave:
4275 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004276 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004277 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004279 case CMD_messages:
4280 xp->xp_context = EXPAND_MESSAGES;
4281 xp->xp_pattern = arg;
4282 break;
4283
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004284#if defined(FEAT_CMDHIST)
4285 case CMD_history:
4286 xp->xp_context = EXPAND_HISTORY;
4287 xp->xp_pattern = arg;
4288 break;
4289#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004290#if defined(FEAT_PROFILE)
4291 case CMD_syntime:
4292 xp->xp_context = EXPAND_SYNTIME;
4293 xp->xp_pattern = arg;
4294 break;
4295#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004296
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297#endif /* FEAT_CMDL_COMPL */
4298
4299 default:
4300 break;
4301 }
4302 return NULL;
4303}
4304
4305/*
4306 * skip a range specifier of the form: addr [,addr] [;addr] ..
4307 *
4308 * Backslashed delimiters after / or ? will be skipped, and commands will
4309 * not be expanded between /'s and ?'s or after "'".
4310 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004311 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 * Returns the "cmd" pointer advanced to beyond the range.
4313 */
4314 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004315skip_range(
4316 char_u *cmd,
4317 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004319 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004321 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004323 if (*cmd == '\\')
4324 {
4325 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4326 ++cmd;
4327 else
4328 break;
4329 }
4330 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 {
4332 if (*++cmd == NUL && ctx != NULL)
4333 *ctx = EXPAND_NOTHING;
4334 }
4335 else if (*cmd == '/' || *cmd == '?')
4336 {
4337 delim = *cmd++;
4338 while (*cmd != NUL && *cmd != delim)
4339 if (*cmd++ == '\\' && *cmd != NUL)
4340 ++cmd;
4341 if (*cmd == NUL && ctx != NULL)
4342 *ctx = EXPAND_NOTHING;
4343 }
4344 if (*cmd != NUL)
4345 ++cmd;
4346 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004347
4348 /* Skip ":" and white space. */
4349 while (*cmd == ':')
4350 cmd = skipwhite(cmd + 1);
4351
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 return cmd;
4353}
4354
4355/*
4356 * get a single EX address
4357 *
4358 * Set ptr to the next character after the part that was interpreted.
4359 * Set ptr to NULL when an error is encountered.
4360 *
4361 * Return MAXLNUM when no Ex address was found.
4362 */
4363 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004364get_address(
4365 exarg_T *eap UNUSED,
4366 char_u **ptr,
4367 int addr_type, /* flag: one of ADDR_LINES, ... */
4368 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004369 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004370 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371{
4372 int c;
4373 int i;
4374 long n;
4375 char_u *cmd;
4376 pos_T pos;
4377 pos_T *fp;
4378 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004379 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380
4381 cmd = skipwhite(*ptr);
4382 lnum = MAXLNUM;
4383 do
4384 {
4385 switch (*cmd)
4386 {
4387 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004388 ++cmd;
4389 switch (addr_type)
4390 {
4391 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 lnum = curwin->w_cursor.lnum;
4393 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004394 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004395 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004396 break;
4397 case ADDR_ARGUMENTS:
4398 lnum = curwin->w_arg_idx + 1;
4399 break;
4400 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004401 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004402 lnum = curbuf->b_fnum;
4403 break;
4404 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004405 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004406 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004407 case ADDR_TABS_RELATIVE:
4408 EMSG(_(e_invrange));
4409 cmd = NULL;
4410 goto error;
4411 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004412#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004413 case ADDR_QUICKFIX:
4414 lnum = qf_get_cur_valid_idx(eap);
4415 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004416#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004417 }
4418 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419
4420 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004421 ++cmd;
4422 switch (addr_type)
4423 {
4424 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 lnum = curbuf->b_ml.ml_line_count;
4426 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004427 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004428 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 break;
4430 case ADDR_ARGUMENTS:
4431 lnum = ARGCOUNT;
4432 break;
4433 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004434 buf = lastbuf;
4435 while (buf->b_ml.ml_mfp == NULL)
4436 {
4437 if (buf->b_prev == NULL)
4438 break;
4439 buf = buf->b_prev;
4440 }
4441 lnum = buf->b_fnum;
4442 break;
4443 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004444 lnum = lastbuf->b_fnum;
4445 break;
4446 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004447 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004448 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004449 case ADDR_TABS_RELATIVE:
4450 EMSG(_(e_invrange));
4451 cmd = NULL;
4452 goto error;
4453 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004454#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004455 case ADDR_QUICKFIX:
4456 lnum = qf_get_size(eap);
4457 if (lnum == 0)
4458 lnum = 1;
4459 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004460#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004461 }
4462 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
4464 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004465 if (*++cmd == NUL)
4466 {
4467 cmd = NULL;
4468 goto error;
4469 }
4470 if (addr_type != ADDR_LINES)
4471 {
4472 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004473 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004474 goto error;
4475 }
4476 if (skip)
4477 ++cmd;
4478 else
4479 {
4480 /* Only accept a mark in another file when it is
4481 * used by itself: ":'M". */
4482 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4483 ++cmd;
4484 if (fp == (pos_T *)-1)
4485 /* Jumped to another file. */
4486 lnum = curwin->w_cursor.lnum;
4487 else
4488 {
4489 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
4491 cmd = NULL;
4492 goto error;
4493 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004494 lnum = fp->lnum;
4495 }
4496 }
4497 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
4499 case '/':
4500 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004501 c = *cmd++;
4502 if (addr_type != ADDR_LINES)
4503 {
4504 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004505 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004506 goto error;
4507 }
4508 if (skip) /* skip "/pat/" */
4509 {
4510 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4511 if (*cmd == c)
4512 ++cmd;
4513 }
4514 else
4515 {
4516 pos = curwin->w_cursor; /* save curwin->w_cursor */
4517 /*
4518 * When '/' or '?' follows another address, start
4519 * from there.
4520 */
4521 if (lnum != MAXLNUM)
4522 curwin->w_cursor.lnum = lnum;
4523 /*
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004524 * Start a forward search at the end of the line (unless
4525 * before the first line).
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004526 * Start a backward search at the start of the line.
4527 * This makes sure we never match in the current
4528 * line, and can match anywhere in the
4529 * next/previous line.
4530 */
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004531 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004532 curwin->w_cursor.col = MAXCOL;
4533 else
4534 curwin->w_cursor.col = 0;
4535 searchcmdlen = 0;
4536 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004537 SEARCH_HIS | SEARCH_MSG, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004538 {
4539 curwin->w_cursor = pos;
4540 cmd = NULL;
4541 goto error;
4542 }
4543 lnum = curwin->w_cursor.lnum;
4544 curwin->w_cursor = pos;
4545 /* adjust command string pointer */
4546 cmd += searchcmdlen;
4547 }
4548 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
4550 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004551 ++cmd;
4552 if (addr_type != ADDR_LINES)
4553 {
4554 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004555 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004556 goto error;
4557 }
4558 if (*cmd == '&')
4559 i = RE_SUBST;
4560 else if (*cmd == '?' || *cmd == '/')
4561 i = RE_SEARCH;
4562 else
4563 {
4564 EMSG(_(e_backslash));
4565 cmd = NULL;
4566 goto error;
4567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004569 if (!skip)
4570 {
4571 /*
4572 * When search follows another address, start from
4573 * there.
4574 */
4575 if (lnum != MAXLNUM)
4576 pos.lnum = lnum;
4577 else
4578 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004580 /*
4581 * Start the search just like for the above
4582 * do_search().
4583 */
4584 if (*cmd != '?')
4585 pos.col = MAXCOL;
4586 else
4587 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004588#ifdef FEAT_VIRTUALEDIT
4589 pos.coladd = 0;
4590#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004591 if (searchit(curwin, curbuf, &pos,
4592 *cmd == '?' ? BACKWARD : FORWARD,
4593 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004594 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004595 lnum = pos.lnum;
4596 else
4597 {
4598 cmd = NULL;
4599 goto error;
4600 }
4601 }
4602 ++cmd;
4603 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604
4605 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004606 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4607 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
4609
4610 for (;;)
4611 {
4612 cmd = skipwhite(cmd);
4613 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4614 break;
4615
4616 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004617 {
4618 switch (addr_type)
4619 {
4620 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004621 /* "+1" is same as ".+1" */
4622 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004623 break;
4624 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004625 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004626 break;
4627 case ADDR_ARGUMENTS:
4628 lnum = curwin->w_arg_idx + 1;
4629 break;
4630 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004631 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004632 lnum = curbuf->b_fnum;
4633 break;
4634 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004635 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004636 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004637 case ADDR_TABS_RELATIVE:
4638 lnum = 1;
4639 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004640#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004641 case ADDR_QUICKFIX:
4642 lnum = qf_get_cur_valid_idx(eap);
4643 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004644#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004645 }
4646 }
4647
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 if (VIM_ISDIGIT(*cmd))
4649 i = '+'; /* "number" is same as "+number" */
4650 else
4651 i = *cmd++;
4652 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4653 n = 1;
4654 else
4655 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004656
4657 if (addr_type == ADDR_TABS_RELATIVE)
4658 {
4659 EMSG(_(e_invrange));
4660 cmd = NULL;
4661 goto error;
4662 }
4663 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004664 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004665 lnum = compute_buffer_local_count(
4666 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004668 {
4669#ifdef FEAT_FOLDING
4670 /* Relative line addressing, need to adjust for folded lines
4671 * now, but only do it after the first address. */
4672 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4673 && address_count >= 2)
4674 (void)hasFolding(lnum, NULL, &lnum);
4675#endif
4676 if (i == '-')
4677 lnum -= n;
4678 else
4679 lnum += n;
4680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 }
4682 } while (*cmd == '/' || *cmd == '?');
4683
4684error:
4685 *ptr = cmd;
4686 return lnum;
4687}
4688
4689/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004690 * Get flags from an Ex command argument.
4691 */
4692 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004693get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004694{
4695 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4696 {
4697 if (*eap->arg == 'l')
4698 eap->flags |= EXFLAG_LIST;
4699 else if (*eap->arg == 'p')
4700 eap->flags |= EXFLAG_PRINT;
4701 else
4702 eap->flags |= EXFLAG_NR;
4703 eap->arg = skipwhite(eap->arg + 1);
4704 }
4705}
4706
4707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 * Function called for command which is Not Implemented. NI!
4709 */
4710 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004711ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712{
4713 if (!eap->skip)
4714 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4715}
4716
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004717#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718/*
4719 * Function called for script command which is Not Implemented. NI!
4720 * Skips over ":perl <<EOF" constructs.
4721 */
4722 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004723ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724{
4725 if (!eap->skip)
4726 ex_ni(eap);
4727 else
4728 vim_free(script_get(eap, eap->arg));
4729}
4730#endif
4731
4732/*
4733 * Check range in Ex command for validity.
4734 * Return NULL when valid, error message when invalid.
4735 */
4736 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004737invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004739 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 if ( eap->line1 < 0
4741 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004742 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004744
4745 if (eap->argt & RANGE)
4746 {
4747 switch(eap->addr_type)
4748 {
4749 case ADDR_LINES:
4750 if (!(eap->argt & NOTADR)
4751 && eap->line2 > curbuf->b_ml.ml_line_count
4752#ifdef FEAT_DIFF
4753 + (eap->cmdidx == CMD_diffget)
4754#endif
4755 )
4756 return (char_u *)_(e_invrange);
4757 break;
4758 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004759 /* add 1 if ARGCOUNT is 0 */
4760 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004761 return (char_u *)_(e_invrange);
4762 break;
4763 case ADDR_BUFFERS:
4764 if (eap->line1 < firstbuf->b_fnum
4765 || eap->line2 > lastbuf->b_fnum)
4766 return (char_u *)_(e_invrange);
4767 break;
4768 case ADDR_LOADED_BUFFERS:
4769 buf = firstbuf;
4770 while (buf->b_ml.ml_mfp == NULL)
4771 {
4772 if (buf->b_next == NULL)
4773 return (char_u *)_(e_invrange);
4774 buf = buf->b_next;
4775 }
4776 if (eap->line1 < buf->b_fnum)
4777 return (char_u *)_(e_invrange);
4778 buf = lastbuf;
4779 while (buf->b_ml.ml_mfp == NULL)
4780 {
4781 if (buf->b_prev == NULL)
4782 return (char_u *)_(e_invrange);
4783 buf = buf->b_prev;
4784 }
4785 if (eap->line2 > buf->b_fnum)
4786 return (char_u *)_(e_invrange);
4787 break;
4788 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004789 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004790 return (char_u *)_(e_invrange);
4791 break;
4792 case ADDR_TABS:
4793 if (eap->line2 > LAST_TAB_NR)
4794 return (char_u *)_(e_invrange);
4795 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004796 case ADDR_TABS_RELATIVE:
4797 /* Do nothing */
4798 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004799#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004800 case ADDR_QUICKFIX:
4801 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4802 return (char_u *)_(e_invrange);
4803 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004804#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004805 }
4806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 return NULL;
4808}
4809
4810/*
4811 * Correct the range for zero line number, if required.
4812 */
4813 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004814correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815{
4816 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4817 {
4818 if (eap->line1 == 0)
4819 eap->line1 = 1;
4820 if (eap->line2 == 0)
4821 eap->line2 = 1;
4822 }
4823}
4824
Bram Moolenaar748bf032005-02-02 23:04:36 +00004825#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004826static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004827
4828/*
4829 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4830 * pattern. Otherwise return eap->arg.
4831 */
4832 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004833skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004834{
4835 char_u *p = eap->arg;
4836
Bram Moolenaara37420f2006-02-04 22:37:47 +00004837 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4838 || eap->cmdidx == CMD_vimgrepadd
4839 || eap->cmdidx == CMD_lvimgrepadd
4840 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004841 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004842 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004843 if (p == NULL)
4844 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004845 }
4846 return p;
4847}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004848
4849/*
4850 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4851 * in the command line, so that things like % get expanded.
4852 */
4853 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004854replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004855{
4856 char_u *new_cmdline;
4857 char_u *program;
4858 char_u *pos;
4859 char_u *ptr;
4860 int len;
4861 int i;
4862
4863 /*
4864 * Don't do it when ":vimgrep" is used for ":grep".
4865 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004866 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4867 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4868 || eap->cmdidx == CMD_grepadd
4869 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004870 && !grep_internal(eap->cmdidx))
4871 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004872 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4873 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004874 {
4875 if (*curbuf->b_p_gp == NUL)
4876 program = p_gp;
4877 else
4878 program = curbuf->b_p_gp;
4879 }
4880 else
4881 {
4882 if (*curbuf->b_p_mp == NUL)
4883 program = p_mp;
4884 else
4885 program = curbuf->b_p_mp;
4886 }
4887
4888 p = skipwhite(p);
4889
4890 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4891 {
4892 /* replace $* by given arguments */
4893 i = 1;
4894 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4895 ++i;
4896 len = (int)STRLEN(p);
4897 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4898 if (new_cmdline == NULL)
4899 return NULL; /* out of memory */
4900 ptr = new_cmdline;
4901 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4902 {
4903 i = (int)(pos - program);
4904 STRNCPY(ptr, program, i);
4905 STRCPY(ptr += i, p);
4906 ptr += len;
4907 program = pos + 2;
4908 }
4909 STRCPY(ptr, program);
4910 }
4911 else
4912 {
4913 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4914 if (new_cmdline == NULL)
4915 return NULL; /* out of memory */
4916 STRCPY(new_cmdline, program);
4917 STRCAT(new_cmdline, " ");
4918 STRCAT(new_cmdline, p);
4919 }
4920 msg_make(p);
4921
4922 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4923 vim_free(*cmdlinep);
4924 *cmdlinep = new_cmdline;
4925 p = new_cmdline;
4926 }
4927 return p;
4928}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004929#endif
4930
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931/*
4932 * Expand file name in Ex command argument.
4933 * Return FAIL for failure, OK otherwise.
4934 */
4935 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004936expand_filename(
4937 exarg_T *eap,
4938 char_u **cmdlinep,
4939 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940{
4941 int has_wildcards; /* need to expand wildcards */
4942 char_u *repl;
4943 int srclen;
4944 char_u *p;
4945 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004946 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947
Bram Moolenaar748bf032005-02-02 23:04:36 +00004948#ifdef FEAT_QUICKFIX
4949 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4950 p = skip_grep_pat(eap);
4951#else
4952 p = eap->arg;
4953#endif
4954
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 /*
4956 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4957 * the file name contains a wildcard it should not cause expanding.
4958 * (it will be expanded anyway if there is a wildcard before replacing).
4959 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004960 has_wildcards = mch_has_wildcard(p);
4961 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004963#ifdef FEAT_EVAL
4964 /* Skip over `=expr`, wildcards in it are not expanded. */
4965 if (p[0] == '`' && p[1] == '=')
4966 {
4967 p += 2;
4968 (void)skip_expr(&p);
4969 if (*p == '`')
4970 ++p;
4971 continue;
4972 }
4973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 /*
4975 * Quick check if this cannot be the start of a special string.
4976 * Also removes backslash before '%', '#' and '<'.
4977 */
4978 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4979 {
4980 ++p;
4981 continue;
4982 }
4983
4984 /*
4985 * Try to find a match at this position.
4986 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004987 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4988 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 if (*errormsgp != NULL) /* error detected */
4990 return FAIL;
4991 if (repl == NULL) /* no match found */
4992 {
4993 p += srclen;
4994 continue;
4995 }
4996
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004997 /* Wildcards won't be expanded below, the replacement is taken
4998 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4999 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5000 {
5001 char_u *l = repl;
5002
5003 repl = expand_env_save(repl);
5004 vim_free(l);
5005 }
5006
Bram Moolenaar63b92542007-03-27 14:57:09 +00005007 /* Need to escape white space et al. with a backslash.
5008 * Don't do this for:
5009 * - replacement that already has been escaped: "##"
5010 * - shell commands (may have to use quotes instead).
5011 * - non-unix systems when there is a single argument (spaces don't
5012 * separate arguments then).
5013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005015 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 && eap->cmdidx != CMD_grep
5018 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005019 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005020 && eap->cmdidx != CMD_lgrep
5021 && eap->cmdidx != CMD_lgrepadd
5022 && eap->cmdidx != CMD_lmake
5023 && eap->cmdidx != CMD_make
5024 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025#ifndef UNIX
5026 && !(eap->argt & NOSPC)
5027#endif
5028 )
5029 {
5030 char_u *l;
5031#ifdef BACKSLASH_IN_FILENAME
5032 /* Don't escape a backslash here, because rem_backslash() doesn't
5033 * remove it later. */
5034 static char_u *nobslash = (char_u *)" \t\"|";
5035# define ESCAPE_CHARS nobslash
5036#else
5037# define ESCAPE_CHARS escape_chars
5038#endif
5039
5040 for (l = repl; *l; ++l)
5041 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5042 {
5043 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5044 if (l != NULL)
5045 {
5046 vim_free(repl);
5047 repl = l;
5048 }
5049 break;
5050 }
5051 }
5052
5053 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005054 if ((eap->usefilter || eap->cmdidx == CMD_bang
5055 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005056 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 {
5058 char_u *l;
5059
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005060 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 if (l != NULL)
5062 {
5063 vim_free(repl);
5064 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 }
5066 }
5067
5068 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5069 vim_free(repl);
5070 if (p == NULL)
5071 return FAIL;
5072 }
5073
5074 /*
5075 * One file argument: Expand wildcards.
5076 * Don't do this with ":r !command" or ":w !command".
5077 */
5078 if ((eap->argt & NOSPC) && !eap->usefilter)
5079 {
5080 /*
5081 * May do this twice:
5082 * 1. Replace environment variables.
5083 * 2. Replace any other wildcards, remove backslashes.
5084 */
5085 for (n = 1; n <= 2; ++n)
5086 {
5087 if (n == 2)
5088 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 /*
5090 * Halve the number of backslashes (this is Vi compatible).
5091 * For Unix and OS/2, when wildcards are expanded, this is
5092 * done by ExpandOne() below.
5093 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005094#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 if (!has_wildcards)
5096#endif
5097 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 }
5099
5100 if (has_wildcards)
5101 {
5102 if (n == 1)
5103 {
5104 /*
5105 * First loop: May expand environment variables. This
5106 * can be done much faster with expand_env() than with
5107 * something else (e.g., calling a shell).
5108 * After expanding environment variables, check again
5109 * if there are still wildcards present.
5110 */
5111 if (vim_strchr(eap->arg, '$') != NULL
5112 || vim_strchr(eap->arg, '~') != NULL)
5113 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005114 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005115 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 has_wildcards = mch_has_wildcard(NameBuff);
5117 p = NameBuff;
5118 }
5119 else
5120 p = NULL;
5121 }
5122 else /* n == 2 */
5123 {
5124 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005125 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126
5127 ExpandInit(&xpc);
5128 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005129 if (p_wic)
5130 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005132 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 if (p == NULL)
5134 return FAIL;
5135 }
5136 if (p != NULL)
5137 {
5138 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5139 p, cmdlinep);
5140 if (n == 2) /* p came from ExpandOne() */
5141 vim_free(p);
5142 }
5143 }
5144 }
5145 }
5146 return OK;
5147}
5148
5149/*
5150 * Replace part of the command line, keeping eap->cmd, eap->arg and
5151 * eap->nextcmd correct.
5152 * "src" points to the part that is to be replaced, of length "srclen".
5153 * "repl" is the replacement string.
5154 * Returns a pointer to the character after the replaced string.
5155 * Returns NULL for failure.
5156 */
5157 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005158repl_cmdline(
5159 exarg_T *eap,
5160 char_u *src,
5161 int srclen,
5162 char_u *repl,
5163 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164{
5165 int len;
5166 int i;
5167 char_u *new_cmdline;
5168
5169 /*
5170 * The new command line is build in new_cmdline[].
5171 * First allocate it.
5172 * Careful: a "+cmd" argument may have been NUL terminated.
5173 */
5174 len = (int)STRLEN(repl);
5175 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005176 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5178 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5179 return NULL; /* out of memory! */
5180
5181 /*
5182 * Copy the stuff before the expanded part.
5183 * Copy the expanded stuff.
5184 * Copy what came after the expanded part.
5185 * Copy the next commands, if there are any.
5186 */
5187 i = (int)(src - *cmdlinep); /* length of part before match */
5188 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005189
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 mch_memmove(new_cmdline + i, repl, (size_t)len);
5191 i += len; /* remember the end of the string */
5192 STRCPY(new_cmdline + i, src + srclen);
5193 src = new_cmdline + i; /* remember where to continue */
5194
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005195 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
5197 i = (int)STRLEN(new_cmdline) + 1;
5198 STRCPY(new_cmdline + i, eap->nextcmd);
5199 eap->nextcmd = new_cmdline + i;
5200 }
5201 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5202 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5203 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5204 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5205 vim_free(*cmdlinep);
5206 *cmdlinep = new_cmdline;
5207
5208 return src;
5209}
5210
5211/*
5212 * Check for '|' to separate commands and '"' to start comments.
5213 */
5214 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005215separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216{
5217 char_u *p;
5218
Bram Moolenaar86b68352004-12-27 21:59:20 +00005219#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005220 p = skip_grep_pat(eap);
5221#else
5222 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005223#endif
5224
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005225 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 {
5227 if (*p == Ctrl_V)
5228 {
5229 if (eap->argt & (USECTRLV | XFILE))
5230 ++p; /* skip CTRL-V and next char */
5231 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005232 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005233 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 if (*p == NUL) /* stop at NUL after CTRL-V */
5235 break;
5236 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005237
5238#ifdef FEAT_EVAL
5239 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005240 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005241 {
5242 p += 2;
5243 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005244 }
5245#endif
5246
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 /* Check for '"': start of comment or '|': next command */
5248 /* :@" and :*" do not start a comment!
5249 * :redir @" doesn't either. */
5250 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5251 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5252 || p != eap->arg)
5253 && (eap->cmdidx != CMD_redir
5254 || p != eap->arg + 1 || p[-1] != '@'))
5255 || *p == '|' || *p == '\n')
5256 {
5257 /*
5258 * We remove the '\' before the '|', unless USECTRLV is used
5259 * AND 'b' is present in 'cpoptions'.
5260 */
5261 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5262 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5263 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005264 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 --p;
5266 }
5267 else
5268 {
5269 eap->nextcmd = check_nextcmd(p);
5270 *p = NUL;
5271 break;
5272 }
5273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005275
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5277 del_trailing_spaces(eap->arg);
5278}
5279
5280/*
5281 * get + command from ex argument
5282 */
5283 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005284getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285{
5286 char_u *arg = *argp;
5287 char_u *command = NULL;
5288
5289 if (*arg == '+') /* +[command] */
5290 {
5291 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005292 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 command = dollar_command;
5294 else
5295 {
5296 command = arg;
5297 arg = skip_cmd_arg(command, TRUE);
5298 if (*arg != NUL)
5299 *arg++ = NUL; /* terminate command with NUL */
5300 }
5301
5302 arg = skipwhite(arg); /* skip over spaces */
5303 *argp = arg;
5304 }
5305 return command;
5306}
5307
5308/*
5309 * Find end of "+command" argument. Skip over "\ " and "\\".
5310 */
5311 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005312skip_cmd_arg(
5313 char_u *p,
5314 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315{
5316 while (*p && !vim_isspace(*p))
5317 {
5318 if (*p == '\\' && p[1] != NUL)
5319 {
5320 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005321 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 else
5323 ++p;
5324 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005325 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 }
5327 return p;
5328}
5329
5330/*
5331 * Get "++opt=arg" argument.
5332 * Return FAIL or OK.
5333 */
5334 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005335getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336{
5337 char_u *arg = eap->arg + 2;
5338 int *pp = NULL;
5339#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005340 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 char_u *p;
5342#endif
5343
5344 /* ":edit ++[no]bin[ary] file" */
5345 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5346 {
5347 if (*arg == 'n')
5348 {
5349 arg += 2;
5350 eap->force_bin = FORCE_NOBIN;
5351 }
5352 else
5353 eap->force_bin = FORCE_BIN;
5354 if (!checkforcmd(&arg, "binary", 3))
5355 return FAIL;
5356 eap->arg = skipwhite(arg);
5357 return OK;
5358 }
5359
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005360 /* ":read ++edit file" */
5361 if (STRNCMP(arg, "edit", 4) == 0)
5362 {
5363 eap->read_edit = TRUE;
5364 eap->arg = skipwhite(arg + 4);
5365 return OK;
5366 }
5367
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 if (STRNCMP(arg, "ff", 2) == 0)
5369 {
5370 arg += 2;
5371 pp = &eap->force_ff;
5372 }
5373 else if (STRNCMP(arg, "fileformat", 10) == 0)
5374 {
5375 arg += 10;
5376 pp = &eap->force_ff;
5377 }
5378#ifdef FEAT_MBYTE
5379 else if (STRNCMP(arg, "enc", 3) == 0)
5380 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005381 if (STRNCMP(arg, "encoding", 8) == 0)
5382 arg += 8;
5383 else
5384 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 pp = &eap->force_enc;
5386 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005387 else if (STRNCMP(arg, "bad", 3) == 0)
5388 {
5389 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005390 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392#endif
5393
5394 if (pp == NULL || *arg != '=')
5395 return FAIL;
5396
5397 ++arg;
5398 *pp = (int)(arg - eap->cmd);
5399 arg = skip_cmd_arg(arg, FALSE);
5400 eap->arg = skipwhite(arg);
5401 *arg = NUL;
5402
5403#ifdef FEAT_MBYTE
5404 if (pp == &eap->force_ff)
5405 {
5406#endif
5407 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5408 return FAIL;
5409#ifdef FEAT_MBYTE
5410 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005411 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 {
5413 /* Make 'fileencoding' lower case. */
5414 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5415 *p = TOLOWER_ASC(*p);
5416 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005417 else
5418 {
5419 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5420 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005421 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005422 if (STRICMP(p, "keep") == 0)
5423 eap->bad_char = BAD_KEEP;
5424 else if (STRICMP(p, "drop") == 0)
5425 eap->bad_char = BAD_DROP;
5426 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5427 eap->bad_char = *p;
5428 else
5429 return FAIL;
5430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431#endif
5432
5433 return OK;
5434}
5435
5436/*
5437 * ":abbreviate" and friends.
5438 */
5439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005440ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
5442 do_exmap(eap, TRUE); /* almost the same as mapping */
5443}
5444
5445/*
5446 * ":map" and friends.
5447 */
5448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005449ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450{
5451 /*
5452 * If we are sourcing .exrc or .vimrc in current directory we
5453 * print the mappings for security reasons.
5454 */
5455 if (secure)
5456 {
5457 secure = 2;
5458 msg_outtrans(eap->cmd);
5459 msg_putchar('\n');
5460 }
5461 do_exmap(eap, FALSE);
5462}
5463
5464/*
5465 * ":unmap" and friends.
5466 */
5467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005468ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469{
5470 do_exmap(eap, FALSE);
5471}
5472
5473/*
5474 * ":mapclear" and friends.
5475 */
5476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005477ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478{
5479 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5480}
5481
5482/*
5483 * ":abclear" and friends.
5484 */
5485 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005486ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487{
5488 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5489}
5490
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005491#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005493ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494{
5495 /*
5496 * Disallow auto commands from .exrc and .vimrc in current
5497 * directory for security reasons.
5498 */
5499 if (secure)
5500 {
5501 secure = 2;
5502 eap->errmsg = e_curdir;
5503 }
5504 else if (eap->cmdidx == CMD_autocmd)
5505 do_autocmd(eap->arg, eap->forceit);
5506 else
5507 do_augroup(eap->arg, eap->forceit);
5508}
5509
5510/*
5511 * ":doautocmd": Apply the automatic commands to the current buffer.
5512 */
5513 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005514ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005516 char_u *arg = eap->arg;
5517 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005518 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005519
Bram Moolenaar1610d052016-06-09 22:53:01 +02005520 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5521 /* Only when there is no <nomodeline>. */
5522 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005523 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524}
5525#endif
5526
5527#ifdef FEAT_LISTCMDS
5528/*
5529 * :[N]bunload[!] [N] [bufname] unload buffer
5530 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5531 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5532 */
5533 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005534ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535{
5536 eap->errmsg = do_bufdel(
5537 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5538 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5539 : DOBUF_UNLOAD, eap->arg,
5540 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5541}
5542
5543/*
5544 * :[N]buffer [N] to buffer N
5545 * :[N]sbuffer [N] to buffer N
5546 */
5547 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005548ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549{
5550 if (*eap->arg)
5551 eap->errmsg = e_trailing;
5552 else
5553 {
5554 if (eap->addr_count == 0) /* default is current buffer */
5555 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5556 else
5557 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005558 if (eap->do_ecmd_cmd != NULL)
5559 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
5561}
5562
5563/*
5564 * :[N]bmodified [N] to next mod. buffer
5565 * :[N]sbmodified [N] to next mod. buffer
5566 */
5567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005568ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
5570 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005571 if (eap->do_ecmd_cmd != NULL)
5572 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573}
5574
5575/*
5576 * :[N]bnext [N] to next buffer
5577 * :[N]sbnext [N] split and to next buffer
5578 */
5579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005580ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
5582 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005583 if (eap->do_ecmd_cmd != NULL)
5584 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585}
5586
5587/*
5588 * :[N]bNext [N] to previous buffer
5589 * :[N]bprevious [N] to previous buffer
5590 * :[N]sbNext [N] split and to previous buffer
5591 * :[N]sbprevious [N] split and to previous buffer
5592 */
5593 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005594ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595{
5596 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005597 if (eap->do_ecmd_cmd != NULL)
5598 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599}
5600
5601/*
5602 * :brewind to first buffer
5603 * :bfirst to first buffer
5604 * :sbrewind split and to first buffer
5605 * :sbfirst split and to first buffer
5606 */
5607 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005608ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609{
5610 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005611 if (eap->do_ecmd_cmd != NULL)
5612 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613}
5614
5615/*
5616 * :blast to last buffer
5617 * :sblast split and to last buffer
5618 */
5619 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005620ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621{
5622 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005623 if (eap->do_ecmd_cmd != NULL)
5624 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625}
5626#endif
5627
5628 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005629ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630{
5631 return (c == NUL || c == '|' || c == '"' || c == '\n');
5632}
5633
5634#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5635 || defined(PROTO)
5636/*
5637 * Return the next command, after the first '|' or '\n'.
5638 * Return NULL if not found.
5639 */
5640 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005641find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642{
5643 while (*p != '|' && *p != '\n')
5644 {
5645 if (*p == NUL)
5646 return NULL;
5647 ++p;
5648 }
5649 return (p + 1);
5650}
5651#endif
5652
5653/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005654 * Check if *p is a separator between Ex commands, skipping over white space.
5655 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 */
5657 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005658check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005660 char_u *s = skipwhite(p);
5661
5662 if (*s == '|' || *s == '\n')
5663 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 else
5665 return NULL;
5666}
5667
5668/*
5669 * - if there are more files to edit
5670 * - and this is the last window
5671 * - and forceit not used
5672 * - and not repeated twice on a row
5673 * return FAIL and give error message if 'message' TRUE
5674 * return OK otherwise
5675 */
5676 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005677check_more(
5678 int message, /* when FALSE check only, no messages */
5679 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680{
5681 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5682
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005683 if (!forceit && only_one_window()
5684 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 {
5686 if (message)
5687 {
5688#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5689 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5690 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005691 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692
5693 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005694 vim_strncpy(buff,
5695 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005696 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005698 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 _("%d more files to edit. Quit anyway?"), n);
5700 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5701 return OK;
5702 return FAIL;
5703 }
5704#endif
5705 if (n == 1)
5706 EMSG(_("E173: 1 more file to edit"));
5707 else
5708 EMSGN(_("E173: %ld more files to edit"), n);
5709 quitmore = 2; /* next try to quit is allowed */
5710 }
5711 return FAIL;
5712 }
5713 return OK;
5714}
5715
5716#ifdef FEAT_CMDL_COMPL
5717/*
5718 * Function given to ExpandGeneric() to obtain the list of command names.
5719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005721get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
5723 if (idx >= (int)CMD_SIZE)
5724# ifdef FEAT_USR_CMDS
5725 return get_user_command_name(idx);
5726# else
5727 return NULL;
5728# endif
5729 return cmdnames[idx].cmd_name;
5730}
5731#endif
5732
5733#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005734static 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);
5735static void uc_list(char_u *name, size_t name_len);
5736static 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);
5737static char_u *uc_split_args(char_u *arg, size_t *lenp);
5738static 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 +00005739
5740 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005741uc_add_command(
5742 char_u *name,
5743 size_t name_len,
5744 char_u *rep,
5745 long argt,
5746 long def,
5747 int flags,
5748 int compl,
5749 char_u *compl_arg,
5750 int addr_type,
5751 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752{
5753 ucmd_T *cmd = NULL;
5754 char_u *p;
5755 int i;
5756 int cmp = 1;
5757 char_u *rep_buf = NULL;
5758 garray_T *gap;
5759
Bram Moolenaar9c102382006-05-03 21:26:49 +00005760 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 if (rep_buf == NULL)
5762 {
5763 /* Can't replace termcodes - try using the string as is */
5764 rep_buf = vim_strsave(rep);
5765
5766 /* Give up if out of memory */
5767 if (rep_buf == NULL)
5768 return FAIL;
5769 }
5770
5771 /* get address of growarray: global or in curbuf */
5772 if (flags & UC_BUFFER)
5773 {
5774 gap = &curbuf->b_ucmds;
5775 if (gap->ga_itemsize == 0)
5776 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5777 }
5778 else
5779 gap = &ucmds;
5780
5781 /* Search for the command in the already defined commands. */
5782 for (i = 0; i < gap->ga_len; ++i)
5783 {
5784 size_t len;
5785
5786 cmd = USER_CMD_GA(gap, i);
5787 len = STRLEN(cmd->uc_name);
5788 cmp = STRNCMP(name, cmd->uc_name, name_len);
5789 if (cmp == 0)
5790 {
5791 if (name_len < len)
5792 cmp = -1;
5793 else if (name_len > len)
5794 cmp = 1;
5795 }
5796
5797 if (cmp == 0)
5798 {
5799 if (!force)
5800 {
5801 EMSG(_("E174: Command already exists: add ! to replace it"));
5802 goto fail;
5803 }
5804
5805 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005806 cmd->uc_rep = NULL;
5807#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5808 vim_free(cmd->uc_compl_arg);
5809 cmd->uc_compl_arg = NULL;
5810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 break;
5812 }
5813
5814 /* Stop as soon as we pass the name to add */
5815 if (cmp < 0)
5816 break;
5817 }
5818
5819 /* Extend the array unless we're replacing an existing command */
5820 if (cmp != 0)
5821 {
5822 if (ga_grow(gap, 1) != OK)
5823 goto fail;
5824 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5825 goto fail;
5826
5827 cmd = USER_CMD_GA(gap, i);
5828 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5829
5830 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
5832 cmd->uc_name = p;
5833 }
5834
5835 cmd->uc_rep = rep_buf;
5836 cmd->uc_argt = argt;
5837 cmd->uc_def = def;
5838 cmd->uc_compl = compl;
5839#ifdef FEAT_EVAL
5840 cmd->uc_scriptID = current_SID;
5841# ifdef FEAT_CMDL_COMPL
5842 cmd->uc_compl_arg = compl_arg;
5843# endif
5844#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005845 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846
5847 return OK;
5848
5849fail:
5850 vim_free(rep_buf);
5851#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5852 vim_free(compl_arg);
5853#endif
5854 return FAIL;
5855}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005858#if defined(FEAT_USR_CMDS)
5859static struct
5860{
5861 int expand;
5862 char *name;
5863} addr_type_complete[] =
5864{
5865 {ADDR_ARGUMENTS, "arguments"},
5866 {ADDR_LINES, "lines"},
5867 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5868 {ADDR_TABS, "tabs"},
5869 {ADDR_BUFFERS, "buffers"},
5870 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005871 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005872 {-1, NULL}
5873};
5874#endif
5875
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005876#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877/*
5878 * List of names for completion for ":command" with the EXPAND_ flag.
5879 * Must be alphabetical for completion.
5880 */
5881static struct
5882{
5883 int expand;
5884 char *name;
5885} command_complete[] =
5886{
5887 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005888 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005890 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005892 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005893#if defined(FEAT_CSCOPE)
5894 {EXPAND_CSCOPE, "cscope"},
5895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5897 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005898 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899#endif
5900 {EXPAND_DIRECTORIES, "dir"},
5901 {EXPAND_ENV_VARS, "environment"},
5902 {EXPAND_EVENTS, "event"},
5903 {EXPAND_EXPRESSION, "expression"},
5904 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005905 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005906 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 {EXPAND_FUNCTIONS, "function"},
5908 {EXPAND_HELP, "help"},
5909 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005910#if defined(FEAT_CMDHIST)
5911 {EXPAND_HISTORY, "history"},
5912#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005913#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005914 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005915 {EXPAND_LOCALES, "locale"},
5916#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005917 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 {EXPAND_MAPPINGS, "mapping"},
5919 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005920 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005921 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005922#if defined(FEAT_PROFILE)
5923 {EXPAND_SYNTIME, "syntime"},
5924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005926 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005927 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005928#if defined(FEAT_SIGNS)
5929 {EXPAND_SIGN, "sign"},
5930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 {EXPAND_TAGS, "tag"},
5932 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005933 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 {EXPAND_USER_VARS, "var"},
5935 {0, NULL}
5936};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005939#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005941uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942{
5943 int i, j;
5944 int found = FALSE;
5945 ucmd_T *cmd;
5946 int len;
5947 long a;
5948 garray_T *gap;
5949
5950 gap = &curbuf->b_ucmds;
5951 for (;;)
5952 {
5953 for (i = 0; i < gap->ga_len; ++i)
5954 {
5955 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005956 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957
Bram Moolenaard29459b2016-08-26 22:29:11 +02005958 /* Skip commands which don't match the requested prefix and
5959 * commands filtered out. */
5960 if (STRNCMP(name, cmd->uc_name, name_len) != 0
5961 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 continue;
5963
5964 /* Put out the title first time */
5965 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005966 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 found = TRUE;
5968 msg_putchar('\n');
5969 if (got_int)
5970 break;
5971
5972 /* Special cases */
5973 msg_putchar(a & BANG ? '!' : ' ');
5974 msg_putchar(a & REGSTR ? '"' : ' ');
5975 msg_putchar(gap != &ucmds ? 'b' : ' ');
5976 msg_putchar(' ');
5977
Bram Moolenaar8820b482017-03-16 17:23:31 +01005978 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 len = (int)STRLEN(cmd->uc_name) + 4;
5980
5981 do {
5982 msg_putchar(' ');
5983 ++len;
5984 } while (len < 16);
5985
5986 len = 0;
5987
5988 /* Arguments */
5989 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5990 {
5991 case 0: IObuff[len++] = '0'; break;
5992 case (EXTRA): IObuff[len++] = '*'; break;
5993 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5994 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5995 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5996 }
5997
5998 do {
5999 IObuff[len++] = ' ';
6000 } while (len < 5);
6001
6002 /* Range */
6003 if (a & (RANGE|COUNT))
6004 {
6005 if (a & COUNT)
6006 {
6007 /* -count=N */
6008 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6009 len += (int)STRLEN(IObuff + len);
6010 }
6011 else if (a & DFLALL)
6012 IObuff[len++] = '%';
6013 else if (cmd->uc_def >= 0)
6014 {
6015 /* -range=N */
6016 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6017 len += (int)STRLEN(IObuff + len);
6018 }
6019 else
6020 IObuff[len++] = '.';
6021 }
6022
6023 do {
6024 IObuff[len++] = ' ';
6025 } while (len < 11);
6026
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006027 /* Address Type */
6028 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6029 if (addr_type_complete[j].expand != ADDR_LINES
6030 && addr_type_complete[j].expand == cmd->uc_addr_type)
6031 {
6032 STRCPY(IObuff + len, addr_type_complete[j].name);
6033 len += (int)STRLEN(IObuff + len);
6034 break;
6035 }
6036
6037 do {
6038 IObuff[len++] = ' ';
6039 } while (len < 21);
6040
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 /* Completion */
6042 for (j = 0; command_complete[j].expand != 0; ++j)
6043 if (command_complete[j].expand == cmd->uc_compl)
6044 {
6045 STRCPY(IObuff + len, command_complete[j].name);
6046 len += (int)STRLEN(IObuff + len);
6047 break;
6048 }
6049
6050 do {
6051 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006052 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053
6054 IObuff[len] = '\0';
6055 msg_outtrans(IObuff);
6056
6057 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006058#ifdef FEAT_EVAL
6059 if (p_verbose > 0)
6060 last_set_msg(cmd->uc_scriptID);
6061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 out_flush();
6063 ui_breakcheck();
6064 if (got_int)
6065 break;
6066 }
6067 if (gap == &ucmds || i < gap->ga_len)
6068 break;
6069 gap = &ucmds;
6070 }
6071
6072 if (!found)
6073 MSG(_("No user-defined commands found"));
6074}
6075
6076 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006077uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078{
6079 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6080 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6081 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6082 0xb9, 0x7f, 0};
6083 int i;
6084
6085 for (i = 0; fcmd[i]; ++i)
6086 IObuff[i] = fcmd[i] - 0x40;
6087 IObuff[i] = 0;
6088 return IObuff;
6089}
6090
6091 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006092uc_scan_attr(
6093 char_u *attr,
6094 size_t len,
6095 long *argt,
6096 long *def,
6097 int *flags,
6098 int *compl,
6099 char_u **compl_arg,
6100 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101{
6102 char_u *p;
6103
6104 if (len == 0)
6105 {
6106 EMSG(_("E175: No attribute specified"));
6107 return FAIL;
6108 }
6109
6110 /* First, try the simple attributes (no arguments) */
6111 if (STRNICMP(attr, "bang", len) == 0)
6112 *argt |= BANG;
6113 else if (STRNICMP(attr, "buffer", len) == 0)
6114 *flags |= UC_BUFFER;
6115 else if (STRNICMP(attr, "register", len) == 0)
6116 *argt |= REGSTR;
6117 else if (STRNICMP(attr, "bar", len) == 0)
6118 *argt |= TRLBAR;
6119 else
6120 {
6121 int i;
6122 char_u *val = NULL;
6123 size_t vallen = 0;
6124 size_t attrlen = len;
6125
6126 /* Look for the attribute name - which is the part before any '=' */
6127 for (i = 0; i < (int)len; ++i)
6128 {
6129 if (attr[i] == '=')
6130 {
6131 val = &attr[i + 1];
6132 vallen = len - i - 1;
6133 attrlen = i;
6134 break;
6135 }
6136 }
6137
6138 if (STRNICMP(attr, "nargs", attrlen) == 0)
6139 {
6140 if (vallen == 1)
6141 {
6142 if (*val == '0')
6143 /* Do nothing - this is the default */;
6144 else if (*val == '1')
6145 *argt |= (EXTRA | NOSPC | NEEDARG);
6146 else if (*val == '*')
6147 *argt |= EXTRA;
6148 else if (*val == '?')
6149 *argt |= (EXTRA | NOSPC);
6150 else if (*val == '+')
6151 *argt |= (EXTRA | NEEDARG);
6152 else
6153 goto wrong_nargs;
6154 }
6155 else
6156 {
6157wrong_nargs:
6158 EMSG(_("E176: Invalid number of arguments"));
6159 return FAIL;
6160 }
6161 }
6162 else if (STRNICMP(attr, "range", attrlen) == 0)
6163 {
6164 *argt |= RANGE;
6165 if (vallen == 1 && *val == '%')
6166 *argt |= DFLALL;
6167 else if (val != NULL)
6168 {
6169 p = val;
6170 if (*def >= 0)
6171 {
6172two_count:
6173 EMSG(_("E177: Count cannot be specified twice"));
6174 return FAIL;
6175 }
6176
6177 *def = getdigits(&p);
6178 *argt |= (ZEROR | NOTADR);
6179
6180 if (p != val + vallen || vallen == 0)
6181 {
6182invalid_count:
6183 EMSG(_("E178: Invalid default value for count"));
6184 return FAIL;
6185 }
6186 }
6187 }
6188 else if (STRNICMP(attr, "count", attrlen) == 0)
6189 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006190 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191
6192 if (val != NULL)
6193 {
6194 p = val;
6195 if (*def >= 0)
6196 goto two_count;
6197
6198 *def = getdigits(&p);
6199
6200 if (p != val + vallen)
6201 goto invalid_count;
6202 }
6203
6204 if (*def < 0)
6205 *def = 0;
6206 }
6207 else if (STRNICMP(attr, "complete", attrlen) == 0)
6208 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 if (val == NULL)
6210 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006211 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 return FAIL;
6213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006215 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6216 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006219 else if (STRNICMP(attr, "addr", attrlen) == 0)
6220 {
6221 *argt |= RANGE;
6222 if (val == NULL)
6223 {
6224 EMSG(_("E179: argument required for -addr"));
6225 return FAIL;
6226 }
6227 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6228 == FAIL)
6229 return FAIL;
6230 if (addr_type_arg != ADDR_LINES)
6231 *argt |= (ZEROR | NOTADR) ;
6232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 else
6234 {
6235 char_u ch = attr[len];
6236 attr[len] = '\0';
6237 EMSG2(_("E181: Invalid attribute: %s"), attr);
6238 attr[len] = ch;
6239 return FAIL;
6240 }
6241 }
6242
6243 return OK;
6244}
6245
Bram Moolenaara850a712009-01-28 14:42:59 +00006246/*
6247 * ":command ..."
6248 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006250ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251{
6252 char_u *name;
6253 char_u *end;
6254 char_u *p;
6255 long argt = 0;
6256 long def = -1;
6257 int flags = 0;
6258 int compl = EXPAND_NOTHING;
6259 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006260 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006262 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263
6264 p = eap->arg;
6265
6266 /* Check for attributes */
6267 while (*p == '-')
6268 {
6269 ++p;
6270 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006271 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6272 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 == FAIL)
6274 return;
6275 p = skipwhite(end);
6276 }
6277
6278 /* Get the name (if any) and skip to the following argument */
6279 name = p;
6280 if (ASCII_ISALPHA(*p))
6281 while (ASCII_ISALNUM(*p))
6282 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006283 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 {
6285 EMSG(_("E182: Invalid command name"));
6286 return;
6287 }
6288 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006289 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290
6291 /* If there is nothing after the name, and no attributes were specified,
6292 * we are listing commands
6293 */
6294 p = skipwhite(end);
6295 if (!has_attr && ends_excmd(*p))
6296 {
6297 uc_list(name, end - name);
6298 }
6299 else if (!ASCII_ISUPPER(*name))
6300 {
6301 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6302 return;
6303 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006304 else if ((name_len == 1 && *name == 'X')
6305 || (name_len <= 4
6306 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6307 {
6308 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6309 return;
6310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 else
6312 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006313 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314}
6315
6316/*
6317 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 * Clear all user commands, global and for current buffer.
6319 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006320 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006321ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322{
6323 uc_clear(&ucmds);
6324 uc_clear(&curbuf->b_ucmds);
6325}
6326
6327/*
6328 * Clear all user commands for "gap".
6329 */
6330 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006331uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332{
6333 int i;
6334 ucmd_T *cmd;
6335
6336 for (i = 0; i < gap->ga_len; ++i)
6337 {
6338 cmd = USER_CMD_GA(gap, i);
6339 vim_free(cmd->uc_name);
6340 vim_free(cmd->uc_rep);
6341# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6342 vim_free(cmd->uc_compl_arg);
6343# endif
6344 }
6345 ga_clear(gap);
6346}
6347
6348 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006349ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350{
6351 int i = 0;
6352 ucmd_T *cmd = NULL;
6353 int cmp = -1;
6354 garray_T *gap;
6355
6356 gap = &curbuf->b_ucmds;
6357 for (;;)
6358 {
6359 for (i = 0; i < gap->ga_len; ++i)
6360 {
6361 cmd = USER_CMD_GA(gap, i);
6362 cmp = STRCMP(eap->arg, cmd->uc_name);
6363 if (cmp <= 0)
6364 break;
6365 }
6366 if (gap == &ucmds || cmp == 0)
6367 break;
6368 gap = &ucmds;
6369 }
6370
6371 if (cmp != 0)
6372 {
6373 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6374 return;
6375 }
6376
6377 vim_free(cmd->uc_name);
6378 vim_free(cmd->uc_rep);
6379# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6380 vim_free(cmd->uc_compl_arg);
6381# endif
6382
6383 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384
6385 if (i < gap->ga_len)
6386 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6387}
6388
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006389/*
6390 * split and quote args for <f-args>
6391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006393uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394{
6395 char_u *buf;
6396 char_u *p;
6397 char_u *q;
6398 int len;
6399
6400 /* Precalculate length */
6401 p = arg;
6402 len = 2; /* Initial and final quotes */
6403
6404 while (*p)
6405 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006406 if (p[0] == '\\' && p[1] == '\\')
6407 {
6408 len += 2;
6409 p += 2;
6410 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006411 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 {
6413 len += 1;
6414 p += 2;
6415 }
6416 else if (*p == '\\' || *p == '"')
6417 {
6418 len += 2;
6419 p += 1;
6420 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006421 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 {
6423 p = skipwhite(p);
6424 if (*p == NUL)
6425 break;
6426 len += 3; /* "," */
6427 }
6428 else
6429 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006430#ifdef FEAT_MBYTE
6431 int charlen = (*mb_ptr2len)(p);
6432 len += charlen;
6433 p += charlen;
6434#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 ++len;
6436 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 }
6439 }
6440
6441 buf = alloc(len + 1);
6442 if (buf == NULL)
6443 {
6444 *lenp = 0;
6445 return buf;
6446 }
6447
6448 p = arg;
6449 q = buf;
6450 *q++ = '"';
6451 while (*p)
6452 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006453 if (p[0] == '\\' && p[1] == '\\')
6454 {
6455 *q++ = '\\';
6456 *q++ = '\\';
6457 p += 2;
6458 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006459 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 {
6461 *q++ = p[1];
6462 p += 2;
6463 }
6464 else if (*p == '\\' || *p == '"')
6465 {
6466 *q++ = '\\';
6467 *q++ = *p++;
6468 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006469 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 {
6471 p = skipwhite(p);
6472 if (*p == NUL)
6473 break;
6474 *q++ = '"';
6475 *q++ = ',';
6476 *q++ = '"';
6477 }
6478 else
6479 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006480 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 }
6482 }
6483 *q++ = '"';
6484 *q = 0;
6485
6486 *lenp = len;
6487 return buf;
6488}
6489
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006490 static size_t
6491add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6492{
6493 size_t result;
6494
6495 result = STRLEN(mod_str);
6496 if (*multi_mods)
6497 result += 1;
6498 if (buf != NULL)
6499 {
6500 if (*multi_mods)
6501 STRCAT(buf, " ");
6502 STRCAT(buf, mod_str);
6503 }
6504
6505 *multi_mods = 1;
6506
6507 return result;
6508}
6509
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510/*
6511 * Check for a <> code in a user command.
6512 * "code" points to the '<'. "len" the length of the <> (inclusive).
6513 * "buf" is where the result is to be added.
6514 * "split_buf" points to a buffer used for splitting, caller should free it.
6515 * "split_len" is the length of what "split_buf" contains.
6516 * Returns the length of the replacement, which has been added to "buf".
6517 * Returns -1 if there was no match, and only the "<" has been copied.
6518 */
6519 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006520uc_check_code(
6521 char_u *code,
6522 size_t len,
6523 char_u *buf,
6524 ucmd_T *cmd, /* the user command we're expanding */
6525 exarg_T *eap, /* ex arguments */
6526 char_u **split_buf,
6527 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528{
6529 size_t result = 0;
6530 char_u *p = code + 1;
6531 size_t l = len - 2;
6532 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006533 enum {
6534 ct_ARGS,
6535 ct_BANG,
6536 ct_COUNT,
6537 ct_LINE1,
6538 ct_LINE2,
6539 ct_RANGE,
6540 ct_MODS,
6541 ct_REGISTER,
6542 ct_LT,
6543 ct_NONE
6544 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545
6546 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6547 {
6548 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6549 p += 2;
6550 l -= 2;
6551 }
6552
Bram Moolenaar371d5402006-03-20 21:47:49 +00006553 ++l;
6554 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006556 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006558 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006560 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006562 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006564 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006566 else if (STRNICMP(p, "range>", l) == 0)
6567 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006568 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006570 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006572 else if (STRNICMP(p, "mods>", l) == 0)
6573 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574
6575 switch (type)
6576 {
6577 case ct_ARGS:
6578 /* Simple case first */
6579 if (*eap->arg == NUL)
6580 {
6581 if (quote == 1)
6582 {
6583 result = 2;
6584 if (buf != NULL)
6585 STRCPY(buf, "''");
6586 }
6587 else
6588 result = 0;
6589 break;
6590 }
6591
6592 /* When specified there is a single argument don't split it.
6593 * Works for ":Cmd %" when % is "a b c". */
6594 if ((eap->argt & NOSPC) && quote == 2)
6595 quote = 1;
6596
6597 switch (quote)
6598 {
6599 case 0: /* No quoting, no splitting */
6600 result = STRLEN(eap->arg);
6601 if (buf != NULL)
6602 STRCPY(buf, eap->arg);
6603 break;
6604 case 1: /* Quote, but don't split */
6605 result = STRLEN(eap->arg) + 2;
6606 for (p = eap->arg; *p; ++p)
6607 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006608#ifdef FEAT_MBYTE
6609 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6610 /* DBCS can contain \ in a trail byte, skip the
6611 * double-byte character. */
6612 ++p;
6613 else
6614#endif
6615 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 ++result;
6617 }
6618
6619 if (buf != NULL)
6620 {
6621 *buf++ = '"';
6622 for (p = eap->arg; *p; ++p)
6623 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006624#ifdef FEAT_MBYTE
6625 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6626 /* DBCS can contain \ in a trail byte, copy the
6627 * double-byte character to avoid escaping. */
6628 *buf++ = *p++;
6629 else
6630#endif
6631 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 *buf++ = '\\';
6633 *buf++ = *p;
6634 }
6635 *buf = '"';
6636 }
6637
6638 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006639 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 /* This is hard, so only do it once, and cache the result */
6641 if (*split_buf == NULL)
6642 *split_buf = uc_split_args(eap->arg, split_len);
6643
6644 result = *split_len;
6645 if (buf != NULL && result != 0)
6646 STRCPY(buf, *split_buf);
6647
6648 break;
6649 }
6650 break;
6651
6652 case ct_BANG:
6653 result = eap->forceit ? 1 : 0;
6654 if (quote)
6655 result += 2;
6656 if (buf != NULL)
6657 {
6658 if (quote)
6659 *buf++ = '"';
6660 if (eap->forceit)
6661 *buf++ = '!';
6662 if (quote)
6663 *buf = '"';
6664 }
6665 break;
6666
6667 case ct_LINE1:
6668 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006669 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 case ct_COUNT:
6671 {
6672 char num_buf[20];
6673 long num = (type == ct_LINE1) ? eap->line1 :
6674 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006675 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6677 size_t num_len;
6678
6679 sprintf(num_buf, "%ld", num);
6680 num_len = STRLEN(num_buf);
6681 result = num_len;
6682
6683 if (quote)
6684 result += 2;
6685
6686 if (buf != NULL)
6687 {
6688 if (quote)
6689 *buf++ = '"';
6690 STRCPY(buf, num_buf);
6691 buf += num_len;
6692 if (quote)
6693 *buf = '"';
6694 }
6695
6696 break;
6697 }
6698
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006699 case ct_MODS:
6700 {
6701 int multi_mods = 0;
6702 typedef struct {
6703 int *varp;
6704 char *name;
6705 } mod_entry_T;
6706 static mod_entry_T mod_entries[] = {
6707#ifdef FEAT_BROWSE_CMD
6708 {&cmdmod.browse, "browse"},
6709#endif
6710#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6711 {&cmdmod.confirm, "confirm"},
6712#endif
6713 {&cmdmod.hide, "hide"},
6714 {&cmdmod.keepalt, "keepalt"},
6715 {&cmdmod.keepjumps, "keepjumps"},
6716 {&cmdmod.keepmarks, "keepmarks"},
6717 {&cmdmod.keeppatterns, "keeppatterns"},
6718 {&cmdmod.lockmarks, "lockmarks"},
6719 {&cmdmod.noswapfile, "noswapfile"},
6720 {NULL, NULL}
6721 };
6722 int i;
6723
6724 result = quote ? 2 : 0;
6725 if (buf != NULL)
6726 {
6727 if (quote)
6728 *buf++ = '"';
6729 *buf = '\0';
6730 }
6731
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006732 /* :aboveleft and :leftabove */
6733 if (cmdmod.split & WSP_ABOVE)
6734 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6735 /* :belowright and :rightbelow */
6736 if (cmdmod.split & WSP_BELOW)
6737 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6738 /* :botright */
6739 if (cmdmod.split & WSP_BOT)
6740 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006741
6742 /* the modifiers that are simple flags */
6743 for (i = 0; mod_entries[i].varp != NULL; ++i)
6744 if (*mod_entries[i].varp)
6745 result += add_cmd_modifier(buf, mod_entries[i].name,
6746 &multi_mods);
6747
6748 /* TODO: How to support :noautocmd? */
6749#ifdef HAVE_SANDBOX
6750 /* TODO: How to support :sandbox?*/
6751#endif
6752 /* :silent */
6753 if (msg_silent > 0)
6754 result += add_cmd_modifier(buf,
6755 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006756 /* :tab */
6757 if (cmdmod.tab > 0)
6758 result += add_cmd_modifier(buf, "tab", &multi_mods);
6759 /* :topleft */
6760 if (cmdmod.split & WSP_TOP)
6761 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006762 /* TODO: How to support :unsilent?*/
6763 /* :verbose */
6764 if (p_verbose > 0)
6765 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006766 /* :vertical */
6767 if (cmdmod.split & WSP_VERT)
6768 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006769 if (quote && buf != NULL)
6770 {
6771 buf += result - 2;
6772 *buf = '"';
6773 }
6774 break;
6775 }
6776
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 case ct_REGISTER:
6778 result = eap->regname ? 1 : 0;
6779 if (quote)
6780 result += 2;
6781 if (buf != NULL)
6782 {
6783 if (quote)
6784 *buf++ = '\'';
6785 if (eap->regname)
6786 *buf++ = eap->regname;
6787 if (quote)
6788 *buf = '\'';
6789 }
6790 break;
6791
6792 case ct_LT:
6793 result = 1;
6794 if (buf != NULL)
6795 *buf = '<';
6796 break;
6797
6798 default:
6799 /* Not recognized: just copy the '<' and return -1. */
6800 result = (size_t)-1;
6801 if (buf != NULL)
6802 *buf = '<';
6803 break;
6804 }
6805
6806 return result;
6807}
6808
6809 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006810do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811{
6812 char_u *buf;
6813 char_u *p;
6814 char_u *q;
6815
6816 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006817 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006818 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 size_t len, totlen;
6820
6821 size_t split_len = 0;
6822 char_u *split_buf = NULL;
6823 ucmd_T *cmd;
6824#ifdef FEAT_EVAL
6825 scid_T save_current_SID = current_SID;
6826#endif
6827
6828 if (eap->cmdidx == CMD_USER)
6829 cmd = USER_CMD(eap->useridx);
6830 else
6831 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6832
6833 /*
6834 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006835 * First round: "buf" is NULL, compute length, allocate "buf".
6836 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 */
6838 buf = NULL;
6839 for (;;)
6840 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006841 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006842 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006844
6845 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006847 start = vim_strchr(p, '<');
6848 if (start != NULL)
6849 end = vim_strchr(start + 1, '>');
6850 if (buf != NULL)
6851 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006852 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6853 ;
6854 if (*ksp == K_SPECIAL
6855 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006856 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6857# ifdef FEAT_GUI
6858 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6859# endif
6860 ))
6861 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006862 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006863 * KS_SPECIAL KE_FILLER, like for mappings, but
6864 * do_cmdline() doesn't handle that, so convert it back.
6865 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6866 len = ksp - p;
6867 if (len > 0)
6868 {
6869 mch_memmove(q, p, len);
6870 q += len;
6871 }
6872 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6873 p = ksp + 3;
6874 continue;
6875 }
6876 }
6877
6878 /* break if there no <item> is found */
6879 if (start == NULL || end == NULL)
6880 break;
6881
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 /* Include the '>' */
6883 ++end;
6884
6885 /* Take everything up to the '<' */
6886 len = start - p;
6887 if (buf == NULL)
6888 totlen += len;
6889 else
6890 {
6891 mch_memmove(q, p, len);
6892 q += len;
6893 }
6894
6895 len = uc_check_code(start, end - start, q, cmd, eap,
6896 &split_buf, &split_len);
6897 if (len == (size_t)-1)
6898 {
6899 /* no match, continue after '<' */
6900 p = start + 1;
6901 len = 1;
6902 }
6903 else
6904 p = end;
6905 if (buf == NULL)
6906 totlen += len;
6907 else
6908 q += len;
6909 }
6910 if (buf != NULL) /* second time here, finished */
6911 {
6912 STRCPY(q, p);
6913 break;
6914 }
6915
6916 totlen += STRLEN(p); /* Add on the trailing characters */
6917 buf = alloc((unsigned)(totlen + 1));
6918 if (buf == NULL)
6919 {
6920 vim_free(split_buf);
6921 return;
6922 }
6923 }
6924
6925#ifdef FEAT_EVAL
6926 current_SID = cmd->uc_scriptID;
6927#endif
6928 (void)do_cmdline(buf, eap->getline, eap->cookie,
6929 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6930#ifdef FEAT_EVAL
6931 current_SID = save_current_SID;
6932#endif
6933 vim_free(buf);
6934 vim_free(split_buf);
6935}
6936
6937# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6938 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006939get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940{
6941 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6942}
6943
6944/*
6945 * Function given to ExpandGeneric() to obtain the list of user command names.
6946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006948get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949{
6950 if (idx < curbuf->b_ucmds.ga_len)
6951 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6952 idx -= curbuf->b_ucmds.ga_len;
6953 if (idx < ucmds.ga_len)
6954 return USER_CMD(idx)->uc_name;
6955 return NULL;
6956}
6957
6958/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006959 * Function given to ExpandGeneric() to obtain the list of user address type names.
6960 */
6961 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006962get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006963{
6964 return (char_u *)addr_type_complete[idx].name;
6965}
6966
6967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 * Function given to ExpandGeneric() to obtain the list of user command
6969 * attributes.
6970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006972get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973{
6974 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006975 {"addr", "bang", "bar", "buffer", "complete",
6976 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006978 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 return NULL;
6980 return (char_u *)user_cmd_flags[idx];
6981}
6982
6983/*
6984 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006987get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988{
6989 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6990
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006991 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 return NULL;
6993 return (char_u *)user_cmd_nargs[idx];
6994}
6995
6996/*
6997 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007000get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001{
7002 return (char_u *)command_complete[idx].name;
7003}
7004# endif /* FEAT_CMDL_COMPL */
7005
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007006/*
7007 * Parse address type argument
7008 */
7009 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007010parse_addr_type_arg(
7011 char_u *value,
7012 int vallen,
7013 long *argt,
7014 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007015{
7016 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007017
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007018 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7019 {
7020 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7021 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7022 if (a && b)
7023 {
7024 *addr_type_arg = addr_type_complete[i].expand;
7025 break;
7026 }
7027 }
7028
7029 if (addr_type_complete[i].expand == -1)
7030 {
7031 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007032
Bram Moolenaar1c465442017-03-12 20:10:05 +01007033 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007034 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007035 err[i] = NUL;
7036 EMSG2(_("E180: Invalid address type value: %s"), err);
7037 return FAIL;
7038 }
7039
7040 if (*addr_type_arg != ADDR_LINES)
7041 *argt |= NOTADR;
7042
7043 return OK;
7044}
7045
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046#endif /* FEAT_USR_CMDS */
7047
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007048#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7049/*
7050 * Parse a completion argument "value[vallen]".
7051 * The detected completion goes in "*complp", argument type in "*argt".
7052 * When there is an argument, for function and user defined completion, it's
7053 * copied to allocated memory and stored in "*compl_arg".
7054 * Returns FAIL if something is wrong.
7055 */
7056 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007057parse_compl_arg(
7058 char_u *value,
7059 int vallen,
7060 int *complp,
7061 long *argt,
7062 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007063{
7064 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007065# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007066 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007067# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007068 int i;
7069 int valend = vallen;
7070
7071 /* Look for any argument part - which is the part after any ',' */
7072 for (i = 0; i < vallen; ++i)
7073 {
7074 if (value[i] == ',')
7075 {
7076 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007077# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007078 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007079# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007080 valend = i;
7081 break;
7082 }
7083 }
7084
7085 for (i = 0; command_complete[i].expand != 0; ++i)
7086 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007087 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007088 && STRNCMP(value, command_complete[i].name, valend) == 0)
7089 {
7090 *complp = command_complete[i].expand;
7091 if (command_complete[i].expand == EXPAND_BUFFERS)
7092 *argt |= BUFNAME;
7093 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7094 || command_complete[i].expand == EXPAND_FILES)
7095 *argt |= XFILE;
7096 break;
7097 }
7098 }
7099
7100 if (command_complete[i].expand == 0)
7101 {
7102 EMSG2(_("E180: Invalid complete value: %s"), value);
7103 return FAIL;
7104 }
7105
7106# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7107 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7108 && arg != NULL)
7109# else
7110 if (arg != NULL)
7111# endif
7112 {
7113 EMSG(_("E468: Completion argument only allowed for custom completion"));
7114 return FAIL;
7115 }
7116
7117# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7118 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7119 && arg == NULL)
7120 {
7121 EMSG(_("E467: Custom completion requires a function argument"));
7122 return FAIL;
7123 }
7124
7125 if (arg != NULL)
7126 *compl_arg = vim_strnsave(arg, (int)arglen);
7127# endif
7128 return OK;
7129}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007130
7131 int
7132cmdcomplete_str_to_type(char_u *complete_str)
7133{
7134 int i;
7135
7136 for (i = 0; command_complete[i].expand != 0; ++i)
7137 if (STRCMP(complete_str, command_complete[i].name) == 0)
7138 return command_complete[i].expand;
7139
7140 return EXPAND_NOTHING;
7141}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007142#endif
7143
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007145ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146{
Bram Moolenaare6850792010-05-14 15:28:44 +02007147 if (*eap->arg == NUL)
7148 {
7149#ifdef FEAT_EVAL
7150 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7151 char_u *p = NULL;
7152
7153 if (expr != NULL)
7154 {
7155 ++emsg_off;
7156 p = eval_to_string(expr, NULL, FALSE);
7157 --emsg_off;
7158 vim_free(expr);
7159 }
7160 if (p != NULL)
7161 {
7162 MSG(p);
7163 vim_free(p);
7164 }
7165 else
7166 MSG("default");
7167#else
7168 MSG(_("unknown"));
7169#endif
7170 }
7171 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007172 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173}
7174
7175 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007176ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177{
7178 if (*eap->arg == NUL && eap->cmd[2] == '!')
7179 MSG(_("Greetings, Vim user!"));
7180 do_highlight(eap->arg, eap->forceit, FALSE);
7181}
7182
7183
7184/*
7185 * Call this function if we thought we were going to exit, but we won't
7186 * (because of an error). May need to restore the terminal mode.
7187 */
7188 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007189not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190{
7191 exiting = FALSE;
7192 settmode(TMODE_RAW);
7193}
7194
7195/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007196 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 */
7198 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007199ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007201 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007202
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203#ifdef FEAT_CMDWIN
7204 if (cmdwin_type != 0)
7205 {
7206 cmdwin_result = Ctrl_C;
7207 return;
7208 }
7209#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007210 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007211 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007212 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007213 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007214 return;
7215 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007216 if (eap->addr_count > 0)
7217 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007218 int wnr = eap->line2;
7219
7220 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7221 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007222 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007223 }
7224 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007225 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007226
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007227#ifdef FEAT_AUTOCMD
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007228 /* Refuse to quit when locked. */
7229 if (curbuf_locked())
7230 return;
7231 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7232 /* Bail out when autocommands closed the window.
7233 * Refuse to quit when the buffer in the last window is being closed (can
7234 * only happen in autocommands). */
7235 if (!win_valid(wp)
Bram Moolenaar0ea50702017-07-08 14:44:50 +02007236 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007237 return;
7238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
7240#ifdef FEAT_NETBEANS_INTG
7241 netbeansForcedQuit = eap->forceit;
7242#endif
7243
7244 /*
7245 * If there are more files or windows we won't exit.
7246 */
7247 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7248 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007249 if ((!buf_hide(wp->w_buffer)
7250 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007251 | (eap->forceit ? CCGD_FORCEIT : 0)
7252 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007254 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 {
7256 not_exiting();
7257 }
7258 else
7259 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007260 /* quit last window
7261 * Note: only_one_window() returns true, even so a help window is
7262 * still open. In that case only quit, if no address has been
7263 * specified. Example:
7264 * :h|wincmd w|1q - don't quit
7265 * :h|wincmd w|q - quit
7266 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007267 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007269 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007270#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007274 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 }
7276}
7277
7278/*
7279 * ":cquit".
7280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007282ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283{
7284 getout(1); /* this does not always pass on the exit code to the Manx
7285 compiler. why? */
7286}
7287
7288/*
7289 * ":qall": try to quit all windows
7290 */
7291 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007292ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293{
7294# ifdef FEAT_CMDWIN
7295 if (cmdwin_type != 0)
7296 {
7297 if (eap->forceit)
7298 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7299 else
7300 cmdwin_result = K_XF2;
7301 return;
7302 }
7303# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007304
7305 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007306 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007307 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007308 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007309 return;
7310 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007311#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007312 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7313 /* Refuse to quit when locked or when the buffer in the last window is
7314 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007315 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007316 return;
7317#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007318
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007320 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 getout(0);
7322 not_exiting();
7323}
7324
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325/*
7326 * ":close": close current window, unless it is the last one
7327 */
7328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007329ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007331 win_T *win;
7332 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007333#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007335 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007337#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007338 if (!text_locked()
7339#ifdef FEAT_AUTOCMD
7340 && !curbuf_locked()
7341#endif
7342 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007343 {
7344 if (eap->addr_count == 0)
7345 ex_win_close(eap->forceit, curwin, NULL);
7346 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007347 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007348 {
7349 winnr++;
7350 if (winnr == eap->line2)
7351 break;
7352 }
7353 if (win == NULL)
7354 win = lastwin;
7355 ex_win_close(eap->forceit, win, NULL);
7356 }
7357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358}
7359
Bram Moolenaar4033c552017-09-16 20:54:51 +02007360#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007361/*
7362 * ":pclose": Close any preview window.
7363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007365ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007366{
7367 win_T *win;
7368
Bram Moolenaar29323592016-07-24 22:04:11 +02007369 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007370 if (win->w_p_pvw)
7371 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007372 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007373 break;
7374 }
7375}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007376#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007377
Bram Moolenaarf740b292006-02-16 22:11:02 +00007378/*
7379 * Close window "win" and take care of handling closing the last window for a
7380 * modified buffer.
7381 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007382 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007383ex_win_close(
7384 int forceit,
7385 win_T *win,
7386 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387{
7388 int need_hide;
7389 buf_T *buf = win->w_buffer;
7390
7391 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007392 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007394#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 if ((p_confirm || cmdmod.confirm) && p_write)
7396 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007397 bufref_T bufref;
7398
7399 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007401 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 return;
7403 need_hide = FALSE;
7404 }
7405 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007408 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 return;
7410 }
7411 }
7412
Bram Moolenaar4033c552017-09-16 20:54:51 +02007413#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007415#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007416
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007418 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007419 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007420 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007421 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422}
7423
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007425 * Handle the argument for a tabpage related ex command.
7426 * Returns a tabpage number.
7427 * When an error is encountered then eap->errmsg is set.
7428 */
7429 static int
7430get_tabpage_arg(exarg_T *eap)
7431{
7432 int tab_number;
7433 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7434
7435 if (eap->arg && *eap->arg != NUL)
7436 {
7437 char_u *p = eap->arg;
7438 char_u *p_save;
7439 int relative = 0; /* argument +N/-N means: go to N places to the
7440 * right/left relative to the current position. */
7441
7442 if (*p == '-')
7443 {
7444 relative = -1;
7445 p++;
7446 }
7447 else if (*p == '+')
7448 {
7449 relative = 1;
7450 p++;
7451 }
7452
7453 p_save = p;
7454 tab_number = getdigits(&p);
7455
7456 if (relative == 0)
7457 {
7458 if (STRCMP(p, "$") == 0)
7459 tab_number = LAST_TAB_NR;
7460 else if (p == p_save || *p_save == '-' || *p != NUL
7461 || tab_number > LAST_TAB_NR)
7462 {
7463 /* No numbers as argument. */
7464 eap->errmsg = e_invarg;
7465 goto theend;
7466 }
7467 }
7468 else
7469 {
7470 if (*p_save == NUL)
7471 tab_number = 1;
7472 else if (p == p_save || *p_save == '-' || *p != NUL
7473 || tab_number == 0)
7474 {
7475 /* No numbers as argument. */
7476 eap->errmsg = e_invarg;
7477 goto theend;
7478 }
7479 tab_number = tab_number * relative + tabpage_index(curtab);
7480 if (!unaccept_arg0 && relative == -1)
7481 --tab_number;
7482 }
7483 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7484 eap->errmsg = e_invarg;
7485 }
7486 else if (eap->addr_count > 0)
7487 {
7488 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007489 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007490 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007491 tab_number = 0;
7492 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007493 else
7494 {
7495 tab_number = eap->line2;
7496 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7497 {
7498 --tab_number;
7499 if (tab_number < unaccept_arg0)
7500 eap->errmsg = e_invarg;
7501 }
7502 }
7503 }
7504 else
7505 {
7506 switch (eap->cmdidx)
7507 {
7508 case CMD_tabnext:
7509 tab_number = tabpage_index(curtab) + 1;
7510 if (tab_number > LAST_TAB_NR)
7511 tab_number = 1;
7512 break;
7513 case CMD_tabmove:
7514 tab_number = LAST_TAB_NR;
7515 break;
7516 default:
7517 tab_number = tabpage_index(curtab);
7518 }
7519 }
7520
7521theend:
7522 return tab_number;
7523}
7524
7525/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007526 * ":tabclose": close current tab page, unless it is the last one.
7527 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 */
7529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007530ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007532 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007533 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007534
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007535# ifdef FEAT_CMDWIN
7536 if (cmdwin_type != 0)
7537 cmdwin_result = K_IGNORE;
7538 else
7539# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007540 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007541 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007542 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007544 tab_number = get_tabpage_arg(eap);
7545 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007546 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007547 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007548 if (tp == NULL)
7549 {
7550 beep_flush();
7551 return;
7552 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007553 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007554 {
7555 tabpage_close_other(tp, eap->forceit);
7556 return;
7557 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007558 else if (!text_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007559#ifdef FEAT_AUTOCMD
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007560 && !curbuf_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007561#endif
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007562 )
7563 tabpage_close(eap->forceit);
7564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007566}
7567
7568/*
7569 * ":tabonly": close all tab pages except the current one
7570 */
7571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007572ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007573{
7574 tabpage_T *tp;
7575 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007576 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007577
7578# ifdef FEAT_CMDWIN
7579 if (cmdwin_type != 0)
7580 cmdwin_result = K_IGNORE;
7581 else
7582# endif
7583 if (first_tabpage->tp_next == NULL)
7584 MSG(_("Already only one tab page"));
7585 else
7586 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007587 tab_number = get_tabpage_arg(eap);
7588 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007589 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007590 goto_tabpage(tab_number);
7591 /* Repeat this up to a 1000 times, because autocommands may
7592 * mess up the lists. */
7593 for (done = 0; done < 1000; ++done)
7594 {
7595 FOR_ALL_TABPAGES(tp)
7596 if (tp->tp_topframe != topframe)
7597 {
7598 tabpage_close_other(tp, eap->forceit);
7599 /* if we failed to close it quit */
7600 if (valid_tabpage(tp))
7601 done = 1000;
7602 /* start over, "tp" is now invalid */
7603 break;
7604 }
7605 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007606 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007607 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007608 }
7609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611
7612/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007613 * Close the current tab page.
7614 */
7615 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007616tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007617{
7618 /* First close all the windows but the current one. If that worked then
7619 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007620 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007621 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007622 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007623 ex_win_close(forceit, curwin, NULL);
7624# ifdef FEAT_GUI
7625 need_mouse_correct = TRUE;
7626# endif
7627}
7628
7629/*
7630 * Close tab page "tp", which is not the current tab page.
7631 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007632 * Also takes care of the tab pages line disappearing when closing the
7633 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007634 */
7635 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007636tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007637{
7638 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007639 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007640 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007641
7642 /* Limit to 1000 windows, autocommands may add a window while we close
7643 * one. OK, so I'm paranoid... */
7644 while (++done < 1000)
7645 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007646 wp = tp->tp_firstwin;
7647 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007648
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007649 /* Autocommands may delete the tab page under our fingers and we may
7650 * fail to close a window with a modified buffer. */
7651 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007652 break;
7653 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007654
Bram Moolenaar29323592016-07-24 22:04:11 +02007655#ifdef FEAT_AUTOCMD
7656 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7657#endif
7658
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007659 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007660 if (h != tabline_height())
7661 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007662}
7663
7664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 * ":only".
7666 */
7667 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007668ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007670 win_T *wp;
7671 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672# ifdef FEAT_GUI
7673 need_mouse_correct = TRUE;
7674# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007675 if (eap->addr_count > 0)
7676 {
7677 wnr = eap->line2;
7678 for (wp = firstwin; --wnr > 0; )
7679 {
7680 if (wp->w_next == NULL)
7681 break;
7682 else
7683 wp = wp->w_next;
7684 }
7685 win_goto(wp);
7686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 close_others(TRUE, eap->forceit);
7688}
7689
7690/*
7691 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007692 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007694 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007695ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696{
7697 if (eap->addr_count == 0)
7698 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007699 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701
7702 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007703ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007705 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007706 if (!eap->skip)
7707 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007708#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007709 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007710#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007711 if (eap->addr_count == 0)
7712 win_close(curwin, FALSE); /* don't free buffer */
7713 else
7714 {
7715 int winnr = 0;
7716 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007717
Bram Moolenaar2256c992016-11-15 21:17:07 +01007718 FOR_ALL_WINDOWS(win)
7719 {
7720 winnr++;
7721 if (winnr == eap->line2)
7722 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007723 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007724 if (win == NULL)
7725 win = lastwin;
7726 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 }
7729}
7730
7731/*
7732 * ":stop" and ":suspend": Suspend Vim.
7733 */
7734 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007735ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736{
7737 /*
7738 * Disallow suspending for "rvim".
7739 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007740 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 {
7742 if (!eap->forceit)
7743 autowrite_all();
7744 windgoto((int)Rows - 1, 0);
7745 out_char('\n');
7746 out_flush();
7747 stoptermcap();
7748 out_flush(); /* needed for SUN to restore xterm buffer */
7749#ifdef FEAT_TITLE
7750 mch_restore_title(3); /* restore window titles */
7751#endif
7752 ui_suspend(); /* call machine specific function */
7753#ifdef FEAT_TITLE
7754 maketitle();
7755 resettitle(); /* force updating the title */
7756#endif
7757 starttermcap();
7758 scroll_start(); /* scroll screen before redrawing */
7759 redraw_later_clear();
7760 shell_resized(); /* may have resized window */
7761 }
7762}
7763
7764/*
7765 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7766 */
7767 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007768ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769{
7770#ifdef FEAT_CMDWIN
7771 if (cmdwin_type != 0)
7772 {
7773 cmdwin_result = Ctrl_C;
7774 return;
7775 }
7776#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007777 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007778 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007779 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007780 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007781 return;
7782 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007783#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007784 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7785 /* Refuse to quit when locked or when the buffer in the last window is
7786 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007787 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007788 return;
7789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790
7791 /*
7792 * if more files or windows we won't exit
7793 */
7794 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7795 exiting = TRUE;
7796 if ( ((eap->cmdidx == CMD_wq
7797 || curbufIsChanged())
7798 && do_write(eap) == FAIL)
7799 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007800 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 {
7802 not_exiting();
7803 }
7804 else
7805 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007808 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809# ifdef FEAT_GUI
7810 need_mouse_correct = TRUE;
7811# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007812 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007813 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 }
7815}
7816
7817/*
7818 * ":print", ":list", ":number".
7819 */
7820 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007821ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007823 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7824 EMSG(_(e_emptybuf));
7825 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007827 for ( ;!got_int; ui_breakcheck())
7828 {
7829 print_line(eap->line1,
7830 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7831 || (eap->flags & EXFLAG_NR)),
7832 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7833 if (++eap->line1 > eap->line2)
7834 break;
7835 out_flush(); /* show one line at a time */
7836 }
7837 setpcmark();
7838 /* put cursor at last line */
7839 curwin->w_cursor.lnum = eap->line2;
7840 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 }
7842
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844}
7845
7846#ifdef FEAT_BYTEOFF
7847 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007848ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849{
7850 goto_byte(eap->line2);
7851}
7852#endif
7853
7854/*
7855 * ":shell".
7856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007858ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
7860 do_shell(NULL, 0);
7861}
7862
Bram Moolenaar4033c552017-09-16 20:54:51 +02007863#if defined(HAVE_DROP_FILE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007865 || defined(FEAT_GUI_MSWIN) \
7866 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 || defined(PROTO)
7868
7869/*
7870 * Handle a file drop. The code is here because a drop is *nearly* like an
7871 * :args command, but not quite (we have a list of exact filenames, so we
7872 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7873 * code is very similar to :args and hence needs access to a lot of the static
7874 * functions in this file.
7875 *
7876 * The list should be allocated using alloc(), as should each item in the
7877 * list. This function takes over responsibility for freeing the list.
7878 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007879 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007880 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 */
7882 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007883handle_drop(
7884 int filec, /* the number of files dropped */
7885 char_u **filev, /* the list of files dropped */
7886 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887{
7888 exarg_T ea;
7889 int save_msg_scroll = msg_scroll;
7890
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007891 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007892 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007894#ifdef FEAT_AUTOCMD
7895 if (curbuf_locked())
7896 return;
7897#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007898 /* When the screen is being updated we should not change buffers and
7899 * windows structures, it may cause freed memory to be used. */
7900 if (updating_screen)
7901 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902
7903 /* Check whether the current buffer is changed. If so, we will need
7904 * to split the current window or data could be lost.
7905 * We don't need to check if the 'hidden' option is set, as in this
7906 * case the buffer won't be lost.
7907 */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007908 if (!buf_hide(curbuf) && !split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 {
7910 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007911 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 --emsg_off;
7913 }
7914 if (split)
7915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 if (win_split(0, 0) == FAIL)
7917 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007918 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919
7920 /* When splitting the window, create a new alist. Otherwise the
7921 * existing one is overwritten. */
7922 alist_unlink(curwin->w_alist);
7923 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 }
7925
7926 /*
7927 * Set up the new argument list.
7928 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007929 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930
7931 /*
7932 * Move to the first file.
7933 */
7934 /* Fake up a minimal "next" command for do_argfile() */
7935 vim_memset(&ea, 0, sizeof(ea));
7936 ea.cmd = (char_u *)"next";
7937 do_argfile(&ea, 0);
7938
7939 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7940 * mode that is not needed here. */
7941 need_start_insertmode = FALSE;
7942
7943 /* Restore msg_scroll, otherwise a following command may cause scrolling
7944 * unexpectedly. The screen will be redrawn by the caller, thus
7945 * msg_scroll being set by displaying a message is irrelevant. */
7946 msg_scroll = save_msg_scroll;
7947}
7948#endif
7949
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950/*
7951 * Clear an argument list: free all file names and reset it to zero entries.
7952 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007953 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007954alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955{
7956 while (--al->al_ga.ga_len >= 0)
7957 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7958 ga_clear(&al->al_ga);
7959}
7960
7961/*
7962 * Init an argument list.
7963 */
7964 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007965alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966{
7967 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7968}
7969
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970/*
7971 * Remove a reference from an argument list.
7972 * Ignored when the argument list is the global one.
7973 * If the argument list is no longer used by any window, free it.
7974 */
7975 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007976alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977{
7978 if (al != &global_alist && --al->al_refcount <= 0)
7979 {
7980 alist_clear(al);
7981 vim_free(al);
7982 }
7983}
7984
Bram Moolenaar4033c552017-09-16 20:54:51 +02007985#if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986/*
7987 * Create a new argument list and use it for the current window.
7988 */
7989 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007990alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991{
7992 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7993 if (curwin->w_alist == NULL)
7994 {
7995 curwin->w_alist = &global_alist;
7996 ++global_alist.al_refcount;
7997 }
7998 else
7999 {
8000 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008001 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 alist_init(curwin->w_alist);
8003 }
8004}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005#endif
8006
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008007#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008/*
8009 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008010 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8011 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 */
8013 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008014alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015{
8016 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008017 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 char_u **new_arg_files;
8019 int new_arg_file_count;
8020 char_u *save_p_su = p_su;
8021 int i;
8022
8023 /* Don't use 'suffixes' here. This should work like the shell did the
8024 * expansion. Also, the vimrc file isn't read yet, thus the user
8025 * can't set the options. */
8026 p_su = empty_option;
8027 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8028 if (old_arg_files != NULL)
8029 {
8030 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008031 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8032 old_arg_count = GARGCOUNT;
8033 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008035 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 && new_arg_file_count > 0)
8037 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008038 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8039 TRUE, fnum_list, fnum_len);
8040 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 }
8043 p_su = save_p_su;
8044}
8045#endif
8046
8047/*
8048 * Set the argument list for the current window.
8049 * Takes over the allocated files[] and the allocated fnames in it.
8050 */
8051 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008052alist_set(
8053 alist_T *al,
8054 int count,
8055 char_u **files,
8056 int use_curbuf,
8057 int *fnum_list,
8058 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059{
8060 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008061 static int recursive = 0;
8062
8063 if (recursive)
8064 {
8065#ifdef FEAT_AUTOCMD
8066 EMSG(_(e_au_recursive));
8067#endif
8068 return;
8069 }
8070 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071
8072 alist_clear(al);
8073 if (ga_grow(&al->al_ga, count) == OK)
8074 {
8075 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008076 {
8077 if (got_int)
8078 {
8079 /* When adding many buffers this can take a long time. Allow
8080 * interrupting here. */
8081 while (i < count)
8082 vim_free(files[i++]);
8083 break;
8084 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008085
8086 /* May set buffer name of a buffer previously used for the
8087 * argument list, so that it's re-used by alist_add. */
8088 if (fnum_list != NULL && i < fnum_len)
8089 buf_set_name(fnum_list[i], files[i]);
8090
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008092 ui_breakcheck();
8093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 vim_free(files);
8095 }
8096 else
8097 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008100
8101 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102}
8103
8104/*
8105 * Add file "fname" to argument list "al".
8106 * "fname" must have been allocated and "al" must have been checked for room.
8107 */
8108 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008109alist_add(
8110 alist_T *al,
8111 char_u *fname,
8112 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113{
8114 if (fname == NULL) /* don't add NULL file names */
8115 return;
8116#ifdef BACKSLASH_IN_FILENAME
8117 slash_adjust(fname);
8118#endif
8119 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8120 if (set_fnum > 0)
8121 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8122 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8123 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124}
8125
8126#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8127/*
8128 * Adjust slashes in file names. Called after 'shellslash' was set.
8129 */
8130 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008131alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132{
8133 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008135 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136
8137 for (i = 0; i < GARGCOUNT; ++i)
8138 if (GARGLIST[i].ae_fname != NULL)
8139 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008140 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 if (wp->w_alist != &global_alist)
8142 for (i = 0; i < WARGCOUNT(wp); ++i)
8143 if (WARGLIST(wp)[i].ae_fname != NULL)
8144 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145}
8146#endif
8147
8148/*
8149 * ":preserve".
8150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008152ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008154 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 ml_preserve(curbuf, TRUE);
8156}
8157
8158/*
8159 * ":recover".
8160 */
8161 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008162ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163{
8164 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8165 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008166 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8167 | CCGD_MULTWIN
8168 | (eap->forceit ? CCGD_FORCEIT : 0)
8169 | CCGD_EXCMD)
8170
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 && (*eap->arg == NUL
8172 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8173 ml_recover();
8174 recoverymode = FALSE;
8175}
8176
8177/*
8178 * Command modifier used in a wrong way.
8179 */
8180 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008181ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182{
8183 eap->errmsg = e_invcmd;
8184}
8185
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186/*
8187 * :sview [+command] file split window with new file, read-only
8188 * :split [[+command] file] split window with current or new file
8189 * :vsplit [[+command] file] split window vertically with current or new file
8190 * :new [[+command] file] split window with no or new file
8191 * :vnew [[+command] file] split vertically window with no or new file
8192 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008193 *
8194 * :tabedit open new Tab page with empty window
8195 * :tabedit [+command] file open new Tab page and edit "file"
8196 * :tabnew [[+command] file] just like :tabedit
8197 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 */
8199 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008200ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008202 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008203#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008205#endif
8206#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209
Bram Moolenaar4033c552017-09-16 20:54:51 +02008210#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213
Bram Moolenaar4033c552017-09-16 20:54:51 +02008214#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008216 * quickfix windows. But it's OK when doing ":tab split". */
8217 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {
8219 if (eap->cmdidx == CMD_split)
8220 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 if (eap->cmdidx == CMD_vsplit)
8222 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225
Bram Moolenaar4033c552017-09-16 20:54:51 +02008226#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008227 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {
8229 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8230 FNAME_MESS, TRUE, curbuf->b_ffname);
8231 if (fname == NULL)
8232 goto theend;
8233 eap->arg = fname;
8234 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008235# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008236 else
8237# endif
8238#endif
8239#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 && eap->cmdidx != CMD_new)
8243 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008244# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008245 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008246# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008247 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008248# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008249 au_has_group((char_u *)"FileExplorer"))
8250 {
8251 /* No browsing supported but we do have the file explorer:
8252 * Edit the directory. */
8253 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8254 eap->arg = (char_u *)".";
8255 }
8256 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008257# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008258 {
8259 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008261 if (fname == NULL)
8262 goto theend;
8263 eap->arg = fname;
8264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 }
8266 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008269 /*
8270 * Either open new tab page or split the window.
8271 */
8272 if (eap->cmdidx == CMD_tabedit
8273 || eap->cmdidx == CMD_tabfind
8274 || eap->cmdidx == CMD_tabnew)
8275 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008276 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8277 : eap->addr_count == 0 ? 0
8278 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008279 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008280 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008281
8282 /* set the alternate buffer for the window we came from */
8283 if (curwin != old_curwin
8284 && win_valid(old_curwin)
8285 && old_curwin->w_buffer != curbuf
8286 && !cmdmod.keepalt)
8287 old_curwin->w_alt_fnum = curbuf->b_fnum;
8288 }
8289 }
8290 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8292 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008293# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 /* Reset 'scrollbind' when editing another file, but keep it when
8295 * doing ":split" without arguments. */
8296 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008297# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008299# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008301 {
8302 RESET_BINDING(curwin);
8303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 else
8305 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008306# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 do_exedit(eap, old_curwin);
8308 }
8309
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008310# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008312# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008314# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315theend:
8316 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008319
8320/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008321 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008322 */
8323 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008324tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008325{
8326 exarg_T ea;
8327
8328 vim_memset(&ea, 0, sizeof(ea));
8329 ea.cmdidx = CMD_tabnew;
8330 ea.cmd = (char_u *)"tabn";
8331 ea.arg = (char_u *)"";
8332 ex_splitview(&ea);
8333}
8334
8335/*
8336 * :tabnext command
8337 */
8338 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008339ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008340{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008341 int tab_number;
8342
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008343 switch (eap->cmdidx)
8344 {
8345 case CMD_tabfirst:
8346 case CMD_tabrewind:
8347 goto_tabpage(1);
8348 break;
8349 case CMD_tablast:
8350 goto_tabpage(9999);
8351 break;
8352 case CMD_tabprevious:
8353 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008354 if (eap->arg && *eap->arg != NUL)
8355 {
8356 char_u *p = eap->arg;
8357 char_u *p_save = p;
8358
8359 tab_number = getdigits(&p);
8360 if (p == p_save || *p_save == '-' || *p != NUL
8361 || tab_number == 0)
8362 {
8363 /* No numbers as argument. */
8364 eap->errmsg = e_invarg;
8365 return;
8366 }
8367 }
8368 else
8369 {
8370 if (eap->addr_count == 0)
8371 tab_number = 1;
8372 else
8373 {
8374 tab_number = eap->line2;
8375 if (tab_number < 1)
8376 {
8377 eap->errmsg = e_invrange;
8378 return;
8379 }
8380 }
8381 }
8382 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008383 break;
8384 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008385 tab_number = get_tabpage_arg(eap);
8386 if (eap->errmsg == NULL)
8387 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008388 break;
8389 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008390}
8391
8392/*
8393 * :tabmove command
8394 */
8395 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008396ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008397{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008398 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008399
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008400 tab_number = get_tabpage_arg(eap);
8401 if (eap->errmsg == NULL)
8402 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008403}
8404
8405/*
8406 * :tabs command: List tabs and their contents.
8407 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008408 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008409ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008410{
8411 tabpage_T *tp;
8412 win_T *wp;
8413 int tabcount = 1;
8414
8415 msg_start();
8416 msg_scroll = TRUE;
8417 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8418 {
8419 msg_putchar('\n');
8420 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008421 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008422 out_flush(); /* output one line at a time */
8423 ui_breakcheck();
8424
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008425 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008426 wp = firstwin;
8427 else
8428 wp = tp->tp_firstwin;
8429 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8430 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008431 msg_putchar('\n');
8432 msg_putchar(wp == curwin ? '>' : ' ');
8433 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008434 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8435 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008436 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008437 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008438 else
8439 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8440 IObuff, IOSIZE, TRUE);
8441 msg_outtrans(IObuff);
8442 out_flush(); /* output one line at a time */
8443 ui_breakcheck();
8444 }
8445 }
8446}
8447
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448/*
8449 * ":mode": Set screen mode.
8450 * If no argument given, just get the screen size and redraw.
8451 */
8452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008453ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455 if (*eap->arg == NUL)
8456 shell_resized();
8457 else
8458 mch_screenmode(eap->arg);
8459}
8460
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461/*
8462 * ":resize".
8463 * set, increment or decrement current window height
8464 */
8465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008466ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467{
8468 int n;
8469 win_T *wp = curwin;
8470
8471 if (eap->addr_count > 0)
8472 {
8473 n = eap->line2;
8474 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8475 ;
8476 }
8477
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008478# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008480# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 if (cmdmod.split & WSP_VERT)
8483 {
8484 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008485 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8487 n = 9999;
8488 win_setwidth_win((int)n, wp);
8489 }
8490 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 {
8492 if (*eap->arg == '-' || *eap->arg == '+')
8493 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008494 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 n = 9999;
8496 win_setheight_win((int)n, wp);
8497 }
8498}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499
8500/*
8501 * ":find [+command] <file>" command.
8502 */
8503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008504ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505{
8506#ifdef FEAT_SEARCHPATH
8507 char_u *fname;
8508 int count;
8509
8510 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8511 TRUE, curbuf->b_ffname);
8512 if (eap->addr_count > 0)
8513 {
8514 /* Repeat finding the file "count" times. This matters when it
8515 * appears several times in the path. */
8516 count = eap->line2;
8517 while (fname != NULL && --count > 0)
8518 {
8519 vim_free(fname);
8520 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8521 FALSE, curbuf->b_ffname);
8522 }
8523 }
8524
8525 if (fname != NULL)
8526 {
8527 eap->arg = fname;
8528#endif
8529 do_exedit(eap, NULL);
8530#ifdef FEAT_SEARCHPATH
8531 vim_free(fname);
8532 }
8533#endif
8534}
8535
8536/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008537 * ":open" simulation: for now just work like ":visual".
8538 */
8539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008540ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008541{
8542 regmatch_T regmatch;
8543 char_u *p;
8544
8545 curwin->w_cursor.lnum = eap->line2;
8546 beginline(BL_SOL | BL_FIX);
8547 if (*eap->arg == '/')
8548 {
8549 /* ":open /pattern/": put cursor in column found with pattern */
8550 ++eap->arg;
8551 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8552 *p = NUL;
8553 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8554 if (regmatch.regprog != NULL)
8555 {
8556 regmatch.rm_ic = p_ic;
8557 p = ml_get_curline();
8558 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008559 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008560 else
8561 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008562 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008563 }
8564 /* Move to the NUL, ignore any other arguments. */
8565 eap->arg += STRLEN(eap->arg);
8566 }
8567 check_cursor();
8568
8569 eap->cmdidx = CMD_visual;
8570 do_exedit(eap, NULL);
8571}
8572
8573/*
8574 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 */
8576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008577ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579 do_exedit(eap, NULL);
8580}
8581
8582/*
8583 * ":edit <file>" command and alikes.
8584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008586do_exedit(
8587 exarg_T *eap,
8588 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589{
8590 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008592 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593
8594 /*
8595 * ":vi" command ends Ex mode.
8596 */
8597 if (exmode_active && (eap->cmdidx == CMD_visual
8598 || eap->cmdidx == CMD_view))
8599 {
8600 exmode_active = FALSE;
8601 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008602 {
8603 /* Special case: ":global/pat/visual\NLvi-commands" */
8604 if (global_busy)
8605 {
8606 int rd = RedrawingDisabled;
8607 int nwr = no_wait_return;
8608 int ms = msg_scroll;
8609#ifdef FEAT_GUI
8610 int he = hold_gui_events;
8611#endif
8612
8613 if (eap->nextcmd != NULL)
8614 {
8615 stuffReadbuff(eap->nextcmd);
8616 eap->nextcmd = NULL;
8617 }
8618
8619 if (exmode_was != EXMODE_VIM)
8620 settmode(TMODE_RAW);
8621 RedrawingDisabled = 0;
8622 no_wait_return = 0;
8623 need_wait_return = FALSE;
8624 msg_scroll = 0;
8625#ifdef FEAT_GUI
8626 hold_gui_events = 0;
8627#endif
8628 must_redraw = CLEAR;
8629
8630 main_loop(FALSE, TRUE);
8631
8632 RedrawingDisabled = rd;
8633 no_wait_return = nwr;
8634 msg_scroll = ms;
8635#ifdef FEAT_GUI
8636 hold_gui_events = he;
8637#endif
8638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 }
8642
8643 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008644 || eap->cmdidx == CMD_tabnew
8645 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008646 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008648 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 setpcmark();
8650 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008651 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8652 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008654 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 || *eap->arg != NUL
8656#ifdef FEAT_BROWSE
8657 || cmdmod.browse
8658#endif
8659 )
8660 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008661#ifdef FEAT_AUTOCMD
8662 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8663 * can bring us here, others are stopped earlier. */
8664 if (*eap->arg != NUL && curbuf_locked())
8665 return;
8666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 n = readonlymode;
8668 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8669 readonlymode = TRUE;
8670 else if (eap->cmdidx == CMD_enew)
8671 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8672 empty buffer */
8673 setpcmark();
8674 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8675 NULL, eap,
8676 /* ":edit" goes to first line if Vi compatible */
8677 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8678 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8679 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008680 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008682 /* after a split we can use an existing buffer */
8683 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684#ifdef FEAT_LISTCMDS
8685 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8686#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008687 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 {
8689 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 if (old_curwin != NULL)
8691 {
8692 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008693 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02008695#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008696 cleanup_T cs;
8697
8698 /* Reset the error/interrupt/exception state here so that
8699 * aborting() returns FALSE when closing a window. */
8700 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008701#endif
8702#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008704#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008705 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008706
Bram Moolenaar4033c552017-09-16 20:54:51 +02008707#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008708 /* Restore the error/interrupt/exception state if not
8709 * discarded by a new aborting error, interrupt, or
8710 * uncaught exception. */
8711 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 }
8714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 }
8716 else if (readonlymode && curbuf->b_nwindows == 1)
8717 {
8718 /* When editing an already visited buffer, 'readonly' won't be set
8719 * but the previous value is kept. With ":view" and ":sview" we
8720 * want the file to be readonly, except when another window is
8721 * editing the same buffer. */
8722 curbuf->b_p_ro = TRUE;
8723 }
8724 readonlymode = n;
8725 }
8726 else
8727 {
8728 if (eap->do_ecmd_cmd != NULL)
8729 do_cmdline_cmd(eap->do_ecmd_cmd);
8730#ifdef FEAT_TITLE
8731 n = curwin->w_arg_idx_invalid;
8732#endif
8733 check_arg_idx(curwin);
8734#ifdef FEAT_TITLE
8735 if (n != curwin->w_arg_idx_invalid)
8736 maketitle();
8737#endif
8738 }
8739
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 /*
8741 * if ":split file" worked, set alternate file name in old window to new
8742 * file
8743 */
8744 if (old_curwin != NULL
8745 && *eap->arg != NUL
8746 && curwin != old_curwin
8747 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008748 && old_curwin->w_buffer != curbuf
8749 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751
8752 ex_no_reprint = TRUE;
8753}
8754
8755#ifndef FEAT_GUI
8756/*
8757 * ":gui" and ":gvim" when there is no GUI.
8758 */
8759 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008760ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761{
8762 eap->errmsg = e_nogvim;
8763}
8764#endif
8765
8766#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8767 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008768ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769{
8770 gui_make_tearoff(eap->arg);
8771}
8772#endif
8773
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008774#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008776ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008778 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779}
8780#endif
8781
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008783ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784{
8785 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8786 MSG(_("No swap file"));
8787 else
8788 msg(curbuf->b_ml.ml_mfp->mf_fname);
8789}
8790
8791/*
8792 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8793 * offset.
8794 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008797ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798{
8799#ifdef FEAT_SCROLLBIND
8800 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008801 win_T *save_curwin = curwin;
8802 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 long topline;
8804 long y;
8805 linenr_T old_linenr = curwin->w_cursor.lnum;
8806
8807 setpcmark();
8808
8809 /*
8810 * determine max topline
8811 */
8812 if (curwin->w_p_scb)
8813 {
8814 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008815 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 {
8817 if (wp->w_p_scb && wp->w_buffer)
8818 {
8819 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8820 if (topline > y)
8821 topline = y;
8822 }
8823 }
8824 if (topline < 1)
8825 topline = 1;
8826 }
8827 else
8828 {
8829 topline = 1;
8830 }
8831
8832
8833 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008834 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008836 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 {
8838 if (curwin->w_p_scb)
8839 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008840 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 y = topline - curwin->w_topline;
8842 if (y > 0)
8843 scrollup(y, TRUE);
8844 else
8845 scrolldown(-y, TRUE);
8846 curwin->w_scbind_pos = topline;
8847 redraw_later(VALID);
8848 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 }
8851 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008852 curwin = save_curwin;
8853 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 if (curwin->w_p_scb)
8855 {
8856 did_syncbind = TRUE;
8857 checkpcmark();
8858 if (old_linenr != curwin->w_cursor.lnum)
8859 {
8860 char_u ctrl_o[2];
8861
8862 ctrl_o[0] = Ctrl_O;
8863 ctrl_o[1] = 0;
8864 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8865 }
8866 }
8867#endif
8868}
8869
8870
8871 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008872ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008874 int i;
8875 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8876 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877
8878 if (eap->usefilter) /* :r!cmd */
8879 do_bang(1, eap, FALSE, FALSE, TRUE);
8880 else
8881 {
8882 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8883 return;
8884
8885#ifdef FEAT_BROWSE
8886 if (cmdmod.browse)
8887 {
8888 char_u *browseFile;
8889
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008890 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 NULL, NULL, NULL, curbuf);
8892 if (browseFile != NULL)
8893 {
8894 i = readfile(browseFile, NULL,
8895 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8896 vim_free(browseFile);
8897 }
8898 else
8899 i = OK;
8900 }
8901 else
8902#endif
8903 if (*eap->arg == NUL)
8904 {
8905 if (check_fname() == FAIL) /* check for no file name */
8906 return;
8907 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8908 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8909 }
8910 else
8911 {
8912 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8913 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8914 i = readfile(eap->arg, NULL,
8915 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8916
8917 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008918 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 {
8920#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8921 if (!aborting())
8922#endif
8923 EMSG2(_(e_notopen), eap->arg);
8924 }
8925 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008926 {
8927 if (empty && exmode_active)
8928 {
8929 /* Delete the empty line that remains. Historically ex does
8930 * this but vi doesn't. */
8931 if (eap->line2 == 0)
8932 lnum = curbuf->b_ml.ml_line_count;
8933 else
8934 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008935 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008936 {
8937 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008938 if (curwin->w_cursor.lnum > 1
8939 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008940 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008941 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008942 }
8943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 }
8947}
8948
Bram Moolenaarea408852005-06-25 22:49:46 +00008949static char_u *prev_dir = NULL;
8950
8951#if defined(EXITFREE) || defined(PROTO)
8952 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008953free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008954{
8955 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008956 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008957
8958 vim_free(globaldir);
8959 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008960}
8961#endif
8962
Bram Moolenaarf4258302013-06-02 18:20:17 +02008963/*
8964 * Deal with the side effects of changing the current directory.
8965 * When "local" is TRUE then this was after an ":lcd" command.
8966 */
8967 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008968post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008969{
8970 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008971 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008972 if (local)
8973 {
8974 /* If still in global directory, need to remember current
8975 * directory as global directory. */
8976 if (globaldir == NULL && prev_dir != NULL)
8977 globaldir = vim_strsave(prev_dir);
8978 /* Remember this local directory for the window. */
8979 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8980 curwin->w_localdir = vim_strsave(NameBuff);
8981 }
8982 else
8983 {
8984 /* We are now in the global directory, no need to remember its
8985 * name. */
8986 vim_free(globaldir);
8987 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008988 }
8989
8990 shorten_fnames(TRUE);
8991}
8992
Bram Moolenaarea408852005-06-25 22:49:46 +00008993
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994/*
8995 * ":cd", ":lcd", ":chdir" and ":lchdir".
8996 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008997 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008998ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 char_u *new_dir;
9001 char_u *tofree;
9002
9003 new_dir = eap->arg;
9004#if !defined(UNIX) && !defined(VMS)
9005 /* for non-UNIX ":cd" means: print current directory */
9006 if (*new_dir == NUL)
9007 ex_pwd(NULL);
9008 else
9009#endif
9010 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009011#ifdef FEAT_AUTOCMD
9012 if (allbuf_locked())
9013 return;
9014#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009015 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9016 && !eap->forceit)
9017 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009018 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009019 return;
9020 }
9021
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 /* ":cd -": Change to previous directory */
9023 if (STRCMP(new_dir, "-") == 0)
9024 {
9025 if (prev_dir == NULL)
9026 {
9027 EMSG(_("E186: No previous directory"));
9028 return;
9029 }
9030 new_dir = prev_dir;
9031 }
9032
9033 /* Save current directory for next ":cd -" */
9034 tofree = prev_dir;
9035 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9036 prev_dir = vim_strsave(NameBuff);
9037 else
9038 prev_dir = NULL;
9039
9040#if defined(UNIX) || defined(VMS)
9041 /* for UNIX ":cd" means: go to home directory */
9042 if (*new_dir == NUL)
9043 {
9044 /* use NameBuff for home directory name */
9045# ifdef VMS
9046 char_u *p;
9047
9048 p = mch_getenv((char_u *)"SYS$LOGIN");
9049 if (p == NULL || *p == NUL) /* empty is the same as not set */
9050 NameBuff[0] = NUL;
9051 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009052 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053# else
9054 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9055# endif
9056 new_dir = NameBuff;
9057 }
9058#endif
9059 if (new_dir == NULL || vim_chdir(new_dir))
9060 EMSG(_(e_failed));
9061 else
9062 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009063 int is_local_chdir = eap->cmdidx == CMD_lcd
9064 || eap->cmdidx == CMD_lchdir;
9065
9066 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067
9068 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009069 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 ex_pwd(eap);
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009071#ifdef FEAT_AUTOCMD
9072 apply_autocmds(EVENT_DIRCHANGED,
9073 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9074 new_dir, FALSE, curbuf);
9075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 }
9077 vim_free(tofree);
9078 }
9079}
9080
9081/*
9082 * ":pwd".
9083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009085ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086{
9087 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9088 {
9089#ifdef BACKSLASH_IN_FILENAME
9090 slash_adjust(NameBuff);
9091#endif
9092 msg(NameBuff);
9093 }
9094 else
9095 EMSG(_("E187: Unknown"));
9096}
9097
9098/*
9099 * ":=".
9100 */
9101 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009102ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103{
9104 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009105 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106}
9107
9108 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009109ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009111 int n;
9112 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113
9114 if (cursor_valid())
9115 {
9116 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9117 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009118 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009120
9121 len = eap->line2;
9122 switch (*eap->arg)
9123 {
9124 case 'm': break;
9125 case NUL: len *= 1000L; break;
9126 default: EMSG2(_(e_invarg2), eap->arg); return;
9127 }
9128 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129}
9130
9131/*
9132 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9133 */
9134 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009135do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136{
9137 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009138 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139
9140 cursor_on();
9141 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009142 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009144 wait_now = msec - done > 1000L ? 1000L : msec - done;
9145#ifdef FEAT_TIMERS
9146 {
9147 long due_time = check_due_timer();
9148
9149 if (due_time > 0 && due_time < wait_now)
9150 wait_now = due_time;
9151 }
9152#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009153#ifdef FEAT_JOB_CHANNEL
9154 if (has_any_channel() && wait_now > 100L)
9155 wait_now = 100L;
9156#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009157 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009158#ifdef FEAT_JOB_CHANNEL
9159 if (has_any_channel())
9160 ui_breakcheck_force(TRUE);
9161 else
9162#endif
9163 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009164#ifdef MESSAGE_QUEUE
9165 /* Process the netbeans and clientserver messages that may have been
9166 * received in the call to ui_breakcheck() when the GUI is in use. This
9167 * may occur when running a test case. */
9168 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 }
9171}
9172
9173 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009174do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175{
9176 int mode;
9177 char_u *cmdp;
9178
9179 cmdp = eap->cmd;
9180 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9181
9182 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9183 eap->arg, mode, isabbrev))
9184 {
9185 case 1: EMSG(_(e_invarg));
9186 break;
9187 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9188 break;
9189 }
9190}
9191
9192/*
9193 * ":winsize" command (obsolete).
9194 */
9195 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009196ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197{
9198 int w, h;
9199 char_u *arg = eap->arg;
9200 char_u *p;
9201
9202 w = getdigits(&arg);
9203 arg = skipwhite(arg);
9204 p = arg;
9205 h = getdigits(&arg);
9206 if (*p != NUL && *arg == NUL)
9207 set_shellsize(w, h, TRUE);
9208 else
9209 EMSG(_("E465: :winsize requires two number arguments"));
9210}
9211
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009213ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214{
9215 int xchar = NUL;
9216 char_u *p;
9217
9218 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9219 {
9220 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9221 if (eap->arg[1] == NUL)
9222 {
9223 EMSG(_(e_invarg));
9224 return;
9225 }
9226 xchar = eap->arg[1];
9227 p = eap->arg + 2;
9228 }
9229 else
9230 p = eap->arg + 1;
9231
9232 eap->nextcmd = check_nextcmd(p);
9233 p = skipwhite(p);
9234 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9235 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009236 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 {
9238 /* Pass flags on for ":vertical wincmd ]". */
9239 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009240 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9242 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009243 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 }
9245}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246
Bram Moolenaar843ee412004-06-30 16:16:41 +00009247#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248/*
9249 * ":winpos".
9250 */
9251 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009252ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253{
9254 int x, y;
9255 char_u *arg = eap->arg;
9256 char_u *p;
9257
9258 if (*arg == NUL)
9259 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009260# if defined(FEAT_GUI) || defined(MSWIN)
9261# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009263# else
9264 if (mch_get_winpos(&x, &y) != FAIL)
9265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 {
9267 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9268 msg(IObuff);
9269 }
9270 else
9271# endif
9272 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9273 }
9274 else
9275 {
9276 x = getdigits(&arg);
9277 arg = skipwhite(arg);
9278 p = arg;
9279 y = getdigits(&arg);
9280 if (*p == NUL || *arg != NUL)
9281 {
9282 EMSG(_("E466: :winpos requires two number arguments"));
9283 return;
9284 }
9285# ifdef FEAT_GUI
9286 if (gui.in_use)
9287 gui_mch_set_winpos(x, y);
9288 else if (gui.starting)
9289 {
9290 /* Remember the coordinates for when the window is opened. */
9291 gui_win_x = x;
9292 gui_win_y = y;
9293 }
9294# ifdef HAVE_TGETENT
9295 else
9296# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009297# else
9298# ifdef MSWIN
9299 mch_set_winpos(x, y);
9300# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301# endif
9302# ifdef HAVE_TGETENT
9303 if (*T_CWP)
9304 term_set_winpos(x, y);
9305# endif
9306 }
9307}
9308#endif
9309
9310/*
9311 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9312 */
9313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009314ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315{
9316 oparg_T oa;
9317
9318 clear_oparg(&oa);
9319 oa.regname = eap->regname;
9320 oa.start.lnum = eap->line1;
9321 oa.end.lnum = eap->line2;
9322 oa.line_count = eap->line2 - eap->line1 + 1;
9323 oa.motion_type = MLINE;
9324#ifdef FEAT_VIRTUALEDIT
9325 virtual_op = FALSE;
9326#endif
9327 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9328 {
9329 setpcmark();
9330 curwin->w_cursor.lnum = eap->line1;
9331 beginline(BL_SOL | BL_FIX);
9332 }
9333
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009334 if (VIsual_active)
9335 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009336
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 switch (eap->cmdidx)
9338 {
9339 case CMD_delete:
9340 oa.op_type = OP_DELETE;
9341 op_delete(&oa);
9342 break;
9343
9344 case CMD_yank:
9345 oa.op_type = OP_YANK;
9346 (void)op_yank(&oa, FALSE, TRUE);
9347 break;
9348
9349 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009350 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009352 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9353#else
9354 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009356 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 oa.op_type = OP_RSHIFT;
9358 else
9359 oa.op_type = OP_LSHIFT;
9360 op_shift(&oa, FALSE, eap->amount);
9361 break;
9362 }
9363#ifdef FEAT_VIRTUALEDIT
9364 virtual_op = MAYBE;
9365#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009366 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367}
9368
9369/*
9370 * ":put".
9371 */
9372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009373ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374{
9375 /* ":0put" works like ":1put!". */
9376 if (eap->line2 == 0)
9377 {
9378 eap->line2 = 1;
9379 eap->forceit = TRUE;
9380 }
9381 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009382 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9383 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384}
9385
9386/*
9387 * Handle ":copy" and ":move".
9388 */
9389 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009390ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391{
9392 long n;
9393
Bram Moolenaarded27822017-01-02 14:27:34 +01009394 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 if (eap->arg == NULL) /* error detected */
9396 {
9397 eap->nextcmd = NULL;
9398 return;
9399 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009400 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401
9402 /*
9403 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9404 */
9405 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9406 {
9407 EMSG(_(e_invaddr));
9408 return;
9409 }
9410
9411 if (eap->cmdidx == CMD_move)
9412 {
9413 if (do_move(eap->line1, eap->line2, n) == FAIL)
9414 return;
9415 }
9416 else
9417 ex_copy(eap->line1, eap->line2, n);
9418 u_clearline();
9419 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009420 ex_may_print(eap);
9421}
9422
9423/*
9424 * Print the current line if flags were given to the Ex command.
9425 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009426 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009427ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009428{
9429 if (eap->flags != 0)
9430 {
9431 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9432 (eap->flags & EXFLAG_LIST));
9433 ex_no_reprint = TRUE;
9434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435}
9436
9437/*
9438 * ":smagic" and ":snomagic".
9439 */
9440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009441ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442{
9443 int magic_save = p_magic;
9444
9445 p_magic = (eap->cmdidx == CMD_smagic);
9446 do_sub(eap);
9447 p_magic = magic_save;
9448}
9449
9450/*
9451 * ":join".
9452 */
9453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009454ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455{
9456 curwin->w_cursor.lnum = eap->line1;
9457 if (eap->line1 == eap->line2)
9458 {
9459 if (eap->addr_count >= 2) /* :2,2join does nothing */
9460 return;
9461 if (eap->line2 == curbuf->b_ml.ml_line_count)
9462 {
9463 beep_flush();
9464 return;
9465 }
9466 ++eap->line2;
9467 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009468 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009470 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471}
9472
9473/*
9474 * ":[addr]@r" or ":[addr]*r": execute register
9475 */
9476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009477ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478{
9479 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009480 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481
9482 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009483 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484
9485#ifdef USE_ON_FLY_SCROLL
9486 dont_scroll = TRUE; /* disallow scrolling here */
9487#endif
9488
9489 /* get the register name. No name means to use the previous one */
9490 c = *eap->arg;
9491 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9492 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009493 /* Put the register in the typeahead buffer with the "silent" flag. */
9494 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9495 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009496 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 else
9500 {
9501 int save_efr = exec_from_reg;
9502
9503 exec_from_reg = TRUE;
9504
9505 /*
9506 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009507 * Continue until the stuff buffer is empty and all added characters
9508 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009510 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9512
9513 exec_from_reg = save_efr;
9514 }
9515}
9516
9517/*
9518 * ":!".
9519 */
9520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009521ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522{
9523 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9524}
9525
9526/*
9527 * ":undo".
9528 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009530ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009532 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009533 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009534 else
9535 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536}
9537
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009538#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009540ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009541{
9542 char_u hash[UNDO_HASH_SIZE];
9543
9544 u_compute_hash(hash);
9545 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9546}
9547
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009549ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009550{
9551 char_u hash[UNDO_HASH_SIZE];
9552
9553 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009554 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009555}
9556#endif
9557
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558/*
9559 * ":redo".
9560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009562ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563{
9564 u_redo(1);
9565}
9566
9567/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009568 * ":earlier" and ":later".
9569 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009570 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009571ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009572{
9573 long count = 0;
9574 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009575 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009576 char_u *p = eap->arg;
9577
9578 if (*p == NUL)
9579 count = 1;
9580 else if (isdigit(*p))
9581 {
9582 count = getdigits(&p);
9583 switch (*p)
9584 {
9585 case 's': ++p; sec = TRUE; break;
9586 case 'm': ++p; sec = TRUE; count *= 60; break;
9587 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009588 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9589 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009590 }
9591 }
9592
9593 if (*p != NUL)
9594 EMSG2(_(e_invarg2), eap->arg);
9595 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009596 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9597 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009598}
9599
9600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 * ":redir": start/stop redirection.
9602 */
9603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009604ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605{
9606 char *mode;
9607 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009608 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609
Bram Moolenaarba768492016-07-08 15:32:54 +02009610#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009611 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009612 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009613 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009614 return;
9615 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009616#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009617
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 if (STRICMP(eap->arg, "END") == 0)
9619 close_redir();
9620 else
9621 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009622 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009624 ++arg;
9625 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009627 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 mode = "a";
9629 }
9630 else
9631 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009632 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633
9634 close_redir();
9635
9636 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009637 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 if (fname == NULL)
9639 return;
9640#ifdef FEAT_BROWSE
9641 if (cmdmod.browse)
9642 {
9643 char_u *browseFile;
9644
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009645 browseFile = do_browse(BROWSE_SAVE,
9646 (char_u *)_("Save Redirection"),
9647 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 if (browseFile == NULL)
9649 return; /* operation cancelled */
9650 vim_free(fname);
9651 fname = browseFile;
9652 eap->forceit = TRUE; /* since dialog already asked */
9653 }
9654#endif
9655
9656 redir_fd = open_exfile(fname, eap->forceit, mode);
9657 vim_free(fname);
9658 }
9659#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009660 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 {
9662 /* redirect to a register a-z (resp. A-Z for appending) */
9663 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009664 ++arg;
9665 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009667 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009668 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009670 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009672 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009673 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009674 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009675 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009677 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009678 if (*arg == '>')
9679 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009680 /* Make register empty when not using @A-@Z and the
9681 * command is valid. */
9682 if (*arg == NUL && !isupper(redir_reg))
9683 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009685 }
9686 if (*arg != NUL)
9687 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009688 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009689 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009690 }
9691 }
9692 else if (*arg == '=' && arg[1] == '>')
9693 {
9694 int append;
9695
9696 /* redirect to a variable */
9697 close_redir();
9698 arg += 2;
9699
9700 if (*arg == '>')
9701 {
9702 ++arg;
9703 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 }
9705 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009706 append = FALSE;
9707
9708 if (var_redir_start(skipwhite(arg), append) == OK)
9709 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 }
9711#endif
9712
9713 /* TODO: redirect to a buffer */
9714
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009716 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009718
9719 /* Make sure redirection is not off. Can happen for cmdline completion
9720 * that indirectly invokes a command to catch its output. */
9721 if (redir_fd != NULL
9722#ifdef FEAT_EVAL
9723 || redir_reg || redir_vname
9724#endif
9725 )
9726 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727}
9728
9729/*
9730 * ":redraw": force redraw
9731 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009732 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009733ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734{
9735 int r = RedrawingDisabled;
9736 int p = p_lz;
9737
9738 RedrawingDisabled = 0;
9739 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009740 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009742 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743#ifdef FEAT_TITLE
9744 if (need_maketitle)
9745 maketitle();
9746#endif
9747 RedrawingDisabled = r;
9748 p_lz = p;
9749
9750 /* Reset msg_didout, so that a message that's there is overwritten. */
9751 msg_didout = FALSE;
9752 msg_col = 0;
9753
Bram Moolenaar56667a52013-07-17 11:54:28 +02009754 /* No need to wait after an intentional redraw. */
9755 need_wait_return = FALSE;
9756
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 out_flush();
9758}
9759
9760/*
9761 * ":redrawstatus": force redraw of status line(s)
9762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009764ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 int r = RedrawingDisabled;
9767 int p = p_lz;
9768
9769 RedrawingDisabled = 0;
9770 p_lz = FALSE;
9771 if (eap->forceit)
9772 status_redraw_all();
9773 else
9774 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009775 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 RedrawingDisabled = r;
9777 p_lz = p;
9778 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779}
9780
9781 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009782close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783{
9784 if (redir_fd != NULL)
9785 {
9786 fclose(redir_fd);
9787 redir_fd = NULL;
9788 }
9789#ifdef FEAT_EVAL
9790 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009791 if (redir_vname)
9792 {
9793 var_redir_stop();
9794 redir_vname = 0;
9795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796#endif
9797}
9798
9799#if defined(FEAT_SESSION) && defined(USE_CRNL)
9800# define MKSESSION_NL
9801static int mksession_nl = FALSE; /* use NL only in put_eol() */
9802#endif
9803
9804/*
9805 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9806 */
9807 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009808ex_mkrc(
9809 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810{
9811 FILE *fd;
9812 int failed = FALSE;
9813 char_u *fname;
9814#ifdef FEAT_BROWSE
9815 char_u *browseFile = NULL;
9816#endif
9817#ifdef FEAT_SESSION
9818 int view_session = FALSE;
9819 int using_vdir = FALSE; /* using 'viewdir'? */
9820 char_u *viewFile = NULL;
9821 unsigned *flagp;
9822#endif
9823
9824 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9825 {
9826#ifdef FEAT_SESSION
9827 view_session = TRUE;
9828#else
9829 ex_ni(eap);
9830 return;
9831#endif
9832 }
9833
9834#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009835 /* Use the short file name until ":lcd" is used. We also don't use the
9836 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009837 did_lcd = FALSE;
9838
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9840 if (eap->cmdidx == CMD_mkview
9841 && (*eap->arg == NUL
9842 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9843 {
9844 eap->forceit = TRUE;
9845 fname = get_view_file(*eap->arg);
9846 if (fname == NULL)
9847 return;
9848 viewFile = fname;
9849 using_vdir = TRUE;
9850 }
9851 else
9852#endif
9853 if (*eap->arg != NUL)
9854 fname = eap->arg;
9855 else if (eap->cmdidx == CMD_mkvimrc)
9856 fname = (char_u *)VIMRC_FILE;
9857#ifdef FEAT_SESSION
9858 else if (eap->cmdidx == CMD_mksession)
9859 fname = (char_u *)SESSION_FILE;
9860#endif
9861 else
9862 fname = (char_u *)EXRC_FILE;
9863
9864#ifdef FEAT_BROWSE
9865 if (cmdmod.browse)
9866 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009867 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868# ifdef FEAT_SESSION
9869 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9870 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9871# endif
9872 (char_u *)_("Save Setup"),
9873 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9874 if (browseFile == NULL)
9875 goto theend;
9876 fname = browseFile;
9877 eap->forceit = TRUE; /* since dialog already asked */
9878 }
9879#endif
9880
9881#if defined(FEAT_SESSION) && defined(vim_mkdir)
9882 /* When using 'viewdir' may have to create the directory. */
9883 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009884 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885#endif
9886
9887 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9888 if (fd != NULL)
9889 {
9890#ifdef FEAT_SESSION
9891 if (eap->cmdidx == CMD_mkview)
9892 flagp = &vop_flags;
9893 else
9894 flagp = &ssop_flags;
9895#endif
9896
9897#ifdef MKSESSION_NL
9898 /* "unix" in 'sessionoptions': use NL line separator */
9899 if (view_session && (*flagp & SSOP_UNIX))
9900 mksession_nl = TRUE;
9901#endif
9902
9903 /* Write the version command for :mkvimrc */
9904 if (eap->cmdidx == CMD_mkvimrc)
9905 (void)put_line(fd, "version 6.0");
9906
9907#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009908 if (eap->cmdidx == CMD_mksession)
9909 {
9910 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9911 failed = TRUE;
9912 }
9913
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 if (eap->cmdidx != CMD_mkview)
9915#endif
9916 {
9917 /* Write setting 'compatible' first, because it has side effects.
9918 * For that same reason only do it when needed. */
9919 if (p_cp)
9920 (void)put_line(fd, "if !&cp | set cp | endif");
9921 else
9922 (void)put_line(fd, "if &cp | set nocp | endif");
9923 }
9924
9925#ifdef FEAT_SESSION
9926 if (!view_session
9927 || (eap->cmdidx == CMD_mksession
9928 && (*flagp & SSOP_OPTIONS)))
9929#endif
9930 failed |= (makemap(fd, NULL) == FAIL
9931 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9932
9933#ifdef FEAT_SESSION
9934 if (!failed && view_session)
9935 {
9936 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9937 failed = TRUE;
9938 if (eap->cmdidx == CMD_mksession)
9939 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009940 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941
Bram Moolenaard9462e32011-04-11 21:35:11 +02009942 dirnow = alloc(MAXPATHL);
9943 if (dirnow == NULL)
9944 failed = TRUE;
9945 else
9946 {
9947 /*
9948 * Change to session file's dir.
9949 */
9950 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009952 *dirnow = NUL;
9953 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9954 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009955 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009956 shorten_fnames(TRUE);
9957 }
9958 else if (*dirnow != NUL
9959 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9960 {
9961 if (mch_chdir((char *)globaldir) == 0)
9962 shorten_fnames(TRUE);
9963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964
Bram Moolenaard9462e32011-04-11 21:35:11 +02009965 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966
Bram Moolenaard9462e32011-04-11 21:35:11 +02009967 /* restore original dir */
9968 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009970 {
9971 if (mch_chdir((char *)dirnow) != 0)
9972 EMSG(_(e_prev_dir));
9973 shorten_fnames(TRUE);
9974 }
9975 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 }
9977 }
9978 else
9979 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009980 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9981 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 }
9983 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9984 == FAIL)
9985 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009986 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9987 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009988 if (eap->cmdidx == CMD_mksession)
9989 {
9990 if (put_line(fd, "unlet SessionLoad") == FAIL)
9991 failed = TRUE;
9992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 }
9994#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009995 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9996 failed = TRUE;
9997
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 failed |= fclose(fd);
9999
10000 if (failed)
10001 EMSG(_(e_write));
10002#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10003 else if (eap->cmdidx == CMD_mksession)
10004 {
10005 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010006 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007
Bram Moolenaard9462e32011-04-11 21:35:11 +020010008 tbuf = alloc(MAXPATHL);
10009 if (tbuf != NULL)
10010 {
10011 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10012 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10013 vim_free(tbuf);
10014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 }
10016#endif
10017#ifdef MKSESSION_NL
10018 mksession_nl = FALSE;
10019#endif
10020 }
10021
10022#ifdef FEAT_BROWSE
10023theend:
10024 vim_free(browseFile);
10025#endif
10026#ifdef FEAT_SESSION
10027 vim_free(viewFile);
10028#endif
10029}
10030
Bram Moolenaardf177f62005-02-22 08:39:57 +000010031#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10032 || defined(PROTO)
10033 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010034vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010035{
10036 if (vim_mkdir(name, prot) != 0)
10037 {
10038 EMSG2(_("E739: Cannot create directory: %s"), name);
10039 return FAIL;
10040 }
10041 return OK;
10042}
10043#endif
10044
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045/*
10046 * Open a file for writing for an Ex command, with some checks.
10047 * Return file descriptor, or NULL on failure.
10048 */
10049 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010050open_exfile(
10051 char_u *fname,
10052 int forceit,
10053 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054{
10055 FILE *fd;
10056
10057#ifdef UNIX
10058 /* with Unix it is possible to open a directory */
10059 if (mch_isdir(fname))
10060 {
10061 EMSG2(_(e_isadir2), fname);
10062 return NULL;
10063 }
10064#endif
10065 if (!forceit && *mode != 'a' && vim_fexists(fname))
10066 {
10067 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10068 return NULL;
10069 }
10070
10071 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10072 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10073
10074 return fd;
10075}
10076
10077/*
10078 * ":mark" and ":k".
10079 */
10080 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010081ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082{
10083 pos_T pos;
10084
10085 if (*eap->arg == NUL) /* No argument? */
10086 EMSG(_(e_argreq));
10087 else if (eap->arg[1] != NUL) /* more than one character? */
10088 EMSG(_(e_trailing));
10089 else
10090 {
10091 pos = curwin->w_cursor; /* save curwin->w_cursor */
10092 curwin->w_cursor.lnum = eap->line2;
10093 beginline(BL_WHITE | BL_FIX);
10094 if (setmark(*eap->arg) == FAIL) /* set mark */
10095 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10096 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10097 }
10098}
10099
10100/*
10101 * Update w_topline, w_leftcol and the cursor position.
10102 */
10103 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010104update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105{
10106 check_cursor(); /* put cursor on valid line */
10107 update_topline();
10108 if (!curwin->w_p_wrap)
10109 validate_cursor();
10110 update_curswant();
10111}
10112
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010114 * Save the current State and go to Normal mode.
10115 * Return TRUE if the typeahead could be saved.
10116 */
10117 int
10118save_current_state(save_state_T *sst)
10119{
10120 sst->save_msg_scroll = msg_scroll;
10121 sst->save_restart_edit = restart_edit;
10122 sst->save_msg_didout = msg_didout;
10123 sst->save_State = State;
10124 sst->save_insertmode = p_im;
10125 sst->save_finish_op = finish_op;
10126 sst->save_opcount = opcount;
10127
10128 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10129 restart_edit = 0; /* don't go to Insert mode */
10130 p_im = FALSE; /* don't use 'insertmode' */
10131
10132 /*
10133 * Save the current typeahead. This is required to allow using ":normal"
10134 * from an event handler and makes sure we don't hang when the argument
10135 * ends with half a command.
10136 */
10137 save_typeahead(&sst->tabuf);
10138 return sst->tabuf.typebuf_valid;
10139}
10140
10141 void
10142restore_current_state(save_state_T *sst)
10143{
10144 /* Restore the previous typeahead. */
10145 restore_typeahead(&sst->tabuf);
10146
10147 msg_scroll = sst->save_msg_scroll;
10148 restart_edit = sst->save_restart_edit;
10149 p_im = sst->save_insertmode;
10150 finish_op = sst->save_finish_op;
10151 opcount = sst->save_opcount;
10152 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10153
10154 /* Restore the state (needed when called from a function executed for
10155 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10156 State = sst->save_State;
10157#ifdef CURSOR_SHAPE
10158 ui_cursor_shape(); /* may show different cursor shape */
10159#endif
10160}
10161
10162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 * ":normal[!] {commands}": Execute normal mode commands.
10164 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010165 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010166ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010168 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169#ifdef FEAT_MBYTE
10170 char_u *arg = NULL;
10171 int l;
10172 char_u *p;
10173#endif
10174
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010175 if (ex_normal_lock > 0)
10176 {
10177 EMSG(_(e_secure));
10178 return;
10179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 if (ex_normal_busy >= p_mmd)
10181 {
10182 EMSG(_("E192: Recursive use of :normal too deep"));
10183 return;
10184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185
10186#ifdef FEAT_MBYTE
10187 /*
10188 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10189 * this for the K_SPECIAL leading byte, otherwise special keys will not
10190 * work.
10191 */
10192 if (has_mbyte)
10193 {
10194 int len = 0;
10195
10196 /* Count the number of characters to be escaped. */
10197 for (p = eap->arg; *p != NUL; ++p)
10198 {
10199# ifdef FEAT_GUI
10200 if (*p == CSI) /* leadbyte CSI */
10201 len += 2;
10202# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010203 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10205# ifdef FEAT_GUI
10206 || *p == CSI
10207# endif
10208 )
10209 len += 2;
10210 }
10211 if (len > 0)
10212 {
10213 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10214 if (arg != NULL)
10215 {
10216 len = 0;
10217 for (p = eap->arg; *p != NUL; ++p)
10218 {
10219 arg[len++] = *p;
10220# ifdef FEAT_GUI
10221 if (*p == CSI)
10222 {
10223 arg[len++] = KS_EXTRA;
10224 arg[len++] = (int)KE_CSI;
10225 }
10226# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010227 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 {
10229 arg[len++] = *++p;
10230 if (*p == K_SPECIAL)
10231 {
10232 arg[len++] = KS_SPECIAL;
10233 arg[len++] = KE_FILLER;
10234 }
10235# ifdef FEAT_GUI
10236 else if (*p == CSI)
10237 {
10238 arg[len++] = KS_EXTRA;
10239 arg[len++] = (int)KE_CSI;
10240 }
10241# endif
10242 }
10243 arg[len] = NUL;
10244 }
10245 }
10246 }
10247 }
10248#endif
10249
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010250 ++ex_normal_busy;
10251 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 {
10253 /*
10254 * Repeat the :normal command for each line in the range. When no
10255 * range given, execute it just once, without positioning the cursor
10256 * first.
10257 */
10258 do
10259 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 if (eap->addr_count != 0)
10261 {
10262 curwin->w_cursor.lnum = eap->line1++;
10263 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010264 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 }
10266
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010267 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268#ifdef FEAT_MBYTE
10269 arg != NULL ? arg :
10270#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010271 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 }
10273 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10274 }
10275
10276 /* Might not return to the main loop when in an event handler. */
10277 update_topline_cursor();
10278
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010279 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010281#ifdef FEAT_MOUSE
10282 setmouse();
10283#endif
10284#ifdef CURSOR_SHAPE
10285 ui_cursor_shape(); /* may show different cursor shape */
10286#endif
10287
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288#ifdef FEAT_MBYTE
10289 vim_free(arg);
10290#endif
10291}
10292
10293/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010294 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 */
10296 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010297ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010299 if (eap->forceit)
10300 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010301 /* cursor line can be zero on startup */
10302 if (!curwin->w_cursor.lnum)
10303 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010304 coladvance((colnr_T)MAXCOL);
10305 curwin->w_curswant = MAXCOL;
10306 curwin->w_set_curswant = FALSE;
10307 }
10308
Bram Moolenaara40c5002005-01-09 21:16:21 +000010309 /* Ignore the command when already in Insert mode. Inserting an
10310 * expression register that invokes a function can do this. */
10311 if (State & INSERT)
10312 return;
10313
Bram Moolenaar64969662005-12-14 21:59:55 +000010314 if (eap->cmdidx == CMD_startinsert)
10315 restart_edit = 'a';
10316 else if (eap->cmdidx == CMD_startreplace)
10317 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010319 restart_edit = 'V';
10320
10321 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010323 if (eap->cmdidx == CMD_startinsert)
10324 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 curwin->w_curswant = 0; /* avoid MAXCOL */
10326 }
10327}
10328
10329/*
10330 * ":stopinsert"
10331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010333ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334{
10335 restart_edit = 0;
10336 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010337 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010340/*
10341 * Execute normal mode command "cmd".
10342 * "remap" can be REMAP_NONE or REMAP_YES.
10343 */
10344 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010345exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010346{
Bram Moolenaar25281632016-01-21 23:32:32 +010010347 /* Stuff the argument into the typeahead buffer. */
10348 ins_typebuf(cmd, remap, 0, TRUE, silent);
10349 exec_normal(FALSE);
10350}
Bram Moolenaar25281632016-01-21 23:32:32 +010010351
Bram Moolenaar25281632016-01-21 23:32:32 +010010352/*
10353 * Execute normal_cmd() until there is no typeahead left.
10354 */
10355 void
10356exec_normal(int was_typed)
10357{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010358 oparg_T oa;
10359
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010360 clear_oparg(&oa);
10361 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010362 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10363 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010364 {
10365 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010366 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010367 }
10368}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010369
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370#ifdef FEAT_FIND_ID
10371 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010372ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373{
10374 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10375 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10376 (linenr_T)1, (linenr_T)MAXLNUM);
10377}
10378
Bram Moolenaar4033c552017-09-16 20:54:51 +020010379#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380/*
10381 * ":psearch"
10382 */
10383 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010384ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385{
10386 g_do_tagpreview = p_pvh;
10387 ex_findpat(eap);
10388 g_do_tagpreview = 0;
10389}
10390#endif
10391
10392 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010393ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394{
10395 int whole = TRUE;
10396 long n;
10397 char_u *p;
10398 int action;
10399
10400 switch (cmdnames[eap->cmdidx].cmd_name[2])
10401 {
10402 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10403 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10404 action = ACTION_GOTO;
10405 else
10406 action = ACTION_SHOW;
10407 break;
10408 case 'i': /* ":ilist" and ":dlist" */
10409 action = ACTION_SHOW_ALL;
10410 break;
10411 case 'u': /* ":ijump" and ":djump" */
10412 action = ACTION_GOTO;
10413 break;
10414 default: /* ":isplit" and ":dsplit" */
10415 action = ACTION_SPLIT;
10416 break;
10417 }
10418
10419 n = 1;
10420 if (vim_isdigit(*eap->arg)) /* get count */
10421 {
10422 n = getdigits(&eap->arg);
10423 eap->arg = skipwhite(eap->arg);
10424 }
10425 if (*eap->arg == '/') /* Match regexp, not just whole words */
10426 {
10427 whole = FALSE;
10428 ++eap->arg;
10429 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10430 if (*p)
10431 {
10432 *p++ = NUL;
10433 p = skipwhite(p);
10434
10435 /* Check for trailing illegal characters */
10436 if (!ends_excmd(*p))
10437 eap->errmsg = e_trailing;
10438 else
10439 eap->nextcmd = check_nextcmd(p);
10440 }
10441 }
10442 if (!eap->skip)
10443 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10444 whole, !eap->forceit,
10445 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10446 n, action, eap->line1, eap->line2);
10447}
10448#endif
10449
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450
Bram Moolenaar4033c552017-09-16 20:54:51 +020010451#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452/*
10453 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10454 */
10455 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010456ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010458 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10460}
10461
10462/*
10463 * ":pedit"
10464 */
10465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010466ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467{
10468 win_T *curwin_save = curwin;
10469
10470 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010471 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010472 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 do_exedit(eap, NULL);
10474 keep_help_flag = FALSE;
10475 if (curwin != curwin_save && win_valid(curwin_save))
10476 {
10477 /* Return cursor to where we were */
10478 validate_cursor();
10479 redraw_later(VALID);
10480 win_enter(curwin_save, TRUE);
10481 }
10482 g_do_tagpreview = 0;
10483}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485
10486/*
10487 * ":stag", ":stselect" and ":stjump".
10488 */
10489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010490ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491{
10492 postponed_split = -1;
10493 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010494 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10496 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010497 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499
10500/*
10501 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10502 */
10503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010504ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505{
10506 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10507}
10508
10509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010510ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511{
10512 int cmd;
10513
10514 switch (name[1])
10515 {
10516 case 'j': cmd = DT_JUMP; /* ":tjump" */
10517 break;
10518 case 's': cmd = DT_SELECT; /* ":tselect" */
10519 break;
10520 case 'p': cmd = DT_PREV; /* ":tprevious" */
10521 break;
10522 case 'N': cmd = DT_PREV; /* ":tNext" */
10523 break;
10524 case 'n': cmd = DT_NEXT; /* ":tnext" */
10525 break;
10526 case 'o': cmd = DT_POP; /* ":pop" */
10527 break;
10528 case 'f': /* ":tfirst" */
10529 case 'r': cmd = DT_FIRST; /* ":trewind" */
10530 break;
10531 case 'l': cmd = DT_LAST; /* ":tlast" */
10532 break;
10533 default: /* ":tag" */
10534#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010535 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010537 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 return;
10539 }
10540#endif
10541 cmd = DT_TAG;
10542 break;
10543 }
10544
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010545 if (name[0] == 'l')
10546 {
10547#ifndef FEAT_QUICKFIX
10548 ex_ni(eap);
10549 return;
10550#else
10551 cmd = DT_LTAG;
10552#endif
10553 }
10554
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10556 eap->forceit, TRUE);
10557}
10558
10559/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010560 * Check "str" for starting with a special cmdline variable.
10561 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10562 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10563 */
10564 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010565find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010566{
10567 int len;
10568 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010569 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010570 "%",
10571#define SPEC_PERC 0
10572 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010573#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010574 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010575#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010576 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010577#define SPEC_CCWORD (SPEC_CWORD + 1)
10578 "<cexpr>", /* expr under cursor */
10579#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010580 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010581#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010582 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010583#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010584 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010585#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010586#ifdef FEAT_AUTOCMD
10587 "<afile>", /* autocommand file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010588# define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010589 "<abuf>", /* autocommand buffer number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010590# define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010591 "<amatch>", /* autocommand match name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010592# define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010593#endif
10594#ifdef FEAT_CLIENTSERVER
10595 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010596# ifdef FEAT_AUTOCMD
Bram Moolenaar65f08472017-09-10 18:16:20 +020010597# define SPEC_CLIENT (SPEC_AMATCH + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010598# else
Bram Moolenaar65f08472017-09-10 18:16:20 +020010599# define SPEC_CLIENT (SPEC_SLNUM + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010600# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010601#endif
10602 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010603
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010604 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010605 {
10606 len = (int)STRLEN(spec_str[i]);
10607 if (STRNCMP(src, spec_str[i], len) == 0)
10608 {
10609 *usedlen = len;
10610 return i;
10611 }
10612 }
10613 return -1;
10614}
10615
10616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 * Evaluate cmdline variables.
10618 *
10619 * change '%' to curbuf->b_ffname
10620 * '#' to curwin->w_altfile
10621 * '<cword>' to word under the cursor
10622 * '<cWORD>' to WORD under the cursor
10623 * '<cfile>' to path name under the cursor
10624 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010625 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 * '<afile>' to file name for autocommand
10627 * '<abuf>' to buffer number for autocommand
10628 * '<amatch>' to matching name for autocommand
10629 *
10630 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10631 * "" for error without a message) and NULL is returned.
10632 * Returns an allocated string if a valid match was found.
10633 * Returns NULL if no match was found. "usedlen" then still contains the
10634 * number of characters to skip.
10635 */
10636 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010637eval_vars(
10638 char_u *src, /* pointer into commandline */
10639 char_u *srcstart, /* beginning of valid memory for src */
10640 int *usedlen, /* characters after src that are used */
10641 linenr_T *lnump, /* line number for :e command, or NULL */
10642 char_u **errormsg, /* pointer to error message */
10643 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010644 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645{
10646 int i;
10647 char_u *s;
10648 char_u *result;
10649 char_u *resultbuf = NULL;
10650 int resultlen;
10651 buf_T *buf;
10652 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10653 int spec_idx;
10654#ifdef FEAT_MODIFY_FNAME
10655 int skip_mod = FALSE;
10656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658
10659 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010660 if (escaped != NULL)
10661 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662
10663 /*
10664 * Check if there is something to do.
10665 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010666 spec_idx = find_cmdline_var(src, usedlen);
10667 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 {
10669 *usedlen = 1;
10670 return NULL;
10671 }
10672
10673 /*
10674 * Skip when preceded with a backslash "\%" and "\#".
10675 * Note: In "\\%" the % is also not recognized!
10676 */
10677 if (src > srcstart && src[-1] == '\\')
10678 {
10679 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010680 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 return NULL;
10682 }
10683
10684 /*
10685 * word or WORD under cursor
10686 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010687 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10688 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010690 resultlen = find_ident_under_cursor(&result,
10691 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10692 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10693 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 if (resultlen == 0)
10695 {
10696 *errormsg = (char_u *)"";
10697 return NULL;
10698 }
10699 }
10700
10701 /*
10702 * '#': Alternate file name
10703 * '%': Current file name
10704 * File name under the cursor
10705 * File name for autocommand
10706 * and following modifiers
10707 */
10708 else
10709 {
10710 switch (spec_idx)
10711 {
10712 case SPEC_PERC: /* '%': current file */
10713 if (curbuf->b_fname == NULL)
10714 {
10715 result = (char_u *)"";
10716 valid = 0; /* Must have ":p:h" to be valid */
10717 }
10718 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 break;
10721
10722 case SPEC_HASH: /* '#' or "#99": alternate file */
10723 if (src[1] == '#') /* "##": the argument list */
10724 {
10725 result = arg_all();
10726 resultbuf = result;
10727 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010728 if (escaped != NULL)
10729 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730#ifdef FEAT_MODIFY_FNAME
10731 skip_mod = TRUE;
10732#endif
10733 break;
10734 }
10735 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010736 if (*s == '<') /* "#<99" uses v:oldfiles */
10737 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010739 if (s == src + 2 && src[1] == '-')
10740 /* just a minus sign, don't skip over it */
10741 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 *usedlen = (int)(s - src); /* length of what we expand */
10743
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010744 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010746 if (*usedlen < 2)
10747 {
10748 /* Should we give an error message for #<text? */
10749 *usedlen = 1;
10750 return NULL;
10751 }
10752#ifdef FEAT_EVAL
10753 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10754 (long)i);
10755 if (result == NULL)
10756 {
10757 *errormsg = (char_u *)"";
10758 return NULL;
10759 }
10760#else
10761 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 }
10765 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010766 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010767 if (i == 0 && src[1] == '<' && *usedlen > 1)
10768 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010769 buf = buflist_findnr(i);
10770 if (buf == NULL)
10771 {
10772 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10773 return NULL;
10774 }
10775 if (lnump != NULL)
10776 *lnump = ECMD_LAST;
10777 if (buf->b_fname == NULL)
10778 {
10779 result = (char_u *)"";
10780 valid = 0; /* Must have ":p:h" to be valid */
10781 }
10782 else
10783 result = buf->b_fname;
10784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 break;
10786
10787#ifdef FEAT_SEARCHPATH
10788 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010789 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 if (result == NULL)
10791 {
10792 *errormsg = (char_u *)"";
10793 return NULL;
10794 }
10795 resultbuf = result; /* remember allocated string */
10796 break;
10797#endif
10798
10799#ifdef FEAT_AUTOCMD
10800 case SPEC_AFILE: /* file name for autocommand */
10801 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010802 if (result != NULL && !autocmd_fname_full)
10803 {
10804 /* Still need to turn the fname into a full path. It is
10805 * postponed to avoid a delay when <afile> is not used. */
10806 autocmd_fname_full = TRUE;
10807 result = FullName_save(autocmd_fname, FALSE);
10808 vim_free(autocmd_fname);
10809 autocmd_fname = result;
10810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 if (result == NULL)
10812 {
10813 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10814 return NULL;
10815 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010816 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 break;
10818
10819 case SPEC_ABUF: /* buffer number for autocommand */
10820 if (autocmd_bufnr <= 0)
10821 {
10822 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10823 return NULL;
10824 }
10825 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10826 result = strbuf;
10827 break;
10828
10829 case SPEC_AMATCH: /* match name for autocommand */
10830 result = autocmd_match;
10831 if (result == NULL)
10832 {
10833 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10834 return NULL;
10835 }
10836 break;
10837
10838#endif
10839 case SPEC_SFILE: /* file name for ":so" command */
10840 result = sourcing_name;
10841 if (result == NULL)
10842 {
10843 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10844 return NULL;
10845 }
10846 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010847 case SPEC_SLNUM: /* line in file for ":so" command */
10848 if (sourcing_name == NULL || sourcing_lnum == 0)
10849 {
10850 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10851 return NULL;
10852 }
10853 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10854 result = strbuf;
10855 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856#if defined(FEAT_CLIENTSERVER)
10857 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010858 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10859 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860 result = strbuf;
10861 break;
10862#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010863 default:
10864 result = (char_u *)""; /* avoid gcc warning */
10865 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 }
10867
10868 resultlen = (int)STRLEN(result); /* length of new string */
10869 if (src[*usedlen] == '<') /* remove the file name extension */
10870 {
10871 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 resultlen = (int)(s - result);
10874 }
10875#ifdef FEAT_MODIFY_FNAME
10876 else if (!skip_mod)
10877 {
10878 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10879 &resultlen);
10880 if (result == NULL)
10881 {
10882 *errormsg = (char_u *)"";
10883 return NULL;
10884 }
10885 }
10886#endif
10887 }
10888
10889 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10890 {
10891 if (valid != VALID_HEAD + VALID_PATH)
10892 /* xgettext:no-c-format */
10893 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10894 else
10895 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10896 result = NULL;
10897 }
10898 else
10899 result = vim_strnsave(result, resultlen);
10900 vim_free(resultbuf);
10901 return result;
10902}
10903
10904/*
10905 * Concatenate all files in the argument list, separated by spaces, and return
10906 * it in one allocated string.
10907 * Spaces and backslashes in the file names are escaped with a backslash.
10908 * Returns NULL when out of memory.
10909 */
10910 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010911arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912{
10913 int len;
10914 int idx;
10915 char_u *retval = NULL;
10916 char_u *p;
10917
10918 /*
10919 * Do this loop two times:
10920 * first time: compute the total length
10921 * second time: concatenate the names
10922 */
10923 for (;;)
10924 {
10925 len = 0;
10926 for (idx = 0; idx < ARGCOUNT; ++idx)
10927 {
10928 p = alist_name(&ARGLIST[idx]);
10929 if (p != NULL)
10930 {
10931 if (len > 0)
10932 {
10933 /* insert a space in between names */
10934 if (retval != NULL)
10935 retval[len] = ' ';
10936 ++len;
10937 }
10938 for ( ; *p != NUL; ++p)
10939 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010940 if (*p == ' '
10941#ifndef BACKSLASH_IN_FILENAME
10942 || *p == '\\'
10943#endif
10944 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 {
10946 /* insert a backslash */
10947 if (retval != NULL)
10948 retval[len] = '\\';
10949 ++len;
10950 }
10951 if (retval != NULL)
10952 retval[len] = *p;
10953 ++len;
10954 }
10955 }
10956 }
10957
10958 /* second time: break here */
10959 if (retval != NULL)
10960 {
10961 retval[len] = NUL;
10962 break;
10963 }
10964
10965 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010966 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 if (retval == NULL)
10968 break;
10969 }
10970
10971 return retval;
10972}
10973
10974#if defined(FEAT_AUTOCMD) || defined(PROTO)
10975/*
10976 * Expand the <sfile> string in "arg".
10977 *
10978 * Returns an allocated string, or NULL for any error.
10979 */
10980 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010981expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982{
10983 char_u *errormsg;
10984 int len;
10985 char_u *result;
10986 char_u *newres;
10987 char_u *repl;
10988 int srclen;
10989 char_u *p;
10990
10991 result = vim_strsave(arg);
10992 if (result == NULL)
10993 return NULL;
10994
10995 for (p = result; *p; )
10996 {
10997 if (STRNCMP(p, "<sfile>", 7) != 0)
10998 ++p;
10999 else
11000 {
11001 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011002 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 if (errormsg != NULL)
11004 {
11005 if (*errormsg)
11006 emsg(errormsg);
11007 vim_free(result);
11008 return NULL;
11009 }
11010 if (repl == NULL) /* no match (cannot happen) */
11011 {
11012 p += srclen;
11013 continue;
11014 }
11015 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11016 newres = alloc(len);
11017 if (newres == NULL)
11018 {
11019 vim_free(repl);
11020 vim_free(result);
11021 return NULL;
11022 }
11023 mch_memmove(newres, result, (size_t)(p - result));
11024 STRCPY(newres + (p - result), repl);
11025 len = (int)STRLEN(newres);
11026 STRCAT(newres, p + srclen);
11027 vim_free(repl);
11028 vim_free(result);
11029 result = newres;
11030 p = newres + len; /* continue after the match */
11031 }
11032 }
11033
11034 return result;
11035}
11036#endif
11037
11038#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011039static int ses_winsizes(FILE *fd, int restore_size,
11040 win_T *tab_firstwin);
11041static int ses_win_rec(FILE *fd, frame_T *fr);
11042static frame_T *ses_skipframe(frame_T *fr);
11043static int ses_do_frame(frame_T *fr);
11044static int ses_do_win(win_T *wp);
11045static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11046static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011047static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048
11049/*
11050 * Write openfile commands for the current buffers to an .exrc file.
11051 * Return FAIL on error, OK otherwise.
11052 */
11053 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011054makeopens(
11055 FILE *fd,
11056 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057{
11058 buf_T *buf;
11059 int only_save_windows = TRUE;
11060 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 int restore_size = TRUE;
11062 win_T *wp;
11063 char_u *sname;
11064 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011065 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011066 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011067 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011068 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011069 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011070 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071
11072 if (ssop_flags & SSOP_BUFFERS)
11073 only_save_windows = FALSE; /* Save ALL buffers */
11074
11075 /*
11076 * Begin by setting the this_session variable, and then other
11077 * sessionable variables.
11078 */
11079#ifdef FEAT_EVAL
11080 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11081 return FAIL;
11082 if (ssop_flags & SSOP_GLOBALS)
11083 if (store_session_globals(fd) == FAIL)
11084 return FAIL;
11085#endif
11086
11087 /*
11088 * Close all windows but one.
11089 */
11090 if (put_line(fd, "silent only") == FAIL)
11091 return FAIL;
11092
11093 /*
11094 * Now a :cd command to the session directory or the current directory
11095 */
11096 if (ssop_flags & SSOP_SESDIR)
11097 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011098 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11099 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 return FAIL;
11101 }
11102 else if (ssop_flags & SSOP_CURDIR)
11103 {
11104 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11105 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011106 || fputs("cd ", fd) < 0
11107 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11108 || put_eol(fd) == FAIL)
11109 {
11110 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113 vim_free(sname);
11114 }
11115
11116 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011117 * If there is an empty, unnamed buffer we will wipe it out later.
11118 * Remember the buffer number.
11119 */
11120 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11121 return FAIL;
11122 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11123 return FAIL;
11124 if (put_line(fd, "endif") == FAIL)
11125 return FAIL;
11126
11127 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128 * Now save the current files, current buffer first.
11129 */
11130 if (put_line(fd, "set shortmess=aoO") == FAIL)
11131 return FAIL;
11132
11133 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011134 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135 {
11136 if (!(only_save_windows && buf->b_nwindows == 0)
11137 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11138 && buf->b_fname != NULL
11139 && buf->b_p_bl)
11140 {
11141 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11142 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011143 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 return FAIL;
11145 }
11146 }
11147
11148 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011149 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11151 return FAIL;
11152
11153 if (ssop_flags & SSOP_RESIZE)
11154 {
11155 /* Note: after the restore we still check it worked!*/
11156 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11157 || put_eol(fd) == FAIL)
11158 return FAIL;
11159 }
11160
11161#ifdef FEAT_GUI
11162 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11163 {
11164 int x, y;
11165
11166 if (gui_mch_get_winpos(&x, &y) == OK)
11167 {
11168 /* Note: after the restore we still check it worked!*/
11169 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11170 return FAIL;
11171 }
11172 }
11173#endif
11174
11175 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011176 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11177 * will be displayed when creating the next tab. That resizes the windows
11178 * in the first tab, which may cause problems. Set 'showtabline' to 2
11179 * temporarily to avoid that.
11180 */
11181 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11182 {
11183 if (put_line(fd, "set stal=2") == FAIL)
11184 return FAIL;
11185 restore_stal = TRUE;
11186 }
11187
11188 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011189 * May repeat putting Windows for each tab, when "tabpages" is in
11190 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011191 * Don't use goto_tabpage(), it may change directory and trigger
11192 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011194 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011195 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011196 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011198 int need_tabnew = FALSE;
11199 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011200
Bram Moolenaar18144c82006-04-12 21:52:12 +000011201 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011203 tabpage_T *tp = find_tabpage(tabnr);
11204
11205 if (tp == NULL)
11206 break; /* done all tab pages */
11207 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011208 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011209 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011210 tab_topframe = topframe;
11211 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011212 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011213 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011214 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011215 tab_topframe = tp->tp_topframe;
11216 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011217 if (tabnr > 1)
11218 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220
Bram Moolenaar18144c82006-04-12 21:52:12 +000011221 /*
11222 * Before creating the window layout, try loading one file. If this
11223 * is aborted we don't end up with a number of useless windows.
11224 * This may have side effects! (e.g., compressed or network file).
11225 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011226 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011227 {
11228 if (ses_do_win(wp)
11229 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011230 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011231#ifdef FEAT_QUICKFIX
11232 && !bt_nofile(wp->w_buffer)
11233#endif
11234 )
11235 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011236 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011237 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11238 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011239 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011240 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011241 if (!wp->w_arg_idx_invalid)
11242 edited_win = wp;
11243 break;
11244 }
11245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011247 /* If no file got edited create an empty tab page. */
11248 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11249 return FAIL;
11250
Bram Moolenaar18144c82006-04-12 21:52:12 +000011251 /*
11252 * Save current window layout.
11253 */
11254 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011256 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011258 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11259 return FAIL;
11260 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11261 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262
Bram Moolenaar18144c82006-04-12 21:52:12 +000011263 /*
11264 * Check if window sizes can be restored (no windows omitted).
11265 * Remember the window number of the current window after restoring.
11266 */
11267 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011268 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011269 {
11270 if (ses_do_win(wp))
11271 ++nr;
11272 else
11273 restore_size = FALSE;
11274 if (curwin == wp)
11275 cnr = nr;
11276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277
Bram Moolenaar18144c82006-04-12 21:52:12 +000011278 /* Go to the first window. */
11279 if (put_line(fd, "wincmd t") == FAIL)
11280 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011281
Bram Moolenaar18144c82006-04-12 21:52:12 +000011282 /*
11283 * If more than one window, see if sizes can be restored.
11284 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11285 * resized when moving between windows.
11286 * Do this before restoring the view, so that the topline and the
11287 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011288 * winminheight and winminwidth need to be set to avoid an error if the
11289 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011290 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011291 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011292 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011293 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011294 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295
Bram Moolenaar18144c82006-04-12 21:52:12 +000011296 /*
11297 * Restore the view of the window (options, file, cursor, etc.).
11298 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011299 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011300 {
11301 if (!ses_do_win(wp))
11302 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011303 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11304 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011305 return FAIL;
11306 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11307 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011308 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011309 }
11310
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011311 /* The argument index in the first tab page is zero, need to set it in
11312 * each window. For further tab pages it's the window where we do
11313 * "tabedit". */
11314 cur_arg_idx = next_arg_idx;
11315
Bram Moolenaar18144c82006-04-12 21:52:12 +000011316 /*
11317 * Restore cursor to the current window if it's not the first one.
11318 */
11319 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11320 || put_eol(fd) == FAIL))
11321 return FAIL;
11322
11323 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011324 * Restore window sizes again after jumping around in windows, because
11325 * the current window has a minimum size while others may not.
11326 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011327 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011328 return FAIL;
11329
Bram Moolenaar18144c82006-04-12 21:52:12 +000011330 /* Don't continue in another tab page when doing only the current one
11331 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011332 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011333 break;
11334 }
11335
11336 if (ssop_flags & SSOP_TABPAGES)
11337 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011338 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11339 || put_eol(fd) == FAIL)
11340 return FAIL;
11341 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011342 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11343 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011344
Bram Moolenaar9c102382006-05-03 21:26:49 +000011345 /*
11346 * Wipe out an empty unnamed buffer we started in.
11347 */
11348 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11349 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011350 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011351 return FAIL;
11352 if (put_line(fd, "endif") == FAIL)
11353 return FAIL;
11354 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11355 return FAIL;
11356
11357 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11358 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11359 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11360 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011361 /* Re-apply 'winminheight' and 'winminwidth'. */
11362 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11363 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11364 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365
11366 /*
11367 * Lastly, execute the x.vim file if it exists.
11368 */
11369 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11370 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011371 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 || put_line(fd, "endif") == FAIL)
11373 return FAIL;
11374
11375 return OK;
11376}
11377
11378 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011379ses_winsizes(
11380 FILE *fd,
11381 int restore_size,
11382 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383{
11384 int n = 0;
11385 win_T *wp;
11386
11387 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11388 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011389 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 {
11391 if (!ses_do_win(wp))
11392 continue;
11393 ++n;
11394
11395 /* restore height when not full height */
11396 if (wp->w_height + wp->w_status_height < topframe->fr_height
11397 && (fprintf(fd,
11398 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11399 n, (long)wp->w_height, Rows / 2, Rows) < 0
11400 || put_eol(fd) == FAIL))
11401 return FAIL;
11402
11403 /* restore width when not full width */
11404 if (wp->w_width < Columns && (fprintf(fd,
11405 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11406 n, (long)wp->w_width, Columns / 2, Columns) < 0
11407 || put_eol(fd) == FAIL))
11408 return FAIL;
11409 }
11410 }
11411 else
11412 {
11413 /* Just equalise window sizes */
11414 if (put_line(fd, "wincmd =") == FAIL)
11415 return FAIL;
11416 }
11417 return OK;
11418}
11419
11420/*
11421 * Write commands to "fd" to recursively create windows for frame "fr",
11422 * horizontally and vertically split.
11423 * After the commands the last window in the frame is the current window.
11424 * Returns FAIL when writing the commands to "fd" fails.
11425 */
11426 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011427ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428{
11429 frame_T *frc;
11430 int count = 0;
11431
11432 if (fr->fr_layout != FR_LEAF)
11433 {
11434 /* Find first frame that's not skipped and then create a window for
11435 * each following one (first frame is already there). */
11436 frc = ses_skipframe(fr->fr_child);
11437 if (frc != NULL)
11438 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11439 {
11440 /* Make window as big as possible so that we have lots of room
11441 * to split. */
11442 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11443 || put_line(fd, fr->fr_layout == FR_COL
11444 ? "split" : "vsplit") == FAIL)
11445 return FAIL;
11446 ++count;
11447 }
11448
11449 /* Go back to the first window. */
11450 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11451 ? "%dwincmd k" : "%dwincmd h", count) < 0
11452 || put_eol(fd) == FAIL))
11453 return FAIL;
11454
11455 /* Recursively create frames/windows in each window of this column or
11456 * row. */
11457 frc = ses_skipframe(fr->fr_child);
11458 while (frc != NULL)
11459 {
11460 ses_win_rec(fd, frc);
11461 frc = ses_skipframe(frc->fr_next);
11462 /* Go to next window. */
11463 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11464 return FAIL;
11465 }
11466 }
11467 return OK;
11468}
11469
11470/*
11471 * Skip frames that don't contain windows we want to save in the Session.
11472 * Returns NULL when there none.
11473 */
11474 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011475ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476{
11477 frame_T *frc;
11478
11479 for (frc = fr; frc != NULL; frc = frc->fr_next)
11480 if (ses_do_frame(frc))
11481 break;
11482 return frc;
11483}
11484
11485/*
11486 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11487 * the Session.
11488 */
11489 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011490ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491{
11492 frame_T *frc;
11493
11494 if (fr->fr_layout == FR_LEAF)
11495 return ses_do_win(fr->fr_win);
11496 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11497 if (ses_do_frame(frc))
11498 return TRUE;
11499 return FALSE;
11500}
11501
11502/*
11503 * Return non-zero if window "wp" is to be stored in the Session.
11504 */
11505 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011506ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507{
11508 if (wp->w_buffer->b_fname == NULL
11509#ifdef FEAT_QUICKFIX
11510 /* When 'buftype' is "nofile" can't restore the window contents. */
11511 || bt_nofile(wp->w_buffer)
11512#endif
11513 )
11514 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011515 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516 return (ssop_flags & SSOP_HELP);
11517 return TRUE;
11518}
11519
11520/*
11521 * Write commands to "fd" to restore the view of a window.
11522 * Caller must make sure 'scrolloff' is zero.
11523 */
11524 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011525put_view(
11526 FILE *fd,
11527 win_T *wp,
11528 int add_edit, /* add ":edit" command to view */
11529 unsigned *flagp, /* vop_flags or ssop_flags */
11530 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011531 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532{
11533 win_T *save_curwin;
11534 int f;
11535 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011536 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537
11538 /* Always restore cursor position for ":mksession". For ":mkview" only
11539 * when 'viewoptions' contains "cursor". */
11540 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11541
11542 /*
11543 * Local argument list.
11544 */
11545 if (wp->w_alist == &global_alist)
11546 {
11547 if (put_line(fd, "argglobal") == FAIL)
11548 return FAIL;
11549 }
11550 else
11551 {
11552 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11553 flagp == &vop_flags
11554 || !(*flagp & SSOP_CURDIR)
11555 || wp->w_localdir != NULL, flagp) == FAIL)
11556 return FAIL;
11557 }
11558
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011559 /* Only when part of a session: restore the argument index. Some
11560 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011561 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011562 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011564 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565 || put_eol(fd) == FAIL)
11566 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011567 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568 }
11569
11570 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011571 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572 {
11573 /*
11574 * Load the file.
11575 */
11576 if (wp->w_buffer->b_ffname != NULL
11577#ifdef FEAT_QUICKFIX
11578 && !bt_nofile(wp->w_buffer)
11579#endif
11580 )
11581 {
11582 /*
11583 * Editing a file in this buffer: use ":edit file".
11584 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011585 *
11586 * Note, if a buffer for that file already exists, use :badd to
11587 * edit that buffer, to not lose folding information (:edit resets
11588 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011590 if (fputs("if bufexists('", fd) < 0
11591 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11592 || fputs("') | buffer ", fd) < 0
11593 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11594 || fputs(" | else | edit ", fd) < 0
11595 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11596 || fputs(" | endif", fd) < 0
11597 ||
11598 put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599 return FAIL;
11600 }
11601 else
11602 {
11603 /* No file in this buffer, just make it empty. */
11604 if (put_line(fd, "enew") == FAIL)
11605 return FAIL;
11606#ifdef FEAT_QUICKFIX
11607 if (wp->w_buffer->b_ffname != NULL)
11608 {
11609 /* The buffer does have a name, but it's not a file name. */
11610 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011611 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612 return FAIL;
11613 }
11614#endif
11615 do_cursor = FALSE;
11616 }
11617 }
11618
11619 /*
11620 * Local mappings and abbreviations.
11621 */
11622 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11623 && makemap(fd, wp->w_buffer) == FAIL)
11624 return FAIL;
11625
11626 /*
11627 * Local options. Need to go to the window temporarily.
11628 * Store only local values when using ":mkview" and when ":mksession" is
11629 * used and 'sessionoptions' doesn't include "options".
11630 * Some folding options are always stored when "folds" is included,
11631 * otherwise the folds would not be restored correctly.
11632 */
11633 save_curwin = curwin;
11634 curwin = wp;
11635 curbuf = curwin->w_buffer;
11636 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11637 f = makeset(fd, OPT_LOCAL,
11638 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11639#ifdef FEAT_FOLDING
11640 else if (*flagp & SSOP_FOLDS)
11641 f = makefoldset(fd);
11642#endif
11643 else
11644 f = OK;
11645 curwin = save_curwin;
11646 curbuf = curwin->w_buffer;
11647 if (f == FAIL)
11648 return FAIL;
11649
11650#ifdef FEAT_FOLDING
11651 /*
11652 * Save Folds when 'buftype' is empty and for help files.
11653 */
11654 if ((*flagp & SSOP_FOLDS)
11655 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011656 && (*wp->w_buffer->b_p_bt == NUL || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657 {
11658 if (put_folds(fd, wp) == FAIL)
11659 return FAIL;
11660 }
11661#endif
11662
11663 /*
11664 * Set the cursor after creating folds, since that moves the cursor.
11665 */
11666 if (do_cursor)
11667 {
11668
11669 /* Restore the cursor line in the file and relatively in the
11670 * window. Don't use "G", it changes the jumplist. */
11671 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11672 (long)wp->w_cursor.lnum,
11673 (long)(wp->w_cursor.lnum - wp->w_topline),
11674 (long)wp->w_height / 2, (long)wp->w_height) < 0
11675 || put_eol(fd) == FAIL
11676 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11677 || put_line(fd, "exe s:l") == FAIL
11678 || put_line(fd, "normal! zt") == FAIL
11679 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11680 || put_eol(fd) == FAIL)
11681 return FAIL;
11682 /* Restore the cursor column and left offset when not wrapping. */
11683 if (wp->w_cursor.col == 0)
11684 {
11685 if (put_line(fd, "normal! 0") == FAIL)
11686 return FAIL;
11687 }
11688 else
11689 {
11690 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11691 {
11692 if (fprintf(fd,
11693 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011694 (long)wp->w_virtcol + 1,
11695 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696 (long)wp->w_width / 2, (long)wp->w_width) < 0
11697 || put_eol(fd) == FAIL
11698 || put_line(fd, "if s:c > 0") == FAIL
11699 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011700 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11701 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 || put_eol(fd) == FAIL
11703 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011704 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 || put_eol(fd) == FAIL
11706 || put_line(fd, "endif") == FAIL)
11707 return FAIL;
11708 }
11709 else
11710 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011711 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712 || put_eol(fd) == FAIL)
11713 return FAIL;
11714 }
11715 }
11716 }
11717
11718 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011719 * Local directory, if the current flag is not view options or the "curdir"
11720 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011722 if (wp->w_localdir != NULL
11723 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011724 {
11725 if (fputs("lcd ", fd) < 0
11726 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11727 || put_eol(fd) == FAIL)
11728 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011729 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730 }
11731
11732 return OK;
11733}
11734
11735/*
11736 * Write an argument list to the session file.
11737 * Returns FAIL if writing fails.
11738 */
11739 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011740ses_arglist(
11741 FILE *fd,
11742 char *cmd,
11743 garray_T *gap,
11744 int fullname, /* TRUE: use full path name */
11745 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746{
11747 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011748 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749 char_u *s;
11750
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011751 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11752 return FAIL;
11753 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 return FAIL;
11755 for (i = 0; i < gap->ga_len; ++i)
11756 {
11757 /* NULL file names are skipped (only happens when out of memory). */
11758 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11759 if (s != NULL)
11760 {
11761 if (fullname)
11762 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011763 buf = alloc(MAXPATHL);
11764 if (buf != NULL)
11765 {
11766 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11767 s = buf;
11768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011770 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011771 || ses_put_fname(fd, s, flagp) == FAIL
11772 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011773 {
11774 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011776 }
11777 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778 }
11779 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011780 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781}
11782
11783/*
11784 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011785 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786 * Returns FAIL if writing fails.
11787 */
11788 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011789ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790{
11791 char_u *name;
11792
11793 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011794 * the session file will be sourced.
11795 * Don't do this for ":mkview", we don't know the current directory.
11796 * Don't do this after ":lcd", we don't keep track of what the current
11797 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798 if (buf->b_sfname != NULL
11799 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011800 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011801#ifdef FEAT_AUTOCHDIR
11802 && !p_acd
11803#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011804 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805 name = buf->b_sfname;
11806 else
11807 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011808 if (ses_put_fname(fd, name, flagp) == FAIL
11809 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 return FAIL;
11811 return OK;
11812}
11813
11814/*
11815 * Write a file name to the session file.
11816 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11817 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011818 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819 */
11820 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011821ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822{
11823 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011824 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826
11827 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011828 if (sname == NULL)
11829 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011831 if (*flagp & SSOP_SLASH)
11832 {
11833 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011834 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011835 if (*p == '\\')
11836 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011838
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011839 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011840 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011842 if (p == NULL)
11843 return FAIL;
11844
11845 /* write the result */
11846 if (fputs((char *)p, fd) < 0)
11847 retval = FAIL;
11848
11849 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850 return retval;
11851}
11852
11853/*
11854 * ":loadview [nr]"
11855 */
11856 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011857ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858{
11859 char_u *fname;
11860
11861 fname = get_view_file(*eap->arg);
11862 if (fname != NULL)
11863 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011864 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 vim_free(fname);
11866 }
11867}
11868
11869/*
11870 * Get the name of the view file for the current buffer.
11871 */
11872 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011873get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874{
11875 int len = 0;
11876 char_u *p, *s;
11877 char_u *retval;
11878 char_u *sname;
11879
11880 if (curbuf->b_ffname == NULL)
11881 {
11882 EMSG(_(e_noname));
11883 return NULL;
11884 }
11885 sname = home_replace_save(NULL, curbuf->b_ffname);
11886 if (sname == NULL)
11887 return NULL;
11888
11889 /*
11890 * We want a file name without separators, because we're not going to make
11891 * a directory.
11892 * "normal" path separator -> "=+"
11893 * "=" -> "=="
11894 * ":" path separator -> "=-"
11895 */
11896 for (p = sname; *p; ++p)
11897 if (*p == '=' || vim_ispathsep(*p))
11898 ++len;
11899 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11900 if (retval != NULL)
11901 {
11902 STRCPY(retval, p_vdir);
11903 add_pathsep(retval);
11904 s = retval + STRLEN(retval);
11905 for (p = sname; *p; ++p)
11906 {
11907 if (*p == '=')
11908 {
11909 *s++ = '=';
11910 *s++ = '=';
11911 }
11912 else if (vim_ispathsep(*p))
11913 {
11914 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011915#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916 if (*p == ':')
11917 *s++ = '-';
11918 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011920 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 }
11922 else
11923 *s++ = *p;
11924 }
11925 *s++ = '=';
11926 *s++ = c;
11927 STRCPY(s, ".vim");
11928 }
11929
11930 vim_free(sname);
11931 return retval;
11932}
11933
11934#endif /* FEAT_SESSION */
11935
11936/*
11937 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11938 * Return FAIL for a write error.
11939 */
11940 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011941put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942{
11943 if (
11944#ifdef USE_CRNL
11945 (
11946# ifdef MKSESSION_NL
11947 !mksession_nl &&
11948# endif
11949 (putc('\r', fd) < 0)) ||
11950#endif
11951 (putc('\n', fd) < 0))
11952 return FAIL;
11953 return OK;
11954}
11955
11956/*
11957 * Write a line to "fd".
11958 * Return FAIL for a write error.
11959 */
11960 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011961put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962{
11963 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11964 return FAIL;
11965 return OK;
11966}
11967
11968#ifdef FEAT_VIMINFO
11969/*
11970 * ":rviminfo" and ":wviminfo".
11971 */
11972 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011973ex_viminfo(
11974 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975{
11976 char_u *save_viminfo;
11977
11978 save_viminfo = p_viminfo;
11979 if (*p_viminfo == NUL)
11980 p_viminfo = (char_u *)"'100";
11981 if (eap->cmdidx == CMD_rviminfo)
11982 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011983 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11984 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985 EMSG(_("E195: Cannot open viminfo file for reading"));
11986 }
11987 else
11988 write_viminfo(eap->arg, eap->forceit);
11989 p_viminfo = save_viminfo;
11990}
11991#endif
11992
11993#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011994/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011995 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011996 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011999dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001 if (fname == NULL)
12002 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012003 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004}
12005#endif
12006
12007/*
12008 * ":behave {mswin,xterm}"
12009 */
12010 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012011ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012{
12013 if (STRCMP(eap->arg, "mswin") == 0)
12014 {
12015 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12016 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12017 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12018 set_option_value((char_u *)"keymodel", 0L,
12019 (char_u *)"startsel,stopsel", 0);
12020 }
12021 else if (STRCMP(eap->arg, "xterm") == 0)
12022 {
12023 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12024 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12025 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12026 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12027 }
12028 else
12029 EMSG2(_(e_invarg2), eap->arg);
12030}
12031
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012032#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12033/*
12034 * Function given to ExpandGeneric() to obtain the possible arguments of the
12035 * ":behave {mswin,xterm}" command.
12036 */
12037 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012038get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012039{
12040 if (idx == 0)
12041 return (char_u *)"mswin";
12042 if (idx == 1)
12043 return (char_u *)"xterm";
12044 return NULL;
12045}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012046
12047/*
12048 * Function given to ExpandGeneric() to obtain the possible arguments of the
12049 * ":messages {clear}" command.
12050 */
12051 char_u *
12052get_messages_arg(expand_T *xp UNUSED, int idx)
12053{
12054 if (idx == 0)
12055 return (char_u *)"clear";
12056 return NULL;
12057}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012058#endif
12059
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012060 char_u *
12061get_mapclear_arg(expand_T *xp UNUSED, int idx)
12062{
12063 if (idx == 0)
12064 return (char_u *)"<buffer>";
12065 return NULL;
12066}
12067
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068#ifdef FEAT_AUTOCMD
12069static int filetype_detect = FALSE;
12070static int filetype_plugin = FALSE;
12071static int filetype_indent = FALSE;
12072
12073/*
12074 * ":filetype [plugin] [indent] {on,off,detect}"
12075 * on: Load the filetype.vim file to install autocommands for file types.
12076 * off: Load the ftoff.vim file to remove all autocommands for file types.
12077 * plugin on: load filetype.vim and ftplugin.vim
12078 * plugin off: load ftplugof.vim
12079 * indent on: load filetype.vim and indent.vim
12080 * indent off: load indoff.vim
12081 */
12082 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012083ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084{
12085 char_u *arg = eap->arg;
12086 int plugin = FALSE;
12087 int indent = FALSE;
12088
12089 if (*eap->arg == NUL)
12090 {
12091 /* Print current status. */
12092 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12093 filetype_detect ? "ON" : "OFF",
12094 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12095 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12096 return;
12097 }
12098
12099 /* Accept "plugin" and "indent" in any order. */
12100 for (;;)
12101 {
12102 if (STRNCMP(arg, "plugin", 6) == 0)
12103 {
12104 plugin = TRUE;
12105 arg = skipwhite(arg + 6);
12106 continue;
12107 }
12108 if (STRNCMP(arg, "indent", 6) == 0)
12109 {
12110 indent = TRUE;
12111 arg = skipwhite(arg + 6);
12112 continue;
12113 }
12114 break;
12115 }
12116 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12117 {
12118 if (*arg == 'o' || !filetype_detect)
12119 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012120 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121 filetype_detect = TRUE;
12122 if (plugin)
12123 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012124 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125 filetype_plugin = TRUE;
12126 }
12127 if (indent)
12128 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012129 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130 filetype_indent = TRUE;
12131 }
12132 }
12133 if (*arg == 'd')
12134 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012135 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012136 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137 }
12138 }
12139 else if (STRCMP(arg, "off") == 0)
12140 {
12141 if (plugin || indent)
12142 {
12143 if (plugin)
12144 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012145 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146 filetype_plugin = FALSE;
12147 }
12148 if (indent)
12149 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012150 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151 filetype_indent = FALSE;
12152 }
12153 }
12154 else
12155 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012156 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 filetype_detect = FALSE;
12158 }
12159 }
12160 else
12161 EMSG2(_(e_invarg2), arg);
12162}
12163
12164/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012165 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166 */
12167 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012168ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169{
12170 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012171 {
12172 char_u *arg = eap->arg;
12173
12174 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12175 arg += 9;
12176
12177 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12178 if (arg != eap->arg)
12179 did_filetype = FALSE;
12180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181}
12182#endif
12183
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012185ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186{
12187#ifdef FEAT_DIGRAPHS
12188 if (*eap->arg != NUL)
12189 putdigraph(eap->arg);
12190 else
12191 listdigraphs();
12192#else
12193 EMSG(_("E196: No digraphs in this version"));
12194#endif
12195}
12196
12197 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012198ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199{
12200 int flags = 0;
12201
12202 if (eap->cmdidx == CMD_setlocal)
12203 flags = OPT_LOCAL;
12204 else if (eap->cmdidx == CMD_setglobal)
12205 flags = OPT_GLOBAL;
12206#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12207 if (cmdmod.browse && flags == 0)
12208 ex_options(eap);
12209 else
12210#endif
12211 (void)do_set(eap->arg, flags);
12212}
12213
12214#ifdef FEAT_SEARCH_EXTRA
12215/*
12216 * ":nohlsearch"
12217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012219ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012221 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012222 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223}
12224
12225/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012226 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227 * Sets nextcmd to the start of the next command, if any. Also called when
12228 * skipping commands to find the next command.
12229 */
12230 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012231ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232{
12233 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012234 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235 char_u *end;
12236 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012237 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012238
12239 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012240 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012241 else
12242 {
12243 EMSG(e_invcmd);
12244 return;
12245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246
12247 /* First clear any old pattern. */
12248 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012249 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250
12251 if (ends_excmd(*eap->arg))
12252 end = eap->arg;
12253 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012254 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255 end = eap->arg + 4;
12256 else
12257 {
12258 p = skiptowhite(eap->arg);
12259 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012260 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 p = skipwhite(p);
12262 if (*p == NUL)
12263 {
12264 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012265 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266 EMSG2(_(e_invarg2), eap->arg);
12267 return;
12268 }
12269 end = skip_regexp(p + 1, *p, TRUE, NULL);
12270 if (!eap->skip)
12271 {
12272 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12273 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012274 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275 eap->errmsg = e_trailing;
12276 return;
12277 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012278 if (*end != *p)
12279 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012280 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012281 EMSG2(_(e_invarg2), p);
12282 return;
12283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284
12285 c = *end;
12286 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012287 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012288 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012289 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 }
12291 }
12292 eap->nextcmd = find_nextcmd(end);
12293}
12294#endif
12295
12296#ifdef FEAT_CRYPT
12297/*
12298 * ":X": Get crypt key
12299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012301ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012303 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012304 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305}
12306#endif
12307
12308#ifdef FEAT_FOLDING
12309 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012310ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311{
12312 if (foldManualAllowed(TRUE))
12313 foldCreate(eap->line1, eap->line2);
12314}
12315
12316 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012317ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318{
12319 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12320 eap->forceit, FALSE);
12321}
12322
12323 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012324ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325{
12326 linenr_T lnum;
12327
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012328#ifdef FEAT_CLIPBOARD
12329 start_global_changes();
12330#endif
12331
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332 /* First set the marks for all lines closed/open. */
12333 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12334 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12335 ml_setmarked(lnum);
12336
12337 /* Execute the command on the marked lines. */
12338 global_exe(eap->arg);
12339 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012340#ifdef FEAT_CLIPBOARD
12341 end_global_changes();
12342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343}
12344#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012345
12346# if defined(FEAT_TIMERS) || defined(PROTO)
12347 int
12348get_pressedreturn(void)
12349{
12350 return ex_pressedreturn;
12351}
12352
12353 void
12354set_pressedreturn(int val)
12355{
12356 ex_pressedreturn = val;
12357}
12358#endif