blob: af68a8ea701bffe8c812694fcff00e9733116298 [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
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020032 sctx_T uc_script_ctx; /* SCTX where the command was defined */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010033# 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
64#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010065static 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 +000066#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010067static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000068static int if_level = 0; /* depth in :if */
69#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +020070static void free_cmdmod(void);
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
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010084static void ex_autocmd(exarg_T *eap);
85static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010086static void ex_bunload(exarg_T *eap);
87static void ex_buffer(exarg_T *eap);
88static void ex_bmodified(exarg_T *eap);
89static void ex_bnext(exarg_T *eap);
90static void ex_bprevious(exarg_T *eap);
91static void ex_brewind(exarg_T *eap);
92static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010093static char_u *getargcmd(char_u **);
94static char_u *skip_cmd_arg(char_u *p, int rembs);
95static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000096#ifndef FEAT_QUICKFIX
97# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000098# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000099# define ex_cc ex_ni
100# define ex_cnext ex_ni
101# define ex_cfile ex_ni
102# define qf_list ex_ni
103# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200104# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000106# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200108#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109# define ex_cclose ex_ni
110# define ex_copen ex_ni
111# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +0200112# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000114#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
115# define ex_cexpr ex_ni
116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200118static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100119static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200120#if !defined(FEAT_PERL) \
121 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
122 || !defined(FEAT_TCL) \
123 || !defined(FEAT_RUBY) \
124 || !defined(FEAT_LUA) \
125 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000126# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100127static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100129static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000131#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100132static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000133#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100134static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
135static void ex_highlight(exarg_T *eap);
136static void ex_colorscheme(exarg_T *eap);
137static void ex_quit(exarg_T *eap);
138static void ex_cquit(exarg_T *eap);
139static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100140static void ex_close(exarg_T *eap);
141static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
142static void ex_only(exarg_T *eap);
143static void ex_resize(exarg_T *eap);
144static void ex_stag(exarg_T *eap);
145static void ex_tabclose(exarg_T *eap);
146static void ex_tabonly(exarg_T *eap);
147static void ex_tabnext(exarg_T *eap);
148static void ex_tabmove(exarg_T *eap);
149static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200150#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100151static void ex_pclose(exarg_T *eap);
152static void ex_ptag(exarg_T *eap);
153static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#else
155# define ex_pclose ex_ni
156# define ex_ptag ex_ni
157# define ex_pedit ex_ni
158#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100159static void ex_hide(exarg_T *eap);
160static void ex_stop(exarg_T *eap);
161static void ex_exit(exarg_T *eap);
162static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100164static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#else
166# define ex_goto ex_ni
167#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100168static void ex_shell(exarg_T *eap);
169static void ex_preserve(exarg_T *eap);
170static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100171static void ex_mode(exarg_T *eap);
172static void ex_wrongmodifier(exarg_T *eap);
173static void ex_find(exarg_T *eap);
174static void ex_open(exarg_T *eap);
175static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#ifndef FEAT_GUI
177# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100178static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100180#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100181static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#else
183# define ex_tearoff ex_ni
184#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100185#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
186 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100187static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#else
189# define ex_popup ex_ni
190#endif
191#ifndef FEAT_GUI_MSWIN
192# define ex_simalt ex_ni
193#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000194#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195# define gui_mch_find_dialog ex_ni
196# define gui_mch_replace_dialog ex_ni
197#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000198#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199# define ex_helpfind ex_ni
200#endif
201#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100202# define ex_cscope ex_ni
203# define ex_scscope ex_ni
204# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205#endif
206#ifndef FEAT_SYN_HL
207# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200208# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000209#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100210#ifndef FEAT_EVAL
211# define ex_packadd ex_ni
212# define ex_packloadall ex_ni
213#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200214#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200215# define ex_syntime ex_ni
216#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000217#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000218# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000219# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000220# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000221# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000222# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000223#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200224#ifndef FEAT_PERSISTENT_UNDO
225# define ex_rundo ex_ni
226# define ex_wundo ex_ni
227#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200228#ifndef FEAT_LUA
229# define ex_lua ex_script_ni
230# define ex_luado ex_ni
231# define ex_luafile ex_ni
232#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000233#ifndef FEAT_MZSCHEME
234# define ex_mzscheme ex_script_ni
235# define ex_mzfile ex_ni
236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237#ifndef FEAT_PERL
238# define ex_perl ex_script_ni
239# define ex_perldo ex_ni
240#endif
241#ifndef FEAT_PYTHON
242# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200243# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244# define ex_pyfile ex_ni
245#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200246#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200247# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200248# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200249# define ex_py3file ex_ni
250#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100251#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
252# define ex_pyx ex_script_ni
253# define ex_pyxdo ex_ni
254# define ex_pyxfile ex_ni
255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256#ifndef FEAT_TCL
257# define ex_tcl ex_script_ni
258# define ex_tcldo ex_ni
259# define ex_tclfile ex_ni
260#endif
261#ifndef FEAT_RUBY
262# define ex_ruby ex_script_ni
263# define ex_rubydo ex_ni
264# define ex_rubyfile ex_ni
265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#ifndef FEAT_KEYMAP
267# define ex_loadkeymap ex_ni
268#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static void ex_swapname(exarg_T *eap);
270static void ex_syncbind(exarg_T *eap);
271static void ex_read(exarg_T *eap);
272static void ex_pwd(exarg_T *eap);
273static void ex_equal(exarg_T *eap);
274static void ex_sleep(exarg_T *eap);
275static void do_exmap(exarg_T *eap, int isabbrev);
276static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100277static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000278#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100279static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280#else
281# define ex_winpos ex_ni
282#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100283static void ex_operators(exarg_T *eap);
284static void ex_put(exarg_T *eap);
285static void ex_copymove(exarg_T *eap);
286static void ex_submagic(exarg_T *eap);
287static void ex_join(exarg_T *eap);
288static void ex_at(exarg_T *eap);
289static void ex_bang(exarg_T *eap);
290static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200291#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100292static void ex_wundo(exarg_T *eap);
293static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200294#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100295static void ex_redo(exarg_T *eap);
296static void ex_later(exarg_T *eap);
297static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100298static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100299static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100300static void close_redir(void);
301static void ex_mkrc(exarg_T *eap);
302static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303#ifdef FEAT_USR_CMDS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100304static char *uc_fun_cmd(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100305static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100307static void ex_startinsert(exarg_T *eap);
308static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100310static void ex_checkpath(exarg_T *eap);
311static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312#else
313# define ex_findpat ex_ni
314# define ex_checkpath ex_ni
315#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200316#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318#else
319# define ex_psearch ex_ni
320#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100321static void ex_tag(exarg_T *eap);
322static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#ifndef FEAT_EVAL
324# define ex_scriptnames ex_ni
325# define ex_finish ex_ni
326# define ex_echo ex_ni
327# define ex_echohl ex_ni
328# define ex_execute ex_ni
329# define ex_call ex_ni
330# define ex_if ex_ni
331# define ex_endif ex_ni
332# define ex_else ex_ni
333# define ex_while ex_ni
334# define ex_continue ex_ni
335# define ex_break ex_ni
336# define ex_endwhile ex_ni
337# define ex_throw ex_ni
338# define ex_try ex_ni
339# define ex_catch ex_ni
340# define ex_finally ex_ni
341# define ex_endtry ex_ni
342# define ex_endfunction ex_ni
343# define ex_let ex_ni
344# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000345# define ex_lockvar ex_ni
346# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347# define ex_function ex_ni
348# define ex_delfunction ex_ni
349# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000350# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100352static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100354static int makeopens(FILE *fd, char_u *dirnow);
355static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
356static void ex_loadview(exarg_T *eap);
357static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000358static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#else
360# define ex_loadview ex_ni
361#endif
362#ifndef FEAT_EVAL
363# define ex_compiler ex_ni
364#endif
365#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100366static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367#else
368# define ex_viminfo ex_ni
369#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100370static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100371static void ex_filetype(exarg_T *eap);
372static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000374# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375# define ex_diffpatch ex_ni
376# define ex_diffgetput ex_ni
377# define ex_diffsplit ex_ni
378# define ex_diffthis ex_ni
379# define ex_diffupdate ex_ni
380#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100381static void ex_digraphs(exarg_T *eap);
382static void ex_set(exarg_T *eap);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100383#if !defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384# define ex_options ex_ni
385#endif
386#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100387static void ex_nohlsearch(exarg_T *eap);
388static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389#else
390# define ex_nohlsearch ex_ni
391# define ex_match ex_ni
392#endif
393#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100394static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395#else
396# define ex_X ex_ni
397#endif
398#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100399static void ex_fold(exarg_T *eap);
400static void ex_foldopen(exarg_T *eap);
401static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402#else
403# define ex_fold ex_ni
404# define ex_foldopen ex_ni
405# define ex_folddo ex_ni
406#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100407#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408# define ex_language ex_ni
409#endif
410#ifndef FEAT_SIGNS
411# define ex_sign ex_ni
412#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000413#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200414# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000415# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200416# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418
419#ifndef FEAT_EVAL
420# define ex_debug ex_ni
421# define ex_breakadd ex_ni
422# define ex_debuggreedy ex_ni
423# define ex_breakdel ex_ni
424# define ex_breaklist ex_ni
425#endif
426
427#ifndef FEAT_CMDHIST
428# define ex_history ex_ni
429#endif
430#ifndef FEAT_JUMPLIST
431# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200432# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433# define ex_changes ex_ni
434#endif
435
Bram Moolenaar05159a02005-02-26 23:04:13 +0000436#ifndef FEAT_PROFILE
437# define ex_profile ex_ni
438#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200439#ifndef FEAT_TERMINAL
440# define ex_terminal ex_ni
441#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000442
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443/*
444 * Declare cmdnames[].
445 */
446#define DO_DECLARE_EXCMD
447#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200448#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100449
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450static char_u dollar_command[2] = {'$', 0};
451
452
453#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000454/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455typedef struct
456{
457 char_u *line; /* command line */
458 linenr_T lnum; /* sourcing_lnum of the line */
459} wcmd_T;
460
461/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000462 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000464 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000466struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467{
468 garray_T *lines_gap; /* growarray with line info */
469 int current_line; /* last read line from growarray */
470 int repeating; /* TRUE when looping a second time */
471 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100472 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 void *cookie;
474};
475
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100476static char_u *get_loop_line(int c, void *cookie, int indent);
477static int store_loop_line(garray_T *gap, char_u *line);
478static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000479
480/* Struct to save a few things while debugging. Used in do_cmdline() only. */
481struct dbg_stuff
482{
483 int trylevel;
484 int force_abort;
485 except_T *caught_stack;
486 char_u *vv_exception;
487 char_u *vv_throwpoint;
488 int did_emsg;
489 int got_int;
490 int did_throw;
491 int need_rethrow;
492 int check_cstack;
493 except_T *current_exception;
494};
495
Bram Moolenaared203462004-06-16 11:19:22 +0000496 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100497save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000498{
499 dsp->trylevel = trylevel; trylevel = 0;
500 dsp->force_abort = force_abort; force_abort = FALSE;
501 dsp->caught_stack = caught_stack; caught_stack = NULL;
502 dsp->vv_exception = v_exception(NULL);
503 dsp->vv_throwpoint = v_throwpoint(NULL);
504
505 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
506 dsp->did_emsg = did_emsg; did_emsg = FALSE;
507 dsp->got_int = got_int; got_int = FALSE;
508 dsp->did_throw = did_throw; did_throw = FALSE;
509 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
510 dsp->check_cstack = check_cstack; check_cstack = FALSE;
511 dsp->current_exception = current_exception; current_exception = NULL;
512}
513
514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100515restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000516{
517 suppress_errthrow = FALSE;
518 trylevel = dsp->trylevel;
519 force_abort = dsp->force_abort;
520 caught_stack = dsp->caught_stack;
521 (void)v_exception(dsp->vv_exception);
522 (void)v_throwpoint(dsp->vv_throwpoint);
523 did_emsg = dsp->did_emsg;
524 got_int = dsp->got_int;
525 did_throw = dsp->did_throw;
526 need_rethrow = dsp->need_rethrow;
527 check_cstack = dsp->check_cstack;
528 current_exception = dsp->current_exception;
529}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530#endif
531
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532/*
533 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
534 * command is given.
535 */
536 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100537do_exmode(
538 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539{
540 int save_msg_scroll;
541 int prev_msg_row;
542 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100543 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544
545 if (improved)
546 exmode_active = EXMODE_VIM;
547 else
548 exmode_active = EXMODE_NORMAL;
549 State = NORMAL;
550
551 /* When using ":global /pat/ visual" and then "Q" we return to continue
552 * the :global command. */
553 if (global_busy)
554 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
556 save_msg_scroll = msg_scroll;
557 ++RedrawingDisabled; /* don't redisplay the window */
558 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559#ifdef FEAT_GUI
560 /* Ignore scrollbar and mouse events in Ex mode */
561 ++hold_gui_events;
562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563
Bram Moolenaar32526b32019-01-19 17:43:09 +0100564 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 while (exmode_active)
566 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000567 /* Check for a ":normal" command and no more characters left. */
568 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
569 {
570 exmode_active = FALSE;
571 break;
572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 msg_scroll = TRUE;
574 need_wait_return = FALSE;
575 ex_pressedreturn = FALSE;
576 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100577 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 prev_msg_row = msg_row;
579 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 if (improved)
581 {
582 cmdline_row = msg_row;
583 do_cmdline(NULL, getexline, NULL, 0);
584 }
585 else
586 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
587 lines_left = Rows - 1;
588
Bram Moolenaardf177f62005-02-22 08:39:57 +0000589 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100590 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000592 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100593 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000594 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000596 if (ex_pressedreturn)
597 {
598 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000599 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000600 msg_row = prev_msg_row;
601 if (prev_msg_row == Rows - 1)
602 msg_row--;
603 }
604 msg_col = 0;
605 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
606 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000609 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
610 {
611 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100612 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000613 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100614 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 }
617
618#ifdef FEAT_GUI
619 --hold_gui_events;
620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 --RedrawingDisabled;
622 --no_wait_return;
623 update_screen(CLEAR);
624 need_wait_return = FALSE;
625 msg_scroll = save_msg_scroll;
626}
627
628/*
629 * Execute a simple command line. Used for translated commands like "*".
630 */
631 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100632do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
634 return do_cmdline(cmd, NULL, NULL,
635 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
636}
637
638/*
639 * do_cmdline(): execute one Ex command line
640 *
641 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100642 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 * 2. Split up in parts separated with '|'.
644 *
645 * This function can be called recursively!
646 *
647 * flags:
648 * DOCMD_VERBOSE - The command will be included in the error message.
649 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100650 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 * DOCMD_KEYTYPED - Don't reset KeyTyped.
652 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
653 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
654 *
655 * return FAIL if cmdline could not be executed, OK otherwise
656 */
657 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100658do_cmdline(
659 char_u *cmdline,
660 char_u *(*fgetline)(int, void *, int),
661 void *cookie, /* argument for fgetline() */
662 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663{
664 char_u *next_cmdline; /* next cmd to execute */
665 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100666 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 static int recursive = 0; /* recursive depth */
668 int msg_didout_before_start = 0;
669 int count = 0; /* line number count */
670 int did_inc = FALSE; /* incremented RedrawingDisabled */
671 int retval = OK;
672#ifdef FEAT_EVAL
673 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000674 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 int current_line = 0; /* active line in lines_ga */
676 char_u *fname = NULL; /* function or script name */
677 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
678 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000679 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 int initial_trylevel;
681 struct msglist **saved_msg_list = NULL;
682 struct msglist *private_msg_list;
683
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100684 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100685 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000687 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000689 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100691# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692# define cmd_cookie cookie
693#endif
694 static int call_depth = 0; /* recursiveness */
695
696#ifdef FEAT_EVAL
697 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
698 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000699 * This ensures that the do_errthrow() call in do_one_cmd() does not
700 * combine the messages stored by an earlier invocation of do_one_cmd()
701 * with the command name of the later one. This would happen when
702 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 saved_msg_list = msg_list;
704 msg_list = &private_msg_list;
705 private_msg_list = NULL;
706#endif
707
708 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100709 * here. The value of 200 allows nested function calls, ":source", etc.
710 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100711 if (call_depth >= 200
712#ifdef FEAT_EVAL
713 && call_depth >= p_mfd
714#endif
715 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100717 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718#ifdef FEAT_EVAL
719 /* When converting to an exception, we do not include the command name
720 * since this is not an error of the specific command. */
721 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
722 msg_list = saved_msg_list;
723#endif
724 return FAIL;
725 }
726 ++call_depth;
727
728#ifdef FEAT_EVAL
729 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000730 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 cstack.cs_trylevel = 0;
732 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000733 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
735
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100736 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
738 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100739 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000740 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 ++ex_nesting_level;
742
743 /* Get the function or script name and the address where the next breakpoint
744 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000745 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 {
747 fname = func_name(real_cookie);
748 breakpoint = func_breakpoint(real_cookie);
749 dbg_tick = func_dbg_tick(real_cookie);
750 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100751 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 {
753 fname = sourcing_name;
754 breakpoint = source_breakpoint(real_cookie);
755 dbg_tick = source_dbg_tick(real_cookie);
756 }
757
758 /*
759 * Initialize "force_abort" and "suppress_errthrow" at the top level.
760 */
761 if (!recursive)
762 {
763 force_abort = FALSE;
764 suppress_errthrow = FALSE;
765 }
766
767 /*
768 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000769 * exception handling (used when debugging). Otherwise clear it to avoid
770 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000772 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000773 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000774 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200775 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
777 initial_trylevel = trylevel;
778
779 /*
780 * "did_throw" will be set to TRUE when an exception is being thrown.
781 */
782 did_throw = FALSE;
783#endif
784 /*
785 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000786 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 * If force_abort is set, we cancel everything.
788 */
789 did_emsg = FALSE;
790
791 /*
792 * KeyTyped is only set when calling vgetc(). Reset it here when not
793 * calling vgetc() (sourced command lines).
794 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100795 if (!(flags & DOCMD_KEYTYPED)
796 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 KeyTyped = FALSE;
798
799 /*
800 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000801 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 * - for multiple commands on one line, separated with '|'
803 * - when repeating until there are no more lines (for ":source")
804 */
805 next_cmdline = cmdline;
806 do
807 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100809 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810#endif
811
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000812 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 if (next_cmdline == NULL
814#ifdef FEAT_EVAL
815 && !force_abort
816 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818#endif
819 )
820 did_emsg = FALSE;
821
822 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000823 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100824 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 * 3. If a line is given: Make a copy, so we can mess with it.
826 */
827
828#ifdef FEAT_EVAL
829 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000830 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 {
832 /* Each '|' separated command is stored separately in lines_ga, to
833 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100834 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835
836 /* Check if a function has returned or, unless it has an unclosed
837 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000838 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000840# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000841 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000842 func_line_end(real_cookie);
843# endif
844 if (func_has_ended(real_cookie))
845 {
846 retval = FAIL;
847 break;
848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000850#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000851 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100852 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000853 script_line_end();
854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
856 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100857 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 {
859 retval = FAIL;
860 break;
861 }
862
863 /* If breakpoints have been added/deleted need to check for it. */
864 if (breakpoint != NULL && dbg_tick != NULL
865 && *dbg_tick != debug_tick)
866 {
867 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100868 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 fname, sourcing_lnum);
870 *dbg_tick = debug_tick;
871 }
872
873 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
874 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
875
876 /* Did we encounter a breakpoint? */
877 if (breakpoint != NULL && *breakpoint != 0
878 && *breakpoint <= sourcing_lnum)
879 {
880 dbg_breakpoint(fname, sourcing_lnum);
881 /* Find next breakpoint. */
882 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 fname, sourcing_lnum);
885 *dbg_tick = debug_tick;
886 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000887# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000888 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000889 {
890 if (getline_is_func)
891 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100892 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000893 script_line_start();
894 }
895# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 }
897
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000898 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000900 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100901 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 * below, so that it stores lines in or reads them from
903 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000904 * while/for loop. */
905 cmd_getline = get_loop_line;
906 cmd_cookie = (void *)&cmd_loop_cookie;
907 cmd_loop_cookie.lines_gap = &lines_ga;
908 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100909 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000910 cmd_loop_cookie.cookie = cookie;
911 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 }
913 else
914 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100915 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 cmd_cookie = cookie;
917 }
918#endif
919
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100920 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if (next_cmdline == NULL)
922 {
923 /*
924 * Need to set msg_didout for the first line after an ":if",
925 * otherwise the ":if" will be overwritten.
926 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100927 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100929 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930#ifdef FEAT_EVAL
931 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
932#else
933 0
934#endif
935 )) == NULL)
936 {
937 /* Don't call wait_return for aborted command line. The NULL
938 * returned for the end of a sourced file or executed function
939 * doesn't do this. */
940 if (KeyTyped && !(flags & DOCMD_REPEAT))
941 need_wait_return = FALSE;
942 retval = FAIL;
943 break;
944 }
945 used_getline = TRUE;
946
947 /*
948 * Keep the first typed line. Clear it when more lines are typed.
949 */
950 if (flags & DOCMD_KEEPLINE)
951 {
952 vim_free(repeat_cmdline);
953 if (count == 0)
954 repeat_cmdline = vim_strsave(next_cmdline);
955 else
956 repeat_cmdline = NULL;
957 }
958 }
959
960 /* 3. Make a copy of the command so we can mess with it. */
961 else if (cmdline_copy == NULL)
962 {
963 next_cmdline = vim_strsave(next_cmdline);
964 if (next_cmdline == NULL)
965 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100966 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 retval = FAIL;
968 break;
969 }
970 }
971 cmdline_copy = next_cmdline;
972
973#ifdef FEAT_EVAL
974 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000975 * Save the current line when inside a ":while" or ":for", and when
976 * the command looks like a ":while" or ":for", because we may need it
977 * later. When there is a '|' and another command, it is stored
978 * separately, because we need to be able to jump back to it from an
979 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000981 if (current_line == lines_ga.ga_len
982 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000984 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 {
986 retval = FAIL;
987 break;
988 }
989 }
990 did_endif = FALSE;
991#endif
992
993 if (count++ == 0)
994 {
995 /*
996 * All output from the commands is put below each other, without
997 * waiting for a return. Don't do this when executing commands
998 * from a script or when being called recursive (e.g. for ":e
999 * +command file").
1000 */
1001 if (!(flags & DOCMD_NOWAIT) && !recursive)
1002 {
1003 msg_didout_before_start = msg_didout;
1004 msg_didany = FALSE; /* no output yet */
1005 msg_start();
1006 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001007 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 ++RedrawingDisabled;
1009 did_inc = TRUE;
1010 }
1011 }
1012
1013 if (p_verbose >= 15 && sourcing_name != NULL)
1014 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001016 verbose_enter_scroll();
1017
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001018 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001020 if (msg_silent == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001021 msg_puts("\n"); /* don't overwrite this */
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001022
1023 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 --no_wait_return;
1025 }
1026
1027 /*
1028 * 2. Execute one '|' separated command.
1029 * do_one_cmd() will return NULL if there is no trailing '|'.
1030 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1031 */
1032 ++recursive;
1033 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1034#ifdef FEAT_EVAL
1035 &cstack,
1036#endif
1037 cmd_getline, cmd_cookie);
1038 --recursive;
1039
1040#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001041 if (cmd_cookie == (void *)&cmd_loop_cookie)
1042 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045#endif
1046
1047 if (next_cmdline == NULL)
1048 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001049 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050#ifdef FEAT_CMDHIST
1051 /*
1052 * If the command was typed, remember it for the ':' register.
1053 * Do this AFTER executing the command to make :@: work.
1054 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001055 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 && new_last_cmdline != NULL)
1057 {
1058 vim_free(last_cmdline);
1059 last_cmdline = new_last_cmdline;
1060 new_last_cmdline = NULL;
1061 }
1062#endif
1063 }
1064 else
1065 {
1066 /* need to copy the command after the '|' to cmdline_copy, for the
1067 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001068 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 next_cmdline = cmdline_copy;
1070 }
1071
1072
1073#ifdef FEAT_EVAL
1074 /* reset did_emsg for a function that is not aborted by an error */
1075 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001076 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 && !func_has_abort(real_cookie))
1078 did_emsg = FALSE;
1079
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001080 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
1082 ++current_line;
1083
1084 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001085 * An ":endwhile", ":endfor" and ":continue" is handled here.
1086 * If we were executing commands, jump back to the ":while" or
1087 * ":for".
1088 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001090 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001092 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 /* Jump back to the matching ":while" or ":for". Be careful
1095 * not to use a cs_line[] from an entry that isn't a ":while"
1096 * or ":for": It would make "current_line" invalid and can
1097 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 if (!did_emsg && !got_int && !did_throw
1099 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001100 && (cstack.cs_flags[cstack.cs_idx]
1101 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 && cstack.cs_line[cstack.cs_idx] >= 0
1103 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1104 {
1105 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001106 /* remember we jumped there */
1107 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 line_breakcheck(); /* check if CTRL-C typed */
1109
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001110 /* Check for the next breakpoint at or after the ":while"
1111 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 if (breakpoint != NULL)
1113 {
1114 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001115 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 fname,
1117 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1118 *dbg_tick = debug_tick;
1119 }
1120 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001121 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001123 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001125 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1126 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 }
1128 }
1129
1130 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001135 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1137 }
1138 }
1139
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001140 /* Check for the next breakpoint after a watchexpression */
1141 if (breakpoint != NULL && has_watchexpr())
1142 {
1143 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1144 *dbg_tick = debug_tick;
1145 }
1146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 /*
1148 * When not inside any ":while" loop, clear remembered lines.
1149 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001150 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {
1152 if (lines_ga.ga_len > 0)
1153 {
1154 sourcing_lnum =
1155 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1156 free_cmdlines(&lines_ga);
1157 }
1158 current_line = 0;
1159 }
1160
1161 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001162 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1163 * being restored at the ":endtry". Reset them here and set the
1164 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1165 * This includes the case where a missing ":endif", ":endwhile" or
1166 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 cstack.cs_lflags &= ~CSL_HAD_FINA;
1171 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1172 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 did_throw ? (void *)current_exception : NULL);
1174 did_emsg = got_int = did_throw = FALSE;
1175 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1176 }
1177
1178 /* Update global "trylevel" for recursive calls to do_cmdline() from
1179 * within this loop. */
1180 trylevel = initial_trylevel + cstack.cs_trylevel;
1181
1182 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001183 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 * files) is aborted because of an error, an interrupt, or an uncaught
1185 * exception, cancel everything. If it is left normally, reset
1186 * force_abort to get the non-EH compatible abortion behavior for
1187 * the rest of the script.
1188 */
1189 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1190 force_abort = FALSE;
1191
1192 /* Convert an interrupt to an exception if appropriate. */
1193 (void)do_intthrow(&cstack);
1194#endif /* FEAT_EVAL */
1195
1196 }
1197 /*
1198 * Continue executing command lines when:
1199 * - no CTRL-C typed, no aborting error, no exception thrown or try
1200 * conditionals need to be checked for executing finally clauses or
1201 * catching an interrupt exception
1202 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001203 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 * looping for ":source" command or function call.
1205 */
1206 while (!((got_int
1207#ifdef FEAT_EVAL
1208 || (did_emsg && force_abort) || did_throw
1209#endif
1210 )
1211#ifdef FEAT_EVAL
1212 && cstack.cs_trylevel == 0
1213#endif
1214 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001215 && !(did_emsg
1216#ifdef FEAT_EVAL
1217 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001218 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001219 * the :endtry to be missed. */
1220 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1221#endif
1222 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001223 && (getline_equal(fgetline, cookie, getexmodeline)
1224 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 && (next_cmdline != NULL
1226#ifdef FEAT_EVAL
1227 || cstack.cs_idx >= 0
1228#endif
1229 || (flags & DOCMD_REPEAT)));
1230
1231 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001232 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233#ifdef FEAT_EVAL
1234 free_cmdlines(&lines_ga);
1235 ga_clear(&lines_ga);
1236
1237 if (cstack.cs_idx >= 0)
1238 {
1239 /*
1240 * If a sourced file or executed function ran to its end, report the
1241 * unclosed conditional.
1242 */
1243 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001244 && ((getline_equal(fgetline, cookie, getsourceline)
1245 && !source_finished(fgetline, cookie))
1246 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 && !func_has_ended(real_cookie))))
1248 {
1249 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001250 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001252 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001253 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001254 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001256 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 }
1258
1259 /*
1260 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1261 * ":endtry" in a sourced file or executed function. If the try
1262 * conditional is in its finally clause, ignore anything pending.
1263 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001264 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 */
1266 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001267 {
1268 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1269
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001270 if (idx >= 0)
1271 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001272 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1273 &cstack.cs_looplevel);
1274 }
1275 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 trylevel = initial_trylevel;
1277 }
1278
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001279 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1280 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001282 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 ? (char_u *)"endfunction" : (char_u *)NULL);
1284
1285 if (trylevel == 0)
1286 {
1287 /*
1288 * When an exception is being thrown out of the outermost try
1289 * conditional, discard the uncaught exception, disable the conversion
1290 * of interrupts or errors to exceptions, and ensure that no more
1291 * commands are executed.
1292 */
1293 if (did_throw)
1294 {
1295 void *p = NULL;
1296 char_u *saved_sourcing_name;
1297 int saved_sourcing_lnum;
1298 struct msglist *messages = NULL, *next;
1299
1300 /*
1301 * If the uncaught exception is a user exception, report it as an
1302 * error. If it is an error exception, display the saved error
1303 * message now. For an interrupt exception, do nothing; the
1304 * interrupt message is given elsewhere.
1305 */
1306 switch (current_exception->type)
1307 {
1308 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001309 vim_snprintf((char *)IObuff, IOSIZE,
1310 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 current_exception->value);
1312 p = vim_strsave(IObuff);
1313 break;
1314 case ET_ERROR:
1315 messages = current_exception->messages;
1316 current_exception->messages = NULL;
1317 break;
1318 case ET_INTERRUPT:
1319 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
1321
1322 saved_sourcing_name = sourcing_name;
1323 saved_sourcing_lnum = sourcing_lnum;
1324 sourcing_name = current_exception->throw_name;
1325 sourcing_lnum = current_exception->throw_lnum;
1326 current_exception->throw_name = NULL;
1327
1328 discard_current_exception(); /* uses IObuff if 'verbose' */
1329 suppress_errthrow = TRUE;
1330 force_abort = TRUE;
1331
1332 if (messages != NULL)
1333 {
1334 do
1335 {
1336 next = messages->next;
1337 emsg(messages->msg);
1338 vim_free(messages->msg);
1339 vim_free(messages);
1340 messages = next;
1341 }
1342 while (messages != NULL);
1343 }
1344 else if (p != NULL)
1345 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001346 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 vim_free(p);
1348 }
1349 vim_free(sourcing_name);
1350 sourcing_name = saved_sourcing_name;
1351 sourcing_lnum = saved_sourcing_lnum;
1352 }
1353
1354 /*
1355 * On an interrupt or an aborting error not converted to an exception,
1356 * disable the conversion of errors to exceptions. (Interrupts are not
1357 * converted any more, here.) This enables also the interrupt message
1358 * when force_abort is set and did_emsg unset in case of an interrupt
1359 * from a finally clause after an error.
1360 */
1361 else if (got_int || (did_emsg && force_abort))
1362 suppress_errthrow = TRUE;
1363 }
1364
1365 /*
1366 * The current cstack will be freed when do_cmdline() returns. An uncaught
1367 * exception will have to be rethrown in the previous cstack. If a function
1368 * has just returned or a script file was just finished and the previous
1369 * cstack belongs to the same function or, respectively, script file, it
1370 * will have to be checked for finally clauses to be executed due to the
1371 * ":return" or ":finish". This is done in do_one_cmd().
1372 */
1373 if (did_throw)
1374 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001375 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001377 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 && ex_nesting_level > func_level(real_cookie) + 1))
1379 {
1380 if (!did_throw)
1381 check_cstack = TRUE;
1382 }
1383 else
1384 {
1385 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001386 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 --ex_nesting_level;
1388 /*
1389 * Go to debug mode when returning from a function in which we are
1390 * single-stepping.
1391 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001392 if ((getline_equal(fgetline, cookie, getsourceline)
1393 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001395 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 ? (char_u *)_("End of sourced file")
1397 : (char_u *)_("End of function"));
1398 }
1399
1400 /*
1401 * Restore the exception environment (done after returning from the
1402 * debugger).
1403 */
1404 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001405 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406
1407 msg_list = saved_msg_list;
1408#endif /* FEAT_EVAL */
1409
1410 /*
1411 * If there was too much output to fit on the command line, ask the user to
1412 * hit return before redrawing the screen. With the ":global" command we do
1413 * this only once after the command is finished.
1414 */
1415 if (did_inc)
1416 {
1417 --RedrawingDisabled;
1418 --no_wait_return;
1419 msg_scroll = FALSE;
1420
1421 /*
1422 * When just finished an ":if"-":else" which was typed, no need to
1423 * wait for hit-return. Also for an error situation.
1424 */
1425 if (retval == FAIL
1426#ifdef FEAT_EVAL
1427 || (did_endif && KeyTyped && !did_emsg)
1428#endif
1429 )
1430 {
1431 need_wait_return = FALSE;
1432 msg_didany = FALSE; /* don't wait when restarting edit */
1433 }
1434 else if (need_wait_return)
1435 {
1436 /*
1437 * The msg_start() above clears msg_didout. The wait_return we do
1438 * here should not overwrite the command that may be shown before
1439 * doing that.
1440 */
1441 msg_didout |= msg_didout_before_start;
1442 wait_return(FALSE);
1443 }
1444 }
1445
Bram Moolenaar2a942252012-11-28 23:03:07 +01001446#ifdef FEAT_EVAL
1447 did_endif = FALSE; /* in case do_cmdline used recursively */
1448#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 /*
1450 * Reset if_level, in case a sourced script file contains more ":if" than
1451 * ":endif" (could be ":if x | foo | endif").
1452 */
1453 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001454#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001455
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 --call_depth;
1457 return retval;
1458}
1459
1460#ifdef FEAT_EVAL
1461/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001462 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 */
1464 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001465get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001467 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 wcmd_T *wp;
1469 char_u *line;
1470
1471 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1472 {
1473 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001474 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001476 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 if (cp->getline == NULL)
1478 line = getcmdline(c, 0L, indent);
1479 else
1480 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001481 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 ++cp->current_line;
1483
1484 return line;
1485 }
1486
1487 KeyTyped = FALSE;
1488 ++cp->current_line;
1489 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1490 sourcing_lnum = wp->lnum;
1491 return vim_strsave(wp->line);
1492}
1493
1494/*
1495 * Store a line in "gap" so that a ":while" loop can execute it again.
1496 */
1497 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001498store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
1500 if (ga_grow(gap, 1) == FAIL)
1501 return FAIL;
1502 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1503 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1504 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 return OK;
1506}
1507
1508/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001509 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 */
1511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001512free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
1514 while (gap->ga_len > 0)
1515 {
1516 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1517 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 }
1519}
1520#endif
1521
1522/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001523 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1524 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001527getline_equal(
1528 char_u *(*fgetline)(int, void *, int),
1529 void *cookie UNUSED, /* argument for fgetline() */
1530 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531{
1532#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001533 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001534 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535
Bram Moolenaar89d40322006-08-29 15:30:07 +00001536 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001537 * function that's originally used to obtain the lines. This may be
1538 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001539 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001540 cp = (struct loop_cookie *)cookie;
1541 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 {
1543 gp = cp->getline;
1544 cp = cp->cookie;
1545 }
1546 return gp == func;
1547#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001548 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549#endif
1550}
1551
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001553 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 * getline function. Otherwise return "cookie".
1555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001557getline_cookie(
1558 char_u *(*fgetline)(int, void *, int) UNUSED,
1559 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
Bram Moolenaar13505972019-01-24 15:04:48 +01001561#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001562 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001563 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564
Bram Moolenaar89d40322006-08-29 15:30:07 +00001565 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001566 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001568 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001569 cp = (struct loop_cookie *)cookie;
1570 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 {
1572 gp = cp->getline;
1573 cp = cp->cookie;
1574 }
1575 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001576#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001579}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001581
1582/*
1583 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1584 * ":bwipeout", etc.
1585 * Returns the buffer number.
1586 */
1587 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001588compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001589{
1590 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001591 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001592 int count = offset;
1593
1594 buf = firstbuf;
1595 while (buf->b_next != NULL && buf->b_fnum < lnum)
1596 buf = buf->b_next;
1597 while (count != 0)
1598 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001599 count += (offset < 0) ? 1 : -1;
1600 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1601 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001602 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001603 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001604 if (addr_type == ADDR_LOADED_BUFFERS)
1605 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001606 while (buf->b_ml.ml_mfp == NULL)
1607 {
1608 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1609 if (nextbuf == NULL)
1610 break;
1611 buf = nextbuf;
1612 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001613 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001614 /* we might have gone too far, last buffer is not loadedd */
1615 if (addr_type == ADDR_LOADED_BUFFERS)
1616 while (buf->b_ml.ml_mfp == NULL)
1617 {
1618 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1619 if (nextbuf == NULL)
1620 break;
1621 buf = nextbuf;
1622 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001623 return buf->b_fnum;
1624}
1625
Bram Moolenaarf240e182014-11-27 18:33:02 +01001626 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001627current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001628{
1629 win_T *wp;
1630 int nr = 0;
1631
Bram Moolenaar29323592016-07-24 22:04:11 +02001632 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001633 {
1634 ++nr;
1635 if (wp == win)
1636 break;
1637 }
1638 return nr;
1639}
1640
1641 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001642current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001643{
1644 tabpage_T *tp;
1645 int nr = 0;
1646
Bram Moolenaar29323592016-07-24 22:04:11 +02001647 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001648 {
1649 ++nr;
1650 if (tp == tab)
1651 break;
1652 }
1653 return nr;
1654}
1655
1656# define CURRENT_WIN_NR current_win_nr(curwin)
1657# define LAST_WIN_NR current_win_nr(NULL)
1658# define CURRENT_TAB_NR current_tab_nr(curtab)
1659# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001660
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661/*
1662 * Execute one Ex command.
1663 *
1664 * If 'sourcing' is TRUE, the command will be included in the error message.
1665 *
1666 * 1. skip comment lines and leading space
1667 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001668 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001669 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001670 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001671 * 6. parse arguments
1672 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001674 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 *
1676 * This function may be called recursively!
1677 */
1678#if (_MSC_VER == 1200)
1679/*
Bram Moolenaared203462004-06-16 11:19:22 +00001680 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001682 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683#endif
1684 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001685do_one_cmd(
1686 char_u **cmdlinep,
1687 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001689 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001691 char_u *(*fgetline)(int, void *, int),
1692 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
1694 char_u *p;
1695 linenr_T lnum;
1696 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001697 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001698 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001700 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 cmdmod_T save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001702 int save_reg_executing = reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001704 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
1706 vim_memset(&ea, 0, sizeof(ea));
1707 ea.line1 = 1;
1708 ea.line2 = 1;
1709#ifdef FEAT_EVAL
1710 ++ex_nesting_level;
1711#endif
1712
Bram Moolenaar21691f82012-12-05 19:13:18 +01001713 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 if (quitmore
1715#ifdef FEAT_EVAL
1716 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001717 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001718#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001719 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001720 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 --quitmore;
1722
1723 /*
1724 * Reset browse, confirm, etc.. They are restored when returning, for
1725 * recursive calls.
1726 */
1727 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001729 /* "#!anything" is handled like a comment. */
1730 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1731 goto doend;
1732
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001733/*
1734 * 1. Skip comment lines and leading white space and colons.
1735 * 2. Handle command modifiers.
1736 */
1737 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001739 ea.cmdlinep = cmdlinep;
1740 ea.getline = fgetline;
1741 ea.cookie = cookie;
1742#ifdef FEAT_EVAL
1743 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001745 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001746 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001748 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749
1750#ifdef FEAT_EVAL
1751 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1752 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1753#else
1754 ea.skip = (if_level > 0);
1755#endif
1756
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001758 * 3. Skip over the range to find the command. Let "p" point to after it.
1759 *
1760 * We need the command to know what kind of range it uses.
1761 */
1762 cmd = ea.cmd;
1763 ea.cmd = skip_range(ea.cmd, NULL);
1764 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1765 ea.cmd = skipwhite(ea.cmd + 1);
1766 p = find_command(&ea, NULL);
1767
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001768#ifdef FEAT_EVAL
1769# ifdef FEAT_PROFILE
1770 // Count this line for profiling if skip is TRUE.
1771 if (do_profiling == PROF_YES
1772 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1773 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1774 {
1775 int skip = did_emsg || got_int || did_throw;
1776
1777 if (ea.cmdidx == CMD_catch)
1778 skip = !skip && !(cstack->cs_idx >= 0
1779 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1780 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1781 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1782 skip = skip || !(cstack->cs_idx >= 0
1783 && !(cstack->cs_flags[cstack->cs_idx]
1784 & (CSF_ACTIVE | CSF_TRUE)));
1785 else if (ea.cmdidx == CMD_finally)
1786 skip = FALSE;
1787 else if (ea.cmdidx != CMD_endif
1788 && ea.cmdidx != CMD_endfor
1789 && ea.cmdidx != CMD_endtry
1790 && ea.cmdidx != CMD_endwhile)
1791 skip = ea.skip;
1792
1793 if (!skip)
1794 {
1795 if (getline_equal(fgetline, cookie, get_func_line))
1796 func_line_exec(getline_cookie(fgetline, cookie));
1797 else if (getline_equal(fgetline, cookie, getsourceline))
1798 script_line_exec();
1799 }
1800 }
1801# endif
1802
1803 /* May go to debug mode. If this happens and the ">quit" debug command is
1804 * used, throw an interrupt exception and skip the next command. */
1805 dbg_check_breakpoint(&ea);
1806 if (!ea.skip && got_int)
1807 {
1808 ea.skip = TRUE;
1809 (void)do_intthrow(cstack);
1810 }
1811#endif
1812
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001813/*
1814 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 *
1816 * where 'addr' is:
1817 *
1818 * % (entire file)
1819 * $ [+-NUM]
1820 * 'x [+-NUM] (where x denotes a currently defined mark)
1821 * . [+-NUM]
1822 * [+-NUM]..
1823 * NUM
1824 *
1825 * The ea.cmd pointer is updated to point to the first character following the
1826 * range spec. If an initial address is found, but no second, the upper bound
1827 * is equal to the lower.
1828 */
1829
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01001830 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001831 if (!IS_USER_CMDIDX(ea.cmdidx))
1832 {
1833 if (ea.cmdidx != CMD_SIZE)
1834 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1835 else
1836 ea.addr_type = ADDR_LINES;
1837
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001838 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01001839 if (ea.cmdidx == CMD_wincmd && p != NULL)
1840 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001841 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001842
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001843 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001844 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001845 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001848 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 */
1850
1851 /*
1852 * Skip ':' and any white space
1853 */
1854 ea.cmd = skipwhite(ea.cmd);
1855 while (*ea.cmd == ':')
1856 ea.cmd = skipwhite(ea.cmd + 1);
1857
1858 /*
1859 * If we got a line, but no command, then go to the line.
1860 * If we find a '|' or '\n' we set ea.nextcmd.
1861 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001862 if (*ea.cmd == NUL || *ea.cmd == '"'
1863 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 {
1865 /*
1866 * strange vi behaviour:
1867 * ":3" jumps to line 3
1868 * ":3|..." prints line 3
1869 * ":|" prints current line
1870 */
1871 if (ea.skip) /* skip this if inside :if */
1872 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001873 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
1875 ea.cmdidx = CMD_print;
1876 ea.argt = RANGE+COUNT+TRLBAR;
1877 if ((errormsg = invalid_range(&ea)) == NULL)
1878 {
1879 correct_range(&ea);
1880 ex_print(&ea);
1881 }
1882 }
1883 else if (ea.addr_count != 0)
1884 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001885 if (ea.line2 > curbuf->b_ml.ml_line_count)
1886 {
1887 /* With '-' in 'cpoptions' a line number past the file is an
1888 * error, otherwise put it at the end of the file. */
1889 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1890 ea.line2 = -1;
1891 else
1892 ea.line2 = curbuf->b_ml.ml_line_count;
1893 }
1894
1895 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001896 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 else
1898 {
1899 if (ea.line2 == 0)
1900 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 else
1902 curwin->w_cursor.lnum = ea.line2;
1903 beginline(BL_SOL | BL_FIX);
1904 }
1905 }
1906 goto doend;
1907 }
1908
Bram Moolenaard5005162014-08-22 23:05:54 +02001909 /* If this looks like an undefined user command and there are CmdUndefined
1910 * autocommands defined, trigger the matching autocommands. */
1911 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1912 && ASCII_ISUPPER(*ea.cmd)
1913 && has_cmdundefined())
1914 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001915 int ret;
1916
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001917 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001918 while (ASCII_ISALNUM(*p))
1919 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001920 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001921 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1922 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001923 /* If the autocommands did something and didn't cause an error, try
1924 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001925 p = (ret
1926#ifdef FEAT_EVAL
1927 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001928#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001929 ) ? find_command(&ea, NULL) : ea.cmd;
1930 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001931
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932#ifdef FEAT_USR_CMDS
1933 if (p == NULL)
1934 {
1935 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001936 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 goto doend;
1938 }
1939 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001940 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1941 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 {
1943 errormsg = uc_fun_cmd();
1944 goto doend;
1945 }
1946#endif
1947 if (ea.cmdidx == CMD_SIZE)
1948 {
1949 if (!ea.skip)
1950 {
1951 STRCPY(IObuff, _("E492: Not an editor command"));
1952 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001953 {
1954 /* If the modifier was parsed OK the error must be in the
1955 * following command */
1956 if (after_modifier != NULL)
1957 append_command(after_modifier);
1958 else
1959 append_command(*cmdlinep);
1960 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001961 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001962 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 }
1964 goto doend;
1965 }
1966
Bram Moolenaar958636c2014-10-21 20:01:58 +02001967 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1968 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001969#ifdef HAVE_EX_SCRIPT_NI
1970 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1971#endif
1972 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973
1974#ifndef FEAT_EVAL
1975 /*
1976 * When the expression evaluation is disabled, recognize the ":if" and
1977 * ":endif" commands and ignore everything in between it.
1978 */
1979 if (ea.cmdidx == CMD_if)
1980 ++if_level;
1981 if (if_level)
1982 {
1983 if (ea.cmdidx == CMD_endif)
1984 --if_level;
1985 goto doend;
1986 }
1987
1988#endif
1989
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001990 /* forced commands */
1991 if (*p == '!' && ea.cmdidx != CMD_substitute
1992 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 {
1994 ++p;
1995 ea.forceit = TRUE;
1996 }
1997 else
1998 ea.forceit = FALSE;
1999
2000/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002001 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002003 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002004 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005
2006 if (!ea.skip)
2007 {
2008#ifdef HAVE_SANDBOX
2009 if (sandbox != 0 && !(ea.argt & SBOXOK))
2010 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002011 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002012 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 goto doend;
2014 }
2015#endif
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002016 if (restricted != 0 && (ea.argt & RESTRICT))
2017 {
2018 errormsg = _("E981: Command not allowed in rvim");
2019 goto doend;
2020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2022 {
2023 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002024 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 goto doend;
2026 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002027
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002028 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002029 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002031 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002032 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 goto doend;
2034 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002035
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002036 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02002037 * Do allow ":checktime" (it is postponed).
2038 * Do allow ":edit" (check for an argument later).
2039 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002040 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002041 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002042 && ea.cmdidx != CMD_edit
2043 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002044 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002045 && curbuf_locked())
2046 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047
2048 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2049 {
2050 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002051 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 goto doend;
2053 }
2054 }
2055
2056 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2057 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002058 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 goto doend;
2060 }
2061
2062 /*
2063 * Don't complain about the range if it is not used
2064 * (could happen if line_count is accidentally set to 0).
2065 */
2066 if (!ea.skip && !ni)
2067 {
2068 /*
2069 * If the range is backwards, ask for confirmation and, if given, swap
2070 * ea.line1 & ea.line2 so it's forwards again.
2071 * When global command is busy, don't ask, will fail below.
2072 */
2073 if (!global_busy && ea.line1 > ea.line2)
2074 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002075 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002077 if (sourcing || exmode_active)
2078 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002079 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002080 goto doend;
2081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 if (ask_yesno((char_u *)
2083 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002084 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 }
2086 lnum = ea.line1;
2087 ea.line1 = ea.line2;
2088 ea.line2 = lnum;
2089 }
2090 if ((errormsg = invalid_range(&ea)) != NULL)
2091 goto doend;
2092 }
2093
2094 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2095 ea.line2 = 1;
2096
2097 correct_range(&ea);
2098
2099#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002100 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2101 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
2103 /* Put the first line at the start of a closed fold, put the last line
2104 * at the end of a closed fold. */
2105 (void)hasFolding(ea.line1, &ea.line1, NULL);
2106 (void)hasFolding(ea.line2, NULL, &ea.line2);
2107 }
2108#endif
2109
2110#ifdef FEAT_QUICKFIX
2111 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002112 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 * option here, so things like % get expanded.
2114 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002115 p = replace_makeprg(&ea, p, cmdlinep);
2116 if (p == NULL)
2117 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118#endif
2119
2120 /*
2121 * Skip to start of argument.
2122 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2123 */
2124 if (ea.cmdidx == CMD_bang)
2125 ea.arg = p;
2126 else
2127 ea.arg = skipwhite(p);
2128
Bram Moolenaar379fb762018-08-30 15:58:28 +02002129 // ":file" cannot be run with an argument when "curbuf_lock" is set
2130 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2131 goto doend;
2132
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 /*
2134 * Check for "++opt=val" argument.
2135 * Must be first, allow ":w ++enc=utf8 !cmd"
2136 */
2137 if (ea.argt & ARGOPT)
2138 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2139 if (getargopt(&ea) == FAIL && !ni)
2140 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002141 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 goto doend;
2143 }
2144
2145 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2146 {
2147 if (*ea.arg == '>') /* append */
2148 {
2149 if (*++ea.arg != '>') /* typed wrong */
2150 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002151 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 goto doend;
2153 }
2154 ea.arg = skipwhite(ea.arg + 1);
2155 ea.append = TRUE;
2156 }
2157 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2158 {
2159 ++ea.arg;
2160 ea.usefilter = TRUE;
2161 }
2162 }
2163
2164 if (ea.cmdidx == CMD_read)
2165 {
2166 if (ea.forceit)
2167 {
2168 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2169 ea.forceit = FALSE;
2170 }
2171 else if (*ea.arg == '!') /* :r !filter */
2172 {
2173 ++ea.arg;
2174 ea.usefilter = TRUE;
2175 }
2176 }
2177
2178 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2179 {
2180 ea.amount = 1;
2181 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2182 {
2183 ++ea.arg;
2184 ++ea.amount;
2185 }
2186 ea.arg = skipwhite(ea.arg);
2187 }
2188
2189 /*
2190 * Check for "+command" argument, before checking for next command.
2191 * Don't do this for ":read !cmd" and ":write !cmd".
2192 */
2193 if ((ea.argt & EDITCMD) && !ea.usefilter)
2194 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2195
2196 /*
2197 * Check for '|' to separate commands and '"' to start comments.
2198 * Don't do this for ":read !cmd" and ":write !cmd".
2199 */
2200 if ((ea.argt & TRLBAR) && !ea.usefilter)
2201 separate_nextcmd(&ea);
2202
2203 /*
2204 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002205 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2206 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002208 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002209 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002210 || ea.cmdidx == CMD_global
2211 || ea.cmdidx == CMD_vglobal
2212 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 {
2214 for (p = ea.arg; *p; ++p)
2215 {
2216 /* Remove one backslash before a newline, so that it's possible to
2217 * pass a newline to the shell and also a newline that is preceded
2218 * with a backslash. This makes it impossible to end a shell
2219 * command in a backslash, but that doesn't appear useful.
2220 * Halving the number of backslashes is incompatible with previous
2221 * versions. */
2222 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002223 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 else if (*p == '\n')
2225 {
2226 ea.nextcmd = p + 1;
2227 *p = NUL;
2228 break;
2229 }
2230 }
2231 }
2232
2233 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2234 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002235 buf_T *buf;
2236
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002238 switch (ea.addr_type)
2239 {
2240 case ADDR_LINES:
2241 ea.line2 = curbuf->b_ml.ml_line_count;
2242 break;
2243 case ADDR_LOADED_BUFFERS:
2244 buf = firstbuf;
2245 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2246 buf = buf->b_next;
2247 ea.line1 = buf->b_fnum;
2248 buf = lastbuf;
2249 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2250 buf = buf->b_prev;
2251 ea.line2 = buf->b_fnum;
2252 break;
2253 case ADDR_BUFFERS:
2254 ea.line1 = firstbuf->b_fnum;
2255 ea.line2 = lastbuf->b_fnum;
2256 break;
2257 case ADDR_WINDOWS:
2258 ea.line2 = LAST_WIN_NR;
2259 break;
2260 case ADDR_TABS:
2261 ea.line2 = LAST_TAB_NR;
2262 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002263 case ADDR_TABS_RELATIVE:
2264 ea.line2 = 1;
2265 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002266 case ADDR_ARGUMENTS:
2267 if (ARGCOUNT == 0)
2268 ea.line1 = ea.line2 = 0;
2269 else
2270 ea.line2 = ARGCOUNT;
2271 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002272#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002273 case ADDR_QUICKFIX:
2274 ea.line2 = qf_get_size(&ea);
2275 if (ea.line2 == 0)
2276 ea.line2 = 1;
2277 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002278#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
2281
2282 /* accept numbered register only when no count allowed (:put) */
2283 if ( (ea.argt & REGSTR)
2284 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002285 /* Do not allow register = for user commands */
2286 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2288 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002289#ifndef FEAT_CLIPBOARD
2290 /* check these explicitly for a more specific error message */
2291 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002293 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002294 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
2296#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002297 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2298 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002299 {
2300 ea.regname = *ea.arg++;
2301#ifdef FEAT_EVAL
2302 /* for '=' register: accept the rest of the line as an expression */
2303 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2304 {
2305 set_expr_line(vim_strsave(ea.arg));
2306 ea.arg += STRLEN(ea.arg);
2307 }
2308#endif
2309 ea.arg = skipwhite(ea.arg);
2310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 }
2312
2313 /*
2314 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2315 * count, it's a buffer name.
2316 */
2317 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2318 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002319 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 {
2321 n = getdigits(&ea.arg);
2322 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002323 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002325 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 goto doend;
2327 }
2328 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2329 {
2330 ea.line2 = n;
2331 if (ea.addr_count == 0)
2332 ea.addr_count = 1;
2333 }
2334 else
2335 {
2336 ea.line1 = ea.line2;
2337 ea.line2 += n - 1;
2338 ++ea.addr_count;
2339 /*
2340 * Be vi compatible: no error message for out of range.
2341 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002342 if (ea.addr_type == ADDR_LINES
2343 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 ea.line2 = curbuf->b_ml.ml_line_count;
2345 }
2346 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002347
2348 /*
2349 * Check for flags: 'l', 'p' and '#'.
2350 */
2351 if (ea.argt & EXFLAGS)
2352 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002354 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002355 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002357 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 goto doend;
2359 }
2360
2361 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2362 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002363 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 goto doend;
2365 }
2366
2367#ifdef FEAT_EVAL
2368 /*
2369 * Skip the command when it's not going to be executed.
2370 * The commands like :if, :endif, etc. always need to be executed.
2371 * Also make an exception for commands that handle a trailing command
2372 * themselves.
2373 */
2374 if (ea.skip)
2375 {
2376 switch (ea.cmdidx)
2377 {
2378 /* commands that need evaluation */
2379 case CMD_while:
2380 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002381 case CMD_for:
2382 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 case CMD_if:
2384 case CMD_elseif:
2385 case CMD_else:
2386 case CMD_endif:
2387 case CMD_try:
2388 case CMD_catch:
2389 case CMD_finally:
2390 case CMD_endtry:
2391 case CMD_function:
2392 break;
2393
2394 /* Commands that handle '|' themselves. Check: A command should
2395 * either have the TRLBAR flag, appear in this list or appear in
2396 * the list at ":help :bar". */
2397 case CMD_aboveleft:
2398 case CMD_and:
2399 case CMD_belowright:
2400 case CMD_botright:
2401 case CMD_browse:
2402 case CMD_call:
2403 case CMD_confirm:
2404 case CMD_delfunction:
2405 case CMD_djump:
2406 case CMD_dlist:
2407 case CMD_dsearch:
2408 case CMD_dsplit:
2409 case CMD_echo:
2410 case CMD_echoerr:
2411 case CMD_echomsg:
2412 case CMD_echon:
2413 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002414 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 case CMD_help:
2416 case CMD_hide:
2417 case CMD_ijump:
2418 case CMD_ilist:
2419 case CMD_isearch:
2420 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002421 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 case CMD_keepjumps:
2423 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002424 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 case CMD_leftabove:
2426 case CMD_let:
2427 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002428 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002430 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002431 case CMD_noautocmd:
2432 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 case CMD_perl:
2434 case CMD_psearch:
2435 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002436 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002437 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 case CMD_return:
2439 case CMD_rightbelow:
2440 case CMD_ruby:
2441 case CMD_silent:
2442 case CMD_smagic:
2443 case CMD_snomagic:
2444 case CMD_substitute:
2445 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002446 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 case CMD_tcl:
2448 case CMD_throw:
2449 case CMD_tilde:
2450 case CMD_topleft:
2451 case CMD_unlet:
2452 case CMD_verbose:
2453 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002454 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 break;
2456
2457 default: goto doend;
2458 }
2459 }
2460#endif
2461
2462 if (ea.argt & XFILE)
2463 {
2464 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2465 goto doend;
2466 }
2467
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 /*
2469 * Accept buffer name. Cannot be used at the same time with a buffer
2470 * number. Don't do this for a user command.
2471 */
2472 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002473 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 {
2475 /*
2476 * :bdelete, :bwipeout and :bunload take several arguments, separated
2477 * by spaces: find next space (skipping over escaped characters).
2478 * The others take one argument: ignore trailing spaces.
2479 */
2480 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2481 || ea.cmdidx == CMD_bunload)
2482 p = skiptowhite_esc(ea.arg);
2483 else
2484 {
2485 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002486 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 --p;
2488 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002489 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2490 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 if (ea.line2 < 0) /* failed */
2492 goto doend;
2493 ea.addr_count = 1;
2494 ea.arg = skipwhite(p);
2495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496
Bram Moolenaar2be57332018-02-13 18:05:18 +01002497 /* The :try command saves the emsg_silent flag, reset it here when
2498 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002499 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002500 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002501 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002502 if (emsg_silent < 0)
2503 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002504 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002505 }
2506
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002508 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510
2511#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002512 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 {
2514 /*
2515 * Execute a user-defined command.
2516 */
2517 do_ucmd(&ea);
2518 }
2519 else
2520#endif
2521 {
2522 /*
2523 * Call the function to execute the command.
2524 */
2525 ea.errmsg = NULL;
2526 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2527 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002528 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 }
2530
2531#ifdef FEAT_EVAL
2532 /*
2533 * If the command just executed called do_cmdline(), any throw or ":return"
2534 * or ":finish" encountered there must also check the cstack of the still
2535 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2536 * exception, or reanimate a returned function or finished script file and
2537 * return or finish it again.
2538 */
2539 if (need_rethrow)
2540 do_throw(cstack);
2541 else if (check_cstack)
2542 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002543 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002545 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 && current_func_returned())
2547 do_return(&ea, TRUE, FALSE, NULL);
2548 }
2549 need_rethrow = check_cstack = FALSE;
2550#endif
2551
2552doend:
2553 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002556 curwin->w_cursor.col = 0;
2557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
2559 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2560 {
2561 if (sourcing)
2562 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002563 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 {
2565 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002566 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002568 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 }
2570 emsg(errormsg);
2571 }
2572#ifdef FEAT_EVAL
2573 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002574 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2575 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576#endif
2577
Bram Moolenaareffed932018-08-14 13:38:17 +02002578 if (ea.verbose_save >= 0)
2579 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002580
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002581 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002583 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
Bram Moolenaareffed932018-08-14 13:38:17 +02002585 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 {
2587 /* messages could be enabled for a serious error, need to check if the
2588 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002589 if (!did_emsg || msg_silent > ea.save_msg_silent)
2590 msg_silent = ea.save_msg_silent;
2591 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 if (emsg_silent < 0)
2593 emsg_silent = 0;
2594 /* Restore msg_scroll, it's set by file I/O commands, even when no
2595 * message is actually displayed. */
2596 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002597
2598 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2599 * somewhere in the line. Put it back in the first column. */
2600 if (redirecting())
2601 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 }
2603
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002604#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002605 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002606 --sandbox;
2607#endif
2608
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2610 ea.nextcmd = NULL;
2611
2612#ifdef FEAT_EVAL
2613 --ex_nesting_level;
2614#endif
2615
2616 return ea.nextcmd;
2617}
2618#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002619 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620#endif
2621
2622/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002623 * Parse and skip over command modifiers:
2624 * - update eap->cmd
2625 * - store flags in "cmdmod".
2626 * - Set ex_pressedreturn for an empty command line.
2627 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002628 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002629 * - set p_verbose for ":verbose"
2630 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002631 * When "skip_only" is TRUE the global variables are not changed, except for
2632 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002633 * Return FAIL when the command is not to be executed.
2634 * May set "errormsg" to an error message.
2635 */
2636 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002637parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002638{
2639 char_u *p;
2640
2641 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2642 eap->verbose_save = -1;
2643 eap->save_msg_silent = -1;
2644
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002645 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002646 for (;;)
2647 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002648 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2649 ++eap->cmd;
2650
2651 /* in ex mode, an empty line works like :+ */
2652 if (*eap->cmd == NUL && exmode_active
2653 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2654 || getline_equal(eap->getline, eap->cookie, getexline))
2655 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2656 {
2657 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002658 if (!skip_only)
2659 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002660 }
2661
2662 /* ignore comment and empty lines */
2663 if (*eap->cmd == '"')
2664 return FAIL;
2665 if (*eap->cmd == NUL)
2666 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002667 if (!skip_only)
2668 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002669 return FAIL;
2670 }
2671
Bram Moolenaareffed932018-08-14 13:38:17 +02002672 p = skip_range(eap->cmd, NULL);
2673 switch (*p)
2674 {
2675 /* When adding an entry, also modify cmd_exists(). */
2676 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2677 break;
2678 cmdmod.split |= WSP_ABOVE;
2679 continue;
2680
2681 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2682 {
2683 cmdmod.split |= WSP_BELOW;
2684 continue;
2685 }
2686 if (checkforcmd(&eap->cmd, "browse", 3))
2687 {
2688#ifdef FEAT_BROWSE_CMD
2689 cmdmod.browse = TRUE;
2690#endif
2691 continue;
2692 }
2693 if (!checkforcmd(&eap->cmd, "botright", 2))
2694 break;
2695 cmdmod.split |= WSP_BOT;
2696 continue;
2697
2698 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2699 break;
2700#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2701 cmdmod.confirm = TRUE;
2702#endif
2703 continue;
2704
2705 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2706 {
2707 cmdmod.keepmarks = TRUE;
2708 continue;
2709 }
2710 if (checkforcmd(&eap->cmd, "keepalt", 5))
2711 {
2712 cmdmod.keepalt = TRUE;
2713 continue;
2714 }
2715 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2716 {
2717 cmdmod.keeppatterns = TRUE;
2718 continue;
2719 }
2720 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2721 break;
2722 cmdmod.keepjumps = TRUE;
2723 continue;
2724
2725 case 'f': /* only accept ":filter {pat} cmd" */
2726 {
2727 char_u *reg_pat;
2728
2729 if (!checkforcmd(&p, "filter", 4)
2730 || *p == NUL || ends_excmd(*p))
2731 break;
2732 if (*p == '!')
2733 {
2734 cmdmod.filter_force = TRUE;
2735 p = skipwhite(p + 1);
2736 if (*p == NUL || ends_excmd(*p))
2737 break;
2738 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002739 if (skip_only)
2740 p = skip_vimgrep_pat(p, NULL, NULL);
2741 else
2742 // NOTE: This puts a NUL after the pattern.
2743 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002744 if (p == NULL || *p == NUL)
2745 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002746 if (!skip_only)
2747 {
2748 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002749 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002750 if (cmdmod.filter_regmatch.regprog == NULL)
2751 break;
2752 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002753 eap->cmd = p;
2754 continue;
2755 }
2756
2757 /* ":hide" and ":hide | cmd" are not modifiers */
2758 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2759 || *p == NUL || ends_excmd(*p))
2760 break;
2761 eap->cmd = p;
2762 cmdmod.hide = TRUE;
2763 continue;
2764
2765 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2766 {
2767 cmdmod.lockmarks = TRUE;
2768 continue;
2769 }
2770
2771 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2772 break;
2773 cmdmod.split |= WSP_ABOVE;
2774 continue;
2775
2776 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2777 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002778 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002779 {
2780 /* Set 'eventignore' to "all". Restore the
2781 * existing option value later. */
2782 cmdmod.save_ei = vim_strsave(p_ei);
2783 set_string_option_direct((char_u *)"ei", -1,
2784 (char_u *)"all", OPT_FREE, SID_NONE);
2785 }
2786 continue;
2787 }
2788 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2789 break;
2790 cmdmod.noswapfile = TRUE;
2791 continue;
2792
2793 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2794 break;
2795 cmdmod.split |= WSP_BELOW;
2796 continue;
2797
2798 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2799 {
2800#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002801 if (!skip_only)
2802 {
2803 if (!eap->did_sandbox)
2804 ++sandbox;
2805 eap->did_sandbox = TRUE;
2806 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002807#endif
2808 continue;
2809 }
2810 if (!checkforcmd(&eap->cmd, "silent", 3))
2811 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002812 if (!skip_only)
2813 {
2814 if (eap->save_msg_silent == -1)
2815 eap->save_msg_silent = msg_silent;
2816 ++msg_silent;
2817 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002818 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2819 {
2820 /* ":silent!", but not "silent !cmd" */
2821 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002822 if (!skip_only)
2823 {
2824 ++emsg_silent;
2825 ++eap->did_esilent;
2826 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002827 }
2828 continue;
2829
2830 case 't': if (checkforcmd(&p, "tab", 3))
2831 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002832 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002833 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002834 long tabnr = get_address(eap, &eap->cmd,
2835 ADDR_TABS, eap->skip,
2836 skip_only, FALSE, 1);
2837 if (tabnr == MAXLNUM)
2838 cmdmod.tab = tabpage_index(curtab) + 1;
2839 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002841 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2842 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002843 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002844 return FAIL;
2845 }
2846 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002847 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002848 }
2849 eap->cmd = p;
2850 continue;
2851 }
2852 if (!checkforcmd(&eap->cmd, "topleft", 2))
2853 break;
2854 cmdmod.split |= WSP_TOP;
2855 continue;
2856
2857 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2858 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002859 if (!skip_only)
2860 {
2861 if (eap->save_msg_silent == -1)
2862 eap->save_msg_silent = msg_silent;
2863 msg_silent = 0;
2864 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002865 continue;
2866
2867 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2868 {
2869 cmdmod.split |= WSP_VERT;
2870 continue;
2871 }
2872 if (!checkforcmd(&p, "verbose", 4))
2873 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002874 if (!skip_only)
2875 {
2876 if (eap->verbose_save < 0)
2877 eap->verbose_save = p_verbose;
2878 if (vim_isdigit(*eap->cmd))
2879 p_verbose = atoi((char *)eap->cmd);
2880 else
2881 p_verbose = 1;
2882 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002883 eap->cmd = p;
2884 continue;
2885 }
2886 break;
2887 }
2888
2889 return OK;
2890}
2891
2892/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002893 * Free contents of "cmdmod".
2894 */
2895 static void
2896free_cmdmod(void)
2897{
2898 if (cmdmod.save_ei != NULL)
2899 {
2900 /* Restore 'eventignore' to the value before ":noautocmd". */
2901 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2902 OPT_FREE, SID_NONE);
2903 free_string_option(cmdmod.save_ei);
2904 }
2905
2906 if (cmdmod.filter_regmatch.regprog != NULL)
2907 vim_regfree(cmdmod.filter_regmatch.regprog);
2908}
2909
2910/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002911 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002912 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002913 * Return FAIL and set "errormsg" or return OK.
2914 */
2915 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002916parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002917{
2918 int address_count = 1;
2919 linenr_T lnum;
2920
2921 // Repeat for all ',' or ';' separated addresses.
2922 for (;;)
2923 {
2924 eap->line1 = eap->line2;
2925 switch (eap->addr_type)
2926 {
2927 case ADDR_LINES:
2928 // default is current line number
2929 eap->line2 = curwin->w_cursor.lnum;
2930 break;
2931 case ADDR_WINDOWS:
2932 eap->line2 = CURRENT_WIN_NR;
2933 break;
2934 case ADDR_ARGUMENTS:
2935 eap->line2 = curwin->w_arg_idx + 1;
2936 if (eap->line2 > ARGCOUNT)
2937 eap->line2 = ARGCOUNT;
2938 break;
2939 case ADDR_LOADED_BUFFERS:
2940 case ADDR_BUFFERS:
2941 eap->line2 = curbuf->b_fnum;
2942 break;
2943 case ADDR_TABS:
2944 eap->line2 = CURRENT_TAB_NR;
2945 break;
2946 case ADDR_TABS_RELATIVE:
2947 eap->line2 = 1;
2948 break;
2949#ifdef FEAT_QUICKFIX
2950 case ADDR_QUICKFIX:
2951 eap->line2 = qf_get_cur_valid_idx(eap);
2952 break;
2953#endif
2954 }
2955 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002956 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002957 eap->addr_count == 0, address_count++);
2958 if (eap->cmd == NULL) // error detected
2959 return FAIL;
2960 if (lnum == MAXLNUM)
2961 {
2962 if (*eap->cmd == '%') // '%' - all lines
2963 {
2964 ++eap->cmd;
2965 switch (eap->addr_type)
2966 {
2967 case ADDR_LINES:
2968 eap->line1 = 1;
2969 eap->line2 = curbuf->b_ml.ml_line_count;
2970 break;
2971 case ADDR_LOADED_BUFFERS:
2972 {
2973 buf_T *buf = firstbuf;
2974
2975 while (buf->b_next != NULL
2976 && buf->b_ml.ml_mfp == NULL)
2977 buf = buf->b_next;
2978 eap->line1 = buf->b_fnum;
2979 buf = lastbuf;
2980 while (buf->b_prev != NULL
2981 && buf->b_ml.ml_mfp == NULL)
2982 buf = buf->b_prev;
2983 eap->line2 = buf->b_fnum;
2984 break;
2985 }
2986 case ADDR_BUFFERS:
2987 eap->line1 = firstbuf->b_fnum;
2988 eap->line2 = lastbuf->b_fnum;
2989 break;
2990 case ADDR_WINDOWS:
2991 case ADDR_TABS:
2992 if (IS_USER_CMDIDX(eap->cmdidx))
2993 {
2994 eap->line1 = 1;
2995 eap->line2 = eap->addr_type == ADDR_WINDOWS
2996 ? LAST_WIN_NR : LAST_TAB_NR;
2997 }
2998 else
2999 {
3000 // there is no Vim command which uses '%' and
3001 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003002 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003003 return FAIL;
3004 }
3005 break;
3006 case ADDR_TABS_RELATIVE:
Bram Moolenaar51a74542018-12-02 18:21:49 +01003007 case ADDR_OTHER:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003008 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003009 return FAIL;
3010 case ADDR_ARGUMENTS:
3011 if (ARGCOUNT == 0)
3012 eap->line1 = eap->line2 = 0;
3013 else
3014 {
3015 eap->line1 = 1;
3016 eap->line2 = ARGCOUNT;
3017 }
3018 break;
3019#ifdef FEAT_QUICKFIX
3020 case ADDR_QUICKFIX:
3021 eap->line1 = 1;
3022 eap->line2 = qf_get_size(eap);
3023 if (eap->line2 == 0)
3024 eap->line2 = 1;
3025 break;
3026#endif
3027 }
3028 ++eap->addr_count;
3029 }
3030 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3031 {
3032 pos_T *fp;
3033
3034 // '*' - visual area
3035 if (eap->addr_type != ADDR_LINES)
3036 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003037 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003038 return FAIL;
3039 }
3040
3041 ++eap->cmd;
3042 if (!eap->skip)
3043 {
3044 fp = getmark('<', FALSE);
3045 if (check_mark(fp) == FAIL)
3046 return FAIL;
3047 eap->line1 = fp->lnum;
3048 fp = getmark('>', FALSE);
3049 if (check_mark(fp) == FAIL)
3050 return FAIL;
3051 eap->line2 = fp->lnum;
3052 ++eap->addr_count;
3053 }
3054 }
3055 }
3056 else
3057 eap->line2 = lnum;
3058 eap->addr_count++;
3059
3060 if (*eap->cmd == ';')
3061 {
3062 if (!eap->skip)
3063 {
3064 curwin->w_cursor.lnum = eap->line2;
3065 // don't leave the cursor on an illegal line or column
3066 check_cursor();
3067 }
3068 }
3069 else if (*eap->cmd != ',')
3070 break;
3071 ++eap->cmd;
3072 }
3073
3074 // One address given: set start and end lines.
3075 if (eap->addr_count == 1)
3076 {
3077 eap->line1 = eap->line2;
3078 // ... but only implicit: really no address given
3079 if (lnum == MAXLNUM)
3080 eap->addr_count = 0;
3081 }
3082 return OK;
3083}
3084
3085/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003086 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 * If there is a match advance "pp" to the argument and return TRUE.
3088 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003089 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003090checkforcmd(
3091 char_u **pp, /* start of command */
3092 char *cmd, /* name of command */
3093 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094{
3095 int i;
3096
3097 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003098 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 break;
3100 if (i >= len && !isalpha((*pp)[i]))
3101 {
3102 *pp = skipwhite(*pp + i);
3103 return TRUE;
3104 }
3105 return FALSE;
3106}
3107
3108/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003109 * Append "cmd" to the error message in IObuff.
3110 * Takes care of limiting the length and handling 0xa0, which would be
3111 * invisible otherwise.
3112 */
3113 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003114append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003115{
3116 char_u *s = cmd;
3117 char_u *d;
3118
3119 STRCAT(IObuff, ": ");
3120 d = IObuff + STRLEN(IObuff);
3121 while (*s != NUL && d - IObuff < IOSIZE - 7)
3122 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003123 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003124 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003125 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003126 STRCPY(d, "<a0>");
3127 d += 4;
3128 }
3129 else
3130 MB_COPY_CHAR(s, d);
3131 }
3132 *d = NUL;
3133}
3134
3135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003137 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003139 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 * Returns NULL for an ambiguous user command.
3141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003143find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144{
3145 int len;
3146 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003147 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148
3149 /*
3150 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003151 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 * - the 'k' command can directly be followed by any character.
3153 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003154 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003156 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 */
3158 p = eap->cmd;
3159 if (*p == 'k')
3160 {
3161 eap->cmdidx = CMD_k;
3162 ++p;
3163 }
3164 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003165 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3166 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 || p[1] == 'g'
3168 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3169 || p[1] == 'I'
3170 || (p[1] == 'r' && p[2] != 'e')))
3171 {
3172 eap->cmdidx = CMD_substitute;
3173 ++p;
3174 }
3175 else
3176 {
3177 while (ASCII_ISALPHA(*p))
3178 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003179 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003180 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003181 while (ASCII_ISALNUM(*p))
3182 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003183
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 /* check for non-alpha command */
3185 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3186 ++p;
3187 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003188 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3189 {
3190 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3191 * :delete with the 'l' flag. Same for 'p'. */
3192 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003193 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003194 break;
3195 if (i == len - 1)
3196 {
3197 --len;
3198 if (p[-1] == 'l')
3199 eap->flags |= EXFLAG_LIST;
3200 else
3201 eap->flags |= EXFLAG_PRINT;
3202 }
3203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003205 if (ASCII_ISLOWER(eap->cmd[0]))
3206 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003207 int c1 = eap->cmd[0];
3208 int c2 = eap->cmd[1];
3209
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003210 if (command_count != (int)CMD_SIZE)
3211 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003212 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003213 getout(1);
3214 }
3215
3216 /* Use a precomputed index for fast look-up in cmdnames[]
3217 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003218 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3219 if (ASCII_ISLOWER(c2))
3220 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003223 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224
3225 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3226 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3227 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3228 (size_t)len) == 0)
3229 {
3230#ifdef FEAT_EVAL
3231 if (full != NULL
3232 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3233 *full = TRUE;
3234#endif
3235 break;
3236 }
3237
3238#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003239 /* Look for a user defined command as a last resort. Let ":Print" be
3240 * overruled by a user defined command. */
3241 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3242 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003244 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 while (ASCII_ISALNUM(*p))
3246 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003247 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 }
3249#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003250 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 eap->cmdidx = CMD_SIZE;
3252 }
3253
3254 return p;
3255}
3256
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003257#ifdef FEAT_USR_CMDS
3258/*
3259 * Search for a user command that matches "eap->cmd".
3260 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3261 * Return a pointer to just after the command.
3262 * Return NULL if there is no matching command.
3263 */
3264 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003265find_ucmd(
3266 exarg_T *eap,
3267 char_u *p, /* end of the command (possibly including count) */
3268 int *full, /* set to TRUE for a full match */
3269 expand_T *xp, /* used for completion, NULL otherwise */
3270 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003271{
3272 int len = (int)(p - eap->cmd);
3273 int j, k, matchlen = 0;
3274 ucmd_T *uc;
3275 int found = FALSE;
3276 int possible = FALSE;
3277 char_u *cp, *np; /* Point into typed cmd and test name */
3278 garray_T *gap;
3279 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3280 only full match global is accepted. */
3281
3282 /*
3283 * Look for buffer-local user commands first, then global ones.
3284 */
3285 gap = &curbuf->b_ucmds;
3286 for (;;)
3287 {
3288 for (j = 0; j < gap->ga_len; ++j)
3289 {
3290 uc = USER_CMD_GA(gap, j);
3291 cp = eap->cmd;
3292 np = uc->uc_name;
3293 k = 0;
3294 while (k < len && *np != NUL && *cp++ == *np++)
3295 k++;
3296 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3297 {
3298 /* If finding a second match, the command is ambiguous. But
3299 * not if a buffer-local command wasn't a full match and a
3300 * global command is a full match. */
3301 if (k == len && found && *np != NUL)
3302 {
3303 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003304 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003305 amb_local = TRUE;
3306 }
3307
3308 if (!found || (k == len && *np == NUL))
3309 {
3310 /* If we matched up to a digit, then there could
3311 * be another command including the digit that we
3312 * should use instead.
3313 */
3314 if (k == len)
3315 found = TRUE;
3316 else
3317 possible = TRUE;
3318
3319 if (gap == &ucmds)
3320 eap->cmdidx = CMD_USER;
3321 else
3322 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003323 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003324 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003325 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003326
3327# ifdef FEAT_CMDL_COMPL
3328 if (compl != NULL)
3329 *compl = uc->uc_compl;
3330# ifdef FEAT_EVAL
3331 if (xp != NULL)
3332 {
3333 xp->xp_arg = uc->uc_compl_arg;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003334 xp->xp_script_ctx = uc->uc_script_ctx;
3335 xp->xp_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003336 }
3337# endif
3338# endif
3339 /* Do not search for further abbreviations
3340 * if this is an exact match. */
3341 matchlen = k;
3342 if (k == len && *np == NUL)
3343 {
3344 if (full != NULL)
3345 *full = TRUE;
3346 amb_local = FALSE;
3347 break;
3348 }
3349 }
3350 }
3351 }
3352
3353 /* Stop if we found a full match or searched all. */
3354 if (j < gap->ga_len || gap == &ucmds)
3355 break;
3356 gap = &ucmds;
3357 }
3358
3359 /* Only found ambiguous matches. */
3360 if (amb_local)
3361 {
3362 if (xp != NULL)
3363 xp->xp_context = EXPAND_UNSUCCESSFUL;
3364 return NULL;
3365 }
3366
3367 /* The match we found may be followed immediately by a number. Move "p"
3368 * back to point to it. */
3369 if (found || possible)
3370 return p + (matchlen - len);
3371 return p;
3372}
3373#endif
3374
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003376static struct cmdmod
3377{
3378 char *name;
3379 int minlen;
3380 int has_count; /* :123verbose :3tab */
3381} cmdmods[] = {
3382 {"aboveleft", 3, FALSE},
3383 {"belowright", 3, FALSE},
3384 {"botright", 2, FALSE},
3385 {"browse", 3, FALSE},
3386 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003387 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003388 {"hide", 3, FALSE},
3389 {"keepalt", 5, FALSE},
3390 {"keepjumps", 5, FALSE},
3391 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003392 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003393 {"leftabove", 5, FALSE},
3394 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003395 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003396 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003397 {"rightbelow", 6, FALSE},
3398 {"sandbox", 3, FALSE},
3399 {"silent", 3, FALSE},
3400 {"tab", 3, TRUE},
3401 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003402 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003403 {"verbose", 4, TRUE},
3404 {"vertical", 4, FALSE},
3405};
3406
3407/*
3408 * Return length of a command modifier (including optional count).
3409 * Return zero when it's not a modifier.
3410 */
3411 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003412modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003413{
3414 int i, j;
3415 char_u *p = cmd;
3416
3417 if (VIM_ISDIGIT(*cmd))
3418 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003419 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003420 {
3421 for (j = 0; p[j] != NUL; ++j)
3422 if (p[j] != cmdmods[i].name[j])
3423 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003424 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003425 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003426 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003427 }
3428 return 0;
3429}
3430
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431/*
3432 * Return > 0 if an Ex command "name" exists.
3433 * Return 2 if there is an exact match.
3434 * Return 3 if there is an ambiguous match.
3435 */
3436 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003437cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
3439 exarg_T ea;
3440 int full = FALSE;
3441 int i;
3442 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003443 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
3445 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003446 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 {
3448 for (j = 0; name[j] != NUL; ++j)
3449 if (name[j] != cmdmods[i].name[j])
3450 break;
3451 if (name[j] == NUL && j >= cmdmods[i].minlen)
3452 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3453 }
3454
Bram Moolenaara9587612006-05-04 21:47:50 +00003455 /* Check built-in commands and user defined commands.
3456 * For ":2match" and ":3match" we need to skip the number. */
3457 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003459 p = find_command(&ea, &full);
3460 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003462 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3463 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003464 if (*skipwhite(p) != NUL)
3465 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3467}
3468#endif
3469
3470/*
3471 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3472 * we don't need/want deleted. Maybe this could be done better if we didn't
3473 * repeat all this stuff. The only problem is that they may not stay
3474 * perfectly compatible with each other, but then the command line syntax
3475 * probably won't change that much -- webb.
3476 */
3477 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003478set_one_cmd_context(
3479 expand_T *xp,
3480 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481{
3482 char_u *p;
3483 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003484 int len = 0;
3485 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3487 int compl = EXPAND_NOTHING;
3488#endif
3489#ifdef FEAT_CMDL_COMPL
3490 int delim;
3491#endif
3492 int forceit = FALSE;
3493 int usefilter = FALSE; /* filter instead of file name */
3494
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003495 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 xp->xp_pattern = buff;
3497 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003498 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499
3500/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003501 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 */
3503 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3504 ;
3505 xp->xp_pattern = cmd;
3506
3507 if (*cmd == NUL)
3508 return NULL;
3509 if (*cmd == '"') /* ignore comment lines */
3510 {
3511 xp->xp_context = EXPAND_NOTHING;
3512 return NULL;
3513 }
3514
3515/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003516 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 */
3518 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 xp->xp_pattern = cmd;
3520 if (*cmd == NUL)
3521 return NULL;
3522 if (*cmd == '"')
3523 {
3524 xp->xp_context = EXPAND_NOTHING;
3525 return NULL;
3526 }
3527
3528 if (*cmd == '|' || *cmd == '\n')
3529 return cmd + 1; /* There's another command */
3530
3531 /*
3532 * Isolate the command and search for it in the command table.
3533 * Exceptions:
3534 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003535 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3537 */
3538 if (*cmd == 'k' && cmd[1] != 'e')
3539 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003540 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 p = cmd + 1;
3542 }
3543 else
3544 {
3545 p = cmd;
3546 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3547 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003548 /* a user command may contain digits */
3549 if (ASCII_ISUPPER(cmd[0]))
3550 while (ASCII_ISALNUM(*p) || *p == '*')
3551 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003552 /* for python 3.x: ":py3*" commands completion */
3553 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003554 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003555 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003556 while (ASCII_ISALPHA(*p) || *p == '*')
3557 ++p;
3558 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003559 /* check for non-alpha command */
3560 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3561 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003562 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003564 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
3566 xp->xp_context = EXPAND_UNSUCCESSFUL;
3567 return NULL;
3568 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003569 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003570 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3571 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3572 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 break;
3574
3575#ifdef FEAT_USR_CMDS
3576 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3578 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579#endif
3580 }
3581
3582 /*
3583 * If the cursor is touching the command, and it ends in an alpha-numeric
3584 * character, complete the command name.
3585 */
3586 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3587 return NULL;
3588
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003589 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 {
3591 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3592 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003593 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 p = cmd + 1;
3595 }
3596#ifdef FEAT_USR_CMDS
3597 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3598 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003599 ea.cmd = cmd;
3600 p = find_ucmd(&ea, p, NULL, xp,
3601# if defined(FEAT_CMDL_COMPL)
3602 &compl
3603# else
3604 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003606 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003607 if (p == NULL)
3608 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 }
3610#endif
3611 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003612 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
3614 /* Not still touching the command and it was an illegal one */
3615 xp->xp_context = EXPAND_UNSUCCESSFUL;
3616 return NULL;
3617 }
3618
3619 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3620
3621 if (*p == '!') /* forced commands */
3622 {
3623 forceit = TRUE;
3624 ++p;
3625 }
3626
3627/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003628 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003630 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003631 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
3633 arg = skipwhite(p);
3634
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003635 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 {
3637 if (*arg == '>') /* append */
3638 {
3639 if (*++arg == '>')
3640 ++arg;
3641 arg = skipwhite(arg);
3642 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003643 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 {
3645 ++arg;
3646 usefilter = TRUE;
3647 }
3648 }
3649
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003650 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
3652 usefilter = forceit; /* :r! filter if forced */
3653 if (*arg == '!') /* :r !filter */
3654 {
3655 ++arg;
3656 usefilter = TRUE;
3657 }
3658 }
3659
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003660 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 {
3662 while (*arg == *cmd) /* allow any number of '>' or '<' */
3663 ++arg;
3664 arg = skipwhite(arg);
3665 }
3666
3667 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003668 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 {
3670 /* Check if we're in the +command */
3671 p = arg + 1;
3672 arg = skip_cmd_arg(arg, FALSE);
3673
3674 /* Still touching the command after '+'? */
3675 if (*arg == NUL)
3676 return p;
3677
3678 /* Skip space(s) after +command to get to the real argument */
3679 arg = skipwhite(arg);
3680 }
3681
3682 /*
3683 * Check for '|' to separate commands and '"' to start comments.
3684 * Don't do this for ":read !cmd" and ":write !cmd".
3685 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003686 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 {
3688 p = arg;
3689 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003690 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 p += 2;
3692 while (*p)
3693 {
3694 if (*p == Ctrl_V)
3695 {
3696 if (p[1] != NUL)
3697 ++p;
3698 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003699 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 || *p == '|' || *p == '\n')
3701 {
3702 if (*(p - 1) != '\\')
3703 {
3704 if (*p == '|' || *p == '\n')
3705 return p + 1;
3706 return NULL; /* It's a comment */
3707 }
3708 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003709 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711 }
3712
3713 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003714 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 vim_strchr((char_u *)"|\"", *arg) == NULL)
3716 return NULL;
3717
3718 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003719 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003721 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003722 while (*p && p < buff + len)
3723 {
3724 if (*p == ' ' || *p == TAB)
3725 {
3726 /* argument starts after a space */
3727 xp->xp_pattern = ++p;
3728 }
3729 else
3730 {
3731 if (*p == '\\' && *(p + 1) != NUL)
3732 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003733 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003734 }
3735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003737 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003739 int c;
3740 int in_quote = FALSE;
3741 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742
3743 /*
3744 * Allow spaces within back-quotes to count as part of the argument
3745 * being expanded.
3746 */
3747 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003748 p = xp->xp_pattern;
3749 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003751 if (has_mbyte)
3752 c = mb_ptr2char(p);
3753 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003754 c = *p;
3755 if (c == '\\' && p[1] != NUL)
3756 ++p;
3757 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 {
3759 if (!in_quote)
3760 {
3761 xp->xp_pattern = p;
3762 bow = p + 1;
3763 }
3764 in_quote = !in_quote;
3765 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003766 /* An argument can contain just about everything, except
3767 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003768 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003769#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003770 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003771#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003772 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003773 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003774 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003775 while (*p != NUL)
3776 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003777 if (has_mbyte)
3778 c = mb_ptr2char(p);
3779 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003780 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003781 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003782 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003783 if (has_mbyte)
3784 len = (*mb_ptr2len)(p);
3785 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003786 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003787 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003788 }
3789 if (in_quote)
3790 bow = p;
3791 else
3792 xp->xp_pattern = p;
3793 p -= len;
3794 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003795 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 }
3797
3798 /*
3799 * If we are still inside the quotes, and we passed a space, just
3800 * expand from there.
3801 */
3802 if (bow != NULL && in_quote)
3803 xp->xp_pattern = bow;
3804 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003805
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003806 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003807 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003808 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003809#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003810 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003811#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003812 /* When still after the command name expand executables. */
3813 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003814 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816
3817 /* Check for environment variable */
3818 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003819#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 || *xp->xp_pattern == '%'
3821#endif
3822 )
3823 {
3824 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3825 if (!vim_isIDc(*p))
3826 break;
3827 if (*p == NUL)
3828 {
3829 xp->xp_context = EXPAND_ENV_VARS;
3830 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003831#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3832 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003833 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003834 compl = EXPAND_ENV_VARS;
3835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003838#if defined(FEAT_CMDL_COMPL)
3839 /* Check for user names */
3840 if (*xp->xp_pattern == '~')
3841 {
3842 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3843 ;
3844 /* Complete ~user only if it partially matches a user name.
3845 * A full match ~user<Tab> will be replaced by user's home
3846 * directory i.e. something like ~user<Tab> -> /home/user/ */
3847 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003848 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003849 {
3850 xp->xp_context = EXPAND_USER;
3851 ++xp->xp_pattern;
3852 }
3853 }
3854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 }
3856
3857/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003858 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003860 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003862 case CMD_find:
3863 case CMD_sfind:
3864 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003865 if (xp->xp_context == EXPAND_FILES)
3866 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003867 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 case CMD_cd:
3869 case CMD_chdir:
3870 case CMD_lcd:
3871 case CMD_lchdir:
3872 if (xp->xp_context == EXPAND_FILES)
3873 xp->xp_context = EXPAND_DIRECTORIES;
3874 break;
3875 case CMD_help:
3876 xp->xp_context = EXPAND_HELP;
3877 xp->xp_pattern = arg;
3878 break;
3879
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003880 /* Command modifiers: return the argument.
3881 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003883 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 case CMD_belowright:
3885 case CMD_botright:
3886 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003887 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003888 case CMD_cdo:
3889 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003891 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 case CMD_folddoclosed:
3893 case CMD_folddoopen:
3894 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003895 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 case CMD_keepjumps:
3897 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003898 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003899 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003901 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003903 case CMD_noautocmd:
3904 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003906 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003908 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003909 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 case CMD_topleft:
3911 case CMD_verbose:
3912 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003913 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 return arg;
3915
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003916 case CMD_filter:
3917 if (*arg != NUL)
3918 arg = skip_vimgrep_pat(arg, NULL, NULL);
3919 if (arg == NULL || *arg == NUL)
3920 {
3921 xp->xp_context = EXPAND_NOTHING;
3922 return NULL;
3923 }
3924 return skipwhite(arg);
3925
Bram Moolenaar4f688582007-07-24 12:34:30 +00003926#ifdef FEAT_CMDL_COMPL
3927# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 case CMD_match:
3929 if (*arg == NUL || !ends_excmd(*arg))
3930 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003931 /* also complete "None" */
3932 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 arg = skipwhite(skiptowhite(arg));
3934 if (*arg != NUL)
3935 {
3936 xp->xp_context = EXPAND_NOTHING;
3937 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3938 }
3939 }
3940 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003941# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943/*
3944 * All completion for the +cmdline_compl feature goes here.
3945 */
3946
3947# ifdef FEAT_USR_CMDS
3948 case CMD_command:
3949 /* Check for attributes */
3950 while (*arg == '-')
3951 {
3952 arg++; /* Skip "-" */
3953 p = skiptowhite(arg);
3954 if (*p == NUL)
3955 {
3956 /* Cursor is still in the attribute */
3957 p = vim_strchr(arg, '=');
3958 if (p == NULL)
3959 {
3960 /* No "=", so complete attribute names */
3961 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3962 xp->xp_pattern = arg;
3963 return NULL;
3964 }
3965
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003966 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 * their arguments as well.
3968 */
3969 if (STRNICMP(arg, "complete", p - arg) == 0)
3970 {
3971 xp->xp_context = EXPAND_USER_COMPLETE;
3972 xp->xp_pattern = p + 1;
3973 return NULL;
3974 }
3975 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3976 {
3977 xp->xp_context = EXPAND_USER_NARGS;
3978 xp->xp_pattern = p + 1;
3979 return NULL;
3980 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003981 else if (STRNICMP(arg, "addr", p - arg) == 0)
3982 {
3983 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3984 xp->xp_pattern = p + 1;
3985 return NULL;
3986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 return NULL;
3988 }
3989 arg = skipwhite(p);
3990 }
3991
3992 /* After the attributes comes the new command name */
3993 p = skiptowhite(arg);
3994 if (*p == NUL)
3995 {
3996 xp->xp_context = EXPAND_USER_COMMANDS;
3997 xp->xp_pattern = arg;
3998 break;
3999 }
4000
4001 /* And finally comes a normal command */
4002 return skipwhite(p);
4003
4004 case CMD_delcommand:
4005 xp->xp_context = EXPAND_USER_COMMANDS;
4006 xp->xp_pattern = arg;
4007 break;
4008# endif
4009
4010 case CMD_global:
4011 case CMD_vglobal:
4012 delim = *arg; /* get the delimiter */
4013 if (delim)
4014 ++arg; /* skip delimiter if there is one */
4015
4016 while (arg[0] != NUL && arg[0] != delim)
4017 {
4018 if (arg[0] == '\\' && arg[1] != NUL)
4019 ++arg;
4020 ++arg;
4021 }
4022 if (arg[0] != NUL)
4023 return arg + 1;
4024 break;
4025 case CMD_and:
4026 case CMD_substitute:
4027 delim = *arg;
4028 if (delim)
4029 {
4030 /* skip "from" part */
4031 ++arg;
4032 arg = skip_regexp(arg, delim, p_magic, NULL);
4033 }
4034 /* skip "to" part */
4035 while (arg[0] != NUL && arg[0] != delim)
4036 {
4037 if (arg[0] == '\\' && arg[1] != NUL)
4038 ++arg;
4039 ++arg;
4040 }
4041 if (arg[0] != NUL) /* skip delimiter */
4042 ++arg;
4043 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4044 ++arg;
4045 if (arg[0] != NUL)
4046 return arg;
4047 break;
4048 case CMD_isearch:
4049 case CMD_dsearch:
4050 case CMD_ilist:
4051 case CMD_dlist:
4052 case CMD_ijump:
4053 case CMD_psearch:
4054 case CMD_djump:
4055 case CMD_isplit:
4056 case CMD_dsplit:
4057 arg = skipwhite(skipdigits(arg)); /* skip count */
4058 if (*arg == '/') /* Match regexp, not just whole words */
4059 {
4060 for (++arg; *arg && *arg != '/'; arg++)
4061 if (*arg == '\\' && arg[1] != NUL)
4062 arg++;
4063 if (*arg)
4064 {
4065 arg = skipwhite(arg + 1);
4066
4067 /* Check for trailing illegal characters */
4068 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4069 xp->xp_context = EXPAND_NOTHING;
4070 else
4071 return arg;
4072 }
4073 }
4074 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004075
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 case CMD_autocmd:
4077 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004079 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 case CMD_set:
4082 set_context_in_set_cmd(xp, arg, 0);
4083 break;
4084 case CMD_setglobal:
4085 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4086 break;
4087 case CMD_setlocal:
4088 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4089 break;
4090 case CMD_tag:
4091 case CMD_stag:
4092 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004093 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 case CMD_tselect:
4095 case CMD_stselect:
4096 case CMD_ptselect:
4097 case CMD_tjump:
4098 case CMD_stjump:
4099 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004100 if (*p_wop != NUL)
4101 xp->xp_context = EXPAND_TAGS_LISTFILES;
4102 else
4103 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 xp->xp_pattern = arg;
4105 break;
4106 case CMD_augroup:
4107 xp->xp_context = EXPAND_AUGROUP;
4108 xp->xp_pattern = arg;
4109 break;
4110#ifdef FEAT_SYN_HL
4111 case CMD_syntax:
4112 set_context_in_syntax_cmd(xp, arg);
4113 break;
4114#endif
4115#ifdef FEAT_EVAL
4116 case CMD_let:
4117 case CMD_if:
4118 case CMD_elseif:
4119 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004120 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 case CMD_echo:
4122 case CMD_echon:
4123 case CMD_execute:
4124 case CMD_echomsg:
4125 case CMD_echoerr:
4126 case CMD_call:
4127 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004128 case CMD_cexpr:
4129 case CMD_caddexpr:
4130 case CMD_cgetexpr:
4131 case CMD_lexpr:
4132 case CMD_laddexpr:
4133 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004134 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 break;
4136
4137 case CMD_unlet:
4138 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4139 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02004140
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 xp->xp_context = EXPAND_USER_VARS;
4142 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02004143
4144 if (*xp->xp_pattern == '$')
4145 {
4146 xp->xp_context = EXPAND_ENV_VARS;
4147 ++xp->xp_pattern;
4148 }
4149
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 break;
4151
4152 case CMD_function:
4153 case CMD_delfunction:
4154 xp->xp_context = EXPAND_USER_FUNC;
4155 xp->xp_pattern = arg;
4156 break;
4157
4158 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004159 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 break;
4161#endif
4162 case CMD_highlight:
4163 set_context_in_highlight_cmd(xp, arg);
4164 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004165#ifdef FEAT_CSCOPE
4166 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004167 case CMD_lcscope:
4168 case CMD_scscope:
4169 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004170 break;
4171#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004172#ifdef FEAT_SIGNS
4173 case CMD_sign:
4174 set_context_in_sign_cmd(xp, arg);
4175 break;
4176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 case CMD_bdelete:
4178 case CMD_bwipeout:
4179 case CMD_bunload:
4180 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4181 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004182 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 case CMD_buffer:
4184 case CMD_sbuffer:
4185 case CMD_checktime:
4186 xp->xp_context = EXPAND_BUFFERS;
4187 xp->xp_pattern = arg;
4188 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189#ifdef FEAT_USR_CMDS
4190 case CMD_USER:
4191 case CMD_USER_BUF:
4192 if (compl != EXPAND_NOTHING)
4193 {
4194 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004195 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 {
4197# ifdef FEAT_MENU
4198 if (compl == EXPAND_MENUS)
4199 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4200# endif
4201 if (compl == EXPAND_COMMANDS)
4202 return arg;
4203 if (compl == EXPAND_MAPPINGS)
4204 return set_context_in_map_cmd(xp, (char_u *)"map",
4205 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004206 /* Find start of last argument. */
4207 p = arg;
4208 while (*p)
4209 {
4210 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004211 /* argument starts after a space */
4212 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004213 else if (*p == '\\' && *(p + 1) != NUL)
4214 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004215 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 xp->xp_pattern = arg;
4218 }
4219 xp->xp_context = compl;
4220 }
4221 break;
4222#endif
4223 case CMD_map: case CMD_noremap:
4224 case CMD_nmap: case CMD_nnoremap:
4225 case CMD_vmap: case CMD_vnoremap:
4226 case CMD_omap: case CMD_onoremap:
4227 case CMD_imap: case CMD_inoremap:
4228 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004229 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004230 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004231 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004232 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004234 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 case CMD_unmap:
4236 case CMD_nunmap:
4237 case CMD_vunmap:
4238 case CMD_ounmap:
4239 case CMD_iunmap:
4240 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004241 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004242 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004243 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004244 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004246 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004247 case CMD_mapclear:
4248 case CMD_nmapclear:
4249 case CMD_vmapclear:
4250 case CMD_omapclear:
4251 case CMD_imapclear:
4252 case CMD_cmapclear:
4253 case CMD_lmapclear:
4254 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004255 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004256 case CMD_xmapclear:
4257 xp->xp_context = EXPAND_MAPCLEAR;
4258 xp->xp_pattern = arg;
4259 break;
4260
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 case CMD_abbreviate: case CMD_noreabbrev:
4262 case CMD_cabbrev: case CMD_cnoreabbrev:
4263 case CMD_iabbrev: case CMD_inoreabbrev:
4264 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004265 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 case CMD_unabbreviate:
4267 case CMD_cunabbrev:
4268 case CMD_iunabbrev:
4269 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004270 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271#ifdef FEAT_MENU
4272 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4273 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4274 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4275 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4276 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4277 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4278 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004279 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 case CMD_tmenu: case CMD_tunmenu:
4281 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4282 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4283#endif
4284
4285 case CMD_colorscheme:
4286 xp->xp_context = EXPAND_COLORS;
4287 xp->xp_pattern = arg;
4288 break;
4289
4290 case CMD_compiler:
4291 xp->xp_context = EXPAND_COMPILER;
4292 xp->xp_pattern = arg;
4293 break;
4294
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004295 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004296 xp->xp_context = EXPAND_OWNSYNTAX;
4297 xp->xp_pattern = arg;
4298 break;
4299
4300 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004301 xp->xp_context = EXPAND_FILETYPE;
4302 xp->xp_pattern = arg;
4303 break;
4304
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004305 case CMD_packadd:
4306 xp->xp_context = EXPAND_PACKADD;
4307 xp->xp_pattern = arg;
4308 break;
4309
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004310#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004312 p = skiptowhite(arg);
4313 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 {
4315 xp->xp_context = EXPAND_LANGUAGE;
4316 xp->xp_pattern = arg;
4317 }
4318 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004319 {
4320 if ( STRNCMP(arg, "messages", p - arg) == 0
4321 || STRNCMP(arg, "ctype", p - arg) == 0
4322 || STRNCMP(arg, "time", p - arg) == 0)
4323 {
4324 xp->xp_context = EXPAND_LOCALES;
4325 xp->xp_pattern = skipwhite(p);
4326 }
4327 else
4328 xp->xp_context = EXPAND_NOTHING;
4329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 break;
4331#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004332#if defined(FEAT_PROFILE)
4333 case CMD_profile:
4334 set_context_in_profile_cmd(xp, arg);
4335 break;
4336#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004337 case CMD_behave:
4338 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004339 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004340 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004342 case CMD_messages:
4343 xp->xp_context = EXPAND_MESSAGES;
4344 xp->xp_pattern = arg;
4345 break;
4346
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004347#if defined(FEAT_CMDHIST)
4348 case CMD_history:
4349 xp->xp_context = EXPAND_HISTORY;
4350 xp->xp_pattern = arg;
4351 break;
4352#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004353#if defined(FEAT_PROFILE)
4354 case CMD_syntime:
4355 xp->xp_context = EXPAND_SYNTIME;
4356 xp->xp_pattern = arg;
4357 break;
4358#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004359
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004360 case CMD_argdelete:
4361 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4362 arg = xp->xp_pattern + 1;
4363 xp->xp_context = EXPAND_ARGLIST;
4364 xp->xp_pattern = arg;
4365 break;
4366
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367#endif /* FEAT_CMDL_COMPL */
4368
4369 default:
4370 break;
4371 }
4372 return NULL;
4373}
4374
4375/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004376 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 *
4378 * Backslashed delimiters after / or ? will be skipped, and commands will
4379 * not be expanded between /'s and ?'s or after "'".
4380 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004381 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 * Returns the "cmd" pointer advanced to beyond the range.
4383 */
4384 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004385skip_range(
4386 char_u *cmd,
4387 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004389 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004391 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004393 if (*cmd == '\\')
4394 {
4395 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4396 ++cmd;
4397 else
4398 break;
4399 }
4400 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 {
4402 if (*++cmd == NUL && ctx != NULL)
4403 *ctx = EXPAND_NOTHING;
4404 }
4405 else if (*cmd == '/' || *cmd == '?')
4406 {
4407 delim = *cmd++;
4408 while (*cmd != NUL && *cmd != delim)
4409 if (*cmd++ == '\\' && *cmd != NUL)
4410 ++cmd;
4411 if (*cmd == NUL && ctx != NULL)
4412 *ctx = EXPAND_NOTHING;
4413 }
4414 if (*cmd != NUL)
4415 ++cmd;
4416 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004417
4418 /* Skip ":" and white space. */
4419 while (*cmd == ':')
4420 cmd = skipwhite(cmd + 1);
4421
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 return cmd;
4423}
4424
4425/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004426 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 *
4428 * Set ptr to the next character after the part that was interpreted.
4429 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004430 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 *
4432 * Return MAXLNUM when no Ex address was found.
4433 */
4434 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004435get_address(
4436 exarg_T *eap UNUSED,
4437 char_u **ptr,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004438 int addr_type, // flag: one of ADDR_LINES, ...
4439 int skip, // only skip the address, don't use it
4440 int silent, // no errors or side effects
4441 int to_other_file, // flag: may jump to other file
4442 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443{
4444 int c;
4445 int i;
4446 long n;
4447 char_u *cmd;
4448 pos_T pos;
4449 pos_T *fp;
4450 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004451 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
4453 cmd = skipwhite(*ptr);
4454 lnum = MAXLNUM;
4455 do
4456 {
4457 switch (*cmd)
4458 {
4459 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004460 ++cmd;
4461 switch (addr_type)
4462 {
4463 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 lnum = curwin->w_cursor.lnum;
4465 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004466 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004467 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004468 break;
4469 case ADDR_ARGUMENTS:
4470 lnum = curwin->w_arg_idx + 1;
4471 break;
4472 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004473 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004474 lnum = curbuf->b_fnum;
4475 break;
4476 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004477 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004478 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004479 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004480 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004481 cmd = NULL;
4482 goto error;
4483 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004484#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004485 case ADDR_QUICKFIX:
4486 lnum = qf_get_cur_valid_idx(eap);
4487 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004488#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004489 }
4490 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491
4492 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004493 ++cmd;
4494 switch (addr_type)
4495 {
4496 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 lnum = curbuf->b_ml.ml_line_count;
4498 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004499 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004500 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004501 break;
4502 case ADDR_ARGUMENTS:
4503 lnum = ARGCOUNT;
4504 break;
4505 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004506 buf = lastbuf;
4507 while (buf->b_ml.ml_mfp == NULL)
4508 {
4509 if (buf->b_prev == NULL)
4510 break;
4511 buf = buf->b_prev;
4512 }
4513 lnum = buf->b_fnum;
4514 break;
4515 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004516 lnum = lastbuf->b_fnum;
4517 break;
4518 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004519 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004520 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004521 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004522 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004523 cmd = NULL;
4524 goto error;
4525 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004526#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004527 case ADDR_QUICKFIX:
4528 lnum = qf_get_size(eap);
4529 if (lnum == 0)
4530 lnum = 1;
4531 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004532#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004533 }
4534 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
4536 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004537 if (*++cmd == NUL)
4538 {
4539 cmd = NULL;
4540 goto error;
4541 }
4542 if (addr_type != ADDR_LINES)
4543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004544 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004545 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004546 goto error;
4547 }
4548 if (skip)
4549 ++cmd;
4550 else
4551 {
4552 /* Only accept a mark in another file when it is
4553 * used by itself: ":'M". */
4554 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4555 ++cmd;
4556 if (fp == (pos_T *)-1)
4557 /* Jumped to another file. */
4558 lnum = curwin->w_cursor.lnum;
4559 else
4560 {
4561 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
4563 cmd = NULL;
4564 goto error;
4565 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004566 lnum = fp->lnum;
4567 }
4568 }
4569 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570
4571 case '/':
4572 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004573 c = *cmd++;
4574 if (addr_type != ADDR_LINES)
4575 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004576 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004577 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004578 goto error;
4579 }
4580 if (skip) /* skip "/pat/" */
4581 {
4582 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4583 if (*cmd == c)
4584 ++cmd;
4585 }
4586 else
4587 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004588 int flags;
4589
4590 pos = curwin->w_cursor; // save curwin->w_cursor
4591
4592 // When '/' or '?' follows another address, start from
4593 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004594 if (lnum != MAXLNUM)
4595 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004596
4597 // Start a forward search at the end of the line (unless
4598 // before the first line).
4599 // Start a backward search at the start of the line.
4600 // This makes sure we never match in the current
4601 // line, and can match anywhere in the
4602 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004603 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004604 curwin->w_cursor.col = MAXCOL;
4605 else
4606 curwin->w_cursor.col = 0;
4607 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004608 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4609 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004610 {
4611 curwin->w_cursor = pos;
4612 cmd = NULL;
4613 goto error;
4614 }
4615 lnum = curwin->w_cursor.lnum;
4616 curwin->w_cursor = pos;
4617 /* adjust command string pointer */
4618 cmd += searchcmdlen;
4619 }
4620 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621
4622 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004623 ++cmd;
4624 if (addr_type != ADDR_LINES)
4625 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004626 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004627 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004628 goto error;
4629 }
4630 if (*cmd == '&')
4631 i = RE_SUBST;
4632 else if (*cmd == '?' || *cmd == '/')
4633 i = RE_SEARCH;
4634 else
4635 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004636 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004637 cmd = NULL;
4638 goto error;
4639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004641 if (!skip)
4642 {
4643 /*
4644 * When search follows another address, start from
4645 * there.
4646 */
4647 if (lnum != MAXLNUM)
4648 pos.lnum = lnum;
4649 else
4650 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004652 /*
4653 * Start the search just like for the above
4654 * do_search().
4655 */
4656 if (*cmd != '?')
4657 pos.col = MAXCOL;
4658 else
4659 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004660 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004661 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004662 *cmd == '?' ? BACKWARD : FORWARD,
4663 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004664 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004665 lnum = pos.lnum;
4666 else
4667 {
4668 cmd = NULL;
4669 goto error;
4670 }
4671 }
4672 ++cmd;
4673 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674
4675 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004676 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4677 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
4679
4680 for (;;)
4681 {
4682 cmd = skipwhite(cmd);
4683 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4684 break;
4685
4686 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004687 {
4688 switch (addr_type)
4689 {
4690 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004691 /* "+1" is same as ".+1" */
4692 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004693 break;
4694 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004695 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004696 break;
4697 case ADDR_ARGUMENTS:
4698 lnum = curwin->w_arg_idx + 1;
4699 break;
4700 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004701 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004702 lnum = curbuf->b_fnum;
4703 break;
4704 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004705 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004706 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004707 case ADDR_TABS_RELATIVE:
4708 lnum = 1;
4709 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004710#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004711 case ADDR_QUICKFIX:
4712 lnum = qf_get_cur_valid_idx(eap);
4713 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004714#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004715 }
4716 }
4717
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 if (VIM_ISDIGIT(*cmd))
4719 i = '+'; /* "number" is same as "+number" */
4720 else
4721 i = *cmd++;
4722 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4723 n = 1;
4724 else
4725 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004726
4727 if (addr_type == ADDR_TABS_RELATIVE)
4728 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004729 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004730 cmd = NULL;
4731 goto error;
4732 }
4733 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004734 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004735 lnum = compute_buffer_local_count(
4736 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004738 {
4739#ifdef FEAT_FOLDING
4740 /* Relative line addressing, need to adjust for folded lines
4741 * now, but only do it after the first address. */
4742 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4743 && address_count >= 2)
4744 (void)hasFolding(lnum, NULL, &lnum);
4745#endif
4746 if (i == '-')
4747 lnum -= n;
4748 else
4749 lnum += n;
4750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 }
4752 } while (*cmd == '/' || *cmd == '?');
4753
4754error:
4755 *ptr = cmd;
4756 return lnum;
4757}
4758
4759/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004760 * Get flags from an Ex command argument.
4761 */
4762 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004763get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004764{
4765 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4766 {
4767 if (*eap->arg == 'l')
4768 eap->flags |= EXFLAG_LIST;
4769 else if (*eap->arg == 'p')
4770 eap->flags |= EXFLAG_PRINT;
4771 else
4772 eap->flags |= EXFLAG_NR;
4773 eap->arg = skipwhite(eap->arg + 1);
4774 }
4775}
4776
4777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 * Function called for command which is Not Implemented. NI!
4779 */
4780 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004781ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782{
4783 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004784 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785}
4786
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004787#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788/*
4789 * Function called for script command which is Not Implemented. NI!
4790 * Skips over ":perl <<EOF" constructs.
4791 */
4792 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004793ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794{
4795 if (!eap->skip)
4796 ex_ni(eap);
4797 else
4798 vim_free(script_get(eap, eap->arg));
4799}
4800#endif
4801
4802/*
4803 * Check range in Ex command for validity.
4804 * Return NULL when valid, error message when invalid.
4805 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004806 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004807invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004809 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 if ( eap->line1 < 0
4811 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004812 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004813 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004814
4815 if (eap->argt & RANGE)
4816 {
4817 switch(eap->addr_type)
4818 {
4819 case ADDR_LINES:
4820 if (!(eap->argt & NOTADR)
4821 && eap->line2 > curbuf->b_ml.ml_line_count
4822#ifdef FEAT_DIFF
4823 + (eap->cmdidx == CMD_diffget)
4824#endif
4825 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004826 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004827 break;
4828 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004829 /* add 1 if ARGCOUNT is 0 */
4830 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004831 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004832 break;
4833 case ADDR_BUFFERS:
4834 if (eap->line1 < firstbuf->b_fnum
4835 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004836 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004837 break;
4838 case ADDR_LOADED_BUFFERS:
4839 buf = firstbuf;
4840 while (buf->b_ml.ml_mfp == NULL)
4841 {
4842 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004843 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004844 buf = buf->b_next;
4845 }
4846 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004847 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004848 buf = lastbuf;
4849 while (buf->b_ml.ml_mfp == NULL)
4850 {
4851 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004852 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004853 buf = buf->b_prev;
4854 }
4855 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004856 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004857 break;
4858 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004859 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004860 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004861 break;
4862 case ADDR_TABS:
4863 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004864 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004865 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004866 case ADDR_TABS_RELATIVE:
4867 /* Do nothing */
4868 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004869#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004870 case ADDR_QUICKFIX:
4871 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004872 return _(e_invrange);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004873 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004874#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004875 }
4876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 return NULL;
4878}
4879
4880/*
4881 * Correct the range for zero line number, if required.
4882 */
4883 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004884correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885{
4886 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4887 {
4888 if (eap->line1 == 0)
4889 eap->line1 = 1;
4890 if (eap->line2 == 0)
4891 eap->line2 = 1;
4892 }
4893}
4894
Bram Moolenaar748bf032005-02-02 23:04:36 +00004895#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004896/*
4897 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4898 * pattern. Otherwise return eap->arg.
4899 */
4900 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004901skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004902{
4903 char_u *p = eap->arg;
4904
Bram Moolenaara37420f2006-02-04 22:37:47 +00004905 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4906 || eap->cmdidx == CMD_vimgrepadd
4907 || eap->cmdidx == CMD_lvimgrepadd
4908 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004909 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004910 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004911 if (p == NULL)
4912 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004913 }
4914 return p;
4915}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004916
4917/*
4918 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4919 * in the command line, so that things like % get expanded.
4920 */
4921 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004922replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004923{
4924 char_u *new_cmdline;
4925 char_u *program;
4926 char_u *pos;
4927 char_u *ptr;
4928 int len;
4929 int i;
4930
4931 /*
4932 * Don't do it when ":vimgrep" is used for ":grep".
4933 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004934 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4935 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4936 || eap->cmdidx == CMD_grepadd
4937 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004938 && !grep_internal(eap->cmdidx))
4939 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004940 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4941 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004942 {
4943 if (*curbuf->b_p_gp == NUL)
4944 program = p_gp;
4945 else
4946 program = curbuf->b_p_gp;
4947 }
4948 else
4949 {
4950 if (*curbuf->b_p_mp == NUL)
4951 program = p_mp;
4952 else
4953 program = curbuf->b_p_mp;
4954 }
4955
4956 p = skipwhite(p);
4957
4958 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4959 {
4960 /* replace $* by given arguments */
4961 i = 1;
4962 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4963 ++i;
4964 len = (int)STRLEN(p);
4965 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4966 if (new_cmdline == NULL)
4967 return NULL; /* out of memory */
4968 ptr = new_cmdline;
4969 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4970 {
4971 i = (int)(pos - program);
4972 STRNCPY(ptr, program, i);
4973 STRCPY(ptr += i, p);
4974 ptr += len;
4975 program = pos + 2;
4976 }
4977 STRCPY(ptr, program);
4978 }
4979 else
4980 {
4981 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4982 if (new_cmdline == NULL)
4983 return NULL; /* out of memory */
4984 STRCPY(new_cmdline, program);
4985 STRCAT(new_cmdline, " ");
4986 STRCAT(new_cmdline, p);
4987 }
4988 msg_make(p);
4989
4990 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4991 vim_free(*cmdlinep);
4992 *cmdlinep = new_cmdline;
4993 p = new_cmdline;
4994 }
4995 return p;
4996}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004997#endif
4998
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999/*
5000 * Expand file name in Ex command argument.
5001 * Return FAIL for failure, OK otherwise.
5002 */
5003 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005004expand_filename(
5005 exarg_T *eap,
5006 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005007 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008{
5009 int has_wildcards; /* need to expand wildcards */
5010 char_u *repl;
5011 int srclen;
5012 char_u *p;
5013 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005014 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015
Bram Moolenaar748bf032005-02-02 23:04:36 +00005016#ifdef FEAT_QUICKFIX
5017 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
5018 p = skip_grep_pat(eap);
5019#else
5020 p = eap->arg;
5021#endif
5022
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 /*
5024 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5025 * the file name contains a wildcard it should not cause expanding.
5026 * (it will be expanded anyway if there is a wildcard before replacing).
5027 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005028 has_wildcards = mch_has_wildcard(p);
5029 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005031#ifdef FEAT_EVAL
5032 /* Skip over `=expr`, wildcards in it are not expanded. */
5033 if (p[0] == '`' && p[1] == '=')
5034 {
5035 p += 2;
5036 (void)skip_expr(&p);
5037 if (*p == '`')
5038 ++p;
5039 continue;
5040 }
5041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 /*
5043 * Quick check if this cannot be the start of a special string.
5044 * Also removes backslash before '%', '#' and '<'.
5045 */
5046 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5047 {
5048 ++p;
5049 continue;
5050 }
5051
5052 /*
5053 * Try to find a match at this position.
5054 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005055 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5056 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 if (*errormsgp != NULL) /* error detected */
5058 return FAIL;
5059 if (repl == NULL) /* no match found */
5060 {
5061 p += srclen;
5062 continue;
5063 }
5064
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005065 /* Wildcards won't be expanded below, the replacement is taken
5066 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5067 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5068 {
5069 char_u *l = repl;
5070
5071 repl = expand_env_save(repl);
5072 vim_free(l);
5073 }
5074
Bram Moolenaar63b92542007-03-27 14:57:09 +00005075 /* Need to escape white space et al. with a backslash.
5076 * Don't do this for:
5077 * - replacement that already has been escaped: "##"
5078 * - shell commands (may have to use quotes instead).
5079 * - non-unix systems when there is a single argument (spaces don't
5080 * separate arguments then).
5081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005083 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 && eap->cmdidx != CMD_grep
5086 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005087 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005088 && eap->cmdidx != CMD_lgrep
5089 && eap->cmdidx != CMD_lgrepadd
5090 && eap->cmdidx != CMD_lmake
5091 && eap->cmdidx != CMD_make
5092 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093#ifndef UNIX
5094 && !(eap->argt & NOSPC)
5095#endif
5096 )
5097 {
5098 char_u *l;
5099#ifdef BACKSLASH_IN_FILENAME
5100 /* Don't escape a backslash here, because rem_backslash() doesn't
5101 * remove it later. */
5102 static char_u *nobslash = (char_u *)" \t\"|";
5103# define ESCAPE_CHARS nobslash
5104#else
5105# define ESCAPE_CHARS escape_chars
5106#endif
5107
5108 for (l = repl; *l; ++l)
5109 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5110 {
5111 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5112 if (l != NULL)
5113 {
5114 vim_free(repl);
5115 repl = l;
5116 }
5117 break;
5118 }
5119 }
5120
5121 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005122 if ((eap->usefilter || eap->cmdidx == CMD_bang
5123 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005124 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 {
5126 char_u *l;
5127
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005128 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 if (l != NULL)
5130 {
5131 vim_free(repl);
5132 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 }
5134 }
5135
5136 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5137 vim_free(repl);
5138 if (p == NULL)
5139 return FAIL;
5140 }
5141
5142 /*
5143 * One file argument: Expand wildcards.
5144 * Don't do this with ":r !command" or ":w !command".
5145 */
5146 if ((eap->argt & NOSPC) && !eap->usefilter)
5147 {
5148 /*
5149 * May do this twice:
5150 * 1. Replace environment variables.
5151 * 2. Replace any other wildcards, remove backslashes.
5152 */
5153 for (n = 1; n <= 2; ++n)
5154 {
5155 if (n == 2)
5156 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 /*
5158 * Halve the number of backslashes (this is Vi compatible).
5159 * For Unix and OS/2, when wildcards are expanded, this is
5160 * done by ExpandOne() below.
5161 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005162#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 if (!has_wildcards)
5164#endif
5165 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
5167
5168 if (has_wildcards)
5169 {
5170 if (n == 1)
5171 {
5172 /*
5173 * First loop: May expand environment variables. This
5174 * can be done much faster with expand_env() than with
5175 * something else (e.g., calling a shell).
5176 * After expanding environment variables, check again
5177 * if there are still wildcards present.
5178 */
5179 if (vim_strchr(eap->arg, '$') != NULL
5180 || vim_strchr(eap->arg, '~') != NULL)
5181 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005182 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005183 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 has_wildcards = mch_has_wildcard(NameBuff);
5185 p = NameBuff;
5186 }
5187 else
5188 p = NULL;
5189 }
5190 else /* n == 2 */
5191 {
5192 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005193 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194
5195 ExpandInit(&xpc);
5196 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005197 if (p_wic)
5198 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005200 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 if (p == NULL)
5202 return FAIL;
5203 }
5204 if (p != NULL)
5205 {
5206 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5207 p, cmdlinep);
5208 if (n == 2) /* p came from ExpandOne() */
5209 vim_free(p);
5210 }
5211 }
5212 }
5213 }
5214 return OK;
5215}
5216
5217/*
5218 * Replace part of the command line, keeping eap->cmd, eap->arg and
5219 * eap->nextcmd correct.
5220 * "src" points to the part that is to be replaced, of length "srclen".
5221 * "repl" is the replacement string.
5222 * Returns a pointer to the character after the replaced string.
5223 * Returns NULL for failure.
5224 */
5225 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005226repl_cmdline(
5227 exarg_T *eap,
5228 char_u *src,
5229 int srclen,
5230 char_u *repl,
5231 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232{
5233 int len;
5234 int i;
5235 char_u *new_cmdline;
5236
5237 /*
5238 * The new command line is build in new_cmdline[].
5239 * First allocate it.
5240 * Careful: a "+cmd" argument may have been NUL terminated.
5241 */
5242 len = (int)STRLEN(repl);
5243 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005244 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5246 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5247 return NULL; /* out of memory! */
5248
5249 /*
5250 * Copy the stuff before the expanded part.
5251 * Copy the expanded stuff.
5252 * Copy what came after the expanded part.
5253 * Copy the next commands, if there are any.
5254 */
5255 i = (int)(src - *cmdlinep); /* length of part before match */
5256 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005257
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 mch_memmove(new_cmdline + i, repl, (size_t)len);
5259 i += len; /* remember the end of the string */
5260 STRCPY(new_cmdline + i, src + srclen);
5261 src = new_cmdline + i; /* remember where to continue */
5262
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005263 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 {
5265 i = (int)STRLEN(new_cmdline) + 1;
5266 STRCPY(new_cmdline + i, eap->nextcmd);
5267 eap->nextcmd = new_cmdline + i;
5268 }
5269 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5270 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5271 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5272 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5273 vim_free(*cmdlinep);
5274 *cmdlinep = new_cmdline;
5275
5276 return src;
5277}
5278
5279/*
5280 * Check for '|' to separate commands and '"' to start comments.
5281 */
5282 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005283separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284{
5285 char_u *p;
5286
Bram Moolenaar86b68352004-12-27 21:59:20 +00005287#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005288 p = skip_grep_pat(eap);
5289#else
5290 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005291#endif
5292
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005293 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 {
5295 if (*p == Ctrl_V)
5296 {
5297 if (eap->argt & (USECTRLV | XFILE))
5298 ++p; /* skip CTRL-V and next char */
5299 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005300 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005301 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 if (*p == NUL) /* stop at NUL after CTRL-V */
5303 break;
5304 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005305
5306#ifdef FEAT_EVAL
5307 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005308 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005309 {
5310 p += 2;
5311 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005312 }
5313#endif
5314
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 /* Check for '"': start of comment or '|': next command */
5316 /* :@" and :*" do not start a comment!
5317 * :redir @" doesn't either. */
5318 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5319 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5320 || p != eap->arg)
5321 && (eap->cmdidx != CMD_redir
5322 || p != eap->arg + 1 || p[-1] != '@'))
5323 || *p == '|' || *p == '\n')
5324 {
5325 /*
5326 * We remove the '\' before the '|', unless USECTRLV is used
5327 * AND 'b' is present in 'cpoptions'.
5328 */
5329 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5330 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5331 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005332 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 --p;
5334 }
5335 else
5336 {
5337 eap->nextcmd = check_nextcmd(p);
5338 *p = NUL;
5339 break;
5340 }
5341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005343
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5345 del_trailing_spaces(eap->arg);
5346}
5347
5348/*
5349 * get + command from ex argument
5350 */
5351 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005352getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353{
5354 char_u *arg = *argp;
5355 char_u *command = NULL;
5356
5357 if (*arg == '+') /* +[command] */
5358 {
5359 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005360 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 command = dollar_command;
5362 else
5363 {
5364 command = arg;
5365 arg = skip_cmd_arg(command, TRUE);
5366 if (*arg != NUL)
5367 *arg++ = NUL; /* terminate command with NUL */
5368 }
5369
5370 arg = skipwhite(arg); /* skip over spaces */
5371 *argp = arg;
5372 }
5373 return command;
5374}
5375
5376/*
5377 * Find end of "+command" argument. Skip over "\ " and "\\".
5378 */
5379 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005380skip_cmd_arg(
5381 char_u *p,
5382 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383{
5384 while (*p && !vim_isspace(*p))
5385 {
5386 if (*p == '\\' && p[1] != NUL)
5387 {
5388 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005389 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 else
5391 ++p;
5392 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005393 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 }
5395 return p;
5396}
5397
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005398 int
5399get_bad_opt(char_u *p, exarg_T *eap)
5400{
5401 if (STRICMP(p, "keep") == 0)
5402 eap->bad_char = BAD_KEEP;
5403 else if (STRICMP(p, "drop") == 0)
5404 eap->bad_char = BAD_DROP;
5405 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5406 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005407 else
5408 return FAIL;
5409 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005410}
5411
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412/*
5413 * Get "++opt=arg" argument.
5414 * Return FAIL or OK.
5415 */
5416 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005417getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418{
5419 char_u *arg = eap->arg + 2;
5420 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005421 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423
5424 /* ":edit ++[no]bin[ary] file" */
5425 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5426 {
5427 if (*arg == 'n')
5428 {
5429 arg += 2;
5430 eap->force_bin = FORCE_NOBIN;
5431 }
5432 else
5433 eap->force_bin = FORCE_BIN;
5434 if (!checkforcmd(&arg, "binary", 3))
5435 return FAIL;
5436 eap->arg = skipwhite(arg);
5437 return OK;
5438 }
5439
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005440 /* ":read ++edit file" */
5441 if (STRNCMP(arg, "edit", 4) == 0)
5442 {
5443 eap->read_edit = TRUE;
5444 eap->arg = skipwhite(arg + 4);
5445 return OK;
5446 }
5447
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 if (STRNCMP(arg, "ff", 2) == 0)
5449 {
5450 arg += 2;
5451 pp = &eap->force_ff;
5452 }
5453 else if (STRNCMP(arg, "fileformat", 10) == 0)
5454 {
5455 arg += 10;
5456 pp = &eap->force_ff;
5457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 else if (STRNCMP(arg, "enc", 3) == 0)
5459 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005460 if (STRNCMP(arg, "encoding", 8) == 0)
5461 arg += 8;
5462 else
5463 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 pp = &eap->force_enc;
5465 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005466 else if (STRNCMP(arg, "bad", 3) == 0)
5467 {
5468 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005469 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471
5472 if (pp == NULL || *arg != '=')
5473 return FAIL;
5474
5475 ++arg;
5476 *pp = (int)(arg - eap->cmd);
5477 arg = skip_cmd_arg(arg, FALSE);
5478 eap->arg = skipwhite(arg);
5479 *arg = NUL;
5480
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 if (pp == &eap->force_ff)
5482 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5484 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005485 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005487 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 {
5489 /* Make 'fileencoding' lower case. */
5490 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5491 *p = TOLOWER_ASC(*p);
5492 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005493 else
5494 {
5495 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5496 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005497 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005498 return FAIL;
5499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500
5501 return OK;
5502}
5503
5504/*
5505 * ":abbreviate" and friends.
5506 */
5507 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005508ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509{
5510 do_exmap(eap, TRUE); /* almost the same as mapping */
5511}
5512
5513/*
5514 * ":map" and friends.
5515 */
5516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005517ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518{
5519 /*
5520 * If we are sourcing .exrc or .vimrc in current directory we
5521 * print the mappings for security reasons.
5522 */
5523 if (secure)
5524 {
5525 secure = 2;
5526 msg_outtrans(eap->cmd);
5527 msg_putchar('\n');
5528 }
5529 do_exmap(eap, FALSE);
5530}
5531
5532/*
5533 * ":unmap" and friends.
5534 */
5535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005536ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537{
5538 do_exmap(eap, FALSE);
5539}
5540
5541/*
5542 * ":mapclear" and friends.
5543 */
5544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005545ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546{
5547 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5548}
5549
5550/*
5551 * ":abclear" and friends.
5552 */
5553 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005554ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555{
5556 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5557}
5558
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005560ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
5562 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005563 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 * directory for security reasons.
5565 */
5566 if (secure)
5567 {
5568 secure = 2;
5569 eap->errmsg = e_curdir;
5570 }
5571 else if (eap->cmdidx == CMD_autocmd)
5572 do_autocmd(eap->arg, eap->forceit);
5573 else
5574 do_augroup(eap->arg, eap->forceit);
5575}
5576
5577/*
5578 * ":doautocmd": Apply the automatic commands to the current buffer.
5579 */
5580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005581ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005583 char_u *arg = eap->arg;
5584 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005585 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005586
Bram Moolenaar1610d052016-06-09 22:53:01 +02005587 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5588 /* Only when there is no <nomodeline>. */
5589 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005590 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593/*
5594 * :[N]bunload[!] [N] [bufname] unload buffer
5595 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5596 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5597 */
5598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005599ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600{
5601 eap->errmsg = do_bufdel(
5602 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5603 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5604 : DOBUF_UNLOAD, eap->arg,
5605 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5606}
5607
5608/*
5609 * :[N]buffer [N] to buffer N
5610 * :[N]sbuffer [N] to buffer N
5611 */
5612 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005613ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614{
5615 if (*eap->arg)
5616 eap->errmsg = e_trailing;
5617 else
5618 {
5619 if (eap->addr_count == 0) /* default is current buffer */
5620 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5621 else
5622 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
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}
5627
5628/*
5629 * :[N]bmodified [N] to next mod. buffer
5630 * :[N]sbmodified [N] to next mod. buffer
5631 */
5632 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005633ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634{
5635 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005636 if (eap->do_ecmd_cmd != NULL)
5637 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638}
5639
5640/*
5641 * :[N]bnext [N] to next buffer
5642 * :[N]sbnext [N] split and to next buffer
5643 */
5644 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005645ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646{
5647 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005648 if (eap->do_ecmd_cmd != NULL)
5649 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650}
5651
5652/*
5653 * :[N]bNext [N] to previous buffer
5654 * :[N]bprevious [N] to previous buffer
5655 * :[N]sbNext [N] split and to previous buffer
5656 * :[N]sbprevious [N] split and to previous buffer
5657 */
5658 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005659ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660{
5661 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005662 if (eap->do_ecmd_cmd != NULL)
5663 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664}
5665
5666/*
5667 * :brewind to first buffer
5668 * :bfirst to first buffer
5669 * :sbrewind split and to first buffer
5670 * :sbfirst split and to first buffer
5671 */
5672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005673ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674{
5675 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005676 if (eap->do_ecmd_cmd != NULL)
5677 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678}
5679
5680/*
5681 * :blast to last buffer
5682 * :sblast split and to last buffer
5683 */
5684 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005685ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686{
5687 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005688 if (eap->do_ecmd_cmd != NULL)
5689 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691
5692 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005693ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694{
5695 return (c == NUL || c == '|' || c == '"' || c == '\n');
5696}
5697
5698#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5699 || defined(PROTO)
5700/*
5701 * Return the next command, after the first '|' or '\n'.
5702 * Return NULL if not found.
5703 */
5704 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005705find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706{
5707 while (*p != '|' && *p != '\n')
5708 {
5709 if (*p == NUL)
5710 return NULL;
5711 ++p;
5712 }
5713 return (p + 1);
5714}
5715#endif
5716
5717/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005718 * Check if *p is a separator between Ex commands, skipping over white space.
5719 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 */
5721 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005722check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005724 char_u *s = skipwhite(p);
5725
5726 if (*s == '|' || *s == '\n')
5727 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 else
5729 return NULL;
5730}
5731
5732/*
5733 * - if there are more files to edit
5734 * - and this is the last window
5735 * - and forceit not used
5736 * - and not repeated twice on a row
5737 * return FAIL and give error message if 'message' TRUE
5738 * return OK otherwise
5739 */
5740 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005741check_more(
5742 int message, /* when FALSE check only, no messages */
5743 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744{
5745 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5746
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005747 if (!forceit && only_one_window()
5748 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 {
5750 if (message)
5751 {
5752#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5753 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5754 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005755 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005757 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5758 NGETTEXT("%d more file to edit. Quit anyway?",
5759 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5761 return OK;
5762 return FAIL;
5763 }
5764#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005765 semsg(NGETTEXT("E173: %d more file to edit",
5766 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 quitmore = 2; /* next try to quit is allowed */
5768 }
5769 return FAIL;
5770 }
5771 return OK;
5772}
5773
5774#ifdef FEAT_CMDL_COMPL
5775/*
5776 * Function given to ExpandGeneric() to obtain the list of command names.
5777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005779get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780{
5781 if (idx >= (int)CMD_SIZE)
5782# ifdef FEAT_USR_CMDS
5783 return get_user_command_name(idx);
5784# else
5785 return NULL;
5786# endif
5787 return cmdnames[idx].cmd_name;
5788}
5789#endif
5790
5791#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005793uc_add_command(
5794 char_u *name,
5795 size_t name_len,
5796 char_u *rep,
5797 long argt,
5798 long def,
5799 int flags,
5800 int compl,
5801 char_u *compl_arg,
5802 int addr_type,
5803 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804{
5805 ucmd_T *cmd = NULL;
5806 char_u *p;
5807 int i;
5808 int cmp = 1;
5809 char_u *rep_buf = NULL;
5810 garray_T *gap;
5811
Bram Moolenaar9c102382006-05-03 21:26:49 +00005812 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 if (rep_buf == NULL)
5814 {
5815 /* Can't replace termcodes - try using the string as is */
5816 rep_buf = vim_strsave(rep);
5817
5818 /* Give up if out of memory */
5819 if (rep_buf == NULL)
5820 return FAIL;
5821 }
5822
5823 /* get address of growarray: global or in curbuf */
5824 if (flags & UC_BUFFER)
5825 {
5826 gap = &curbuf->b_ucmds;
5827 if (gap->ga_itemsize == 0)
5828 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5829 }
5830 else
5831 gap = &ucmds;
5832
5833 /* Search for the command in the already defined commands. */
5834 for (i = 0; i < gap->ga_len; ++i)
5835 {
5836 size_t len;
5837
5838 cmd = USER_CMD_GA(gap, i);
5839 len = STRLEN(cmd->uc_name);
5840 cmp = STRNCMP(name, cmd->uc_name, name_len);
5841 if (cmp == 0)
5842 {
5843 if (name_len < len)
5844 cmp = -1;
5845 else if (name_len > len)
5846 cmp = 1;
5847 }
5848
5849 if (cmp == 0)
5850 {
Bram Moolenaar55d46912018-12-08 16:03:28 +01005851 // Command can be replaced with "command!" and when sourcing the
5852 // same script again, but only once.
5853 if (!force && (cmd->uc_script_ctx.sc_sid != current_sctx.sc_sid
5854 || cmd->uc_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005856 semsg(_("E174: Command already exists: add ! to replace it: %s"),
Bram Moolenaar55d46912018-12-08 16:03:28 +01005857 name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 goto fail;
5859 }
5860
Bram Moolenaard23a8232018-02-10 18:45:26 +01005861 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005862#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005863 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 break;
5866 }
5867
5868 /* Stop as soon as we pass the name to add */
5869 if (cmp < 0)
5870 break;
5871 }
5872
5873 /* Extend the array unless we're replacing an existing command */
5874 if (cmp != 0)
5875 {
5876 if (ga_grow(gap, 1) != OK)
5877 goto fail;
5878 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5879 goto fail;
5880
5881 cmd = USER_CMD_GA(gap, i);
5882 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5883
5884 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885
5886 cmd->uc_name = p;
5887 }
5888
5889 cmd->uc_rep = rep_buf;
5890 cmd->uc_argt = argt;
5891 cmd->uc_def = def;
5892 cmd->uc_compl = compl;
5893#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005894 cmd->uc_script_ctx = current_sctx;
5895 cmd->uc_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896# ifdef FEAT_CMDL_COMPL
5897 cmd->uc_compl_arg = compl_arg;
5898# endif
5899#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005900 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901
5902 return OK;
5903
5904fail:
5905 vim_free(rep_buf);
5906#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5907 vim_free(compl_arg);
5908#endif
5909 return FAIL;
5910}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005913#if defined(FEAT_USR_CMDS)
5914static struct
5915{
5916 int expand;
5917 char *name;
5918} addr_type_complete[] =
5919{
5920 {ADDR_ARGUMENTS, "arguments"},
5921 {ADDR_LINES, "lines"},
5922 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5923 {ADDR_TABS, "tabs"},
5924 {ADDR_BUFFERS, "buffers"},
5925 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005926 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaar51a74542018-12-02 18:21:49 +01005927 {ADDR_OTHER, "other"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005928 {-1, NULL}
5929};
5930#endif
5931
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005932#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933/*
5934 * List of names for completion for ":command" with the EXPAND_ flag.
5935 * Must be alphabetical for completion.
5936 */
5937static struct
5938{
5939 int expand;
5940 char *name;
5941} command_complete[] =
5942{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005943 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005945 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005947 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005949 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005950#if defined(FEAT_CSCOPE)
5951 {EXPAND_CSCOPE, "cscope"},
5952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5954 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005955 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956#endif
5957 {EXPAND_DIRECTORIES, "dir"},
5958 {EXPAND_ENV_VARS, "environment"},
5959 {EXPAND_EVENTS, "event"},
5960 {EXPAND_EXPRESSION, "expression"},
5961 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005962 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005963 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 {EXPAND_FUNCTIONS, "function"},
5965 {EXPAND_HELP, "help"},
5966 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005967#if defined(FEAT_CMDHIST)
5968 {EXPAND_HISTORY, "history"},
5969#endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005970#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005971 {EXPAND_LOCALES, "locale"},
5972#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005973 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 {EXPAND_MAPPINGS, "mapping"},
5975 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005976 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005977 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005978#if defined(FEAT_PROFILE)
5979 {EXPAND_SYNTIME, "syntime"},
5980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005982 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005983 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005984#if defined(FEAT_SIGNS)
5985 {EXPAND_SIGN, "sign"},
5986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 {EXPAND_TAGS, "tag"},
5988 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005989 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 {EXPAND_USER_VARS, "var"},
5991 {0, NULL}
5992};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005995#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005997uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998{
5999 int i, j;
6000 int found = FALSE;
6001 ucmd_T *cmd;
6002 int len;
Bram Moolenaar725310d2019-04-24 23:08:23 +02006003 int over;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 long a;
6005 garray_T *gap;
6006
6007 gap = &curbuf->b_ucmds;
6008 for (;;)
6009 {
6010 for (i = 0; i < gap->ga_len; ++i)
6011 {
6012 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006013 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014
Bram Moolenaard29459b2016-08-26 22:29:11 +02006015 /* Skip commands which don't match the requested prefix and
6016 * commands filtered out. */
6017 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6018 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 continue;
6020
6021 /* Put out the title first time */
6022 if (!found)
Bram Moolenaar725310d2019-04-24 23:08:23 +02006023 msg_puts_title(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 found = TRUE;
6025 msg_putchar('\n');
6026 if (got_int)
6027 break;
6028
Bram Moolenaar725310d2019-04-24 23:08:23 +02006029 // Special cases
6030 len = 4;
6031 if (a & BANG)
6032 {
6033 msg_putchar('!');
6034 --len;
6035 }
6036 if (a & REGSTR)
6037 {
6038 msg_putchar('"');
6039 --len;
6040 }
6041 if (gap != &ucmds)
6042 {
6043 msg_putchar('b');
6044 --len;
6045 }
6046 if (a & TRLBAR)
6047 {
6048 msg_putchar('|');
6049 --len;
6050 }
6051 while (len-- > 0)
6052 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053
Bram Moolenaar8820b482017-03-16 17:23:31 +01006054 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 len = (int)STRLEN(cmd->uc_name) + 4;
6056
6057 do {
6058 msg_putchar(' ');
6059 ++len;
Bram Moolenaar725310d2019-04-24 23:08:23 +02006060 } while (len < 22);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061
Bram Moolenaar725310d2019-04-24 23:08:23 +02006062 // "over" is how much longer the name is than the column width for
6063 // the name, we'll try to align what comes after.
6064 over = len - 22;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 len = 0;
6066
Bram Moolenaar725310d2019-04-24 23:08:23 +02006067 // Arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6069 {
Bram Moolenaar725310d2019-04-24 23:08:23 +02006070 case 0: IObuff[len++] = '0'; break;
6071 case (EXTRA): IObuff[len++] = '*'; break;
6072 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6073 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6074 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 }
6076
6077 do {
6078 IObuff[len++] = ' ';
Bram Moolenaar725310d2019-04-24 23:08:23 +02006079 } while (len < 5 - over);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080
Bram Moolenaar725310d2019-04-24 23:08:23 +02006081 // Address / Range
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 if (a & (RANGE|COUNT))
6083 {
6084 if (a & COUNT)
6085 {
Bram Moolenaar725310d2019-04-24 23:08:23 +02006086 // -count=N
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6088 len += (int)STRLEN(IObuff + len);
6089 }
6090 else if (a & DFLALL)
6091 IObuff[len++] = '%';
6092 else if (cmd->uc_def >= 0)
6093 {
Bram Moolenaar725310d2019-04-24 23:08:23 +02006094 // -range=N
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6096 len += (int)STRLEN(IObuff + len);
6097 }
6098 else
6099 IObuff[len++] = '.';
6100 }
6101
6102 do {
6103 IObuff[len++] = ' ';
Bram Moolenaar725310d2019-04-24 23:08:23 +02006104 } while (len < 9 - over);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105
Bram Moolenaar725310d2019-04-24 23:08:23 +02006106 // Address Type
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006107 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6108 if (addr_type_complete[j].expand != ADDR_LINES
6109 && addr_type_complete[j].expand == cmd->uc_addr_type)
6110 {
6111 STRCPY(IObuff + len, addr_type_complete[j].name);
6112 len += (int)STRLEN(IObuff + len);
6113 break;
6114 }
6115
6116 do {
6117 IObuff[len++] = ' ';
Bram Moolenaar725310d2019-04-24 23:08:23 +02006118 } while (len < 13 - over);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006119
Bram Moolenaar725310d2019-04-24 23:08:23 +02006120 // Completion
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 for (j = 0; command_complete[j].expand != 0; ++j)
6122 if (command_complete[j].expand == cmd->uc_compl)
6123 {
6124 STRCPY(IObuff + len, command_complete[j].name);
6125 len += (int)STRLEN(IObuff + len);
6126 break;
6127 }
6128
6129 do {
6130 IObuff[len++] = ' ';
Bram Moolenaar725310d2019-04-24 23:08:23 +02006131 } while (len < 24 - over);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132
6133 IObuff[len] = '\0';
6134 msg_outtrans(IObuff);
6135
Bram Moolenaar725310d2019-04-24 23:08:23 +02006136 msg_outtrans_special(cmd->uc_rep, FALSE,
6137 name_len == 0 ? Columns - 46 : 0);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006138#ifdef FEAT_EVAL
6139 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006140 last_set_msg(cmd->uc_script_ctx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 out_flush();
6143 ui_breakcheck();
6144 if (got_int)
6145 break;
6146 }
6147 if (gap == &ucmds || i < gap->ga_len)
6148 break;
6149 gap = &ucmds;
6150 }
6151
6152 if (!found)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006153 msg(_("No user-defined commands found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154}
6155
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006156 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006157uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158{
6159 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6160 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6161 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6162 0xb9, 0x7f, 0};
6163 int i;
6164
6165 for (i = 0; fcmd[i]; ++i)
6166 IObuff[i] = fcmd[i] - 0x40;
6167 IObuff[i] = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006168 return (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169}
6170
6171 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006172uc_scan_attr(
6173 char_u *attr,
6174 size_t len,
6175 long *argt,
6176 long *def,
6177 int *flags,
6178 int *compl,
6179 char_u **compl_arg,
6180 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181{
6182 char_u *p;
6183
6184 if (len == 0)
6185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006186 emsg(_("E175: No attribute specified"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 return FAIL;
6188 }
6189
6190 /* First, try the simple attributes (no arguments) */
6191 if (STRNICMP(attr, "bang", len) == 0)
6192 *argt |= BANG;
6193 else if (STRNICMP(attr, "buffer", len) == 0)
6194 *flags |= UC_BUFFER;
6195 else if (STRNICMP(attr, "register", len) == 0)
6196 *argt |= REGSTR;
6197 else if (STRNICMP(attr, "bar", len) == 0)
6198 *argt |= TRLBAR;
6199 else
6200 {
6201 int i;
6202 char_u *val = NULL;
6203 size_t vallen = 0;
6204 size_t attrlen = len;
6205
6206 /* Look for the attribute name - which is the part before any '=' */
6207 for (i = 0; i < (int)len; ++i)
6208 {
6209 if (attr[i] == '=')
6210 {
6211 val = &attr[i + 1];
6212 vallen = len - i - 1;
6213 attrlen = i;
6214 break;
6215 }
6216 }
6217
6218 if (STRNICMP(attr, "nargs", attrlen) == 0)
6219 {
6220 if (vallen == 1)
6221 {
6222 if (*val == '0')
6223 /* Do nothing - this is the default */;
6224 else if (*val == '1')
6225 *argt |= (EXTRA | NOSPC | NEEDARG);
6226 else if (*val == '*')
6227 *argt |= EXTRA;
6228 else if (*val == '?')
6229 *argt |= (EXTRA | NOSPC);
6230 else if (*val == '+')
6231 *argt |= (EXTRA | NEEDARG);
6232 else
6233 goto wrong_nargs;
6234 }
6235 else
6236 {
6237wrong_nargs:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006238 emsg(_("E176: Invalid number of arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 return FAIL;
6240 }
6241 }
6242 else if (STRNICMP(attr, "range", attrlen) == 0)
6243 {
6244 *argt |= RANGE;
6245 if (vallen == 1 && *val == '%')
6246 *argt |= DFLALL;
6247 else if (val != NULL)
6248 {
6249 p = val;
6250 if (*def >= 0)
6251 {
6252two_count:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006253 emsg(_("E177: Count cannot be specified twice"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 return FAIL;
6255 }
6256
6257 *def = getdigits(&p);
6258 *argt |= (ZEROR | NOTADR);
6259
6260 if (p != val + vallen || vallen == 0)
6261 {
6262invalid_count:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006263 emsg(_("E178: Invalid default value for count"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 return FAIL;
6265 }
6266 }
6267 }
6268 else if (STRNICMP(attr, "count", attrlen) == 0)
6269 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006270 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271
6272 if (val != NULL)
6273 {
6274 p = val;
6275 if (*def >= 0)
6276 goto two_count;
6277
6278 *def = getdigits(&p);
6279
6280 if (p != val + vallen)
6281 goto invalid_count;
6282 }
6283
6284 if (*def < 0)
6285 *def = 0;
6286 }
6287 else if (STRNICMP(attr, "complete", attrlen) == 0)
6288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 if (val == NULL)
6290 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006291 emsg(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 return FAIL;
6293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006295 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6296 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006299 else if (STRNICMP(attr, "addr", attrlen) == 0)
6300 {
6301 *argt |= RANGE;
6302 if (val == NULL)
6303 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006304 emsg(_("E179: argument required for -addr"));
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006305 return FAIL;
6306 }
6307 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6308 == FAIL)
6309 return FAIL;
6310 if (addr_type_arg != ADDR_LINES)
6311 *argt |= (ZEROR | NOTADR) ;
6312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 else
6314 {
6315 char_u ch = attr[len];
6316 attr[len] = '\0';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006317 semsg(_("E181: Invalid attribute: %s"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 attr[len] = ch;
6319 return FAIL;
6320 }
6321 }
6322
6323 return OK;
6324}
6325
Bram Moolenaara850a712009-01-28 14:42:59 +00006326/*
6327 * ":command ..."
6328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006330ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331{
6332 char_u *name;
6333 char_u *end;
6334 char_u *p;
6335 long argt = 0;
6336 long def = -1;
6337 int flags = 0;
6338 int compl = EXPAND_NOTHING;
6339 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006340 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006342 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343
6344 p = eap->arg;
6345
6346 /* Check for attributes */
6347 while (*p == '-')
6348 {
6349 ++p;
6350 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006351 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6352 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 == FAIL)
6354 return;
6355 p = skipwhite(end);
6356 }
6357
6358 /* Get the name (if any) and skip to the following argument */
6359 name = p;
6360 if (ASCII_ISALPHA(*p))
6361 while (ASCII_ISALNUM(*p))
6362 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006363 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006365 emsg(_("E182: Invalid command name"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 return;
6367 }
6368 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006369 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370
Bram Moolenaar725310d2019-04-24 23:08:23 +02006371 // If there is nothing after the name, and no attributes were specified,
6372 // we are listing commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 p = skipwhite(end);
6374 if (!has_attr && ends_excmd(*p))
6375 {
6376 uc_list(name, end - name);
6377 }
6378 else if (!ASCII_ISUPPER(*name))
6379 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006380 emsg(_("E183: User defined commands must start with an uppercase letter"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 return;
6382 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006383 else if ((name_len == 1 && *name == 'X')
6384 || (name_len <= 4
6385 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6386 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006387 emsg(_("E841: Reserved name, cannot be used for user defined command"));
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006388 return;
6389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 else
6391 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006392 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393}
6394
6395/*
6396 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 * Clear all user commands, global and for current buffer.
6398 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006399 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006400ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401{
6402 uc_clear(&ucmds);
6403 uc_clear(&curbuf->b_ucmds);
6404}
6405
6406/*
6407 * Clear all user commands for "gap".
6408 */
6409 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006410uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411{
6412 int i;
6413 ucmd_T *cmd;
6414
6415 for (i = 0; i < gap->ga_len; ++i)
6416 {
6417 cmd = USER_CMD_GA(gap, i);
6418 vim_free(cmd->uc_name);
6419 vim_free(cmd->uc_rep);
6420# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6421 vim_free(cmd->uc_compl_arg);
6422# endif
6423 }
6424 ga_clear(gap);
6425}
6426
6427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006428ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429{
6430 int i = 0;
6431 ucmd_T *cmd = NULL;
6432 int cmp = -1;
6433 garray_T *gap;
6434
6435 gap = &curbuf->b_ucmds;
6436 for (;;)
6437 {
6438 for (i = 0; i < gap->ga_len; ++i)
6439 {
6440 cmd = USER_CMD_GA(gap, i);
6441 cmp = STRCMP(eap->arg, cmd->uc_name);
6442 if (cmp <= 0)
6443 break;
6444 }
6445 if (gap == &ucmds || cmp == 0)
6446 break;
6447 gap = &ucmds;
6448 }
6449
6450 if (cmp != 0)
6451 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006452 semsg(_("E184: No such user-defined command: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 return;
6454 }
6455
6456 vim_free(cmd->uc_name);
6457 vim_free(cmd->uc_rep);
6458# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6459 vim_free(cmd->uc_compl_arg);
6460# endif
6461
6462 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463
6464 if (i < gap->ga_len)
6465 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6466}
6467
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006468/*
6469 * split and quote args for <f-args>
6470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006472uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473{
6474 char_u *buf;
6475 char_u *p;
6476 char_u *q;
6477 int len;
6478
6479 /* Precalculate length */
6480 p = arg;
6481 len = 2; /* Initial and final quotes */
6482
6483 while (*p)
6484 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006485 if (p[0] == '\\' && p[1] == '\\')
6486 {
6487 len += 2;
6488 p += 2;
6489 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006490 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 {
6492 len += 1;
6493 p += 2;
6494 }
6495 else if (*p == '\\' || *p == '"')
6496 {
6497 len += 2;
6498 p += 1;
6499 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006500 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 {
6502 p = skipwhite(p);
6503 if (*p == NUL)
6504 break;
6505 len += 3; /* "," */
6506 }
6507 else
6508 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006509 int charlen = (*mb_ptr2len)(p);
Bram Moolenaar13505972019-01-24 15:04:48 +01006510
Bram Moolenaardfef1542012-07-10 19:25:10 +02006511 len += charlen;
6512 p += charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 }
6514 }
6515
6516 buf = alloc(len + 1);
6517 if (buf == NULL)
6518 {
6519 *lenp = 0;
6520 return buf;
6521 }
6522
6523 p = arg;
6524 q = buf;
6525 *q++ = '"';
6526 while (*p)
6527 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006528 if (p[0] == '\\' && p[1] == '\\')
6529 {
6530 *q++ = '\\';
6531 *q++ = '\\';
6532 p += 2;
6533 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006534 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 {
6536 *q++ = p[1];
6537 p += 2;
6538 }
6539 else if (*p == '\\' || *p == '"')
6540 {
6541 *q++ = '\\';
6542 *q++ = *p++;
6543 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006544 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 {
6546 p = skipwhite(p);
6547 if (*p == NUL)
6548 break;
6549 *q++ = '"';
6550 *q++ = ',';
6551 *q++ = '"';
6552 }
6553 else
6554 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006555 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 }
6557 }
6558 *q++ = '"';
6559 *q = 0;
6560
6561 *lenp = len;
6562 return buf;
6563}
6564
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006565 static size_t
6566add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6567{
6568 size_t result;
6569
6570 result = STRLEN(mod_str);
6571 if (*multi_mods)
6572 result += 1;
6573 if (buf != NULL)
6574 {
6575 if (*multi_mods)
6576 STRCAT(buf, " ");
6577 STRCAT(buf, mod_str);
6578 }
6579
6580 *multi_mods = 1;
6581
6582 return result;
6583}
6584
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585/*
6586 * Check for a <> code in a user command.
6587 * "code" points to the '<'. "len" the length of the <> (inclusive).
6588 * "buf" is where the result is to be added.
6589 * "split_buf" points to a buffer used for splitting, caller should free it.
6590 * "split_len" is the length of what "split_buf" contains.
6591 * Returns the length of the replacement, which has been added to "buf".
6592 * Returns -1 if there was no match, and only the "<" has been copied.
6593 */
6594 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006595uc_check_code(
6596 char_u *code,
6597 size_t len,
6598 char_u *buf,
6599 ucmd_T *cmd, /* the user command we're expanding */
6600 exarg_T *eap, /* ex arguments */
6601 char_u **split_buf,
6602 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603{
6604 size_t result = 0;
6605 char_u *p = code + 1;
6606 size_t l = len - 2;
6607 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006608 enum {
6609 ct_ARGS,
6610 ct_BANG,
6611 ct_COUNT,
6612 ct_LINE1,
6613 ct_LINE2,
6614 ct_RANGE,
6615 ct_MODS,
6616 ct_REGISTER,
6617 ct_LT,
6618 ct_NONE
6619 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620
6621 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6622 {
6623 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6624 p += 2;
6625 l -= 2;
6626 }
6627
Bram Moolenaar371d5402006-03-20 21:47:49 +00006628 ++l;
6629 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006631 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006633 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006635 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006637 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006639 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006641 else if (STRNICMP(p, "range>", l) == 0)
6642 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006643 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006645 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006647 else if (STRNICMP(p, "mods>", l) == 0)
6648 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649
6650 switch (type)
6651 {
6652 case ct_ARGS:
6653 /* Simple case first */
6654 if (*eap->arg == NUL)
6655 {
6656 if (quote == 1)
6657 {
6658 result = 2;
6659 if (buf != NULL)
6660 STRCPY(buf, "''");
6661 }
6662 else
6663 result = 0;
6664 break;
6665 }
6666
6667 /* When specified there is a single argument don't split it.
6668 * Works for ":Cmd %" when % is "a b c". */
6669 if ((eap->argt & NOSPC) && quote == 2)
6670 quote = 1;
6671
6672 switch (quote)
6673 {
6674 case 0: /* No quoting, no splitting */
6675 result = STRLEN(eap->arg);
6676 if (buf != NULL)
6677 STRCPY(buf, eap->arg);
6678 break;
6679 case 1: /* Quote, but don't split */
6680 result = STRLEN(eap->arg) + 2;
6681 for (p = eap->arg; *p; ++p)
6682 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006683 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6684 /* DBCS can contain \ in a trail byte, skip the
6685 * double-byte character. */
6686 ++p;
6687 else
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006688 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 ++result;
6690 }
6691
6692 if (buf != NULL)
6693 {
6694 *buf++ = '"';
6695 for (p = eap->arg; *p; ++p)
6696 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006697 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6698 /* DBCS can contain \ in a trail byte, copy the
6699 * double-byte character to avoid escaping. */
6700 *buf++ = *p++;
6701 else
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006702 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 *buf++ = '\\';
6704 *buf++ = *p;
6705 }
6706 *buf = '"';
6707 }
6708
6709 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006710 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 /* This is hard, so only do it once, and cache the result */
6712 if (*split_buf == NULL)
6713 *split_buf = uc_split_args(eap->arg, split_len);
6714
6715 result = *split_len;
6716 if (buf != NULL && result != 0)
6717 STRCPY(buf, *split_buf);
6718
6719 break;
6720 }
6721 break;
6722
6723 case ct_BANG:
6724 result = eap->forceit ? 1 : 0;
6725 if (quote)
6726 result += 2;
6727 if (buf != NULL)
6728 {
6729 if (quote)
6730 *buf++ = '"';
6731 if (eap->forceit)
6732 *buf++ = '!';
6733 if (quote)
6734 *buf = '"';
6735 }
6736 break;
6737
6738 case ct_LINE1:
6739 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006740 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 case ct_COUNT:
6742 {
6743 char num_buf[20];
6744 long num = (type == ct_LINE1) ? eap->line1 :
6745 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006746 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6748 size_t num_len;
6749
6750 sprintf(num_buf, "%ld", num);
6751 num_len = STRLEN(num_buf);
6752 result = num_len;
6753
6754 if (quote)
6755 result += 2;
6756
6757 if (buf != NULL)
6758 {
6759 if (quote)
6760 *buf++ = '"';
6761 STRCPY(buf, num_buf);
6762 buf += num_len;
6763 if (quote)
6764 *buf = '"';
6765 }
6766
6767 break;
6768 }
6769
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006770 case ct_MODS:
6771 {
6772 int multi_mods = 0;
6773 typedef struct {
6774 int *varp;
6775 char *name;
6776 } mod_entry_T;
6777 static mod_entry_T mod_entries[] = {
6778#ifdef FEAT_BROWSE_CMD
6779 {&cmdmod.browse, "browse"},
6780#endif
6781#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6782 {&cmdmod.confirm, "confirm"},
6783#endif
6784 {&cmdmod.hide, "hide"},
6785 {&cmdmod.keepalt, "keepalt"},
6786 {&cmdmod.keepjumps, "keepjumps"},
6787 {&cmdmod.keepmarks, "keepmarks"},
6788 {&cmdmod.keeppatterns, "keeppatterns"},
6789 {&cmdmod.lockmarks, "lockmarks"},
6790 {&cmdmod.noswapfile, "noswapfile"},
6791 {NULL, NULL}
6792 };
6793 int i;
6794
6795 result = quote ? 2 : 0;
6796 if (buf != NULL)
6797 {
6798 if (quote)
6799 *buf++ = '"';
6800 *buf = '\0';
6801 }
6802
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006803 /* :aboveleft and :leftabove */
6804 if (cmdmod.split & WSP_ABOVE)
6805 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6806 /* :belowright and :rightbelow */
6807 if (cmdmod.split & WSP_BELOW)
6808 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6809 /* :botright */
6810 if (cmdmod.split & WSP_BOT)
6811 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006812
6813 /* the modifiers that are simple flags */
6814 for (i = 0; mod_entries[i].varp != NULL; ++i)
6815 if (*mod_entries[i].varp)
6816 result += add_cmd_modifier(buf, mod_entries[i].name,
6817 &multi_mods);
6818
6819 /* TODO: How to support :noautocmd? */
6820#ifdef HAVE_SANDBOX
6821 /* TODO: How to support :sandbox?*/
6822#endif
6823 /* :silent */
6824 if (msg_silent > 0)
6825 result += add_cmd_modifier(buf,
6826 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006827 /* :tab */
6828 if (cmdmod.tab > 0)
6829 result += add_cmd_modifier(buf, "tab", &multi_mods);
6830 /* :topleft */
6831 if (cmdmod.split & WSP_TOP)
6832 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006833 /* TODO: How to support :unsilent?*/
6834 /* :verbose */
6835 if (p_verbose > 0)
6836 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006837 /* :vertical */
6838 if (cmdmod.split & WSP_VERT)
6839 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006840 if (quote && buf != NULL)
6841 {
6842 buf += result - 2;
6843 *buf = '"';
6844 }
6845 break;
6846 }
6847
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 case ct_REGISTER:
6849 result = eap->regname ? 1 : 0;
6850 if (quote)
6851 result += 2;
6852 if (buf != NULL)
6853 {
6854 if (quote)
6855 *buf++ = '\'';
6856 if (eap->regname)
6857 *buf++ = eap->regname;
6858 if (quote)
6859 *buf = '\'';
6860 }
6861 break;
6862
6863 case ct_LT:
6864 result = 1;
6865 if (buf != NULL)
6866 *buf = '<';
6867 break;
6868
6869 default:
6870 /* Not recognized: just copy the '<' and return -1. */
6871 result = (size_t)-1;
6872 if (buf != NULL)
6873 *buf = '<';
6874 break;
6875 }
6876
6877 return result;
6878}
6879
6880 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006881do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882{
6883 char_u *buf;
6884 char_u *p;
6885 char_u *q;
6886
6887 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006888 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006889 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 size_t len, totlen;
6891
6892 size_t split_len = 0;
6893 char_u *split_buf = NULL;
6894 ucmd_T *cmd;
6895#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006896 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897#endif
6898
6899 if (eap->cmdidx == CMD_USER)
6900 cmd = USER_CMD(eap->useridx);
6901 else
6902 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6903
6904 /*
6905 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006906 * First round: "buf" is NULL, compute length, allocate "buf".
6907 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 */
6909 buf = NULL;
6910 for (;;)
6911 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006912 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006913 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006915
6916 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006918 start = vim_strchr(p, '<');
6919 if (start != NULL)
6920 end = vim_strchr(start + 1, '>');
6921 if (buf != NULL)
6922 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006923 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6924 ;
6925 if (*ksp == K_SPECIAL
6926 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006927 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6928# ifdef FEAT_GUI
6929 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6930# endif
6931 ))
6932 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006933 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006934 * KS_SPECIAL KE_FILLER, like for mappings, but
6935 * do_cmdline() doesn't handle that, so convert it back.
6936 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6937 len = ksp - p;
6938 if (len > 0)
6939 {
6940 mch_memmove(q, p, len);
6941 q += len;
6942 }
6943 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6944 p = ksp + 3;
6945 continue;
6946 }
6947 }
6948
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006949 /* break if no <item> is found */
Bram Moolenaara850a712009-01-28 14:42:59 +00006950 if (start == NULL || end == NULL)
6951 break;
6952
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 /* Include the '>' */
6954 ++end;
6955
6956 /* Take everything up to the '<' */
6957 len = start - p;
6958 if (buf == NULL)
6959 totlen += len;
6960 else
6961 {
6962 mch_memmove(q, p, len);
6963 q += len;
6964 }
6965
6966 len = uc_check_code(start, end - start, q, cmd, eap,
6967 &split_buf, &split_len);
6968 if (len == (size_t)-1)
6969 {
6970 /* no match, continue after '<' */
6971 p = start + 1;
6972 len = 1;
6973 }
6974 else
6975 p = end;
6976 if (buf == NULL)
6977 totlen += len;
6978 else
6979 q += len;
6980 }
6981 if (buf != NULL) /* second time here, finished */
6982 {
6983 STRCPY(q, p);
6984 break;
6985 }
6986
6987 totlen += STRLEN(p); /* Add on the trailing characters */
6988 buf = alloc((unsigned)(totlen + 1));
6989 if (buf == NULL)
6990 {
6991 vim_free(split_buf);
6992 return;
6993 }
6994 }
6995
6996#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006997 current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998#endif
6999 (void)do_cmdline(buf, eap->getline, eap->cookie,
7000 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
7001#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007002 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003#endif
7004 vim_free(buf);
7005 vim_free(split_buf);
7006}
7007
7008# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7009 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007010get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011{
7012 return get_user_commands(NULL, idx - (int)CMD_SIZE);
7013}
7014
7015/*
7016 * Function given to ExpandGeneric() to obtain the list of user command names.
7017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007019get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020{
7021 if (idx < curbuf->b_ucmds.ga_len)
7022 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
7023 idx -= curbuf->b_ucmds.ga_len;
7024 if (idx < ucmds.ga_len)
7025 return USER_CMD(idx)->uc_name;
7026 return NULL;
7027}
7028
7029/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007030 * Function given to ExpandGeneric() to obtain the list of user address type names.
7031 */
7032 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007033get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007034{
7035 return (char_u *)addr_type_complete[idx].name;
7036}
7037
7038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 * Function given to ExpandGeneric() to obtain the list of user command
7040 * attributes.
7041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007043get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044{
7045 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007046 {"addr", "bang", "bar", "buffer", "complete",
7047 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007049 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 return NULL;
7051 return (char_u *)user_cmd_flags[idx];
7052}
7053
7054/*
7055 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007058get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059{
7060 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7061
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007062 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 return NULL;
7064 return (char_u *)user_cmd_nargs[idx];
7065}
7066
7067/*
7068 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007071get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072{
7073 return (char_u *)command_complete[idx].name;
7074}
7075# endif /* FEAT_CMDL_COMPL */
7076
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007077/*
7078 * Parse address type argument
7079 */
7080 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007081parse_addr_type_arg(
7082 char_u *value,
7083 int vallen,
7084 long *argt,
7085 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007086{
7087 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007088
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007089 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7090 {
7091 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7092 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7093 if (a && b)
7094 {
7095 *addr_type_arg = addr_type_complete[i].expand;
7096 break;
7097 }
7098 }
7099
7100 if (addr_type_complete[i].expand == -1)
7101 {
7102 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007103
Bram Moolenaar1c465442017-03-12 20:10:05 +01007104 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007105 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007106 err[i] = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007107 semsg(_("E180: Invalid address type value: %s"), err);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007108 return FAIL;
7109 }
7110
7111 if (*addr_type_arg != ADDR_LINES)
7112 *argt |= NOTADR;
7113
7114 return OK;
7115}
7116
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117#endif /* FEAT_USR_CMDS */
7118
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007119#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7120/*
7121 * Parse a completion argument "value[vallen]".
7122 * The detected completion goes in "*complp", argument type in "*argt".
7123 * When there is an argument, for function and user defined completion, it's
7124 * copied to allocated memory and stored in "*compl_arg".
7125 * Returns FAIL if something is wrong.
7126 */
7127 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007128parse_compl_arg(
7129 char_u *value,
7130 int vallen,
7131 int *complp,
7132 long *argt,
7133 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007134{
7135 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007136# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007137 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007138# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007139 int i;
7140 int valend = vallen;
7141
7142 /* Look for any argument part - which is the part after any ',' */
7143 for (i = 0; i < vallen; ++i)
7144 {
7145 if (value[i] == ',')
7146 {
7147 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007148# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007149 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007150# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007151 valend = i;
7152 break;
7153 }
7154 }
7155
7156 for (i = 0; command_complete[i].expand != 0; ++i)
7157 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007158 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007159 && STRNCMP(value, command_complete[i].name, valend) == 0)
7160 {
7161 *complp = command_complete[i].expand;
7162 if (command_complete[i].expand == EXPAND_BUFFERS)
7163 *argt |= BUFNAME;
7164 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7165 || command_complete[i].expand == EXPAND_FILES)
7166 *argt |= XFILE;
7167 break;
7168 }
7169 }
7170
7171 if (command_complete[i].expand == 0)
7172 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007173 semsg(_("E180: Invalid complete value: %s"), value);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007174 return FAIL;
7175 }
7176
7177# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7178 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7179 && arg != NULL)
7180# else
7181 if (arg != NULL)
7182# endif
7183 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007184 emsg(_("E468: Completion argument only allowed for custom completion"));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007185 return FAIL;
7186 }
7187
7188# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7189 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7190 && arg == NULL)
7191 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007192 emsg(_("E467: Custom completion requires a function argument"));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007193 return FAIL;
7194 }
7195
7196 if (arg != NULL)
7197 *compl_arg = vim_strnsave(arg, (int)arglen);
7198# endif
7199 return OK;
7200}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007201
7202 int
7203cmdcomplete_str_to_type(char_u *complete_str)
7204{
7205 int i;
7206
7207 for (i = 0; command_complete[i].expand != 0; ++i)
7208 if (STRCMP(complete_str, command_complete[i].name) == 0)
7209 return command_complete[i].expand;
7210
7211 return EXPAND_NOTHING;
7212}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007213#endif
7214
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007216ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217{
Bram Moolenaare6850792010-05-14 15:28:44 +02007218 if (*eap->arg == NUL)
7219 {
7220#ifdef FEAT_EVAL
7221 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7222 char_u *p = NULL;
7223
7224 if (expr != NULL)
7225 {
7226 ++emsg_off;
7227 p = eval_to_string(expr, NULL, FALSE);
7228 --emsg_off;
7229 vim_free(expr);
7230 }
7231 if (p != NULL)
7232 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007233 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02007234 vim_free(p);
7235 }
7236 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007237 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02007238#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007239 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02007240#endif
7241 }
7242 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007243 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01007244
7245#ifdef FEAT_VTP
7246 else if (has_vtp_working())
7247 {
7248 // background color change requires clear + redraw
7249 update_screen(CLEAR);
7250 redrawcmd();
7251 }
7252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253}
7254
7255 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007256ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257{
7258 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01007259 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 do_highlight(eap->arg, eap->forceit, FALSE);
7261}
7262
7263
7264/*
7265 * Call this function if we thought we were going to exit, but we won't
7266 * (because of an error). May need to restore the terminal mode.
7267 */
7268 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007269not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270{
7271 exiting = FALSE;
7272 settmode(TMODE_RAW);
7273}
7274
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007275 static int
7276before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7277{
7278 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7279
7280 /* Bail out when autocommands closed the window.
7281 * Refuse to quit when the buffer in the last window is being closed (can
7282 * only happen in autocommands). */
7283 if (!win_valid(wp)
7284 || curbuf_locked()
7285 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7286 return TRUE;
7287
7288 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7289 {
7290 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7291 /* Refuse to quit when locked or when the buffer in the last window is
7292 * being closed (can only happen in autocommands). */
7293 if (curbuf_locked()
7294 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7295 return TRUE;
7296 }
7297
7298 return FALSE;
7299}
7300
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007302 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007303 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 */
7305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007306ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007308 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007309
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310#ifdef FEAT_CMDWIN
7311 if (cmdwin_type != 0)
7312 {
7313 cmdwin_result = Ctrl_C;
7314 return;
7315 }
7316#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007317 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007318 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007319 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007320 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007321 return;
7322 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007323 if (eap->addr_count > 0)
7324 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007325 int wnr = eap->line2;
7326
7327 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7328 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007329 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007330 }
7331 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007332 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007333
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007334 /* Refuse to quit when locked. */
7335 if (curbuf_locked())
7336 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007337
7338 /* Trigger QuitPre and maybe ExitPre */
7339 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007340 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341
7342#ifdef FEAT_NETBEANS_INTG
7343 netbeansForcedQuit = eap->forceit;
7344#endif
7345
7346 /*
7347 * If there are more files or windows we won't exit.
7348 */
7349 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7350 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007351 if ((!buf_hide(wp->w_buffer)
7352 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007353 | (eap->forceit ? CCGD_FORCEIT : 0)
7354 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007356 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 {
7358 not_exiting();
7359 }
7360 else
7361 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007362 /* quit last window
7363 * Note: only_one_window() returns true, even so a help window is
7364 * still open. In that case only quit, if no address has been
7365 * specified. Example:
7366 * :h|wincmd w|1q - don't quit
7367 * :h|wincmd w|q - quit
7368 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007369 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007371 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007372#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007376 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 }
7378}
7379
7380/*
7381 * ":cquit".
7382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007384ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385{
7386 getout(1); /* this does not always pass on the exit code to the Manx
7387 compiler. why? */
7388}
7389
7390/*
7391 * ":qall": try to quit all windows
7392 */
7393 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007394ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395{
7396# ifdef FEAT_CMDWIN
7397 if (cmdwin_type != 0)
7398 {
7399 if (eap->forceit)
7400 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7401 else
7402 cmdwin_result = K_XF2;
7403 return;
7404 }
7405# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007406
7407 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007408 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007409 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007410 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007411 return;
7412 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007413
7414 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007415 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007416
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007418 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 getout(0);
7420 not_exiting();
7421}
7422
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423/*
7424 * ":close": close current window, unless it is the last one
7425 */
7426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007427ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007429 win_T *win;
7430 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007431#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007433 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007435#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007436 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007437 {
7438 if (eap->addr_count == 0)
7439 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02007440 else
7441 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007442 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007443 {
7444 winnr++;
7445 if (winnr == eap->line2)
7446 break;
7447 }
7448 if (win == NULL)
7449 win = lastwin;
7450 ex_win_close(eap->forceit, win, NULL);
7451 }
7452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453}
7454
Bram Moolenaar4033c552017-09-16 20:54:51 +02007455#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007456/*
7457 * ":pclose": Close any preview window.
7458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007460ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007461{
7462 win_T *win;
7463
Bram Moolenaar29323592016-07-24 22:04:11 +02007464 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007465 if (win->w_p_pvw)
7466 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007467 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007468 break;
7469 }
7470}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007471#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007472
Bram Moolenaarf740b292006-02-16 22:11:02 +00007473/*
7474 * Close window "win" and take care of handling closing the last window for a
7475 * modified buffer.
7476 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007477 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007478ex_win_close(
7479 int forceit,
7480 win_T *win,
7481 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482{
7483 int need_hide;
7484 buf_T *buf = win->w_buffer;
7485
7486 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007487 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007489#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 if ((p_confirm || cmdmod.confirm) && p_write)
7491 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007492 bufref_T bufref;
7493
7494 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007496 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 return;
7498 need_hide = FALSE;
7499 }
7500 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007503 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 return;
7505 }
7506 }
7507
Bram Moolenaar4033c552017-09-16 20:54:51 +02007508#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007510#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007511
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007513 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007514 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007515 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007516 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517}
7518
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007520 * Handle the argument for a tabpage related ex command.
7521 * Returns a tabpage number.
7522 * When an error is encountered then eap->errmsg is set.
7523 */
7524 static int
7525get_tabpage_arg(exarg_T *eap)
7526{
7527 int tab_number;
7528 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7529
7530 if (eap->arg && *eap->arg != NUL)
7531 {
7532 char_u *p = eap->arg;
7533 char_u *p_save;
7534 int relative = 0; /* argument +N/-N means: go to N places to the
7535 * right/left relative to the current position. */
7536
7537 if (*p == '-')
7538 {
7539 relative = -1;
7540 p++;
7541 }
7542 else if (*p == '+')
7543 {
7544 relative = 1;
7545 p++;
7546 }
7547
7548 p_save = p;
7549 tab_number = getdigits(&p);
7550
7551 if (relative == 0)
7552 {
7553 if (STRCMP(p, "$") == 0)
7554 tab_number = LAST_TAB_NR;
7555 else if (p == p_save || *p_save == '-' || *p != NUL
7556 || tab_number > LAST_TAB_NR)
7557 {
7558 /* No numbers as argument. */
7559 eap->errmsg = e_invarg;
7560 goto theend;
7561 }
7562 }
7563 else
7564 {
7565 if (*p_save == NUL)
7566 tab_number = 1;
7567 else if (p == p_save || *p_save == '-' || *p != NUL
7568 || tab_number == 0)
7569 {
7570 /* No numbers as argument. */
7571 eap->errmsg = e_invarg;
7572 goto theend;
7573 }
7574 tab_number = tab_number * relative + tabpage_index(curtab);
7575 if (!unaccept_arg0 && relative == -1)
7576 --tab_number;
7577 }
7578 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7579 eap->errmsg = e_invarg;
7580 }
7581 else if (eap->addr_count > 0)
7582 {
7583 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007584 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007585 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007586 tab_number = 0;
7587 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007588 else
7589 {
7590 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01007591 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01007592 {
7593 --tab_number;
7594 if (tab_number < unaccept_arg0)
7595 eap->errmsg = e_invarg;
7596 }
7597 }
7598 }
7599 else
7600 {
7601 switch (eap->cmdidx)
7602 {
7603 case CMD_tabnext:
7604 tab_number = tabpage_index(curtab) + 1;
7605 if (tab_number > LAST_TAB_NR)
7606 tab_number = 1;
7607 break;
7608 case CMD_tabmove:
7609 tab_number = LAST_TAB_NR;
7610 break;
7611 default:
7612 tab_number = tabpage_index(curtab);
7613 }
7614 }
7615
7616theend:
7617 return tab_number;
7618}
7619
7620/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007621 * ":tabclose": close current tab page, unless it is the last one.
7622 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 */
7624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007625ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007627 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007628 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007629
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007630# ifdef FEAT_CMDWIN
7631 if (cmdwin_type != 0)
7632 cmdwin_result = K_IGNORE;
7633 else
7634# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007635 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007636 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007637 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007639 tab_number = get_tabpage_arg(eap);
7640 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007641 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007642 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007643 if (tp == NULL)
7644 {
7645 beep_flush();
7646 return;
7647 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007648 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007649 {
7650 tabpage_close_other(tp, eap->forceit);
7651 return;
7652 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007653 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007654 tabpage_close(eap->forceit);
7655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007657}
7658
7659/*
7660 * ":tabonly": close all tab pages except the current one
7661 */
7662 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007663ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007664{
7665 tabpage_T *tp;
7666 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007667 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007668
7669# ifdef FEAT_CMDWIN
7670 if (cmdwin_type != 0)
7671 cmdwin_result = K_IGNORE;
7672 else
7673# endif
7674 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007675 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007676 else
7677 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007678 tab_number = get_tabpage_arg(eap);
7679 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007680 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007681 goto_tabpage(tab_number);
7682 /* Repeat this up to a 1000 times, because autocommands may
7683 * mess up the lists. */
7684 for (done = 0; done < 1000; ++done)
7685 {
7686 FOR_ALL_TABPAGES(tp)
7687 if (tp->tp_topframe != topframe)
7688 {
7689 tabpage_close_other(tp, eap->forceit);
7690 /* if we failed to close it quit */
7691 if (valid_tabpage(tp))
7692 done = 1000;
7693 /* start over, "tp" is now invalid */
7694 break;
7695 }
7696 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007697 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007698 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007699 }
7700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702
7703/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007704 * Close the current tab page.
7705 */
7706 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007707tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007708{
7709 /* First close all the windows but the current one. If that worked then
7710 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007711 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007712 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007713 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007714 ex_win_close(forceit, curwin, NULL);
7715# ifdef FEAT_GUI
7716 need_mouse_correct = TRUE;
7717# endif
7718}
7719
7720/*
7721 * Close tab page "tp", which is not the current tab page.
7722 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007723 * Also takes care of the tab pages line disappearing when closing the
7724 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007725 */
7726 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007727tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007728{
7729 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007730 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007731 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007732
7733 /* Limit to 1000 windows, autocommands may add a window while we close
7734 * one. OK, so I'm paranoid... */
7735 while (++done < 1000)
7736 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007737 wp = tp->tp_firstwin;
7738 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007739
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007740 /* Autocommands may delete the tab page under our fingers and we may
7741 * fail to close a window with a modified buffer. */
7742 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007743 break;
7744 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007745
Bram Moolenaar29323592016-07-24 22:04:11 +02007746 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007747
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007748 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007749 if (h != tabline_height())
7750 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007751}
7752
7753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 * ":only".
7755 */
7756 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007757ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007759 win_T *wp;
7760 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761# ifdef FEAT_GUI
7762 need_mouse_correct = TRUE;
7763# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007764 if (eap->addr_count > 0)
7765 {
7766 wnr = eap->line2;
7767 for (wp = firstwin; --wnr > 0; )
7768 {
7769 if (wp->w_next == NULL)
7770 break;
7771 else
7772 wp = wp->w_next;
7773 }
7774 win_goto(wp);
7775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 close_others(TRUE, eap->forceit);
7777}
7778
7779/*
7780 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007781 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007783 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007784ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785{
7786 if (eap->addr_count == 0)
7787 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007788 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790
7791 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007792ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007794 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007795 if (!eap->skip)
7796 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007797#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007798 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007799#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007800 if (eap->addr_count == 0)
7801 win_close(curwin, FALSE); /* don't free buffer */
7802 else
7803 {
7804 int winnr = 0;
7805 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007806
Bram Moolenaar2256c992016-11-15 21:17:07 +01007807 FOR_ALL_WINDOWS(win)
7808 {
7809 winnr++;
7810 if (winnr == eap->line2)
7811 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007812 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007813 if (win == NULL)
7814 win = lastwin;
7815 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 }
7818}
7819
7820/*
7821 * ":stop" and ":suspend": Suspend Vim.
7822 */
7823 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007824ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825{
7826 /*
7827 * Disallow suspending for "rvim".
7828 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007829 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {
7831 if (!eap->forceit)
7832 autowrite_all();
7833 windgoto((int)Rows - 1, 0);
7834 out_char('\n');
7835 out_flush();
7836 stoptermcap();
7837 out_flush(); /* needed for SUN to restore xterm buffer */
7838#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02007839 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840#endif
7841 ui_suspend(); /* call machine specific function */
7842#ifdef FEAT_TITLE
7843 maketitle();
7844 resettitle(); /* force updating the title */
7845#endif
7846 starttermcap();
7847 scroll_start(); /* scroll screen before redrawing */
7848 redraw_later_clear();
7849 shell_resized(); /* may have resized window */
7850 }
7851}
7852
7853/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007854 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 */
7856 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007857ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858{
7859#ifdef FEAT_CMDWIN
7860 if (cmdwin_type != 0)
7861 {
7862 cmdwin_result = Ctrl_C;
7863 return;
7864 }
7865#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007866 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007867 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007868 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007869 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007870 return;
7871 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007872
7873 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007874 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875
7876 /*
7877 * if more files or windows we won't exit
7878 */
7879 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7880 exiting = TRUE;
7881 if ( ((eap->cmdidx == CMD_wq
7882 || curbufIsChanged())
7883 && do_write(eap) == FAIL)
7884 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007885 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {
7887 not_exiting();
7888 }
7889 else
7890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007893 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894# ifdef FEAT_GUI
7895 need_mouse_correct = TRUE;
7896# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007897 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007898 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 }
7900}
7901
7902/*
7903 * ":print", ":list", ":number".
7904 */
7905 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007906ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007908 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007909 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007910 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007912 for ( ;!got_int; ui_breakcheck())
7913 {
7914 print_line(eap->line1,
7915 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7916 || (eap->flags & EXFLAG_NR)),
7917 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7918 if (++eap->line1 > eap->line2)
7919 break;
7920 out_flush(); /* show one line at a time */
7921 }
7922 setpcmark();
7923 /* put cursor at last line */
7924 curwin->w_cursor.lnum = eap->line2;
7925 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 }
7927
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929}
7930
7931#ifdef FEAT_BYTEOFF
7932 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007933ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934{
7935 goto_byte(eap->line2);
7936}
7937#endif
7938
7939/*
7940 * ":shell".
7941 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007943ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944{
7945 do_shell(NULL, 0);
7946}
7947
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007948#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007950static int drop_busy = FALSE;
7951static int drop_filec;
7952static char_u **drop_filev = NULL;
7953static int drop_split;
7954static void (*drop_callback)(void *);
7955static void *drop_cookie;
7956
7957 static void
7958handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959{
7960 exarg_T ea;
7961 int save_msg_scroll = msg_scroll;
7962
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007963 // Setting the argument list may cause screen updates and being called
7964 // recursively. Avoid that by setting drop_busy.
7965 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966
7967 /* Check whether the current buffer is changed. If so, we will need
7968 * to split the current window or data could be lost.
7969 * We don't need to check if the 'hidden' option is set, as in this
7970 * case the buffer won't be lost.
7971 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007972 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {
7974 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007975 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 --emsg_off;
7977 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007978 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 if (win_split(0, 0) == FAIL)
7981 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007982 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983
7984 /* When splitting the window, create a new alist. Otherwise the
7985 * existing one is overwritten. */
7986 alist_unlink(curwin->w_alist);
7987 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 }
7989
7990 /*
7991 * Set up the new argument list.
7992 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007993 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994
7995 /*
7996 * Move to the first file.
7997 */
7998 /* Fake up a minimal "next" command for do_argfile() */
7999 vim_memset(&ea, 0, sizeof(ea));
8000 ea.cmd = (char_u *)"next";
8001 do_argfile(&ea, 0);
8002
8003 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
8004 * mode that is not needed here. */
8005 need_start_insertmode = FALSE;
8006
8007 /* Restore msg_scroll, otherwise a following command may cause scrolling
8008 * unexpectedly. The screen will be redrawn by the caller, thus
8009 * msg_scroll being set by displaying a message is irrelevant. */
8010 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02008011
8012 if (drop_callback != NULL)
8013 drop_callback(drop_cookie);
8014
8015 drop_filev = NULL;
8016 drop_busy = FALSE;
8017}
8018
8019/*
8020 * Handle a file drop. The code is here because a drop is *nearly* like an
8021 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008022 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02008023 * code is very similar to :args and hence needs access to a lot of the static
8024 * functions in this file.
8025 *
8026 * The "filev" list must have been allocated using alloc(), as should each item
8027 * in the list. This function takes over responsibility for freeing the "filev"
8028 * list.
8029 */
8030 void
8031handle_drop(
8032 int filec, // the number of files dropped
8033 char_u **filev, // the list of files dropped
8034 int split, // force splitting the window
8035 void (*callback)(void *), // to be called after setting the argument
8036 // list
8037 void *cookie) // argument for "callback" (allocated)
8038{
8039 // Cannot handle recursive drops, finish the pending one.
8040 if (drop_busy)
8041 {
8042 FreeWild(filec, filev);
8043 vim_free(cookie);
8044 return;
8045 }
8046
8047 // When calling handle_drop() more than once in a row we only use the last
8048 // one.
8049 if (drop_filev != NULL)
8050 {
8051 FreeWild(drop_filec, drop_filev);
8052 vim_free(drop_cookie);
8053 }
8054
8055 drop_filec = filec;
8056 drop_filev = filev;
8057 drop_split = split;
8058 drop_callback = callback;
8059 drop_cookie = cookie;
8060
8061 // Postpone this when:
8062 // - editing the command line
8063 // - not possible to change the current buffer
8064 // - updating the screen
8065 // As it may change buffers and window structures that are in use and cause
8066 // freed memory to be used.
8067 if (text_locked() || curbuf_locked() || updating_screen)
8068 return;
8069
8070 handle_drop_internal();
8071}
8072
8073/*
8074 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
8075 * reset: Handle a postponed drop.
8076 */
8077 void
8078handle_any_postponed_drop(void)
8079{
8080 if (!drop_busy && drop_filev != NULL
8081 && !text_locked() && !curbuf_locked() && !updating_screen)
8082 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083}
8084#endif
8085
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086/*
8087 * Clear an argument list: free all file names and reset it to zero entries.
8088 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008089 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008090alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091{
8092 while (--al->al_ga.ga_len >= 0)
8093 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8094 ga_clear(&al->al_ga);
8095}
8096
8097/*
8098 * Init an argument list.
8099 */
8100 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008101alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102{
8103 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8104}
8105
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106/*
8107 * Remove a reference from an argument list.
8108 * Ignored when the argument list is the global one.
8109 * If the argument list is no longer used by any window, free it.
8110 */
8111 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008112alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113{
8114 if (al != &global_alist && --al->al_refcount <= 0)
8115 {
8116 alist_clear(al);
8117 vim_free(al);
8118 }
8119}
8120
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121/*
8122 * Create a new argument list and use it for the current window.
8123 */
8124 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008125alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126{
8127 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8128 if (curwin->w_alist == NULL)
8129 {
8130 curwin->w_alist = &global_alist;
8131 ++global_alist.al_refcount;
8132 }
8133 else
8134 {
8135 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008136 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 alist_init(curwin->w_alist);
8138 }
8139}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008141#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142/*
8143 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008144 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8145 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 */
8147 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008148alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149{
8150 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008151 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 char_u **new_arg_files;
8153 int new_arg_file_count;
8154 char_u *save_p_su = p_su;
8155 int i;
8156
8157 /* Don't use 'suffixes' here. This should work like the shell did the
8158 * expansion. Also, the vimrc file isn't read yet, thus the user
8159 * can't set the options. */
8160 p_su = empty_option;
8161 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8162 if (old_arg_files != NULL)
8163 {
8164 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008165 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8166 old_arg_count = GARGCOUNT;
8167 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008169 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 && new_arg_file_count > 0)
8171 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008172 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8173 TRUE, fnum_list, fnum_len);
8174 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 }
8177 p_su = save_p_su;
8178}
8179#endif
8180
8181/*
8182 * Set the argument list for the current window.
8183 * Takes over the allocated files[] and the allocated fnames in it.
8184 */
8185 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008186alist_set(
8187 alist_T *al,
8188 int count,
8189 char_u **files,
8190 int use_curbuf,
8191 int *fnum_list,
8192 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193{
8194 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008195 static int recursive = 0;
8196
8197 if (recursive)
8198 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008199 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008200 return;
8201 }
8202 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203
8204 alist_clear(al);
8205 if (ga_grow(&al->al_ga, count) == OK)
8206 {
8207 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008208 {
8209 if (got_int)
8210 {
8211 /* When adding many buffers this can take a long time. Allow
8212 * interrupting here. */
8213 while (i < count)
8214 vim_free(files[i++]);
8215 break;
8216 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008217
8218 /* May set buffer name of a buffer previously used for the
8219 * argument list, so that it's re-used by alist_add. */
8220 if (fnum_list != NULL && i < fnum_len)
8221 buf_set_name(fnum_list[i], files[i]);
8222
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008224 ui_breakcheck();
8225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 vim_free(files);
8227 }
8228 else
8229 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008232
8233 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234}
8235
8236/*
8237 * Add file "fname" to argument list "al".
8238 * "fname" must have been allocated and "al" must have been checked for room.
8239 */
8240 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008241alist_add(
8242 alist_T *al,
8243 char_u *fname,
8244 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245{
8246 if (fname == NULL) /* don't add NULL file names */
8247 return;
8248#ifdef BACKSLASH_IN_FILENAME
8249 slash_adjust(fname);
8250#endif
8251 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8252 if (set_fnum > 0)
8253 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8254 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8255 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256}
8257
8258#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8259/*
8260 * Adjust slashes in file names. Called after 'shellslash' was set.
8261 */
8262 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008263alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264{
8265 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008267 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268
8269 for (i = 0; i < GARGCOUNT; ++i)
8270 if (GARGLIST[i].ae_fname != NULL)
8271 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008272 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 if (wp->w_alist != &global_alist)
8274 for (i = 0; i < WARGCOUNT(wp); ++i)
8275 if (WARGLIST(wp)[i].ae_fname != NULL)
8276 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277}
8278#endif
8279
8280/*
8281 * ":preserve".
8282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008284ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008286 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 ml_preserve(curbuf, TRUE);
8288}
8289
8290/*
8291 * ":recover".
8292 */
8293 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008294ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295{
8296 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8297 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008298 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8299 | CCGD_MULTWIN
8300 | (eap->forceit ? CCGD_FORCEIT : 0)
8301 | CCGD_EXCMD)
8302
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 && (*eap->arg == NUL
8304 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8305 ml_recover();
8306 recoverymode = FALSE;
8307}
8308
8309/*
8310 * Command modifier used in a wrong way.
8311 */
8312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008313ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314{
8315 eap->errmsg = e_invcmd;
8316}
8317
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318/*
8319 * :sview [+command] file split window with new file, read-only
8320 * :split [[+command] file] split window with current or new file
8321 * :vsplit [[+command] file] split window vertically with current or new file
8322 * :new [[+command] file] split window with no or new file
8323 * :vnew [[+command] file] split vertically window with no or new file
8324 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008325 *
8326 * :tabedit open new Tab page with empty window
8327 * :tabedit [+command] file open new Tab page and edit "file"
8328 * :tabnew [[+command] file] just like :tabedit
8329 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 */
8331 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008332ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008334 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008335#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008337#endif
8338#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008340#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02008341 int use_tab = eap->cmdidx == CMD_tabedit
8342 || eap->cmdidx == CMD_tabfind
8343 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344
Bram Moolenaar4033c552017-09-16 20:54:51 +02008345#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348
Bram Moolenaar4033c552017-09-16 20:54:51 +02008349#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008351 * quickfix windows. But it's OK when doing ":tab split". */
8352 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {
8354 if (eap->cmdidx == CMD_split)
8355 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 if (eap->cmdidx == CMD_vsplit)
8357 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360
Bram Moolenaar4033c552017-09-16 20:54:51 +02008361#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008362 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 {
8364 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8365 FNAME_MESS, TRUE, curbuf->b_ffname);
8366 if (fname == NULL)
8367 goto theend;
8368 eap->arg = fname;
8369 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008370# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008371 else
8372# endif
8373#endif
8374#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 && eap->cmdidx != CMD_new)
8378 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008379 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008380# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008381 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008382# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008383 au_has_group((char_u *)"FileExplorer"))
8384 {
8385 /* No browsing supported but we do have the file explorer:
8386 * Edit the directory. */
8387 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8388 eap->arg = (char_u *)".";
8389 }
8390 else
8391 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02008392 fname = do_browse(0, (char_u *)(use_tab
8393 ? _("Edit File in new tab page")
8394 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008396 if (fname == NULL)
8397 goto theend;
8398 eap->arg = fname;
8399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 }
8401 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008404 /*
8405 * Either open new tab page or split the window.
8406 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02008407 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008408 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008409 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8410 : eap->addr_count == 0 ? 0
8411 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008412 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008413 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008414
8415 /* set the alternate buffer for the window we came from */
8416 if (curwin != old_curwin
8417 && win_valid(old_curwin)
8418 && old_curwin->w_buffer != curbuf
8419 && !cmdmod.keepalt)
8420 old_curwin->w_alt_fnum = curbuf->b_fnum;
8421 }
8422 }
8423 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8425 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 /* Reset 'scrollbind' when editing another file, but keep it when
8427 * doing ":split" without arguments. */
8428 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008429# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008431# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008433 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 else
8435 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 do_exedit(eap, old_curwin);
8437 }
8438
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008439# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008441# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008443# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444theend:
8445 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008446# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008448
8449/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008450 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008451 */
8452 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008453tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008454{
8455 exarg_T ea;
8456
8457 vim_memset(&ea, 0, sizeof(ea));
8458 ea.cmdidx = CMD_tabnew;
8459 ea.cmd = (char_u *)"tabn";
8460 ea.arg = (char_u *)"";
8461 ex_splitview(&ea);
8462}
8463
8464/*
8465 * :tabnext command
8466 */
8467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008468ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008469{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008470 int tab_number;
8471
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008472 switch (eap->cmdidx)
8473 {
8474 case CMD_tabfirst:
8475 case CMD_tabrewind:
8476 goto_tabpage(1);
8477 break;
8478 case CMD_tablast:
8479 goto_tabpage(9999);
8480 break;
8481 case CMD_tabprevious:
8482 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008483 if (eap->arg && *eap->arg != NUL)
8484 {
8485 char_u *p = eap->arg;
8486 char_u *p_save = p;
8487
8488 tab_number = getdigits(&p);
8489 if (p == p_save || *p_save == '-' || *p != NUL
8490 || tab_number == 0)
8491 {
8492 /* No numbers as argument. */
8493 eap->errmsg = e_invarg;
8494 return;
8495 }
8496 }
8497 else
8498 {
8499 if (eap->addr_count == 0)
8500 tab_number = 1;
8501 else
8502 {
8503 tab_number = eap->line2;
8504 if (tab_number < 1)
8505 {
8506 eap->errmsg = e_invrange;
8507 return;
8508 }
8509 }
8510 }
8511 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008512 break;
8513 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008514 tab_number = get_tabpage_arg(eap);
8515 if (eap->errmsg == NULL)
8516 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008517 break;
8518 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008519}
8520
8521/*
8522 * :tabmove command
8523 */
8524 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008525ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008526{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008527 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008528
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008529 tab_number = get_tabpage_arg(eap);
8530 if (eap->errmsg == NULL)
8531 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008532}
8533
8534/*
8535 * :tabs command: List tabs and their contents.
8536 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008538ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008539{
8540 tabpage_T *tp;
8541 win_T *wp;
8542 int tabcount = 1;
8543
8544 msg_start();
8545 msg_scroll = TRUE;
8546 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8547 {
8548 msg_putchar('\n');
8549 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008550 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008551 out_flush(); /* output one line at a time */
8552 ui_breakcheck();
8553
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008554 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008555 wp = firstwin;
8556 else
8557 wp = tp->tp_firstwin;
8558 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8559 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008560 msg_putchar('\n');
8561 msg_putchar(wp == curwin ? '>' : ' ');
8562 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008563 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8564 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008565 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008566 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008567 else
8568 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8569 IObuff, IOSIZE, TRUE);
8570 msg_outtrans(IObuff);
8571 out_flush(); /* output one line at a time */
8572 ui_breakcheck();
8573 }
8574 }
8575}
8576
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577/*
8578 * ":mode": Set screen mode.
8579 * If no argument given, just get the screen size and redraw.
8580 */
8581 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008582ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583{
8584 if (*eap->arg == NUL)
8585 shell_resized();
8586 else
8587 mch_screenmode(eap->arg);
8588}
8589
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590/*
8591 * ":resize".
8592 * set, increment or decrement current window height
8593 */
8594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008595ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596{
8597 int n;
8598 win_T *wp = curwin;
8599
8600 if (eap->addr_count > 0)
8601 {
8602 n = eap->line2;
8603 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8604 ;
8605 }
8606
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008607# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008609# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 if (cmdmod.split & WSP_VERT)
8612 {
8613 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008614 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8616 n = 9999;
8617 win_setwidth_win((int)n, wp);
8618 }
8619 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 {
8621 if (*eap->arg == '-' || *eap->arg == '+')
8622 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008623 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 n = 9999;
8625 win_setheight_win((int)n, wp);
8626 }
8627}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628
8629/*
8630 * ":find [+command] <file>" command.
8631 */
8632 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008633ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634{
8635#ifdef FEAT_SEARCHPATH
8636 char_u *fname;
8637 int count;
8638
8639 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8640 TRUE, curbuf->b_ffname);
8641 if (eap->addr_count > 0)
8642 {
8643 /* Repeat finding the file "count" times. This matters when it
8644 * appears several times in the path. */
8645 count = eap->line2;
8646 while (fname != NULL && --count > 0)
8647 {
8648 vim_free(fname);
8649 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8650 FALSE, curbuf->b_ffname);
8651 }
8652 }
8653
8654 if (fname != NULL)
8655 {
8656 eap->arg = fname;
8657#endif
8658 do_exedit(eap, NULL);
8659#ifdef FEAT_SEARCHPATH
8660 vim_free(fname);
8661 }
8662#endif
8663}
8664
8665/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008666 * ":open" simulation: for now just work like ":visual".
8667 */
8668 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008669ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008670{
8671 regmatch_T regmatch;
8672 char_u *p;
8673
8674 curwin->w_cursor.lnum = eap->line2;
8675 beginline(BL_SOL | BL_FIX);
8676 if (*eap->arg == '/')
8677 {
8678 /* ":open /pattern/": put cursor in column found with pattern */
8679 ++eap->arg;
8680 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8681 *p = NUL;
8682 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8683 if (regmatch.regprog != NULL)
8684 {
8685 regmatch.rm_ic = p_ic;
8686 p = ml_get_curline();
8687 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008688 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008689 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008690 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008691 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008692 }
8693 /* Move to the NUL, ignore any other arguments. */
8694 eap->arg += STRLEN(eap->arg);
8695 }
8696 check_cursor();
8697
8698 eap->cmdidx = CMD_visual;
8699 do_exedit(eap, NULL);
8700}
8701
8702/*
8703 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 */
8705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008706ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707{
8708 do_exedit(eap, NULL);
8709}
8710
8711/*
8712 * ":edit <file>" command and alikes.
8713 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008715do_exedit(
8716 exarg_T *eap,
8717 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718{
8719 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008721 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722
8723 /*
8724 * ":vi" command ends Ex mode.
8725 */
8726 if (exmode_active && (eap->cmdidx == CMD_visual
8727 || eap->cmdidx == CMD_view))
8728 {
8729 exmode_active = FALSE;
8730 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008731 {
8732 /* Special case: ":global/pat/visual\NLvi-commands" */
8733 if (global_busy)
8734 {
8735 int rd = RedrawingDisabled;
8736 int nwr = no_wait_return;
8737 int ms = msg_scroll;
8738#ifdef FEAT_GUI
8739 int he = hold_gui_events;
8740#endif
8741
8742 if (eap->nextcmd != NULL)
8743 {
8744 stuffReadbuff(eap->nextcmd);
8745 eap->nextcmd = NULL;
8746 }
8747
8748 if (exmode_was != EXMODE_VIM)
8749 settmode(TMODE_RAW);
8750 RedrawingDisabled = 0;
8751 no_wait_return = 0;
8752 need_wait_return = FALSE;
8753 msg_scroll = 0;
8754#ifdef FEAT_GUI
8755 hold_gui_events = 0;
8756#endif
8757 must_redraw = CLEAR;
8758
8759 main_loop(FALSE, TRUE);
8760
8761 RedrawingDisabled = rd;
8762 no_wait_return = nwr;
8763 msg_scroll = ms;
8764#ifdef FEAT_GUI
8765 hold_gui_events = he;
8766#endif
8767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 }
8771
8772 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008773 || eap->cmdidx == CMD_tabnew
8774 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008775 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008777 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 setpcmark();
8779 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008780 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8781 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008783 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 || *eap->arg != NUL
8785#ifdef FEAT_BROWSE
8786 || cmdmod.browse
8787#endif
8788 )
8789 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008790 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8791 * can bring us here, others are stopped earlier. */
8792 if (*eap->arg != NUL && curbuf_locked())
8793 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008794
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 n = readonlymode;
8796 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8797 readonlymode = TRUE;
8798 else if (eap->cmdidx == CMD_enew)
8799 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8800 empty buffer */
8801 setpcmark();
8802 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8803 NULL, eap,
8804 /* ":edit" goes to first line if Vi compatible */
8805 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8806 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8807 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008808 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008810 /* after a split we can use an existing buffer */
8811 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008813 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 {
8815 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 if (old_curwin != NULL)
8817 {
8818 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008819 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008821#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008822 cleanup_T cs;
8823
8824 /* Reset the error/interrupt/exception state here so that
8825 * aborting() returns FALSE when closing a window. */
8826 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008827#endif
8828#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008830#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008831 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008832
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008833#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008834 /* Restore the error/interrupt/exception state if not
8835 * discarded by a new aborting error, interrupt, or
8836 * uncaught exception. */
8837 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 }
8840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 }
8842 else if (readonlymode && curbuf->b_nwindows == 1)
8843 {
8844 /* When editing an already visited buffer, 'readonly' won't be set
8845 * but the previous value is kept. With ":view" and ":sview" we
8846 * want the file to be readonly, except when another window is
8847 * editing the same buffer. */
8848 curbuf->b_p_ro = TRUE;
8849 }
8850 readonlymode = n;
8851 }
8852 else
8853 {
8854 if (eap->do_ecmd_cmd != NULL)
8855 do_cmdline_cmd(eap->do_ecmd_cmd);
8856#ifdef FEAT_TITLE
8857 n = curwin->w_arg_idx_invalid;
8858#endif
8859 check_arg_idx(curwin);
8860#ifdef FEAT_TITLE
8861 if (n != curwin->w_arg_idx_invalid)
8862 maketitle();
8863#endif
8864 }
8865
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 /*
8867 * if ":split file" worked, set alternate file name in old window to new
8868 * file
8869 */
8870 if (old_curwin != NULL
8871 && *eap->arg != NUL
8872 && curwin != old_curwin
8873 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008874 && old_curwin->w_buffer != curbuf
8875 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877
8878 ex_no_reprint = TRUE;
8879}
8880
8881#ifndef FEAT_GUI
8882/*
8883 * ":gui" and ":gvim" when there is no GUI.
8884 */
8885 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008886ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887{
8888 eap->errmsg = e_nogvim;
8889}
8890#endif
8891
Bram Moolenaar4f974752019-02-17 17:44:42 +01008892#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008894ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895{
8896 gui_make_tearoff(eap->arg);
8897}
8898#endif
8899
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008900#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8901 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008903ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008905# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8906 if (gui.in_use)
8907 gui_make_popup(eap->arg, eap->forceit);
8908# ifdef FEAT_TERM_POPUP_MENU
8909 else
8910# endif
8911# endif
8912# ifdef FEAT_TERM_POPUP_MENU
8913 pum_make_popup(eap->arg, eap->forceit);
8914# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915}
8916#endif
8917
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008919ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920{
8921 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008922 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01008924 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925}
8926
8927/*
8928 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8929 * offset.
8930 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008933ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008936 win_T *save_curwin = curwin;
8937 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 long topline;
8939 long y;
8940 linenr_T old_linenr = curwin->w_cursor.lnum;
8941
8942 setpcmark();
8943
8944 /*
8945 * determine max topline
8946 */
8947 if (curwin->w_p_scb)
8948 {
8949 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008950 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 {
8952 if (wp->w_p_scb && wp->w_buffer)
8953 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01008954 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 if (topline > y)
8956 topline = y;
8957 }
8958 }
8959 if (topline < 1)
8960 topline = 1;
8961 }
8962 else
8963 {
8964 topline = 1;
8965 }
8966
8967
8968 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008969 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008971 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 {
8973 if (curwin->w_p_scb)
8974 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008975 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 y = topline - curwin->w_topline;
8977 if (y > 0)
8978 scrollup(y, TRUE);
8979 else
8980 scrolldown(-y, TRUE);
8981 curwin->w_scbind_pos = topline;
8982 redraw_later(VALID);
8983 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 }
8986 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008987 curwin = save_curwin;
8988 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 if (curwin->w_p_scb)
8990 {
8991 did_syncbind = TRUE;
8992 checkpcmark();
8993 if (old_linenr != curwin->w_cursor.lnum)
8994 {
8995 char_u ctrl_o[2];
8996
8997 ctrl_o[0] = Ctrl_O;
8998 ctrl_o[1] = 0;
8999 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
9000 }
9001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002}
9003
9004
9005 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009006ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007{
Bram Moolenaardf177f62005-02-22 08:39:57 +00009008 int i;
9009 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
9010 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011
9012 if (eap->usefilter) /* :r!cmd */
9013 do_bang(1, eap, FALSE, FALSE, TRUE);
9014 else
9015 {
9016 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
9017 return;
9018
9019#ifdef FEAT_BROWSE
9020 if (cmdmod.browse)
9021 {
9022 char_u *browseFile;
9023
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009024 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 NULL, NULL, NULL, curbuf);
9026 if (browseFile != NULL)
9027 {
9028 i = readfile(browseFile, NULL,
9029 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9030 vim_free(browseFile);
9031 }
9032 else
9033 i = OK;
9034 }
9035 else
9036#endif
9037 if (*eap->arg == NUL)
9038 {
9039 if (check_fname() == FAIL) /* check for no file name */
9040 return;
9041 i = readfile(curbuf->b_ffname, curbuf->b_fname,
9042 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9043 }
9044 else
9045 {
9046 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
9047 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
9048 i = readfile(eap->arg, NULL,
9049 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9050
9051 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01009052 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009054#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 if (!aborting())
9056#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009057 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 }
9059 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009060 {
9061 if (empty && exmode_active)
9062 {
9063 /* Delete the empty line that remains. Historically ex does
9064 * this but vi doesn't. */
9065 if (eap->line2 == 0)
9066 lnum = curbuf->b_ml.ml_line_count;
9067 else
9068 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009069 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009070 {
9071 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009072 if (curwin->w_cursor.lnum > 1
9073 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009074 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009075 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009076 }
9077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 }
9081}
9082
Bram Moolenaarea408852005-06-25 22:49:46 +00009083static char_u *prev_dir = NULL;
9084
9085#if defined(EXITFREE) || defined(PROTO)
9086 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009087free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009088{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009089 VIM_CLEAR(prev_dir);
9090 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00009091}
9092#endif
9093
Bram Moolenaarf4258302013-06-02 18:20:17 +02009094/*
9095 * Deal with the side effects of changing the current directory.
9096 * When "local" is TRUE then this was after an ":lcd" command.
9097 */
9098 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009099post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009100{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009101 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009102 if (local)
9103 {
9104 /* If still in global directory, need to remember current
9105 * directory as global directory. */
9106 if (globaldir == NULL && prev_dir != NULL)
9107 globaldir = vim_strsave(prev_dir);
9108 /* Remember this local directory for the window. */
9109 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9110 curwin->w_localdir = vim_strsave(NameBuff);
9111 }
9112 else
9113 {
9114 /* We are now in the global directory, no need to remember its
9115 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01009116 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009117 }
9118
9119 shorten_fnames(TRUE);
9120}
9121
Bram Moolenaarea408852005-06-25 22:49:46 +00009122
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123/*
9124 * ":cd", ":lcd", ":chdir" and ":lchdir".
9125 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009126 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009127ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009129 char_u *new_dir;
9130 char_u *tofree;
9131 int dir_differs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132
9133 new_dir = eap->arg;
9134#if !defined(UNIX) && !defined(VMS)
9135 /* for non-UNIX ":cd" means: print current directory */
9136 if (*new_dir == NUL)
9137 ex_pwd(NULL);
9138 else
9139#endif
9140 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009141 if (allbuf_locked())
9142 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009143 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9144 && !eap->forceit)
9145 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009146 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009147 return;
9148 }
9149
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 /* ":cd -": Change to previous directory */
9151 if (STRCMP(new_dir, "-") == 0)
9152 {
9153 if (prev_dir == NULL)
9154 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009155 emsg(_("E186: No previous directory"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 return;
9157 }
9158 new_dir = prev_dir;
9159 }
9160
9161 /* Save current directory for next ":cd -" */
9162 tofree = prev_dir;
9163 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9164 prev_dir = vim_strsave(NameBuff);
9165 else
9166 prev_dir = NULL;
9167
9168#if defined(UNIX) || defined(VMS)
9169 /* for UNIX ":cd" means: go to home directory */
9170 if (*new_dir == NUL)
9171 {
9172 /* use NameBuff for home directory name */
9173# ifdef VMS
9174 char_u *p;
9175
9176 p = mch_getenv((char_u *)"SYS$LOGIN");
9177 if (p == NULL || *p == NUL) /* empty is the same as not set */
9178 NameBuff[0] = NUL;
9179 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009180 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181# else
9182 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9183# endif
9184 new_dir = NameBuff;
9185 }
9186#endif
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009187 dir_differs = new_dir == NULL || prev_dir == NULL
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01009188 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009189 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009190 emsg(_(e_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 else
9192 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009193 int is_local_chdir = eap->cmdidx == CMD_lcd
9194 || eap->cmdidx == CMD_lchdir;
9195
9196 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197
9198 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009199 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 ex_pwd(eap);
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009201
9202 if (dir_differs)
9203 apply_autocmds(EVENT_DIRCHANGED,
9204 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9205 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 }
9207 vim_free(tofree);
9208 }
9209}
9210
9211/*
9212 * ":pwd".
9213 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009215ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216{
9217 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9218 {
9219#ifdef BACKSLASH_IN_FILENAME
9220 slash_adjust(NameBuff);
9221#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01009222 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 }
9224 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009225 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226}
9227
9228/*
9229 * ":=".
9230 */
9231 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009232ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009234 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009235 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236}
9237
9238 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009239ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009241 int n;
9242 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243
9244 if (cursor_valid())
9245 {
9246 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9247 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009248 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009250
9251 len = eap->line2;
9252 switch (*eap->arg)
9253 {
9254 case 'm': break;
9255 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009256 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009257 }
9258 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259}
9260
9261/*
9262 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9263 */
9264 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009265do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266{
9267 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009268 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269
9270 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02009271 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01009272 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009274 wait_now = msec - done > 1000L ? 1000L : msec - done;
9275#ifdef FEAT_TIMERS
9276 {
9277 long due_time = check_due_timer();
9278
9279 if (due_time > 0 && due_time < wait_now)
9280 wait_now = due_time;
9281 }
9282#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009283#ifdef FEAT_JOB_CHANNEL
9284 if (has_any_channel() && wait_now > 100L)
9285 wait_now = 100L;
9286#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009287 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009288#ifdef FEAT_JOB_CHANNEL
9289 if (has_any_channel())
9290 ui_breakcheck_force(TRUE);
9291 else
9292#endif
9293 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009294#ifdef MESSAGE_QUEUE
9295 /* Process the netbeans and clientserver messages that may have been
9296 * received in the call to ui_breakcheck() when the GUI is in use. This
9297 * may occur when running a test case. */
9298 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02009301
9302 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
9303 // input buffer, otherwise a following call to input() fails.
9304 if (got_int)
9305 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306}
9307
9308 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009309do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310{
9311 int mode;
9312 char_u *cmdp;
9313
9314 cmdp = eap->cmd;
9315 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9316
9317 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9318 eap->arg, mode, isabbrev))
9319 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009320 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009322 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 break;
9324 }
9325}
9326
9327/*
9328 * ":winsize" command (obsolete).
9329 */
9330 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009331ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332{
9333 int w, h;
9334 char_u *arg = eap->arg;
9335 char_u *p;
9336
9337 w = getdigits(&arg);
9338 arg = skipwhite(arg);
9339 p = arg;
9340 h = getdigits(&arg);
9341 if (*p != NUL && *arg == NUL)
9342 set_shellsize(w, h, TRUE);
9343 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009344 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345}
9346
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009348ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349{
9350 int xchar = NUL;
9351 char_u *p;
9352
9353 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9354 {
9355 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9356 if (eap->arg[1] == NUL)
9357 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009358 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 return;
9360 }
9361 xchar = eap->arg[1];
9362 p = eap->arg + 2;
9363 }
9364 else
9365 p = eap->arg + 1;
9366
9367 eap->nextcmd = check_nextcmd(p);
9368 p = skipwhite(p);
9369 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009370 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009371 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 {
9373 /* Pass flags on for ":vertical wincmd ]". */
9374 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009375 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9377 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009378 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 }
9380}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381
Bram Moolenaar843ee412004-06-30 16:16:41 +00009382#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383/*
9384 * ":winpos".
9385 */
9386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009387ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388{
9389 int x, y;
9390 char_u *arg = eap->arg;
9391 char_u *p;
9392
9393 if (*arg == NUL)
9394 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009395# if defined(FEAT_GUI) || defined(MSWIN)
9396# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009398# else
9399 if (mch_get_winpos(&x, &y) != FAIL)
9400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 {
9402 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01009403 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 }
9405 else
9406# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009407 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 }
9409 else
9410 {
9411 x = getdigits(&arg);
9412 arg = skipwhite(arg);
9413 p = arg;
9414 y = getdigits(&arg);
9415 if (*p == NUL || *arg != NUL)
9416 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009417 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 return;
9419 }
9420# ifdef FEAT_GUI
9421 if (gui.in_use)
9422 gui_mch_set_winpos(x, y);
9423 else if (gui.starting)
9424 {
9425 /* Remember the coordinates for when the window is opened. */
9426 gui_win_x = x;
9427 gui_win_y = y;
9428 }
9429# ifdef HAVE_TGETENT
9430 else
9431# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009432# else
9433# ifdef MSWIN
9434 mch_set_winpos(x, y);
9435# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436# endif
9437# ifdef HAVE_TGETENT
9438 if (*T_CWP)
9439 term_set_winpos(x, y);
9440# endif
9441 }
9442}
9443#endif
9444
9445/*
9446 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9447 */
9448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009449ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450{
9451 oparg_T oa;
9452
9453 clear_oparg(&oa);
9454 oa.regname = eap->regname;
9455 oa.start.lnum = eap->line1;
9456 oa.end.lnum = eap->line2;
9457 oa.line_count = eap->line2 - eap->line1 + 1;
9458 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9461 {
9462 setpcmark();
9463 curwin->w_cursor.lnum = eap->line1;
9464 beginline(BL_SOL | BL_FIX);
9465 }
9466
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009467 if (VIsual_active)
9468 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009469
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 switch (eap->cmdidx)
9471 {
9472 case CMD_delete:
9473 oa.op_type = OP_DELETE;
9474 op_delete(&oa);
9475 break;
9476
9477 case CMD_yank:
9478 oa.op_type = OP_YANK;
9479 (void)op_yank(&oa, FALSE, TRUE);
9480 break;
9481
9482 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009483 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009485 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9486#else
9487 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009489 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 oa.op_type = OP_RSHIFT;
9491 else
9492 oa.op_type = OP_LSHIFT;
9493 op_shift(&oa, FALSE, eap->amount);
9494 break;
9495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009497 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498}
9499
9500/*
9501 * ":put".
9502 */
9503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009504ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505{
9506 /* ":0put" works like ":1put!". */
9507 if (eap->line2 == 0)
9508 {
9509 eap->line2 = 1;
9510 eap->forceit = TRUE;
9511 }
9512 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009513 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9514 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515}
9516
9517/*
9518 * Handle ":copy" and ":move".
9519 */
9520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009521ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522{
9523 long n;
9524
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02009525 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 if (eap->arg == NULL) /* error detected */
9527 {
9528 eap->nextcmd = NULL;
9529 return;
9530 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009531 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532
9533 /*
9534 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9535 */
9536 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9537 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009538 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 return;
9540 }
9541
9542 if (eap->cmdidx == CMD_move)
9543 {
9544 if (do_move(eap->line1, eap->line2, n) == FAIL)
9545 return;
9546 }
9547 else
9548 ex_copy(eap->line1, eap->line2, n);
9549 u_clearline();
9550 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009551 ex_may_print(eap);
9552}
9553
9554/*
9555 * Print the current line if flags were given to the Ex command.
9556 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009557 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009558ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009559{
9560 if (eap->flags != 0)
9561 {
9562 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9563 (eap->flags & EXFLAG_LIST));
9564 ex_no_reprint = TRUE;
9565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566}
9567
9568/*
9569 * ":smagic" and ":snomagic".
9570 */
9571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009572ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573{
9574 int magic_save = p_magic;
9575
9576 p_magic = (eap->cmdidx == CMD_smagic);
9577 do_sub(eap);
9578 p_magic = magic_save;
9579}
9580
9581/*
9582 * ":join".
9583 */
9584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009585ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586{
9587 curwin->w_cursor.lnum = eap->line1;
9588 if (eap->line1 == eap->line2)
9589 {
9590 if (eap->addr_count >= 2) /* :2,2join does nothing */
9591 return;
9592 if (eap->line2 == curbuf->b_ml.ml_line_count)
9593 {
9594 beep_flush();
9595 return;
9596 }
9597 ++eap->line2;
9598 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009599 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009601 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602}
9603
9604/*
9605 * ":[addr]@r" or ":[addr]*r": execute register
9606 */
9607 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009608ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609{
9610 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009611 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612
9613 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009614 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615
9616#ifdef USE_ON_FLY_SCROLL
9617 dont_scroll = TRUE; /* disallow scrolling here */
9618#endif
9619
9620 /* get the register name. No name means to use the previous one */
9621 c = *eap->arg;
9622 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9623 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009624 /* Put the register in the typeahead buffer with the "silent" flag. */
9625 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9626 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009627 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 else
9631 {
9632 int save_efr = exec_from_reg;
9633
9634 exec_from_reg = TRUE;
9635
9636 /*
9637 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009638 * Continue until the stuff buffer is empty and all added characters
9639 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009641 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9643
9644 exec_from_reg = save_efr;
9645 }
9646}
9647
9648/*
9649 * ":!".
9650 */
9651 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009652ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653{
9654 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9655}
9656
9657/*
9658 * ":undo".
9659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009661ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009663 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009664 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009665 else
9666 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667}
9668
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009669#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009671ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009672{
9673 char_u hash[UNDO_HASH_SIZE];
9674
9675 u_compute_hash(hash);
9676 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9677}
9678
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009679 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009680ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009681{
9682 char_u hash[UNDO_HASH_SIZE];
9683
9684 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009685 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009686}
9687#endif
9688
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689/*
9690 * ":redo".
9691 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009693ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694{
9695 u_redo(1);
9696}
9697
9698/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009699 * ":earlier" and ":later".
9700 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009701 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009702ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009703{
9704 long count = 0;
9705 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009706 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009707 char_u *p = eap->arg;
9708
9709 if (*p == NUL)
9710 count = 1;
9711 else if (isdigit(*p))
9712 {
9713 count = getdigits(&p);
9714 switch (*p)
9715 {
9716 case 's': ++p; sec = TRUE; break;
9717 case 'm': ++p; sec = TRUE; count *= 60; break;
9718 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009719 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9720 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009721 }
9722 }
9723
9724 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009725 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009726 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009727 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9728 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009729}
9730
9731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 * ":redir": start/stop redirection.
9733 */
9734 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009735ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736{
9737 char *mode;
9738 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009739 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740
Bram Moolenaarba768492016-07-08 15:32:54 +02009741#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009742 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009743 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009744 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009745 return;
9746 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009747#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009748
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 if (STRICMP(eap->arg, "END") == 0)
9750 close_redir();
9751 else
9752 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009753 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009755 ++arg;
9756 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009758 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 mode = "a";
9760 }
9761 else
9762 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009763 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764
9765 close_redir();
9766
9767 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009768 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 if (fname == NULL)
9770 return;
9771#ifdef FEAT_BROWSE
9772 if (cmdmod.browse)
9773 {
9774 char_u *browseFile;
9775
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009776 browseFile = do_browse(BROWSE_SAVE,
9777 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009778 fname, NULL, NULL,
9779 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 if (browseFile == NULL)
9781 return; /* operation cancelled */
9782 vim_free(fname);
9783 fname = browseFile;
9784 eap->forceit = TRUE; /* since dialog already asked */
9785 }
9786#endif
9787
9788 redir_fd = open_exfile(fname, eap->forceit, mode);
9789 vim_free(fname);
9790 }
9791#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009792 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 {
9794 /* redirect to a register a-z (resp. A-Z for appending) */
9795 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009796 ++arg;
9797 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009799 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009800 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009802 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009804 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009805 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009806 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009807 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009809 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009810 if (*arg == '>')
9811 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009812 /* Make register empty when not using @A-@Z and the
9813 * command is valid. */
9814 if (*arg == NUL && !isupper(redir_reg))
9815 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009817 }
9818 if (*arg != NUL)
9819 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009820 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009821 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009822 }
9823 }
9824 else if (*arg == '=' && arg[1] == '>')
9825 {
9826 int append;
9827
9828 /* redirect to a variable */
9829 close_redir();
9830 arg += 2;
9831
9832 if (*arg == '>')
9833 {
9834 ++arg;
9835 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 }
9837 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009838 append = FALSE;
9839
9840 if (var_redir_start(skipwhite(arg), append) == OK)
9841 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 }
9843#endif
9844
9845 /* TODO: redirect to a buffer */
9846
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009848 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009850
9851 /* Make sure redirection is not off. Can happen for cmdline completion
9852 * that indirectly invokes a command to catch its output. */
9853 if (redir_fd != NULL
9854#ifdef FEAT_EVAL
9855 || redir_reg || redir_vname
9856#endif
9857 )
9858 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859}
9860
9861/*
9862 * ":redraw": force redraw
9863 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009864 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009865ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866{
9867 int r = RedrawingDisabled;
9868 int p = p_lz;
9869
9870 RedrawingDisabled = 0;
9871 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009872 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009874 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875#ifdef FEAT_TITLE
9876 if (need_maketitle)
9877 maketitle();
9878#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01009879#if defined(MSWIN) && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01009880 resize_console_buf();
9881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 RedrawingDisabled = r;
9883 p_lz = p;
9884
9885 /* Reset msg_didout, so that a message that's there is overwritten. */
9886 msg_didout = FALSE;
9887 msg_col = 0;
9888
Bram Moolenaar56667a52013-07-17 11:54:28 +02009889 /* No need to wait after an intentional redraw. */
9890 need_wait_return = FALSE;
9891
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 out_flush();
9893}
9894
9895/*
9896 * ":redrawstatus": force redraw of status line(s)
9897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009899ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 int r = RedrawingDisabled;
9902 int p = p_lz;
9903
9904 RedrawingDisabled = 0;
9905 p_lz = FALSE;
9906 if (eap->forceit)
9907 status_redraw_all();
9908 else
9909 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009910 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 RedrawingDisabled = r;
9912 p_lz = p;
9913 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914}
9915
Bram Moolenaare12bab32019-01-08 22:02:56 +01009916/*
9917 * ":redrawtabline": force redraw of the tabline
9918 */
9919 static void
9920ex_redrawtabline(exarg_T *eap UNUSED)
9921{
9922 int r = RedrawingDisabled;
9923 int p = p_lz;
9924
9925 RedrawingDisabled = 0;
9926 p_lz = FALSE;
9927
9928 draw_tabline();
9929
9930 RedrawingDisabled = r;
9931 p_lz = p;
9932 out_flush();
9933}
9934
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009936close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937{
9938 if (redir_fd != NULL)
9939 {
9940 fclose(redir_fd);
9941 redir_fd = NULL;
9942 }
9943#ifdef FEAT_EVAL
9944 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009945 if (redir_vname)
9946 {
9947 var_redir_stop();
9948 redir_vname = 0;
9949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950#endif
9951}
9952
9953#if defined(FEAT_SESSION) && defined(USE_CRNL)
9954# define MKSESSION_NL
9955static int mksession_nl = FALSE; /* use NL only in put_eol() */
9956#endif
9957
9958/*
9959 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9960 */
9961 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009962ex_mkrc(
9963 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964{
9965 FILE *fd;
9966 int failed = FALSE;
9967 char_u *fname;
9968#ifdef FEAT_BROWSE
9969 char_u *browseFile = NULL;
9970#endif
9971#ifdef FEAT_SESSION
9972 int view_session = FALSE;
9973 int using_vdir = FALSE; /* using 'viewdir'? */
9974 char_u *viewFile = NULL;
9975 unsigned *flagp;
9976#endif
9977
9978 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9979 {
9980#ifdef FEAT_SESSION
9981 view_session = TRUE;
9982#else
9983 ex_ni(eap);
9984 return;
9985#endif
9986 }
9987
9988#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009989 /* Use the short file name until ":lcd" is used. We also don't use the
9990 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009991 did_lcd = FALSE;
9992
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9994 if (eap->cmdidx == CMD_mkview
9995 && (*eap->arg == NUL
9996 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9997 {
9998 eap->forceit = TRUE;
9999 fname = get_view_file(*eap->arg);
10000 if (fname == NULL)
10001 return;
10002 viewFile = fname;
10003 using_vdir = TRUE;
10004 }
10005 else
10006#endif
10007 if (*eap->arg != NUL)
10008 fname = eap->arg;
10009 else if (eap->cmdidx == CMD_mkvimrc)
10010 fname = (char_u *)VIMRC_FILE;
10011#ifdef FEAT_SESSION
10012 else if (eap->cmdidx == CMD_mksession)
10013 fname = (char_u *)SESSION_FILE;
10014#endif
10015 else
10016 fname = (char_u *)EXRC_FILE;
10017
10018#ifdef FEAT_BROWSE
10019 if (cmdmod.browse)
10020 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +000010021 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022# ifdef FEAT_SESSION
10023 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
10024 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
10025# endif
10026 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +020010027 fname, (char_u *)"vim", NULL,
10028 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 if (browseFile == NULL)
10030 goto theend;
10031 fname = browseFile;
10032 eap->forceit = TRUE; /* since dialog already asked */
10033 }
10034#endif
10035
10036#if defined(FEAT_SESSION) && defined(vim_mkdir)
10037 /* When using 'viewdir' may have to create the directory. */
10038 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +000010039 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040#endif
10041
10042 fd = open_exfile(fname, eap->forceit, WRITEBIN);
10043 if (fd != NULL)
10044 {
10045#ifdef FEAT_SESSION
10046 if (eap->cmdidx == CMD_mkview)
10047 flagp = &vop_flags;
10048 else
10049 flagp = &ssop_flags;
10050#endif
10051
10052#ifdef MKSESSION_NL
10053 /* "unix" in 'sessionoptions': use NL line separator */
10054 if (view_session && (*flagp & SSOP_UNIX))
10055 mksession_nl = TRUE;
10056#endif
10057
10058 /* Write the version command for :mkvimrc */
10059 if (eap->cmdidx == CMD_mkvimrc)
10060 (void)put_line(fd, "version 6.0");
10061
10062#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010063 if (eap->cmdidx == CMD_mksession)
10064 {
10065 if (put_line(fd, "let SessionLoad = 1") == FAIL)
10066 failed = TRUE;
10067 }
10068
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 if (eap->cmdidx != CMD_mkview)
10070#endif
10071 {
10072 /* Write setting 'compatible' first, because it has side effects.
10073 * For that same reason only do it when needed. */
10074 if (p_cp)
10075 (void)put_line(fd, "if !&cp | set cp | endif");
10076 else
10077 (void)put_line(fd, "if &cp | set nocp | endif");
10078 }
10079
10080#ifdef FEAT_SESSION
10081 if (!view_session
10082 || (eap->cmdidx == CMD_mksession
10083 && (*flagp & SSOP_OPTIONS)))
10084#endif
10085 failed |= (makemap(fd, NULL) == FAIL
10086 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10087
10088#ifdef FEAT_SESSION
10089 if (!failed && view_session)
10090 {
10091 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10092 failed = TRUE;
10093 if (eap->cmdidx == CMD_mksession)
10094 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010095 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096
Bram Moolenaard9462e32011-04-11 21:35:11 +020010097 dirnow = alloc(MAXPATHL);
10098 if (dirnow == NULL)
10099 failed = TRUE;
10100 else
10101 {
10102 /*
10103 * Change to session file's dir.
10104 */
10105 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010107 *dirnow = NUL;
10108 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10109 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +010010110 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010111 shorten_fnames(TRUE);
10112 }
10113 else if (*dirnow != NUL
10114 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10115 {
10116 if (mch_chdir((char *)globaldir) == 0)
10117 shorten_fnames(TRUE);
10118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119
Bram Moolenaard9462e32011-04-11 21:35:11 +020010120 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121
Bram Moolenaard9462e32011-04-11 21:35:11 +020010122 /* restore original dir */
10123 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010125 {
10126 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010127 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +020010128 shorten_fnames(TRUE);
10129 }
10130 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 }
10132 }
10133 else
10134 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010135 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10136 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 }
10138 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10139 == FAIL)
10140 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +010010141# ifdef FEAT_SEARCH_EXTRA
10142 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
10143 failed = TRUE;
10144# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010145 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10146 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010147 if (eap->cmdidx == CMD_mksession)
10148 {
10149 if (put_line(fd, "unlet SessionLoad") == FAIL)
10150 failed = TRUE;
10151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 }
10153#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010154 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10155 failed = TRUE;
10156
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 failed |= fclose(fd);
10158
10159 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010160 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10162 else if (eap->cmdidx == CMD_mksession)
10163 {
10164 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010165 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166
Bram Moolenaard9462e32011-04-11 21:35:11 +020010167 tbuf = alloc(MAXPATHL);
10168 if (tbuf != NULL)
10169 {
10170 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10171 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10172 vim_free(tbuf);
10173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 }
10175#endif
10176#ifdef MKSESSION_NL
10177 mksession_nl = FALSE;
10178#endif
10179 }
10180
10181#ifdef FEAT_BROWSE
10182theend:
10183 vim_free(browseFile);
10184#endif
10185#ifdef FEAT_SESSION
10186 vim_free(viewFile);
10187#endif
10188}
10189
Bram Moolenaardf177f62005-02-22 08:39:57 +000010190#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10191 || defined(PROTO)
10192 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010193vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010194{
10195 if (vim_mkdir(name, prot) != 0)
10196 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010197 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +000010198 return FAIL;
10199 }
10200 return OK;
10201}
10202#endif
10203
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204/*
10205 * Open a file for writing for an Ex command, with some checks.
10206 * Return file descriptor, or NULL on failure.
10207 */
10208 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010209open_exfile(
10210 char_u *fname,
10211 int forceit,
10212 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213{
10214 FILE *fd;
10215
10216#ifdef UNIX
10217 /* with Unix it is possible to open a directory */
10218 if (mch_isdir(fname))
10219 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010220 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 return NULL;
10222 }
10223#endif
10224 if (!forceit && *mode != 'a' && vim_fexists(fname))
10225 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010226 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 return NULL;
10228 }
10229
10230 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010231 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232
10233 return fd;
10234}
10235
10236/*
10237 * ":mark" and ":k".
10238 */
10239 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010240ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241{
10242 pos_T pos;
10243
10244 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010245 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010247 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 else
10249 {
10250 pos = curwin->w_cursor; /* save curwin->w_cursor */
10251 curwin->w_cursor.lnum = eap->line2;
10252 beginline(BL_WHITE | BL_FIX);
10253 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010254 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10256 }
10257}
10258
10259/*
10260 * Update w_topline, w_leftcol and the cursor position.
10261 */
10262 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010263update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264{
10265 check_cursor(); /* put cursor on valid line */
10266 update_topline();
10267 if (!curwin->w_p_wrap)
10268 validate_cursor();
10269 update_curswant();
10270}
10271
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010273 * Save the current State and go to Normal mode.
10274 * Return TRUE if the typeahead could be saved.
10275 */
10276 int
10277save_current_state(save_state_T *sst)
10278{
10279 sst->save_msg_scroll = msg_scroll;
10280 sst->save_restart_edit = restart_edit;
10281 sst->save_msg_didout = msg_didout;
10282 sst->save_State = State;
10283 sst->save_insertmode = p_im;
10284 sst->save_finish_op = finish_op;
10285 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +010010286 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010287
10288 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10289 restart_edit = 0; /* don't go to Insert mode */
10290 p_im = FALSE; /* don't use 'insertmode' */
10291
10292 /*
10293 * Save the current typeahead. This is required to allow using ":normal"
10294 * from an event handler and makes sure we don't hang when the argument
10295 * ends with half a command.
10296 */
10297 save_typeahead(&sst->tabuf);
10298 return sst->tabuf.typebuf_valid;
10299}
10300
10301 void
10302restore_current_state(save_state_T *sst)
10303{
10304 /* Restore the previous typeahead. */
10305 restore_typeahead(&sst->tabuf);
10306
10307 msg_scroll = sst->save_msg_scroll;
10308 restart_edit = sst->save_restart_edit;
10309 p_im = sst->save_insertmode;
10310 finish_op = sst->save_finish_op;
10311 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +010010312 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010313 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10314
10315 /* Restore the state (needed when called from a function executed for
10316 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10317 State = sst->save_State;
10318#ifdef CURSOR_SHAPE
10319 ui_cursor_shape(); /* may show different cursor shape */
10320#endif
10321}
10322
10323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 * ":normal[!] {commands}": Execute normal mode commands.
10325 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010326 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010327ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010329 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 char_u *arg = NULL;
10331 int l;
10332 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010334 if (ex_normal_lock > 0)
10335 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010336 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010337 return;
10338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 if (ex_normal_busy >= p_mmd)
10340 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010341 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 return;
10343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 /*
10346 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10347 * this for the K_SPECIAL leading byte, otherwise special keys will not
10348 * work.
10349 */
10350 if (has_mbyte)
10351 {
10352 int len = 0;
10353
10354 /* Count the number of characters to be escaped. */
10355 for (p = eap->arg; *p != NUL; ++p)
10356 {
Bram Moolenaar13505972019-01-24 15:04:48 +010010357#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 if (*p == CSI) /* leadbyte CSI */
10359 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +010010360#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010361 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +010010363#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +010010365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 )
10367 len += 2;
10368 }
10369 if (len > 0)
10370 {
10371 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10372 if (arg != NULL)
10373 {
10374 len = 0;
10375 for (p = eap->arg; *p != NUL; ++p)
10376 {
10377 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +010010378#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 if (*p == CSI)
10380 {
10381 arg[len++] = KS_EXTRA;
10382 arg[len++] = (int)KE_CSI;
10383 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010384#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010385 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 {
10387 arg[len++] = *++p;
10388 if (*p == K_SPECIAL)
10389 {
10390 arg[len++] = KS_SPECIAL;
10391 arg[len++] = KE_FILLER;
10392 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010393#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 else if (*p == CSI)
10395 {
10396 arg[len++] = KS_EXTRA;
10397 arg[len++] = (int)KE_CSI;
10398 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 }
10401 arg[len] = NUL;
10402 }
10403 }
10404 }
10405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010407 ++ex_normal_busy;
10408 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409 {
10410 /*
10411 * Repeat the :normal command for each line in the range. When no
10412 * range given, execute it just once, without positioning the cursor
10413 * first.
10414 */
10415 do
10416 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417 if (eap->addr_count != 0)
10418 {
10419 curwin->w_cursor.lnum = eap->line1++;
10420 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010421 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 }
10423
Bram Moolenaar13505972019-01-24 15:04:48 +010010424 exec_normal_cmd(arg != NULL
10425 ? arg
10426 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 }
10428 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10429 }
10430
10431 /* Might not return to the main loop when in an event handler. */
10432 update_topline_cursor();
10433
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010434 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010436#ifdef FEAT_MOUSE
10437 setmouse();
10438#endif
10439#ifdef CURSOR_SHAPE
10440 ui_cursor_shape(); /* may show different cursor shape */
10441#endif
10442
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444}
10445
10446/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010447 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 */
10449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010450ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010452 if (eap->forceit)
10453 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010454 /* cursor line can be zero on startup */
10455 if (!curwin->w_cursor.lnum)
10456 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010457 coladvance((colnr_T)MAXCOL);
10458 curwin->w_curswant = MAXCOL;
10459 curwin->w_set_curswant = FALSE;
10460 }
10461
Bram Moolenaara40c5002005-01-09 21:16:21 +000010462 /* Ignore the command when already in Insert mode. Inserting an
10463 * expression register that invokes a function can do this. */
10464 if (State & INSERT)
10465 return;
10466
Bram Moolenaar64969662005-12-14 21:59:55 +000010467 if (eap->cmdidx == CMD_startinsert)
10468 restart_edit = 'a';
10469 else if (eap->cmdidx == CMD_startreplace)
10470 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010472 restart_edit = 'V';
10473
10474 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010476 if (eap->cmdidx == CMD_startinsert)
10477 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 curwin->w_curswant = 0; /* avoid MAXCOL */
10479 }
10480}
10481
10482/*
10483 * ":stopinsert"
10484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010486ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487{
10488 restart_edit = 0;
10489 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010490 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010493/*
10494 * Execute normal mode command "cmd".
10495 * "remap" can be REMAP_NONE or REMAP_YES.
10496 */
10497 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010498exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010499{
Bram Moolenaar25281632016-01-21 23:32:32 +010010500 /* Stuff the argument into the typeahead buffer. */
10501 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010502 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +010010503}
Bram Moolenaar25281632016-01-21 23:32:32 +010010504
Bram Moolenaar25281632016-01-21 23:32:32 +010010505/*
10506 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010507 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +010010508 */
10509 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010510exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +010010511{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010512 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +020010513 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010514
Bram Moolenaar905dd902019-04-07 14:21:47 +020010515 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
10516 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010517 clear_oparg(&oa);
10518 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010519 while ((!stuff_empty()
10520 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +020010521 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010522 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010523 {
10524 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010525#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010526 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010527 && oa.op_type == OP_NOP && oa.regname == NUL
10528 && !VIsual_active)
10529 {
10530 /* If terminal_loop() returns OK we got a key that is handled
10531 * in Normal model. With FAIL we first need to position the
10532 * cursor and the screen needs to be redrawn. */
10533 if (terminal_loop(TRUE) == OK)
10534 normal_cmd(&oa, TRUE);
10535 }
10536 else
10537#endif
10538 /* execute a Normal mode cmd */
10539 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010540 }
10541}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010542
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543#ifdef FEAT_FIND_ID
10544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010545ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546{
10547 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10548 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10549 (linenr_T)1, (linenr_T)MAXLNUM);
10550}
10551
Bram Moolenaar4033c552017-09-16 20:54:51 +020010552#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553/*
10554 * ":psearch"
10555 */
10556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010557ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558{
10559 g_do_tagpreview = p_pvh;
10560 ex_findpat(eap);
10561 g_do_tagpreview = 0;
10562}
10563#endif
10564
10565 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010566ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567{
10568 int whole = TRUE;
10569 long n;
10570 char_u *p;
10571 int action;
10572
10573 switch (cmdnames[eap->cmdidx].cmd_name[2])
10574 {
10575 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10576 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10577 action = ACTION_GOTO;
10578 else
10579 action = ACTION_SHOW;
10580 break;
10581 case 'i': /* ":ilist" and ":dlist" */
10582 action = ACTION_SHOW_ALL;
10583 break;
10584 case 'u': /* ":ijump" and ":djump" */
10585 action = ACTION_GOTO;
10586 break;
10587 default: /* ":isplit" and ":dsplit" */
10588 action = ACTION_SPLIT;
10589 break;
10590 }
10591
10592 n = 1;
10593 if (vim_isdigit(*eap->arg)) /* get count */
10594 {
10595 n = getdigits(&eap->arg);
10596 eap->arg = skipwhite(eap->arg);
10597 }
10598 if (*eap->arg == '/') /* Match regexp, not just whole words */
10599 {
10600 whole = FALSE;
10601 ++eap->arg;
10602 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10603 if (*p)
10604 {
10605 *p++ = NUL;
10606 p = skipwhite(p);
10607
10608 /* Check for trailing illegal characters */
10609 if (!ends_excmd(*p))
10610 eap->errmsg = e_trailing;
10611 else
10612 eap->nextcmd = check_nextcmd(p);
10613 }
10614 }
10615 if (!eap->skip)
10616 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10617 whole, !eap->forceit,
10618 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10619 n, action, eap->line1, eap->line2);
10620}
10621#endif
10622
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623
Bram Moolenaar4033c552017-09-16 20:54:51 +020010624#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625/*
10626 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10627 */
10628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010629ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010631 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10633}
10634
10635/*
10636 * ":pedit"
10637 */
10638 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010639ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640{
10641 win_T *curwin_save = curwin;
10642
10643 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010644 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010645 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 do_exedit(eap, NULL);
10647 keep_help_flag = FALSE;
10648 if (curwin != curwin_save && win_valid(curwin_save))
10649 {
10650 /* Return cursor to where we were */
10651 validate_cursor();
10652 redraw_later(VALID);
10653 win_enter(curwin_save, TRUE);
10654 }
10655 g_do_tagpreview = 0;
10656}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658
10659/*
10660 * ":stag", ":stselect" and ":stjump".
10661 */
10662 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010663ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664{
10665 postponed_split = -1;
10666 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010667 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10669 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010670 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672
10673/*
10674 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10675 */
10676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010677ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678{
10679 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10680}
10681
10682 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010683ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684{
10685 int cmd;
10686
10687 switch (name[1])
10688 {
10689 case 'j': cmd = DT_JUMP; /* ":tjump" */
10690 break;
10691 case 's': cmd = DT_SELECT; /* ":tselect" */
10692 break;
10693 case 'p': cmd = DT_PREV; /* ":tprevious" */
10694 break;
10695 case 'N': cmd = DT_PREV; /* ":tNext" */
10696 break;
10697 case 'n': cmd = DT_NEXT; /* ":tnext" */
10698 break;
10699 case 'o': cmd = DT_POP; /* ":pop" */
10700 break;
10701 case 'f': /* ":tfirst" */
10702 case 'r': cmd = DT_FIRST; /* ":trewind" */
10703 break;
10704 case 'l': cmd = DT_LAST; /* ":tlast" */
10705 break;
10706 default: /* ":tag" */
10707#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010708 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010710 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 return;
10712 }
10713#endif
10714 cmd = DT_TAG;
10715 break;
10716 }
10717
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010718 if (name[0] == 'l')
10719 {
10720#ifndef FEAT_QUICKFIX
10721 ex_ni(eap);
10722 return;
10723#else
10724 cmd = DT_LTAG;
10725#endif
10726 }
10727
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10729 eap->forceit, TRUE);
10730}
10731
10732/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010733 * Check "str" for starting with a special cmdline variable.
10734 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10735 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10736 */
10737 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010738find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010739{
10740 int len;
10741 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010742 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010743 "%",
10744#define SPEC_PERC 0
10745 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010746#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010747 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010748#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010749 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010750#define SPEC_CCWORD (SPEC_CWORD + 1)
10751 "<cexpr>", /* expr under cursor */
10752#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010753 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010754#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010755 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010756#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010757 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010758#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010759 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010760#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010761 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010762#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010763 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010764#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010765 "<sflnum>", /* script file line number */
10766#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010767#ifdef FEAT_CLIENTSERVER
10768 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010769# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010770#endif
10771 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010772
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010773 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010774 {
10775 len = (int)STRLEN(spec_str[i]);
10776 if (STRNCMP(src, spec_str[i], len) == 0)
10777 {
10778 *usedlen = len;
10779 return i;
10780 }
10781 }
10782 return -1;
10783}
10784
10785/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786 * Evaluate cmdline variables.
10787 *
10788 * change '%' to curbuf->b_ffname
10789 * '#' to curwin->w_altfile
10790 * '<cword>' to word under the cursor
10791 * '<cWORD>' to WORD under the cursor
10792 * '<cfile>' to path name under the cursor
10793 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010794 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 * '<afile>' to file name for autocommand
10796 * '<abuf>' to buffer number for autocommand
10797 * '<amatch>' to matching name for autocommand
10798 *
10799 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10800 * "" for error without a message) and NULL is returned.
10801 * Returns an allocated string if a valid match was found.
10802 * Returns NULL if no match was found. "usedlen" then still contains the
10803 * number of characters to skip.
10804 */
10805 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010806eval_vars(
10807 char_u *src, /* pointer into commandline */
10808 char_u *srcstart, /* beginning of valid memory for src */
10809 int *usedlen, /* characters after src that are used */
10810 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010811 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010812 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010813 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814{
10815 int i;
10816 char_u *s;
10817 char_u *result;
10818 char_u *resultbuf = NULL;
10819 int resultlen;
10820 buf_T *buf;
10821 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10822 int spec_idx;
10823#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +020010824 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 int skip_mod = FALSE;
10826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828
10829 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010830 if (escaped != NULL)
10831 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832
10833 /*
10834 * Check if there is something to do.
10835 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010836 spec_idx = find_cmdline_var(src, usedlen);
10837 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 {
10839 *usedlen = 1;
10840 return NULL;
10841 }
10842
10843 /*
10844 * Skip when preceded with a backslash "\%" and "\#".
10845 * Note: In "\\%" the % is also not recognized!
10846 */
10847 if (src > srcstart && src[-1] == '\\')
10848 {
10849 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010850 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 return NULL;
10852 }
10853
10854 /*
10855 * word or WORD under cursor
10856 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010857 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10858 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010860 resultlen = find_ident_under_cursor(&result,
10861 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10862 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10863 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 if (resultlen == 0)
10865 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010866 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 return NULL;
10868 }
10869 }
10870
10871 /*
10872 * '#': Alternate file name
10873 * '%': Current file name
10874 * File name under the cursor
10875 * File name for autocommand
10876 * and following modifiers
10877 */
10878 else
10879 {
10880 switch (spec_idx)
10881 {
10882 case SPEC_PERC: /* '%': current file */
10883 if (curbuf->b_fname == NULL)
10884 {
10885 result = (char_u *)"";
10886 valid = 0; /* Must have ":p:h" to be valid */
10887 }
10888 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010889 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010891#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010892 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010893#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 break;
10896
10897 case SPEC_HASH: /* '#' or "#99": alternate file */
10898 if (src[1] == '#') /* "##": the argument list */
10899 {
10900 result = arg_all();
10901 resultbuf = result;
10902 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010903 if (escaped != NULL)
10904 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905#ifdef FEAT_MODIFY_FNAME
10906 skip_mod = TRUE;
10907#endif
10908 break;
10909 }
10910 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010911 if (*s == '<') /* "#<99" uses v:oldfiles */
10912 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010914 if (s == src + 2 && src[1] == '-')
10915 /* just a minus sign, don't skip over it */
10916 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 *usedlen = (int)(s - src); /* length of what we expand */
10918
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010919 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010921 if (*usedlen < 2)
10922 {
10923 /* Should we give an error message for #<text? */
10924 *usedlen = 1;
10925 return NULL;
10926 }
10927#ifdef FEAT_EVAL
10928 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10929 (long)i);
10930 if (result == NULL)
10931 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010932 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +000010933 return NULL;
10934 }
10935#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +010010936 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 }
10940 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010941 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010942 if (i == 0 && src[1] == '<' && *usedlen > 1)
10943 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010944 buf = buflist_findnr(i);
10945 if (buf == NULL)
10946 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010947 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +000010948 return NULL;
10949 }
10950 if (lnump != NULL)
10951 *lnump = ECMD_LAST;
10952 if (buf->b_fname == NULL)
10953 {
10954 result = (char_u *)"";
10955 valid = 0; /* Must have ":p:h" to be valid */
10956 }
10957 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010958 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010959 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010960#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010961 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010962#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010963 }
Bram Moolenaard812df62008-11-09 12:46:09 +000010964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 break;
10966
10967#ifdef FEAT_SEARCHPATH
10968 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010969 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 if (result == NULL)
10971 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010972 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 return NULL;
10974 }
10975 resultbuf = result; /* remember allocated string */
10976 break;
10977#endif
10978
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 case SPEC_AFILE: /* file name for autocommand */
10980 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010981 if (result != NULL && !autocmd_fname_full)
10982 {
10983 /* Still need to turn the fname into a full path. It is
10984 * postponed to avoid a delay when <afile> is not used. */
10985 autocmd_fname_full = TRUE;
10986 result = FullName_save(autocmd_fname, FALSE);
10987 vim_free(autocmd_fname);
10988 autocmd_fname = result;
10989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 if (result == NULL)
10991 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010992 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993 return NULL;
10994 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010995 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 break;
10997
10998 case SPEC_ABUF: /* buffer number for autocommand */
10999 if (autocmd_bufnr <= 0)
11000 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011001 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 return NULL;
11003 }
11004 sprintf((char *)strbuf, "%d", autocmd_bufnr);
11005 result = strbuf;
11006 break;
11007
11008 case SPEC_AMATCH: /* match name for autocommand */
11009 result = autocmd_match;
11010 if (result == NULL)
11011 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011012 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 return NULL;
11014 }
11015 break;
11016
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 case SPEC_SFILE: /* file name for ":so" command */
11018 result = sourcing_name;
11019 if (result == NULL)
11020 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011021 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 return NULL;
11023 }
11024 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011025
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010011026 case SPEC_SLNUM: /* line in file for ":so" command */
11027 if (sourcing_name == NULL || sourcing_lnum == 0)
11028 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011029 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010011030 return NULL;
11031 }
11032 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
11033 result = strbuf;
11034 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011035
11036#ifdef FEAT_EVAL
11037 case SPEC_SFLNUM: /* line in script file */
11038 if (current_sctx.sc_lnum + sourcing_lnum == 0)
11039 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011040 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011041 return NULL;
11042 }
11043 sprintf((char *)strbuf, "%ld",
11044 (long)(current_sctx.sc_lnum + sourcing_lnum));
11045 result = strbuf;
11046 break;
11047#endif
11048
11049#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011051 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
11052 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 result = strbuf;
11054 break;
11055#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011056
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020011057 default:
11058 result = (char_u *)""; /* avoid gcc warning */
11059 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060 }
11061
11062 resultlen = (int)STRLEN(result); /* length of new string */
11063 if (src[*usedlen] == '<') /* remove the file name extension */
11064 {
11065 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 resultlen = (int)(s - result);
11068 }
11069#ifdef FEAT_MODIFY_FNAME
11070 else if (!skip_mod)
11071 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020011072 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 &resultlen);
11074 if (result == NULL)
11075 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011076 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 return NULL;
11078 }
11079 }
11080#endif
11081 }
11082
11083 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
11084 {
11085 if (valid != VALID_HEAD + VALID_PATH)
11086 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011087 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011089 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 result = NULL;
11091 }
11092 else
11093 result = vim_strnsave(result, resultlen);
11094 vim_free(resultbuf);
11095 return result;
11096}
11097
11098/*
11099 * Concatenate all files in the argument list, separated by spaces, and return
11100 * it in one allocated string.
11101 * Spaces and backslashes in the file names are escaped with a backslash.
11102 * Returns NULL when out of memory.
11103 */
11104 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011105arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106{
11107 int len;
11108 int idx;
11109 char_u *retval = NULL;
11110 char_u *p;
11111
11112 /*
11113 * Do this loop two times:
11114 * first time: compute the total length
11115 * second time: concatenate the names
11116 */
11117 for (;;)
11118 {
11119 len = 0;
11120 for (idx = 0; idx < ARGCOUNT; ++idx)
11121 {
11122 p = alist_name(&ARGLIST[idx]);
11123 if (p != NULL)
11124 {
11125 if (len > 0)
11126 {
11127 /* insert a space in between names */
11128 if (retval != NULL)
11129 retval[len] = ' ';
11130 ++len;
11131 }
11132 for ( ; *p != NUL; ++p)
11133 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011134 if (*p == ' '
11135#ifndef BACKSLASH_IN_FILENAME
11136 || *p == '\\'
11137#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +020011138 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 {
11140 /* insert a backslash */
11141 if (retval != NULL)
11142 retval[len] = '\\';
11143 ++len;
11144 }
11145 if (retval != NULL)
11146 retval[len] = *p;
11147 ++len;
11148 }
11149 }
11150 }
11151
11152 /* second time: break here */
11153 if (retval != NULL)
11154 {
11155 retval[len] = NUL;
11156 break;
11157 }
11158
11159 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011160 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 if (retval == NULL)
11162 break;
11163 }
11164
11165 return retval;
11166}
11167
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168/*
11169 * Expand the <sfile> string in "arg".
11170 *
11171 * Returns an allocated string, or NULL for any error.
11172 */
11173 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011174expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011176 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177 int len;
11178 char_u *result;
11179 char_u *newres;
11180 char_u *repl;
11181 int srclen;
11182 char_u *p;
11183
11184 result = vim_strsave(arg);
11185 if (result == NULL)
11186 return NULL;
11187
11188 for (p = result; *p; )
11189 {
11190 if (STRNCMP(p, "<sfile>", 7) != 0)
11191 ++p;
11192 else
11193 {
11194 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011195 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196 if (errormsg != NULL)
11197 {
11198 if (*errormsg)
11199 emsg(errormsg);
11200 vim_free(result);
11201 return NULL;
11202 }
11203 if (repl == NULL) /* no match (cannot happen) */
11204 {
11205 p += srclen;
11206 continue;
11207 }
11208 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11209 newres = alloc(len);
11210 if (newres == NULL)
11211 {
11212 vim_free(repl);
11213 vim_free(result);
11214 return NULL;
11215 }
11216 mch_memmove(newres, result, (size_t)(p - result));
11217 STRCPY(newres + (p - result), repl);
11218 len = (int)STRLEN(newres);
11219 STRCAT(newres, p + srclen);
11220 vim_free(repl);
11221 vim_free(result);
11222 result = newres;
11223 p = newres + len; /* continue after the match */
11224 }
11225 }
11226
11227 return result;
11228}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229
11230#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011231static int ses_winsizes(FILE *fd, int restore_size,
11232 win_T *tab_firstwin);
11233static int ses_win_rec(FILE *fd, frame_T *fr);
11234static frame_T *ses_skipframe(frame_T *fr);
11235static int ses_do_frame(frame_T *fr);
11236static int ses_do_win(win_T *wp);
11237static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11238static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011239static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240
11241/*
11242 * Write openfile commands for the current buffers to an .exrc file.
11243 * Return FAIL on error, OK otherwise.
11244 */
11245 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011246makeopens(
11247 FILE *fd,
11248 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249{
11250 buf_T *buf;
11251 int only_save_windows = TRUE;
11252 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 int restore_size = TRUE;
11254 win_T *wp;
11255 char_u *sname;
11256 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011257 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011258 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011259 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011260 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011261 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011262 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263
11264 if (ssop_flags & SSOP_BUFFERS)
11265 only_save_windows = FALSE; /* Save ALL buffers */
11266
11267 /*
11268 * Begin by setting the this_session variable, and then other
11269 * sessionable variables.
11270 */
11271#ifdef FEAT_EVAL
11272 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11273 return FAIL;
11274 if (ssop_flags & SSOP_GLOBALS)
11275 if (store_session_globals(fd) == FAIL)
11276 return FAIL;
11277#endif
11278
11279 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011280 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 */
11282 if (put_line(fd, "silent only") == FAIL)
11283 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011284 if ((ssop_flags & SSOP_TABPAGES)
11285 && put_line(fd, "silent tabonly") == FAIL)
11286 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287
11288 /*
11289 * Now a :cd command to the session directory or the current directory
11290 */
11291 if (ssop_flags & SSOP_SESDIR)
11292 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011293 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11294 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 return FAIL;
11296 }
11297 else if (ssop_flags & SSOP_CURDIR)
11298 {
11299 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11300 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011301 || fputs("cd ", fd) < 0
11302 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11303 || put_eol(fd) == FAIL)
11304 {
11305 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308 vim_free(sname);
11309 }
11310
11311 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011312 * If there is an empty, unnamed buffer we will wipe it out later.
11313 * Remember the buffer number.
11314 */
11315 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11316 return FAIL;
11317 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11318 return FAIL;
11319 if (put_line(fd, "endif") == FAIL)
11320 return FAIL;
11321
11322 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323 * Now save the current files, current buffer first.
11324 */
11325 if (put_line(fd, "set shortmess=aoO") == FAIL)
11326 return FAIL;
11327
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011329 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11331 return FAIL;
11332
11333 if (ssop_flags & SSOP_RESIZE)
11334 {
11335 /* Note: after the restore we still check it worked!*/
11336 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11337 || put_eol(fd) == FAIL)
11338 return FAIL;
11339 }
11340
11341#ifdef FEAT_GUI
11342 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11343 {
11344 int x, y;
11345
11346 if (gui_mch_get_winpos(&x, &y) == OK)
11347 {
11348 /* Note: after the restore we still check it worked!*/
11349 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11350 return FAIL;
11351 }
11352 }
11353#endif
11354
11355 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011356 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11357 * will be displayed when creating the next tab. That resizes the windows
11358 * in the first tab, which may cause problems. Set 'showtabline' to 2
11359 * temporarily to avoid that.
11360 */
11361 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11362 {
11363 if (put_line(fd, "set stal=2") == FAIL)
11364 return FAIL;
11365 restore_stal = TRUE;
11366 }
11367
11368 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011369 * May repeat putting Windows for each tab, when "tabpages" is in
11370 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011371 * Don't use goto_tabpage(), it may change directory and trigger
11372 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011374 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011375 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011376 if ((ssop_flags & SSOP_TABPAGES))
11377 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011378 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011379
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011380 // Similar to ses_win_rec() below, populate the tab pages first so
11381 // later local options won't be copied to the new tabs.
11382 FOR_ALL_TABPAGES(tp)
11383 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011384 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011385 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011386 return FAIL;
11387 }
Bram Moolenaar18144c82006-04-12 21:52:12 +000011388 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011390 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +020011391 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011392
Bram Moolenaar18144c82006-04-12 21:52:12 +000011393 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011395 tabpage_T *tp = find_tabpage(tabnr);
11396
11397 if (tp == NULL)
11398 break; /* done all tab pages */
11399 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011400 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011401 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011402 tab_topframe = topframe;
11403 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011404 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011405 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011406 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011407 tab_topframe = tp->tp_topframe;
11408 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011409 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011410 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412
Bram Moolenaar18144c82006-04-12 21:52:12 +000011413 /*
11414 * Before creating the window layout, try loading one file. If this
11415 * is aborted we don't end up with a number of useless windows.
11416 * This may have side effects! (e.g., compressed or network file).
11417 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011418 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011419 {
11420 if (ses_do_win(wp)
11421 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011422 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011423#ifdef FEAT_QUICKFIX
11424 && !bt_nofile(wp->w_buffer)
11425#endif
11426 )
11427 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011428 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
11429 return FAIL;
11430 need_tabnext = FALSE;
11431
11432 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011433 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11434 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011435 return FAIL;
11436 if (!wp->w_arg_idx_invalid)
11437 edited_win = wp;
11438 break;
11439 }
11440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011442 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011443 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011444 return FAIL;
11445
Bram Moolenaar18144c82006-04-12 21:52:12 +000011446 /*
11447 * Save current window layout.
11448 */
11449 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011451 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011453 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11454 return FAIL;
11455 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11456 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457
Bram Moolenaar18144c82006-04-12 21:52:12 +000011458 /*
11459 * Check if window sizes can be restored (no windows omitted).
11460 * Remember the window number of the current window after restoring.
11461 */
11462 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011463 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011464 {
11465 if (ses_do_win(wp))
11466 ++nr;
11467 else
11468 restore_size = FALSE;
11469 if (curwin == wp)
11470 cnr = nr;
11471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472
Bram Moolenaar18144c82006-04-12 21:52:12 +000011473 /* Go to the first window. */
11474 if (put_line(fd, "wincmd t") == FAIL)
11475 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011476
Bram Moolenaar18144c82006-04-12 21:52:12 +000011477 /*
11478 * If more than one window, see if sizes can be restored.
11479 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11480 * resized when moving between windows.
11481 * Do this before restoring the view, so that the topline and the
11482 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011483 * winminheight and winminwidth need to be set to avoid an error if the
11484 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011485 */
Bram Moolenaar19834012018-06-12 17:03:39 +020011486 if (put_line(fd, "set winminheight=0") == FAIL
11487 || put_line(fd, "set winheight=1") == FAIL
11488 || put_line(fd, "set winminwidth=0") == FAIL
11489 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011490 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011491 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011492 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493
Bram Moolenaar18144c82006-04-12 21:52:12 +000011494 /*
11495 * Restore the view of the window (options, file, cursor, etc.).
11496 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011497 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011498 {
11499 if (!ses_do_win(wp))
11500 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011501 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11502 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011503 return FAIL;
11504 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11505 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011506 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011507 }
11508
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011509 /* The argument index in the first tab page is zero, need to set it in
11510 * each window. For further tab pages it's the window where we do
11511 * "tabedit". */
11512 cur_arg_idx = next_arg_idx;
11513
Bram Moolenaar18144c82006-04-12 21:52:12 +000011514 /*
11515 * Restore cursor to the current window if it's not the first one.
11516 */
11517 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11518 || put_eol(fd) == FAIL))
11519 return FAIL;
11520
11521 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011522 * Restore window sizes again after jumping around in windows, because
11523 * the current window has a minimum size while others may not.
11524 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011525 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011526 return FAIL;
11527
Bram Moolenaar18144c82006-04-12 21:52:12 +000011528 /* Don't continue in another tab page when doing only the current one
11529 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011530 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011531 break;
11532 }
11533
11534 if (ssop_flags & SSOP_TABPAGES)
11535 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011536 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11537 || put_eol(fd) == FAIL)
11538 return FAIL;
11539 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011540 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11541 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011542
Bram Moolenaard39e2752019-01-26 20:07:38 +010011543 // Now put the remaining buffers into the buffer list.
11544 // This is near the end, so that when 'hidden' is set we don't create extra
11545 // buffers. If the buffer was already created with another command the
11546 // ":badd" will have no effect.
11547 FOR_ALL_BUFFERS(buf)
11548 {
11549 if (!(only_save_windows && buf->b_nwindows == 0)
11550 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11551#ifdef FEAT_TERMINAL
11552 // Skip terminal buffers: finished ones are not useful, others
11553 // will be resurrected and result in a new buffer.
11554 && !bt_terminal(buf)
11555#endif
11556 && buf->b_fname != NULL
11557 && buf->b_p_bl)
11558 {
11559 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11560 : buf->b_wininfo->wi_fpos.lnum) < 0
11561 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
11562 return FAIL;
11563 }
11564 }
11565
Bram Moolenaar9c102382006-05-03 21:26:49 +000011566 /*
11567 * Wipe out an empty unnamed buffer we started in.
11568 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011569 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011570 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011571 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011572 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011573 return FAIL;
11574 if (put_line(fd, "endif") == FAIL)
11575 return FAIL;
11576 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11577 return FAIL;
11578
11579 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11580 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11581 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11582 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011583 /* Re-apply 'winminheight' and 'winminwidth'. */
11584 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11585 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11586 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011587
11588 /*
11589 * Lastly, execute the x.vim file if it exists.
11590 */
11591 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11592 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011593 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594 || put_line(fd, "endif") == FAIL)
11595 return FAIL;
11596
11597 return OK;
11598}
11599
11600 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011601ses_winsizes(
11602 FILE *fd,
11603 int restore_size,
11604 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605{
11606 int n = 0;
11607 win_T *wp;
11608
11609 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11610 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011611 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612 {
11613 if (!ses_do_win(wp))
11614 continue;
11615 ++n;
11616
11617 /* restore height when not full height */
11618 if (wp->w_height + wp->w_status_height < topframe->fr_height
11619 && (fprintf(fd,
11620 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11621 n, (long)wp->w_height, Rows / 2, Rows) < 0
11622 || put_eol(fd) == FAIL))
11623 return FAIL;
11624
11625 /* restore width when not full width */
11626 if (wp->w_width < Columns && (fprintf(fd,
11627 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11628 n, (long)wp->w_width, Columns / 2, Columns) < 0
11629 || put_eol(fd) == FAIL))
11630 return FAIL;
11631 }
11632 }
11633 else
11634 {
11635 /* Just equalise window sizes */
11636 if (put_line(fd, "wincmd =") == FAIL)
11637 return FAIL;
11638 }
11639 return OK;
11640}
11641
11642/*
11643 * Write commands to "fd" to recursively create windows for frame "fr",
11644 * horizontally and vertically split.
11645 * After the commands the last window in the frame is the current window.
11646 * Returns FAIL when writing the commands to "fd" fails.
11647 */
11648 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011649ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650{
11651 frame_T *frc;
11652 int count = 0;
11653
11654 if (fr->fr_layout != FR_LEAF)
11655 {
11656 /* Find first frame that's not skipped and then create a window for
11657 * each following one (first frame is already there). */
11658 frc = ses_skipframe(fr->fr_child);
11659 if (frc != NULL)
11660 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11661 {
11662 /* Make window as big as possible so that we have lots of room
11663 * to split. */
11664 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11665 || put_line(fd, fr->fr_layout == FR_COL
11666 ? "split" : "vsplit") == FAIL)
11667 return FAIL;
11668 ++count;
11669 }
11670
11671 /* Go back to the first window. */
11672 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11673 ? "%dwincmd k" : "%dwincmd h", count) < 0
11674 || put_eol(fd) == FAIL))
11675 return FAIL;
11676
11677 /* Recursively create frames/windows in each window of this column or
11678 * row. */
11679 frc = ses_skipframe(fr->fr_child);
11680 while (frc != NULL)
11681 {
11682 ses_win_rec(fd, frc);
11683 frc = ses_skipframe(frc->fr_next);
11684 /* Go to next window. */
11685 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11686 return FAIL;
11687 }
11688 }
11689 return OK;
11690}
11691
11692/*
11693 * Skip frames that don't contain windows we want to save in the Session.
11694 * Returns NULL when there none.
11695 */
11696 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011697ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698{
11699 frame_T *frc;
11700
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011701 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 if (ses_do_frame(frc))
11703 break;
11704 return frc;
11705}
11706
11707/*
11708 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11709 * the Session.
11710 */
11711 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011712ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713{
11714 frame_T *frc;
11715
11716 if (fr->fr_layout == FR_LEAF)
11717 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011718 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 if (ses_do_frame(frc))
11720 return TRUE;
11721 return FALSE;
11722}
11723
11724/*
11725 * Return non-zero if window "wp" is to be stored in the Session.
11726 */
11727 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011728ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011729{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011730#ifdef FEAT_TERMINAL
11731 if (bt_terminal(wp->w_buffer))
11732 return !term_is_finished(wp->w_buffer)
11733 && (ssop_flags & SSOP_TERMINAL)
11734 && term_should_restore(wp->w_buffer);
11735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736 if (wp->w_buffer->b_fname == NULL
11737#ifdef FEAT_QUICKFIX
11738 /* When 'buftype' is "nofile" can't restore the window contents. */
11739 || bt_nofile(wp->w_buffer)
11740#endif
11741 )
11742 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011743 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744 return (ssop_flags & SSOP_HELP);
11745 return TRUE;
11746}
11747
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011748 static int
11749put_view_curpos(FILE *fd, win_T *wp, char *spaces)
11750{
11751 int r;
11752
11753 if (wp->w_curswant == MAXCOL)
11754 r = fprintf(fd, "%snormal! $", spaces);
11755 else
11756 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
11757 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
11758}
11759
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760/*
11761 * Write commands to "fd" to restore the view of a window.
11762 * Caller must make sure 'scrolloff' is zero.
11763 */
11764 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011765put_view(
11766 FILE *fd,
11767 win_T *wp,
11768 int add_edit, /* add ":edit" command to view */
11769 unsigned *flagp, /* vop_flags or ssop_flags */
11770 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011771 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772{
11773 win_T *save_curwin;
11774 int f;
11775 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011776 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011777
11778 /* Always restore cursor position for ":mksession". For ":mkview" only
11779 * when 'viewoptions' contains "cursor". */
11780 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11781
11782 /*
11783 * Local argument list.
11784 */
11785 if (wp->w_alist == &global_alist)
11786 {
11787 if (put_line(fd, "argglobal") == FAIL)
11788 return FAIL;
11789 }
11790 else
11791 {
11792 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11793 flagp == &vop_flags
11794 || !(*flagp & SSOP_CURDIR)
11795 || wp->w_localdir != NULL, flagp) == FAIL)
11796 return FAIL;
11797 }
11798
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011799 /* Only when part of a session: restore the argument index. Some
11800 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011801 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011802 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011804 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805 || put_eol(fd) == FAIL)
11806 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011807 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808 }
11809
11810 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011811 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011813# ifdef FEAT_TERMINAL
11814 if (bt_terminal(wp->w_buffer))
11815 {
11816 if (term_write_session(fd, wp) == FAIL)
11817 return FAIL;
11818 }
11819 else
11820# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821 /*
11822 * Load the file.
11823 */
11824 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011825# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011827# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828 )
11829 {
11830 /*
11831 * Editing a file in this buffer: use ":edit file".
11832 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011833 *
11834 * Note, if a buffer for that file already exists, use :badd to
11835 * edit that buffer, to not lose folding information (:edit resets
11836 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 */
Bram Moolenaarad36a352019-01-24 13:34:42 +010011838 if (fputs("if bufexists(\"", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011839 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
Bram Moolenaarad36a352019-01-24 13:34:42 +010011840 || fputs("\") | buffer ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011841 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11842 || fputs(" | else | edit ", fd) < 0
11843 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11844 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011845 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846 return FAIL;
11847 }
11848 else
11849 {
11850 /* No file in this buffer, just make it empty. */
11851 if (put_line(fd, "enew") == FAIL)
11852 return FAIL;
11853#ifdef FEAT_QUICKFIX
11854 if (wp->w_buffer->b_ffname != NULL)
11855 {
11856 /* The buffer does have a name, but it's not a file name. */
11857 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011858 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859 return FAIL;
11860 }
11861#endif
11862 do_cursor = FALSE;
11863 }
11864 }
11865
11866 /*
11867 * Local mappings and abbreviations.
11868 */
11869 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11870 && makemap(fd, wp->w_buffer) == FAIL)
11871 return FAIL;
11872
11873 /*
11874 * Local options. Need to go to the window temporarily.
11875 * Store only local values when using ":mkview" and when ":mksession" is
11876 * used and 'sessionoptions' doesn't include "options".
11877 * Some folding options are always stored when "folds" is included,
11878 * otherwise the folds would not be restored correctly.
11879 */
11880 save_curwin = curwin;
11881 curwin = wp;
11882 curbuf = curwin->w_buffer;
11883 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11884 f = makeset(fd, OPT_LOCAL,
11885 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11886#ifdef FEAT_FOLDING
11887 else if (*flagp & SSOP_FOLDS)
11888 f = makefoldset(fd);
11889#endif
11890 else
11891 f = OK;
11892 curwin = save_curwin;
11893 curbuf = curwin->w_buffer;
11894 if (f == FAIL)
11895 return FAIL;
11896
11897#ifdef FEAT_FOLDING
11898 /*
11899 * Save Folds when 'buftype' is empty and for help files.
11900 */
11901 if ((*flagp & SSOP_FOLDS)
11902 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020011903 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904 {
11905 if (put_folds(fd, wp) == FAIL)
11906 return FAIL;
11907 }
11908#endif
11909
11910 /*
11911 * Set the cursor after creating folds, since that moves the cursor.
11912 */
11913 if (do_cursor)
11914 {
11915
11916 /* Restore the cursor line in the file and relatively in the
11917 * window. Don't use "G", it changes the jumplist. */
11918 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11919 (long)wp->w_cursor.lnum,
11920 (long)(wp->w_cursor.lnum - wp->w_topline),
11921 (long)wp->w_height / 2, (long)wp->w_height) < 0
11922 || put_eol(fd) == FAIL
11923 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11924 || put_line(fd, "exe s:l") == FAIL
11925 || put_line(fd, "normal! zt") == FAIL
11926 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11927 || put_eol(fd) == FAIL)
11928 return FAIL;
11929 /* Restore the cursor column and left offset when not wrapping. */
11930 if (wp->w_cursor.col == 0)
11931 {
11932 if (put_line(fd, "normal! 0") == FAIL)
11933 return FAIL;
11934 }
11935 else
11936 {
11937 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11938 {
11939 if (fprintf(fd,
11940 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011941 (long)wp->w_virtcol + 1,
11942 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943 (long)wp->w_width / 2, (long)wp->w_width) < 0
11944 || put_eol(fd) == FAIL
11945 || put_line(fd, "if s:c > 0") == FAIL
11946 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011947 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11948 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949 || put_eol(fd) == FAIL
11950 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011951 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952 || put_line(fd, "endif") == FAIL)
11953 return FAIL;
11954 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011955 else if (put_view_curpos(fd, wp, "") == FAIL)
11956 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957 }
11958 }
11959
11960 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011961 * Local directory, if the current flag is not view options or the "curdir"
11962 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011964 if (wp->w_localdir != NULL
11965 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966 {
11967 if (fputs("lcd ", fd) < 0
11968 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11969 || put_eol(fd) == FAIL)
11970 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011971 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972 }
11973
11974 return OK;
11975}
11976
11977/*
11978 * Write an argument list to the session file.
11979 * Returns FAIL if writing fails.
11980 */
11981 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011982ses_arglist(
11983 FILE *fd,
11984 char *cmd,
11985 garray_T *gap,
11986 int fullname, /* TRUE: use full path name */
11987 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988{
11989 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011990 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991 char_u *s;
11992
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011993 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11994 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010011995 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 return FAIL;
11997 for (i = 0; i < gap->ga_len; ++i)
11998 {
11999 /* NULL file names are skipped (only happens when out of memory). */
12000 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
12001 if (s != NULL)
12002 {
12003 if (fullname)
12004 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020012005 buf = alloc(MAXPATHL);
12006 if (buf != NULL)
12007 {
12008 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
12009 s = buf;
12010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010012012 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010012013 || ses_put_fname(fd, s, flagp) == FAIL
12014 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020012015 {
12016 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012018 }
12019 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 }
12021 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010012022 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023}
12024
12025/*
12026 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012027 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028 * Returns FAIL if writing fails.
12029 */
12030 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012031ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032{
12033 char_u *name;
12034
12035 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012036 * the session file will be sourced.
12037 * Don't do this for ":mkview", we don't know the current directory.
12038 * Don't do this after ":lcd", we don't keep track of what the current
12039 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040 if (buf->b_sfname != NULL
12041 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012042 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000012043#ifdef FEAT_AUTOCHDIR
12044 && !p_acd
12045#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012046 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047 name = buf->b_sfname;
12048 else
12049 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012050 if (ses_put_fname(fd, name, flagp) == FAIL
12051 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052 return FAIL;
12053 return OK;
12054}
12055
12056/*
12057 * Write a file name to the session file.
12058 * Takes care of the "slash" option in 'sessionoptions' and escapes special
12059 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012060 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061 */
12062 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012063ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064{
12065 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012066 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068
12069 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012070 if (sname == NULL)
12071 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012073 if (*flagp & SSOP_SLASH)
12074 {
12075 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010012076 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012077 if (*p == '\\')
12078 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012080
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020012081 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012082 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012084 if (p == NULL)
12085 return FAIL;
12086
12087 /* write the result */
12088 if (fputs((char *)p, fd) < 0)
12089 retval = FAIL;
12090
12091 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012092 return retval;
12093}
12094
12095/*
12096 * ":loadview [nr]"
12097 */
12098 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012099ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100{
12101 char_u *fname;
12102
12103 fname = get_view_file(*eap->arg);
12104 if (fname != NULL)
12105 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012106 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107 vim_free(fname);
12108 }
12109}
12110
12111/*
12112 * Get the name of the view file for the current buffer.
12113 */
12114 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012115get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116{
12117 int len = 0;
12118 char_u *p, *s;
12119 char_u *retval;
12120 char_u *sname;
12121
12122 if (curbuf->b_ffname == NULL)
12123 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012124 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125 return NULL;
12126 }
12127 sname = home_replace_save(NULL, curbuf->b_ffname);
12128 if (sname == NULL)
12129 return NULL;
12130
12131 /*
12132 * We want a file name without separators, because we're not going to make
12133 * a directory.
12134 * "normal" path separator -> "=+"
12135 * "=" -> "=="
12136 * ":" path separator -> "=-"
12137 */
12138 for (p = sname; *p; ++p)
12139 if (*p == '=' || vim_ispathsep(*p))
12140 ++len;
12141 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
12142 if (retval != NULL)
12143 {
12144 STRCPY(retval, p_vdir);
12145 add_pathsep(retval);
12146 s = retval + STRLEN(retval);
12147 for (p = sname; *p; ++p)
12148 {
12149 if (*p == '=')
12150 {
12151 *s++ = '=';
12152 *s++ = '=';
12153 }
12154 else if (vim_ispathsep(*p))
12155 {
12156 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020012157#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158 if (*p == ':')
12159 *s++ = '-';
12160 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012162 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163 }
12164 else
12165 *s++ = *p;
12166 }
12167 *s++ = '=';
12168 *s++ = c;
12169 STRCPY(s, ".vim");
12170 }
12171
12172 vim_free(sname);
12173 return retval;
12174}
12175
12176#endif /* FEAT_SESSION */
12177
12178/*
12179 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12180 * Return FAIL for a write error.
12181 */
12182 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012183put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184{
12185 if (
12186#ifdef USE_CRNL
12187 (
12188# ifdef MKSESSION_NL
12189 !mksession_nl &&
12190# endif
12191 (putc('\r', fd) < 0)) ||
12192#endif
12193 (putc('\n', fd) < 0))
12194 return FAIL;
12195 return OK;
12196}
12197
12198/*
12199 * Write a line to "fd".
12200 * Return FAIL for a write error.
12201 */
12202 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012203put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204{
12205 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12206 return FAIL;
12207 return OK;
12208}
12209
12210#ifdef FEAT_VIMINFO
12211/*
12212 * ":rviminfo" and ":wviminfo".
12213 */
12214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012215ex_viminfo(
12216 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012217{
12218 char_u *save_viminfo;
12219
12220 save_viminfo = p_viminfo;
12221 if (*p_viminfo == NUL)
12222 p_viminfo = (char_u *)"'100";
12223 if (eap->cmdidx == CMD_rviminfo)
12224 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012225 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12226 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012227 emsg(_("E195: Cannot open viminfo file for reading"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228 }
12229 else
12230 write_viminfo(eap->arg, eap->forceit);
12231 p_viminfo = save_viminfo;
12232}
12233#endif
12234
12235#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012236/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012237 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012238 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012239 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012241dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243 if (fname == NULL)
12244 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012245 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246}
12247#endif
12248
12249/*
12250 * ":behave {mswin,xterm}"
12251 */
12252 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012253ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012254{
12255 if (STRCMP(eap->arg, "mswin") == 0)
12256 {
12257 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12258 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12259 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12260 set_option_value((char_u *)"keymodel", 0L,
12261 (char_u *)"startsel,stopsel", 0);
12262 }
12263 else if (STRCMP(eap->arg, "xterm") == 0)
12264 {
12265 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12266 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12267 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12268 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12269 }
12270 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012271 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272}
12273
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012274#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12275/*
12276 * Function given to ExpandGeneric() to obtain the possible arguments of the
12277 * ":behave {mswin,xterm}" command.
12278 */
12279 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012280get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012281{
12282 if (idx == 0)
12283 return (char_u *)"mswin";
12284 if (idx == 1)
12285 return (char_u *)"xterm";
12286 return NULL;
12287}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012288
12289/*
12290 * Function given to ExpandGeneric() to obtain the possible arguments of the
12291 * ":messages {clear}" command.
12292 */
12293 char_u *
12294get_messages_arg(expand_T *xp UNUSED, int idx)
12295{
12296 if (idx == 0)
12297 return (char_u *)"clear";
12298 return NULL;
12299}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012300#endif
12301
Bram Moolenaar113e1072019-01-20 15:30:40 +010012302#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012303 char_u *
12304get_mapclear_arg(expand_T *xp UNUSED, int idx)
12305{
12306 if (idx == 0)
12307 return (char_u *)"<buffer>";
12308 return NULL;
12309}
Bram Moolenaar113e1072019-01-20 15:30:40 +010012310#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012311
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312static int filetype_detect = FALSE;
12313static int filetype_plugin = FALSE;
12314static int filetype_indent = FALSE;
12315
12316/*
12317 * ":filetype [plugin] [indent] {on,off,detect}"
12318 * on: Load the filetype.vim file to install autocommands for file types.
12319 * off: Load the ftoff.vim file to remove all autocommands for file types.
12320 * plugin on: load filetype.vim and ftplugin.vim
12321 * plugin off: load ftplugof.vim
12322 * indent on: load filetype.vim and indent.vim
12323 * indent off: load indoff.vim
12324 */
12325 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012326ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327{
12328 char_u *arg = eap->arg;
12329 int plugin = FALSE;
12330 int indent = FALSE;
12331
12332 if (*eap->arg == NUL)
12333 {
12334 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012335 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336 filetype_detect ? "ON" : "OFF",
12337 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12338 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12339 return;
12340 }
12341
12342 /* Accept "plugin" and "indent" in any order. */
12343 for (;;)
12344 {
12345 if (STRNCMP(arg, "plugin", 6) == 0)
12346 {
12347 plugin = TRUE;
12348 arg = skipwhite(arg + 6);
12349 continue;
12350 }
12351 if (STRNCMP(arg, "indent", 6) == 0)
12352 {
12353 indent = TRUE;
12354 arg = skipwhite(arg + 6);
12355 continue;
12356 }
12357 break;
12358 }
12359 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12360 {
12361 if (*arg == 'o' || !filetype_detect)
12362 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012363 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364 filetype_detect = TRUE;
12365 if (plugin)
12366 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012367 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368 filetype_plugin = TRUE;
12369 }
12370 if (indent)
12371 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012372 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373 filetype_indent = TRUE;
12374 }
12375 }
12376 if (*arg == 'd')
12377 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012378 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012379 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380 }
12381 }
12382 else if (STRCMP(arg, "off") == 0)
12383 {
12384 if (plugin || indent)
12385 {
12386 if (plugin)
12387 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012388 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389 filetype_plugin = FALSE;
12390 }
12391 if (indent)
12392 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012393 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394 filetype_indent = FALSE;
12395 }
12396 }
12397 else
12398 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012399 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400 filetype_detect = FALSE;
12401 }
12402 }
12403 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012404 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405}
12406
12407/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012408 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409 */
12410 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012411ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412{
12413 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012414 {
12415 char_u *arg = eap->arg;
12416
12417 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12418 arg += 9;
12419
12420 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12421 if (arg != eap->arg)
12422 did_filetype = FALSE;
12423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012427ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428{
12429#ifdef FEAT_DIGRAPHS
12430 if (*eap->arg != NUL)
12431 putdigraph(eap->arg);
12432 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010012433 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012435 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436#endif
12437}
12438
12439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012440ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441{
12442 int flags = 0;
12443
12444 if (eap->cmdidx == CMD_setlocal)
12445 flags = OPT_LOCAL;
12446 else if (eap->cmdidx == CMD_setglobal)
12447 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012448#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449 if (cmdmod.browse && flags == 0)
12450 ex_options(eap);
12451 else
12452#endif
12453 (void)do_set(eap->arg, flags);
12454}
12455
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012456#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
12457 void
12458set_no_hlsearch(int flag)
12459{
12460 no_hlsearch = flag;
12461# ifdef FEAT_EVAL
12462 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
12463# endif
12464}
12465
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466/*
12467 * ":nohlsearch"
12468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012470ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012471{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012472 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012473 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012474}
12475
12476/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012477 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478 * Sets nextcmd to the start of the next command, if any. Also called when
12479 * skipping commands to find the next command.
12480 */
12481 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012482ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483{
12484 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012485 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486 char_u *end;
12487 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012488 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012489
12490 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012491 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012492 else
12493 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012494 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012495 return;
12496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497
12498 /* First clear any old pattern. */
12499 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012500 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501
12502 if (ends_excmd(*eap->arg))
12503 end = eap->arg;
12504 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012505 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506 end = eap->arg + 4;
12507 else
12508 {
12509 p = skiptowhite(eap->arg);
12510 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012511 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512 p = skipwhite(p);
12513 if (*p == NUL)
12514 {
12515 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012516 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012517 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012518 return;
12519 }
12520 end = skip_regexp(p + 1, *p, TRUE, NULL);
12521 if (!eap->skip)
12522 {
12523 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12524 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012525 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526 eap->errmsg = e_trailing;
12527 return;
12528 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012529 if (*end != *p)
12530 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012531 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012532 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012533 return;
12534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535
12536 c = *end;
12537 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012538 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012539 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012540 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541 }
12542 }
12543 eap->nextcmd = find_nextcmd(end);
12544}
12545#endif
12546
12547#ifdef FEAT_CRYPT
12548/*
12549 * ":X": Get crypt key
12550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012552ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012554 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012555 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556}
12557#endif
12558
12559#ifdef FEAT_FOLDING
12560 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012561ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562{
12563 if (foldManualAllowed(TRUE))
12564 foldCreate(eap->line1, eap->line2);
12565}
12566
12567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012568ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012569{
12570 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12571 eap->forceit, FALSE);
12572}
12573
12574 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012575ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576{
12577 linenr_T lnum;
12578
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012579#ifdef FEAT_CLIPBOARD
12580 start_global_changes();
12581#endif
12582
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583 /* First set the marks for all lines closed/open. */
12584 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12585 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12586 ml_setmarked(lnum);
12587
12588 /* Execute the command on the marked lines. */
12589 global_exe(eap->arg);
12590 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012591#ifdef FEAT_CLIPBOARD
12592 end_global_changes();
12593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012594}
12595#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012596
Bram Moolenaar39665952018-08-15 20:59:48 +020012597#ifdef FEAT_QUICKFIX
12598/*
12599 * Returns TRUE if the supplied Ex cmdidx is for a location list command
12600 * instead of a quickfix command.
12601 */
12602 int
12603is_loclist_cmd(int cmdidx)
12604{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020012605 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020012606 return FALSE;
12607 return cmdnames[cmdidx].cmd_name[0] == 'l';
12608}
12609#endif
12610
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012611# if defined(FEAT_TIMERS) || defined(PROTO)
12612 int
12613get_pressedreturn(void)
12614{
12615 return ex_pressedreturn;
12616}
12617
12618 void
12619set_pressedreturn(int val)
12620{
12621 ex_pressedreturn = val;
12622}
12623#endif