blob: 6b2fe4628eb1b7fe44e05392b6ece79d6aa767b9 [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;
Bram Moolenaara561a412019-04-25 21:27:58 +02005918 char *shortname;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005919} addr_type_complete[] =
5920{
Bram Moolenaara561a412019-04-25 21:27:58 +02005921 {ADDR_ARGUMENTS, "arguments", "arg"},
5922 {ADDR_LINES, "lines", "line"},
5923 {ADDR_LOADED_BUFFERS, "loaded_buffers", "load"},
5924 {ADDR_TABS, "tabs", "tab"},
5925 {ADDR_BUFFERS, "buffers", "buf"},
5926 {ADDR_WINDOWS, "windows", "win"},
5927 {ADDR_QUICKFIX, "quickfix", "qf"},
5928 {ADDR_OTHER, "other", "?"},
5929 {-1, NULL, NULL}
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005930};
5931#endif
5932
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005933#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934/*
5935 * List of names for completion for ":command" with the EXPAND_ flag.
5936 * Must be alphabetical for completion.
5937 */
5938static struct
5939{
5940 int expand;
5941 char *name;
5942} command_complete[] =
5943{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005944 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005946 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005948 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005950 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005951#if defined(FEAT_CSCOPE)
5952 {EXPAND_CSCOPE, "cscope"},
5953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5955 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005956 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957#endif
5958 {EXPAND_DIRECTORIES, "dir"},
5959 {EXPAND_ENV_VARS, "environment"},
5960 {EXPAND_EVENTS, "event"},
5961 {EXPAND_EXPRESSION, "expression"},
5962 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005963 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005964 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 {EXPAND_FUNCTIONS, "function"},
5966 {EXPAND_HELP, "help"},
5967 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005968#if defined(FEAT_CMDHIST)
5969 {EXPAND_HISTORY, "history"},
5970#endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005971#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005972 {EXPAND_LOCALES, "locale"},
5973#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005974 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 {EXPAND_MAPPINGS, "mapping"},
5976 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005977 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005978 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005979#if defined(FEAT_PROFILE)
5980 {EXPAND_SYNTIME, "syntime"},
5981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005983 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005984 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005985#if defined(FEAT_SIGNS)
5986 {EXPAND_SIGN, "sign"},
5987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 {EXPAND_TAGS, "tag"},
5989 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005990 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 {EXPAND_USER_VARS, "var"},
5992 {0, NULL}
5993};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005996#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005998uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999{
6000 int i, j;
6001 int found = FALSE;
6002 ucmd_T *cmd;
6003 int len;
Bram Moolenaar725310d2019-04-24 23:08:23 +02006004 int over;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 long a;
6006 garray_T *gap;
6007
6008 gap = &curbuf->b_ucmds;
6009 for (;;)
6010 {
6011 for (i = 0; i < gap->ga_len; ++i)
6012 {
6013 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006014 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015
Bram Moolenaard29459b2016-08-26 22:29:11 +02006016 /* Skip commands which don't match the requested prefix and
6017 * commands filtered out. */
6018 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6019 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 continue;
6021
6022 /* Put out the title first time */
6023 if (!found)
Bram Moolenaara561a412019-04-25 21:27:58 +02006024 msg_puts_title(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 found = TRUE;
6026 msg_putchar('\n');
6027 if (got_int)
6028 break;
6029
Bram Moolenaar725310d2019-04-24 23:08:23 +02006030 // Special cases
6031 len = 4;
6032 if (a & BANG)
6033 {
6034 msg_putchar('!');
6035 --len;
6036 }
6037 if (a & REGSTR)
6038 {
6039 msg_putchar('"');
6040 --len;
6041 }
6042 if (gap != &ucmds)
6043 {
6044 msg_putchar('b');
6045 --len;
6046 }
6047 if (a & TRLBAR)
6048 {
6049 msg_putchar('|');
6050 --len;
6051 }
6052 while (len-- > 0)
6053 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054
Bram Moolenaar8820b482017-03-16 17:23:31 +01006055 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 len = (int)STRLEN(cmd->uc_name) + 4;
6057
6058 do {
6059 msg_putchar(' ');
6060 ++len;
Bram Moolenaar725310d2019-04-24 23:08:23 +02006061 } while (len < 22);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062
Bram Moolenaar725310d2019-04-24 23:08:23 +02006063 // "over" is how much longer the name is than the column width for
6064 // the name, we'll try to align what comes after.
6065 over = len - 22;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 len = 0;
6067
Bram Moolenaar725310d2019-04-24 23:08:23 +02006068 // Arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6070 {
Bram Moolenaar725310d2019-04-24 23:08:23 +02006071 case 0: IObuff[len++] = '0'; break;
6072 case (EXTRA): IObuff[len++] = '*'; break;
6073 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6074 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6075 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 }
6077
6078 do {
6079 IObuff[len++] = ' ';
Bram Moolenaar725310d2019-04-24 23:08:23 +02006080 } while (len < 5 - over);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081
Bram Moolenaar725310d2019-04-24 23:08:23 +02006082 // Address / Range
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 if (a & (RANGE|COUNT))
6084 {
6085 if (a & COUNT)
6086 {
Bram Moolenaar725310d2019-04-24 23:08:23 +02006087 // -count=N
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6089 len += (int)STRLEN(IObuff + len);
6090 }
6091 else if (a & DFLALL)
6092 IObuff[len++] = '%';
6093 else if (cmd->uc_def >= 0)
6094 {
Bram Moolenaar725310d2019-04-24 23:08:23 +02006095 // -range=N
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6097 len += (int)STRLEN(IObuff + len);
6098 }
6099 else
6100 IObuff[len++] = '.';
6101 }
6102
6103 do {
6104 IObuff[len++] = ' ';
Bram Moolenaara561a412019-04-25 21:27:58 +02006105 } while (len < 8 - over);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106
Bram Moolenaar725310d2019-04-24 23:08:23 +02006107 // Address Type
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006108 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6109 if (addr_type_complete[j].expand != ADDR_LINES
6110 && addr_type_complete[j].expand == cmd->uc_addr_type)
6111 {
Bram Moolenaara561a412019-04-25 21:27:58 +02006112 STRCPY(IObuff + len, addr_type_complete[j].shortname);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006113 len += (int)STRLEN(IObuff + len);
6114 break;
6115 }
6116
6117 do {
6118 IObuff[len++] = ' ';
Bram Moolenaar725310d2019-04-24 23:08:23 +02006119 } while (len < 13 - over);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006120
Bram Moolenaar725310d2019-04-24 23:08:23 +02006121 // Completion
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 for (j = 0; command_complete[j].expand != 0; ++j)
6123 if (command_complete[j].expand == cmd->uc_compl)
6124 {
6125 STRCPY(IObuff + len, command_complete[j].name);
6126 len += (int)STRLEN(IObuff + len);
6127 break;
6128 }
6129
6130 do {
6131 IObuff[len++] = ' ';
Bram Moolenaara561a412019-04-25 21:27:58 +02006132 } while (len < 25 - over);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133
6134 IObuff[len] = '\0';
6135 msg_outtrans(IObuff);
6136
Bram Moolenaar725310d2019-04-24 23:08:23 +02006137 msg_outtrans_special(cmd->uc_rep, FALSE,
Bram Moolenaara561a412019-04-25 21:27:58 +02006138 name_len == 0 ? Columns - 47 : 0);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006139#ifdef FEAT_EVAL
6140 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006141 last_set_msg(cmd->uc_script_ctx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 out_flush();
6144 ui_breakcheck();
6145 if (got_int)
6146 break;
6147 }
6148 if (gap == &ucmds || i < gap->ga_len)
6149 break;
6150 gap = &ucmds;
6151 }
6152
6153 if (!found)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006154 msg(_("No user-defined commands found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155}
6156
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006157 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006158uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159{
6160 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6161 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6162 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6163 0xb9, 0x7f, 0};
6164 int i;
6165
6166 for (i = 0; fcmd[i]; ++i)
6167 IObuff[i] = fcmd[i] - 0x40;
6168 IObuff[i] = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006169 return (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170}
6171
6172 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006173uc_scan_attr(
6174 char_u *attr,
6175 size_t len,
6176 long *argt,
6177 long *def,
6178 int *flags,
6179 int *compl,
6180 char_u **compl_arg,
6181 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182{
6183 char_u *p;
6184
6185 if (len == 0)
6186 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006187 emsg(_("E175: No attribute specified"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 return FAIL;
6189 }
6190
6191 /* First, try the simple attributes (no arguments) */
6192 if (STRNICMP(attr, "bang", len) == 0)
6193 *argt |= BANG;
6194 else if (STRNICMP(attr, "buffer", len) == 0)
6195 *flags |= UC_BUFFER;
6196 else if (STRNICMP(attr, "register", len) == 0)
6197 *argt |= REGSTR;
6198 else if (STRNICMP(attr, "bar", len) == 0)
6199 *argt |= TRLBAR;
6200 else
6201 {
6202 int i;
6203 char_u *val = NULL;
6204 size_t vallen = 0;
6205 size_t attrlen = len;
6206
6207 /* Look for the attribute name - which is the part before any '=' */
6208 for (i = 0; i < (int)len; ++i)
6209 {
6210 if (attr[i] == '=')
6211 {
6212 val = &attr[i + 1];
6213 vallen = len - i - 1;
6214 attrlen = i;
6215 break;
6216 }
6217 }
6218
6219 if (STRNICMP(attr, "nargs", attrlen) == 0)
6220 {
6221 if (vallen == 1)
6222 {
6223 if (*val == '0')
6224 /* Do nothing - this is the default */;
6225 else if (*val == '1')
6226 *argt |= (EXTRA | NOSPC | NEEDARG);
6227 else if (*val == '*')
6228 *argt |= EXTRA;
6229 else if (*val == '?')
6230 *argt |= (EXTRA | NOSPC);
6231 else if (*val == '+')
6232 *argt |= (EXTRA | NEEDARG);
6233 else
6234 goto wrong_nargs;
6235 }
6236 else
6237 {
6238wrong_nargs:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006239 emsg(_("E176: Invalid number of arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 return FAIL;
6241 }
6242 }
6243 else if (STRNICMP(attr, "range", attrlen) == 0)
6244 {
6245 *argt |= RANGE;
6246 if (vallen == 1 && *val == '%')
6247 *argt |= DFLALL;
6248 else if (val != NULL)
6249 {
6250 p = val;
6251 if (*def >= 0)
6252 {
6253two_count:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006254 emsg(_("E177: Count cannot be specified twice"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 return FAIL;
6256 }
6257
6258 *def = getdigits(&p);
6259 *argt |= (ZEROR | NOTADR);
6260
6261 if (p != val + vallen || vallen == 0)
6262 {
6263invalid_count:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006264 emsg(_("E178: Invalid default value for count"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 return FAIL;
6266 }
6267 }
6268 }
6269 else if (STRNICMP(attr, "count", attrlen) == 0)
6270 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006271 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272
6273 if (val != NULL)
6274 {
6275 p = val;
6276 if (*def >= 0)
6277 goto two_count;
6278
6279 *def = getdigits(&p);
6280
6281 if (p != val + vallen)
6282 goto invalid_count;
6283 }
6284
6285 if (*def < 0)
6286 *def = 0;
6287 }
6288 else if (STRNICMP(attr, "complete", attrlen) == 0)
6289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 if (val == NULL)
6291 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006292 emsg(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 return FAIL;
6294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006296 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6297 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006300 else if (STRNICMP(attr, "addr", attrlen) == 0)
6301 {
6302 *argt |= RANGE;
6303 if (val == NULL)
6304 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006305 emsg(_("E179: argument required for -addr"));
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006306 return FAIL;
6307 }
6308 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6309 == FAIL)
6310 return FAIL;
6311 if (addr_type_arg != ADDR_LINES)
6312 *argt |= (ZEROR | NOTADR) ;
6313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 else
6315 {
6316 char_u ch = attr[len];
6317 attr[len] = '\0';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006318 semsg(_("E181: Invalid attribute: %s"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 attr[len] = ch;
6320 return FAIL;
6321 }
6322 }
6323
6324 return OK;
6325}
6326
Bram Moolenaara850a712009-01-28 14:42:59 +00006327/*
6328 * ":command ..."
6329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006331ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332{
6333 char_u *name;
6334 char_u *end;
6335 char_u *p;
6336 long argt = 0;
6337 long def = -1;
6338 int flags = 0;
6339 int compl = EXPAND_NOTHING;
6340 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006341 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006343 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344
6345 p = eap->arg;
6346
6347 /* Check for attributes */
6348 while (*p == '-')
6349 {
6350 ++p;
6351 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006352 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6353 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 == FAIL)
6355 return;
6356 p = skipwhite(end);
6357 }
6358
6359 /* Get the name (if any) and skip to the following argument */
6360 name = p;
6361 if (ASCII_ISALPHA(*p))
6362 while (ASCII_ISALNUM(*p))
6363 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006364 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006366 emsg(_("E182: Invalid command name"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 return;
6368 }
6369 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006370 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371
Bram Moolenaar725310d2019-04-24 23:08:23 +02006372 // If there is nothing after the name, and no attributes were specified,
6373 // we are listing commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 p = skipwhite(end);
6375 if (!has_attr && ends_excmd(*p))
6376 {
6377 uc_list(name, end - name);
6378 }
6379 else if (!ASCII_ISUPPER(*name))
6380 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006381 emsg(_("E183: User defined commands must start with an uppercase letter"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 return;
6383 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006384 else if ((name_len == 1 && *name == 'X')
6385 || (name_len <= 4
6386 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6387 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006388 emsg(_("E841: Reserved name, cannot be used for user defined command"));
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006389 return;
6390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 else
6392 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006393 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394}
6395
6396/*
6397 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 * Clear all user commands, global and for current buffer.
6399 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006400 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006401ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402{
6403 uc_clear(&ucmds);
6404 uc_clear(&curbuf->b_ucmds);
6405}
6406
6407/*
6408 * Clear all user commands for "gap".
6409 */
6410 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006411uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412{
6413 int i;
6414 ucmd_T *cmd;
6415
6416 for (i = 0; i < gap->ga_len; ++i)
6417 {
6418 cmd = USER_CMD_GA(gap, i);
6419 vim_free(cmd->uc_name);
6420 vim_free(cmd->uc_rep);
6421# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6422 vim_free(cmd->uc_compl_arg);
6423# endif
6424 }
6425 ga_clear(gap);
6426}
6427
6428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006429ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430{
6431 int i = 0;
6432 ucmd_T *cmd = NULL;
6433 int cmp = -1;
6434 garray_T *gap;
6435
6436 gap = &curbuf->b_ucmds;
6437 for (;;)
6438 {
6439 for (i = 0; i < gap->ga_len; ++i)
6440 {
6441 cmd = USER_CMD_GA(gap, i);
6442 cmp = STRCMP(eap->arg, cmd->uc_name);
6443 if (cmp <= 0)
6444 break;
6445 }
6446 if (gap == &ucmds || cmp == 0)
6447 break;
6448 gap = &ucmds;
6449 }
6450
6451 if (cmp != 0)
6452 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006453 semsg(_("E184: No such user-defined command: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 return;
6455 }
6456
6457 vim_free(cmd->uc_name);
6458 vim_free(cmd->uc_rep);
6459# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6460 vim_free(cmd->uc_compl_arg);
6461# endif
6462
6463 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464
6465 if (i < gap->ga_len)
6466 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6467}
6468
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006469/*
6470 * split and quote args for <f-args>
6471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006473uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474{
6475 char_u *buf;
6476 char_u *p;
6477 char_u *q;
6478 int len;
6479
6480 /* Precalculate length */
6481 p = arg;
6482 len = 2; /* Initial and final quotes */
6483
6484 while (*p)
6485 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006486 if (p[0] == '\\' && p[1] == '\\')
6487 {
6488 len += 2;
6489 p += 2;
6490 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006491 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 {
6493 len += 1;
6494 p += 2;
6495 }
6496 else if (*p == '\\' || *p == '"')
6497 {
6498 len += 2;
6499 p += 1;
6500 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006501 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 {
6503 p = skipwhite(p);
6504 if (*p == NUL)
6505 break;
6506 len += 3; /* "," */
6507 }
6508 else
6509 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006510 int charlen = (*mb_ptr2len)(p);
Bram Moolenaar13505972019-01-24 15:04:48 +01006511
Bram Moolenaardfef1542012-07-10 19:25:10 +02006512 len += charlen;
6513 p += charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 }
6515 }
6516
6517 buf = alloc(len + 1);
6518 if (buf == NULL)
6519 {
6520 *lenp = 0;
6521 return buf;
6522 }
6523
6524 p = arg;
6525 q = buf;
6526 *q++ = '"';
6527 while (*p)
6528 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006529 if (p[0] == '\\' && p[1] == '\\')
6530 {
6531 *q++ = '\\';
6532 *q++ = '\\';
6533 p += 2;
6534 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006535 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 {
6537 *q++ = p[1];
6538 p += 2;
6539 }
6540 else if (*p == '\\' || *p == '"')
6541 {
6542 *q++ = '\\';
6543 *q++ = *p++;
6544 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006545 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 {
6547 p = skipwhite(p);
6548 if (*p == NUL)
6549 break;
6550 *q++ = '"';
6551 *q++ = ',';
6552 *q++ = '"';
6553 }
6554 else
6555 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006556 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 }
6558 }
6559 *q++ = '"';
6560 *q = 0;
6561
6562 *lenp = len;
6563 return buf;
6564}
6565
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006566 static size_t
6567add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6568{
6569 size_t result;
6570
6571 result = STRLEN(mod_str);
6572 if (*multi_mods)
6573 result += 1;
6574 if (buf != NULL)
6575 {
6576 if (*multi_mods)
6577 STRCAT(buf, " ");
6578 STRCAT(buf, mod_str);
6579 }
6580
6581 *multi_mods = 1;
6582
6583 return result;
6584}
6585
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586/*
6587 * Check for a <> code in a user command.
6588 * "code" points to the '<'. "len" the length of the <> (inclusive).
6589 * "buf" is where the result is to be added.
6590 * "split_buf" points to a buffer used for splitting, caller should free it.
6591 * "split_len" is the length of what "split_buf" contains.
6592 * Returns the length of the replacement, which has been added to "buf".
6593 * Returns -1 if there was no match, and only the "<" has been copied.
6594 */
6595 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006596uc_check_code(
6597 char_u *code,
6598 size_t len,
6599 char_u *buf,
6600 ucmd_T *cmd, /* the user command we're expanding */
6601 exarg_T *eap, /* ex arguments */
6602 char_u **split_buf,
6603 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604{
6605 size_t result = 0;
6606 char_u *p = code + 1;
6607 size_t l = len - 2;
6608 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006609 enum {
6610 ct_ARGS,
6611 ct_BANG,
6612 ct_COUNT,
6613 ct_LINE1,
6614 ct_LINE2,
6615 ct_RANGE,
6616 ct_MODS,
6617 ct_REGISTER,
6618 ct_LT,
6619 ct_NONE
6620 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621
6622 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6623 {
6624 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6625 p += 2;
6626 l -= 2;
6627 }
6628
Bram Moolenaar371d5402006-03-20 21:47:49 +00006629 ++l;
6630 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006632 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006634 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006636 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006638 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006640 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006642 else if (STRNICMP(p, "range>", l) == 0)
6643 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006644 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006646 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006648 else if (STRNICMP(p, "mods>", l) == 0)
6649 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650
6651 switch (type)
6652 {
6653 case ct_ARGS:
6654 /* Simple case first */
6655 if (*eap->arg == NUL)
6656 {
6657 if (quote == 1)
6658 {
6659 result = 2;
6660 if (buf != NULL)
6661 STRCPY(buf, "''");
6662 }
6663 else
6664 result = 0;
6665 break;
6666 }
6667
6668 /* When specified there is a single argument don't split it.
6669 * Works for ":Cmd %" when % is "a b c". */
6670 if ((eap->argt & NOSPC) && quote == 2)
6671 quote = 1;
6672
6673 switch (quote)
6674 {
6675 case 0: /* No quoting, no splitting */
6676 result = STRLEN(eap->arg);
6677 if (buf != NULL)
6678 STRCPY(buf, eap->arg);
6679 break;
6680 case 1: /* Quote, but don't split */
6681 result = STRLEN(eap->arg) + 2;
6682 for (p = eap->arg; *p; ++p)
6683 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006684 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6685 /* DBCS can contain \ in a trail byte, skip the
6686 * double-byte character. */
6687 ++p;
6688 else
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006689 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 ++result;
6691 }
6692
6693 if (buf != NULL)
6694 {
6695 *buf++ = '"';
6696 for (p = eap->arg; *p; ++p)
6697 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006698 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6699 /* DBCS can contain \ in a trail byte, copy the
6700 * double-byte character to avoid escaping. */
6701 *buf++ = *p++;
6702 else
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006703 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 *buf++ = '\\';
6705 *buf++ = *p;
6706 }
6707 *buf = '"';
6708 }
6709
6710 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006711 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 /* This is hard, so only do it once, and cache the result */
6713 if (*split_buf == NULL)
6714 *split_buf = uc_split_args(eap->arg, split_len);
6715
6716 result = *split_len;
6717 if (buf != NULL && result != 0)
6718 STRCPY(buf, *split_buf);
6719
6720 break;
6721 }
6722 break;
6723
6724 case ct_BANG:
6725 result = eap->forceit ? 1 : 0;
6726 if (quote)
6727 result += 2;
6728 if (buf != NULL)
6729 {
6730 if (quote)
6731 *buf++ = '"';
6732 if (eap->forceit)
6733 *buf++ = '!';
6734 if (quote)
6735 *buf = '"';
6736 }
6737 break;
6738
6739 case ct_LINE1:
6740 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006741 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 case ct_COUNT:
6743 {
6744 char num_buf[20];
6745 long num = (type == ct_LINE1) ? eap->line1 :
6746 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006747 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6749 size_t num_len;
6750
6751 sprintf(num_buf, "%ld", num);
6752 num_len = STRLEN(num_buf);
6753 result = num_len;
6754
6755 if (quote)
6756 result += 2;
6757
6758 if (buf != NULL)
6759 {
6760 if (quote)
6761 *buf++ = '"';
6762 STRCPY(buf, num_buf);
6763 buf += num_len;
6764 if (quote)
6765 *buf = '"';
6766 }
6767
6768 break;
6769 }
6770
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006771 case ct_MODS:
6772 {
6773 int multi_mods = 0;
6774 typedef struct {
6775 int *varp;
6776 char *name;
6777 } mod_entry_T;
6778 static mod_entry_T mod_entries[] = {
6779#ifdef FEAT_BROWSE_CMD
6780 {&cmdmod.browse, "browse"},
6781#endif
6782#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6783 {&cmdmod.confirm, "confirm"},
6784#endif
6785 {&cmdmod.hide, "hide"},
6786 {&cmdmod.keepalt, "keepalt"},
6787 {&cmdmod.keepjumps, "keepjumps"},
6788 {&cmdmod.keepmarks, "keepmarks"},
6789 {&cmdmod.keeppatterns, "keeppatterns"},
6790 {&cmdmod.lockmarks, "lockmarks"},
6791 {&cmdmod.noswapfile, "noswapfile"},
6792 {NULL, NULL}
6793 };
6794 int i;
6795
6796 result = quote ? 2 : 0;
6797 if (buf != NULL)
6798 {
6799 if (quote)
6800 *buf++ = '"';
6801 *buf = '\0';
6802 }
6803
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006804 /* :aboveleft and :leftabove */
6805 if (cmdmod.split & WSP_ABOVE)
6806 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6807 /* :belowright and :rightbelow */
6808 if (cmdmod.split & WSP_BELOW)
6809 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6810 /* :botright */
6811 if (cmdmod.split & WSP_BOT)
6812 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006813
6814 /* the modifiers that are simple flags */
6815 for (i = 0; mod_entries[i].varp != NULL; ++i)
6816 if (*mod_entries[i].varp)
6817 result += add_cmd_modifier(buf, mod_entries[i].name,
6818 &multi_mods);
6819
6820 /* TODO: How to support :noautocmd? */
6821#ifdef HAVE_SANDBOX
6822 /* TODO: How to support :sandbox?*/
6823#endif
6824 /* :silent */
6825 if (msg_silent > 0)
6826 result += add_cmd_modifier(buf,
6827 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006828 /* :tab */
6829 if (cmdmod.tab > 0)
6830 result += add_cmd_modifier(buf, "tab", &multi_mods);
6831 /* :topleft */
6832 if (cmdmod.split & WSP_TOP)
6833 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006834 /* TODO: How to support :unsilent?*/
6835 /* :verbose */
6836 if (p_verbose > 0)
6837 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006838 /* :vertical */
6839 if (cmdmod.split & WSP_VERT)
6840 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006841 if (quote && buf != NULL)
6842 {
6843 buf += result - 2;
6844 *buf = '"';
6845 }
6846 break;
6847 }
6848
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 case ct_REGISTER:
6850 result = eap->regname ? 1 : 0;
6851 if (quote)
6852 result += 2;
6853 if (buf != NULL)
6854 {
6855 if (quote)
6856 *buf++ = '\'';
6857 if (eap->regname)
6858 *buf++ = eap->regname;
6859 if (quote)
6860 *buf = '\'';
6861 }
6862 break;
6863
6864 case ct_LT:
6865 result = 1;
6866 if (buf != NULL)
6867 *buf = '<';
6868 break;
6869
6870 default:
6871 /* Not recognized: just copy the '<' and return -1. */
6872 result = (size_t)-1;
6873 if (buf != NULL)
6874 *buf = '<';
6875 break;
6876 }
6877
6878 return result;
6879}
6880
6881 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006882do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883{
6884 char_u *buf;
6885 char_u *p;
6886 char_u *q;
6887
6888 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006889 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006890 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 size_t len, totlen;
6892
6893 size_t split_len = 0;
6894 char_u *split_buf = NULL;
6895 ucmd_T *cmd;
6896#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006897 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898#endif
6899
6900 if (eap->cmdidx == CMD_USER)
6901 cmd = USER_CMD(eap->useridx);
6902 else
6903 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6904
6905 /*
6906 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006907 * First round: "buf" is NULL, compute length, allocate "buf".
6908 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 */
6910 buf = NULL;
6911 for (;;)
6912 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006913 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006914 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006916
6917 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006919 start = vim_strchr(p, '<');
6920 if (start != NULL)
6921 end = vim_strchr(start + 1, '>');
6922 if (buf != NULL)
6923 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006924 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6925 ;
6926 if (*ksp == K_SPECIAL
6927 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006928 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6929# ifdef FEAT_GUI
6930 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6931# endif
6932 ))
6933 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006934 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006935 * KS_SPECIAL KE_FILLER, like for mappings, but
6936 * do_cmdline() doesn't handle that, so convert it back.
6937 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6938 len = ksp - p;
6939 if (len > 0)
6940 {
6941 mch_memmove(q, p, len);
6942 q += len;
6943 }
6944 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6945 p = ksp + 3;
6946 continue;
6947 }
6948 }
6949
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006950 /* break if no <item> is found */
Bram Moolenaara850a712009-01-28 14:42:59 +00006951 if (start == NULL || end == NULL)
6952 break;
6953
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 /* Include the '>' */
6955 ++end;
6956
6957 /* Take everything up to the '<' */
6958 len = start - p;
6959 if (buf == NULL)
6960 totlen += len;
6961 else
6962 {
6963 mch_memmove(q, p, len);
6964 q += len;
6965 }
6966
6967 len = uc_check_code(start, end - start, q, cmd, eap,
6968 &split_buf, &split_len);
6969 if (len == (size_t)-1)
6970 {
6971 /* no match, continue after '<' */
6972 p = start + 1;
6973 len = 1;
6974 }
6975 else
6976 p = end;
6977 if (buf == NULL)
6978 totlen += len;
6979 else
6980 q += len;
6981 }
6982 if (buf != NULL) /* second time here, finished */
6983 {
6984 STRCPY(q, p);
6985 break;
6986 }
6987
6988 totlen += STRLEN(p); /* Add on the trailing characters */
6989 buf = alloc((unsigned)(totlen + 1));
6990 if (buf == NULL)
6991 {
6992 vim_free(split_buf);
6993 return;
6994 }
6995 }
6996
6997#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006998 current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999#endif
7000 (void)do_cmdline(buf, eap->getline, eap->cookie,
7001 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
7002#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007003 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004#endif
7005 vim_free(buf);
7006 vim_free(split_buf);
7007}
7008
7009# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7010 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007011get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012{
7013 return get_user_commands(NULL, idx - (int)CMD_SIZE);
7014}
7015
7016/*
7017 * Function given to ExpandGeneric() to obtain the list of user command names.
7018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007020get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021{
7022 if (idx < curbuf->b_ucmds.ga_len)
7023 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
7024 idx -= curbuf->b_ucmds.ga_len;
7025 if (idx < ucmds.ga_len)
7026 return USER_CMD(idx)->uc_name;
7027 return NULL;
7028}
7029
7030/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007031 * Function given to ExpandGeneric() to obtain the list of user address type names.
7032 */
7033 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007034get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007035{
7036 return (char_u *)addr_type_complete[idx].name;
7037}
7038
7039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 * Function given to ExpandGeneric() to obtain the list of user command
7041 * attributes.
7042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007044get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045{
7046 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007047 {"addr", "bang", "bar", "buffer", "complete",
7048 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007050 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 return NULL;
7052 return (char_u *)user_cmd_flags[idx];
7053}
7054
7055/*
7056 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007059get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060{
7061 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7062
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007063 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 return NULL;
7065 return (char_u *)user_cmd_nargs[idx];
7066}
7067
7068/*
7069 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007072get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073{
7074 return (char_u *)command_complete[idx].name;
7075}
7076# endif /* FEAT_CMDL_COMPL */
7077
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007078/*
7079 * Parse address type argument
7080 */
7081 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007082parse_addr_type_arg(
7083 char_u *value,
7084 int vallen,
7085 long *argt,
7086 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007087{
7088 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007089
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007090 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7091 {
7092 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7093 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7094 if (a && b)
7095 {
7096 *addr_type_arg = addr_type_complete[i].expand;
7097 break;
7098 }
7099 }
7100
7101 if (addr_type_complete[i].expand == -1)
7102 {
7103 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007104
Bram Moolenaar1c465442017-03-12 20:10:05 +01007105 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007106 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007107 err[i] = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007108 semsg(_("E180: Invalid address type value: %s"), err);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007109 return FAIL;
7110 }
7111
7112 if (*addr_type_arg != ADDR_LINES)
7113 *argt |= NOTADR;
7114
7115 return OK;
7116}
7117
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118#endif /* FEAT_USR_CMDS */
7119
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007120#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7121/*
7122 * Parse a completion argument "value[vallen]".
7123 * The detected completion goes in "*complp", argument type in "*argt".
7124 * When there is an argument, for function and user defined completion, it's
7125 * copied to allocated memory and stored in "*compl_arg".
7126 * Returns FAIL if something is wrong.
7127 */
7128 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007129parse_compl_arg(
7130 char_u *value,
7131 int vallen,
7132 int *complp,
7133 long *argt,
7134 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007135{
7136 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007137# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007138 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007139# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007140 int i;
7141 int valend = vallen;
7142
7143 /* Look for any argument part - which is the part after any ',' */
7144 for (i = 0; i < vallen; ++i)
7145 {
7146 if (value[i] == ',')
7147 {
7148 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007149# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007150 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007151# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007152 valend = i;
7153 break;
7154 }
7155 }
7156
7157 for (i = 0; command_complete[i].expand != 0; ++i)
7158 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007159 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007160 && STRNCMP(value, command_complete[i].name, valend) == 0)
7161 {
7162 *complp = command_complete[i].expand;
7163 if (command_complete[i].expand == EXPAND_BUFFERS)
7164 *argt |= BUFNAME;
7165 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7166 || command_complete[i].expand == EXPAND_FILES)
7167 *argt |= XFILE;
7168 break;
7169 }
7170 }
7171
7172 if (command_complete[i].expand == 0)
7173 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007174 semsg(_("E180: Invalid complete value: %s"), value);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007175 return FAIL;
7176 }
7177
7178# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7179 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7180 && arg != NULL)
7181# else
7182 if (arg != NULL)
7183# endif
7184 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007185 emsg(_("E468: Completion argument only allowed for custom completion"));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007186 return FAIL;
7187 }
7188
7189# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7190 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7191 && arg == NULL)
7192 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007193 emsg(_("E467: Custom completion requires a function argument"));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007194 return FAIL;
7195 }
7196
7197 if (arg != NULL)
7198 *compl_arg = vim_strnsave(arg, (int)arglen);
7199# endif
7200 return OK;
7201}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007202
7203 int
7204cmdcomplete_str_to_type(char_u *complete_str)
7205{
7206 int i;
7207
7208 for (i = 0; command_complete[i].expand != 0; ++i)
7209 if (STRCMP(complete_str, command_complete[i].name) == 0)
7210 return command_complete[i].expand;
7211
7212 return EXPAND_NOTHING;
7213}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007214#endif
7215
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007217ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218{
Bram Moolenaare6850792010-05-14 15:28:44 +02007219 if (*eap->arg == NUL)
7220 {
7221#ifdef FEAT_EVAL
7222 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7223 char_u *p = NULL;
7224
7225 if (expr != NULL)
7226 {
7227 ++emsg_off;
7228 p = eval_to_string(expr, NULL, FALSE);
7229 --emsg_off;
7230 vim_free(expr);
7231 }
7232 if (p != NULL)
7233 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007234 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02007235 vim_free(p);
7236 }
7237 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007238 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02007239#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007240 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02007241#endif
7242 }
7243 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007244 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01007245
7246#ifdef FEAT_VTP
7247 else if (has_vtp_working())
7248 {
7249 // background color change requires clear + redraw
7250 update_screen(CLEAR);
7251 redrawcmd();
7252 }
7253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254}
7255
7256 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007257ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258{
7259 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01007260 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 do_highlight(eap->arg, eap->forceit, FALSE);
7262}
7263
7264
7265/*
7266 * Call this function if we thought we were going to exit, but we won't
7267 * (because of an error). May need to restore the terminal mode.
7268 */
7269 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007270not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271{
7272 exiting = FALSE;
7273 settmode(TMODE_RAW);
7274}
7275
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007276 static int
7277before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7278{
7279 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7280
7281 /* Bail out when autocommands closed the window.
7282 * Refuse to quit when the buffer in the last window is being closed (can
7283 * only happen in autocommands). */
7284 if (!win_valid(wp)
7285 || curbuf_locked()
7286 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7287 return TRUE;
7288
7289 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7290 {
7291 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7292 /* Refuse to quit when locked or when the buffer in the last window is
7293 * being closed (can only happen in autocommands). */
7294 if (curbuf_locked()
7295 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7296 return TRUE;
7297 }
7298
7299 return FALSE;
7300}
7301
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007303 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007304 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 */
7306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007307ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007309 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007310
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311#ifdef FEAT_CMDWIN
7312 if (cmdwin_type != 0)
7313 {
7314 cmdwin_result = Ctrl_C;
7315 return;
7316 }
7317#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007318 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007319 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007320 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007321 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007322 return;
7323 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007324 if (eap->addr_count > 0)
7325 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007326 int wnr = eap->line2;
7327
7328 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7329 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007330 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007331 }
7332 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007333 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007334
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007335 /* Refuse to quit when locked. */
7336 if (curbuf_locked())
7337 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007338
7339 /* Trigger QuitPre and maybe ExitPre */
7340 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007341 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342
7343#ifdef FEAT_NETBEANS_INTG
7344 netbeansForcedQuit = eap->forceit;
7345#endif
7346
7347 /*
7348 * If there are more files or windows we won't exit.
7349 */
7350 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7351 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007352 if ((!buf_hide(wp->w_buffer)
7353 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007354 | (eap->forceit ? CCGD_FORCEIT : 0)
7355 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007357 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 {
7359 not_exiting();
7360 }
7361 else
7362 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007363 /* quit last window
7364 * Note: only_one_window() returns true, even so a help window is
7365 * still open. In that case only quit, if no address has been
7366 * specified. Example:
7367 * :h|wincmd w|1q - don't quit
7368 * :h|wincmd w|q - quit
7369 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007370 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007372 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007373#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007377 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 }
7379}
7380
7381/*
7382 * ":cquit".
7383 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007385ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386{
7387 getout(1); /* this does not always pass on the exit code to the Manx
7388 compiler. why? */
7389}
7390
7391/*
7392 * ":qall": try to quit all windows
7393 */
7394 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007395ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396{
7397# ifdef FEAT_CMDWIN
7398 if (cmdwin_type != 0)
7399 {
7400 if (eap->forceit)
7401 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7402 else
7403 cmdwin_result = K_XF2;
7404 return;
7405 }
7406# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007407
7408 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007409 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007410 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007411 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007412 return;
7413 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007414
7415 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007416 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007417
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007419 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 getout(0);
7421 not_exiting();
7422}
7423
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424/*
7425 * ":close": close current window, unless it is the last one
7426 */
7427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007428ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007430 win_T *win;
7431 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007432#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007434 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007436#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007437 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007438 {
7439 if (eap->addr_count == 0)
7440 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02007441 else
7442 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007443 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007444 {
7445 winnr++;
7446 if (winnr == eap->line2)
7447 break;
7448 }
7449 if (win == NULL)
7450 win = lastwin;
7451 ex_win_close(eap->forceit, win, NULL);
7452 }
7453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454}
7455
Bram Moolenaar4033c552017-09-16 20:54:51 +02007456#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007457/*
7458 * ":pclose": Close any preview window.
7459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007461ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007462{
7463 win_T *win;
7464
Bram Moolenaar29323592016-07-24 22:04:11 +02007465 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007466 if (win->w_p_pvw)
7467 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007468 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007469 break;
7470 }
7471}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007472#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007473
Bram Moolenaarf740b292006-02-16 22:11:02 +00007474/*
7475 * Close window "win" and take care of handling closing the last window for a
7476 * modified buffer.
7477 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007478 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007479ex_win_close(
7480 int forceit,
7481 win_T *win,
7482 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483{
7484 int need_hide;
7485 buf_T *buf = win->w_buffer;
7486
7487 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007488 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007490#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 if ((p_confirm || cmdmod.confirm) && p_write)
7492 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007493 bufref_T bufref;
7494
7495 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007497 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 return;
7499 need_hide = FALSE;
7500 }
7501 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007504 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 return;
7506 }
7507 }
7508
Bram Moolenaar4033c552017-09-16 20:54:51 +02007509#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007511#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007512
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007514 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007515 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007516 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007517 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518}
7519
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007521 * Handle the argument for a tabpage related ex command.
7522 * Returns a tabpage number.
7523 * When an error is encountered then eap->errmsg is set.
7524 */
7525 static int
7526get_tabpage_arg(exarg_T *eap)
7527{
7528 int tab_number;
7529 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7530
7531 if (eap->arg && *eap->arg != NUL)
7532 {
7533 char_u *p = eap->arg;
7534 char_u *p_save;
7535 int relative = 0; /* argument +N/-N means: go to N places to the
7536 * right/left relative to the current position. */
7537
7538 if (*p == '-')
7539 {
7540 relative = -1;
7541 p++;
7542 }
7543 else if (*p == '+')
7544 {
7545 relative = 1;
7546 p++;
7547 }
7548
7549 p_save = p;
7550 tab_number = getdigits(&p);
7551
7552 if (relative == 0)
7553 {
7554 if (STRCMP(p, "$") == 0)
7555 tab_number = LAST_TAB_NR;
7556 else if (p == p_save || *p_save == '-' || *p != NUL
7557 || tab_number > LAST_TAB_NR)
7558 {
7559 /* No numbers as argument. */
7560 eap->errmsg = e_invarg;
7561 goto theend;
7562 }
7563 }
7564 else
7565 {
7566 if (*p_save == NUL)
7567 tab_number = 1;
7568 else if (p == p_save || *p_save == '-' || *p != NUL
7569 || tab_number == 0)
7570 {
7571 /* No numbers as argument. */
7572 eap->errmsg = e_invarg;
7573 goto theend;
7574 }
7575 tab_number = tab_number * relative + tabpage_index(curtab);
7576 if (!unaccept_arg0 && relative == -1)
7577 --tab_number;
7578 }
7579 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7580 eap->errmsg = e_invarg;
7581 }
7582 else if (eap->addr_count > 0)
7583 {
7584 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007585 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007586 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007587 tab_number = 0;
7588 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007589 else
7590 {
7591 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01007592 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01007593 {
7594 --tab_number;
7595 if (tab_number < unaccept_arg0)
7596 eap->errmsg = e_invarg;
7597 }
7598 }
7599 }
7600 else
7601 {
7602 switch (eap->cmdidx)
7603 {
7604 case CMD_tabnext:
7605 tab_number = tabpage_index(curtab) + 1;
7606 if (tab_number > LAST_TAB_NR)
7607 tab_number = 1;
7608 break;
7609 case CMD_tabmove:
7610 tab_number = LAST_TAB_NR;
7611 break;
7612 default:
7613 tab_number = tabpage_index(curtab);
7614 }
7615 }
7616
7617theend:
7618 return tab_number;
7619}
7620
7621/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007622 * ":tabclose": close current tab page, unless it is the last one.
7623 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 */
7625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007626ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007628 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007629 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007630
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007631# ifdef FEAT_CMDWIN
7632 if (cmdwin_type != 0)
7633 cmdwin_result = K_IGNORE;
7634 else
7635# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007636 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007637 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007638 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007640 tab_number = get_tabpage_arg(eap);
7641 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007642 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007643 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007644 if (tp == NULL)
7645 {
7646 beep_flush();
7647 return;
7648 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007649 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007650 {
7651 tabpage_close_other(tp, eap->forceit);
7652 return;
7653 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007654 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007655 tabpage_close(eap->forceit);
7656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007658}
7659
7660/*
7661 * ":tabonly": close all tab pages except the current one
7662 */
7663 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007664ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007665{
7666 tabpage_T *tp;
7667 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007668 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007669
7670# ifdef FEAT_CMDWIN
7671 if (cmdwin_type != 0)
7672 cmdwin_result = K_IGNORE;
7673 else
7674# endif
7675 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007676 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007677 else
7678 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007679 tab_number = get_tabpage_arg(eap);
7680 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007681 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007682 goto_tabpage(tab_number);
7683 /* Repeat this up to a 1000 times, because autocommands may
7684 * mess up the lists. */
7685 for (done = 0; done < 1000; ++done)
7686 {
7687 FOR_ALL_TABPAGES(tp)
7688 if (tp->tp_topframe != topframe)
7689 {
7690 tabpage_close_other(tp, eap->forceit);
7691 /* if we failed to close it quit */
7692 if (valid_tabpage(tp))
7693 done = 1000;
7694 /* start over, "tp" is now invalid */
7695 break;
7696 }
7697 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007698 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007699 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007700 }
7701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703
7704/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007705 * Close the current tab page.
7706 */
7707 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007708tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007709{
7710 /* First close all the windows but the current one. If that worked then
7711 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007712 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007713 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007714 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007715 ex_win_close(forceit, curwin, NULL);
7716# ifdef FEAT_GUI
7717 need_mouse_correct = TRUE;
7718# endif
7719}
7720
7721/*
7722 * Close tab page "tp", which is not the current tab page.
7723 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007724 * Also takes care of the tab pages line disappearing when closing the
7725 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007726 */
7727 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007728tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007729{
7730 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007731 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007732 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007733
7734 /* Limit to 1000 windows, autocommands may add a window while we close
7735 * one. OK, so I'm paranoid... */
7736 while (++done < 1000)
7737 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007738 wp = tp->tp_firstwin;
7739 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007740
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007741 /* Autocommands may delete the tab page under our fingers and we may
7742 * fail to close a window with a modified buffer. */
7743 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007744 break;
7745 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007746
Bram Moolenaar29323592016-07-24 22:04:11 +02007747 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007748
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007749 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007750 if (h != tabline_height())
7751 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007752}
7753
7754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 * ":only".
7756 */
7757 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007758ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007760 win_T *wp;
7761 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762# ifdef FEAT_GUI
7763 need_mouse_correct = TRUE;
7764# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007765 if (eap->addr_count > 0)
7766 {
7767 wnr = eap->line2;
7768 for (wp = firstwin; --wnr > 0; )
7769 {
7770 if (wp->w_next == NULL)
7771 break;
7772 else
7773 wp = wp->w_next;
7774 }
7775 win_goto(wp);
7776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 close_others(TRUE, eap->forceit);
7778}
7779
7780/*
7781 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007782 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007784 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007785ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786{
7787 if (eap->addr_count == 0)
7788 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007789 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791
7792 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007793ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007795 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007796 if (!eap->skip)
7797 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007798#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007799 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007800#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007801 if (eap->addr_count == 0)
7802 win_close(curwin, FALSE); /* don't free buffer */
7803 else
7804 {
7805 int winnr = 0;
7806 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007807
Bram Moolenaar2256c992016-11-15 21:17:07 +01007808 FOR_ALL_WINDOWS(win)
7809 {
7810 winnr++;
7811 if (winnr == eap->line2)
7812 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007813 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007814 if (win == NULL)
7815 win = lastwin;
7816 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 }
7819}
7820
7821/*
7822 * ":stop" and ":suspend": Suspend Vim.
7823 */
7824 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007825ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826{
7827 /*
7828 * Disallow suspending for "rvim".
7829 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007830 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 {
7832 if (!eap->forceit)
7833 autowrite_all();
7834 windgoto((int)Rows - 1, 0);
7835 out_char('\n');
7836 out_flush();
7837 stoptermcap();
7838 out_flush(); /* needed for SUN to restore xterm buffer */
7839#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02007840 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841#endif
7842 ui_suspend(); /* call machine specific function */
7843#ifdef FEAT_TITLE
7844 maketitle();
7845 resettitle(); /* force updating the title */
7846#endif
7847 starttermcap();
7848 scroll_start(); /* scroll screen before redrawing */
7849 redraw_later_clear();
7850 shell_resized(); /* may have resized window */
7851 }
7852}
7853
7854/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007855 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 */
7857 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007858ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
7860#ifdef FEAT_CMDWIN
7861 if (cmdwin_type != 0)
7862 {
7863 cmdwin_result = Ctrl_C;
7864 return;
7865 }
7866#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007867 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007868 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007869 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007870 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007871 return;
7872 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007873
7874 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007875 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876
7877 /*
7878 * if more files or windows we won't exit
7879 */
7880 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7881 exiting = TRUE;
7882 if ( ((eap->cmdidx == CMD_wq
7883 || curbufIsChanged())
7884 && do_write(eap) == FAIL)
7885 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007886 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {
7888 not_exiting();
7889 }
7890 else
7891 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007894 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895# ifdef FEAT_GUI
7896 need_mouse_correct = TRUE;
7897# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007898 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007899 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 }
7901}
7902
7903/*
7904 * ":print", ":list", ":number".
7905 */
7906 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007907ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007909 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007910 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007911 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007913 for ( ;!got_int; ui_breakcheck())
7914 {
7915 print_line(eap->line1,
7916 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7917 || (eap->flags & EXFLAG_NR)),
7918 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7919 if (++eap->line1 > eap->line2)
7920 break;
7921 out_flush(); /* show one line at a time */
7922 }
7923 setpcmark();
7924 /* put cursor at last line */
7925 curwin->w_cursor.lnum = eap->line2;
7926 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 }
7928
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930}
7931
7932#ifdef FEAT_BYTEOFF
7933 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007934ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935{
7936 goto_byte(eap->line2);
7937}
7938#endif
7939
7940/*
7941 * ":shell".
7942 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007944ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945{
7946 do_shell(NULL, 0);
7947}
7948
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007949#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007951static int drop_busy = FALSE;
7952static int drop_filec;
7953static char_u **drop_filev = NULL;
7954static int drop_split;
7955static void (*drop_callback)(void *);
7956static void *drop_cookie;
7957
7958 static void
7959handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960{
7961 exarg_T ea;
7962 int save_msg_scroll = msg_scroll;
7963
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007964 // Setting the argument list may cause screen updates and being called
7965 // recursively. Avoid that by setting drop_busy.
7966 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967
7968 /* Check whether the current buffer is changed. If so, we will need
7969 * to split the current window or data could be lost.
7970 * We don't need to check if the 'hidden' option is set, as in this
7971 * case the buffer won't be lost.
7972 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007973 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 {
7975 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007976 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 --emsg_off;
7978 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007979 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 if (win_split(0, 0) == FAIL)
7982 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007983 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984
7985 /* When splitting the window, create a new alist. Otherwise the
7986 * existing one is overwritten. */
7987 alist_unlink(curwin->w_alist);
7988 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 }
7990
7991 /*
7992 * Set up the new argument list.
7993 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007994 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995
7996 /*
7997 * Move to the first file.
7998 */
7999 /* Fake up a minimal "next" command for do_argfile() */
8000 vim_memset(&ea, 0, sizeof(ea));
8001 ea.cmd = (char_u *)"next";
8002 do_argfile(&ea, 0);
8003
8004 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
8005 * mode that is not needed here. */
8006 need_start_insertmode = FALSE;
8007
8008 /* Restore msg_scroll, otherwise a following command may cause scrolling
8009 * unexpectedly. The screen will be redrawn by the caller, thus
8010 * msg_scroll being set by displaying a message is irrelevant. */
8011 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02008012
8013 if (drop_callback != NULL)
8014 drop_callback(drop_cookie);
8015
8016 drop_filev = NULL;
8017 drop_busy = FALSE;
8018}
8019
8020/*
8021 * Handle a file drop. The code is here because a drop is *nearly* like an
8022 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008023 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02008024 * code is very similar to :args and hence needs access to a lot of the static
8025 * functions in this file.
8026 *
8027 * The "filev" list must have been allocated using alloc(), as should each item
8028 * in the list. This function takes over responsibility for freeing the "filev"
8029 * list.
8030 */
8031 void
8032handle_drop(
8033 int filec, // the number of files dropped
8034 char_u **filev, // the list of files dropped
8035 int split, // force splitting the window
8036 void (*callback)(void *), // to be called after setting the argument
8037 // list
8038 void *cookie) // argument for "callback" (allocated)
8039{
8040 // Cannot handle recursive drops, finish the pending one.
8041 if (drop_busy)
8042 {
8043 FreeWild(filec, filev);
8044 vim_free(cookie);
8045 return;
8046 }
8047
8048 // When calling handle_drop() more than once in a row we only use the last
8049 // one.
8050 if (drop_filev != NULL)
8051 {
8052 FreeWild(drop_filec, drop_filev);
8053 vim_free(drop_cookie);
8054 }
8055
8056 drop_filec = filec;
8057 drop_filev = filev;
8058 drop_split = split;
8059 drop_callback = callback;
8060 drop_cookie = cookie;
8061
8062 // Postpone this when:
8063 // - editing the command line
8064 // - not possible to change the current buffer
8065 // - updating the screen
8066 // As it may change buffers and window structures that are in use and cause
8067 // freed memory to be used.
8068 if (text_locked() || curbuf_locked() || updating_screen)
8069 return;
8070
8071 handle_drop_internal();
8072}
8073
8074/*
8075 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
8076 * reset: Handle a postponed drop.
8077 */
8078 void
8079handle_any_postponed_drop(void)
8080{
8081 if (!drop_busy && drop_filev != NULL
8082 && !text_locked() && !curbuf_locked() && !updating_screen)
8083 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084}
8085#endif
8086
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087/*
8088 * Clear an argument list: free all file names and reset it to zero entries.
8089 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008090 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008091alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092{
8093 while (--al->al_ga.ga_len >= 0)
8094 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8095 ga_clear(&al->al_ga);
8096}
8097
8098/*
8099 * Init an argument list.
8100 */
8101 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008102alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103{
8104 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8105}
8106
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107/*
8108 * Remove a reference from an argument list.
8109 * Ignored when the argument list is the global one.
8110 * If the argument list is no longer used by any window, free it.
8111 */
8112 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008113alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114{
8115 if (al != &global_alist && --al->al_refcount <= 0)
8116 {
8117 alist_clear(al);
8118 vim_free(al);
8119 }
8120}
8121
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122/*
8123 * Create a new argument list and use it for the current window.
8124 */
8125 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008126alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127{
8128 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8129 if (curwin->w_alist == NULL)
8130 {
8131 curwin->w_alist = &global_alist;
8132 ++global_alist.al_refcount;
8133 }
8134 else
8135 {
8136 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008137 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 alist_init(curwin->w_alist);
8139 }
8140}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008142#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143/*
8144 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008145 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8146 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 */
8148 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008149alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150{
8151 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008152 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 char_u **new_arg_files;
8154 int new_arg_file_count;
8155 char_u *save_p_su = p_su;
8156 int i;
8157
8158 /* Don't use 'suffixes' here. This should work like the shell did the
8159 * expansion. Also, the vimrc file isn't read yet, thus the user
8160 * can't set the options. */
8161 p_su = empty_option;
8162 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8163 if (old_arg_files != NULL)
8164 {
8165 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008166 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8167 old_arg_count = GARGCOUNT;
8168 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008170 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 && new_arg_file_count > 0)
8172 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008173 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8174 TRUE, fnum_list, fnum_len);
8175 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 }
8178 p_su = save_p_su;
8179}
8180#endif
8181
8182/*
8183 * Set the argument list for the current window.
8184 * Takes over the allocated files[] and the allocated fnames in it.
8185 */
8186 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008187alist_set(
8188 alist_T *al,
8189 int count,
8190 char_u **files,
8191 int use_curbuf,
8192 int *fnum_list,
8193 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194{
8195 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008196 static int recursive = 0;
8197
8198 if (recursive)
8199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008200 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008201 return;
8202 }
8203 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204
8205 alist_clear(al);
8206 if (ga_grow(&al->al_ga, count) == OK)
8207 {
8208 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008209 {
8210 if (got_int)
8211 {
8212 /* When adding many buffers this can take a long time. Allow
8213 * interrupting here. */
8214 while (i < count)
8215 vim_free(files[i++]);
8216 break;
8217 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008218
8219 /* May set buffer name of a buffer previously used for the
8220 * argument list, so that it's re-used by alist_add. */
8221 if (fnum_list != NULL && i < fnum_len)
8222 buf_set_name(fnum_list[i], files[i]);
8223
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008225 ui_breakcheck();
8226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 vim_free(files);
8228 }
8229 else
8230 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008233
8234 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235}
8236
8237/*
8238 * Add file "fname" to argument list "al".
8239 * "fname" must have been allocated and "al" must have been checked for room.
8240 */
8241 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008242alist_add(
8243 alist_T *al,
8244 char_u *fname,
8245 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246{
8247 if (fname == NULL) /* don't add NULL file names */
8248 return;
8249#ifdef BACKSLASH_IN_FILENAME
8250 slash_adjust(fname);
8251#endif
8252 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8253 if (set_fnum > 0)
8254 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8255 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8256 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257}
8258
8259#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8260/*
8261 * Adjust slashes in file names. Called after 'shellslash' was set.
8262 */
8263 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008264alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265{
8266 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008268 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269
8270 for (i = 0; i < GARGCOUNT; ++i)
8271 if (GARGLIST[i].ae_fname != NULL)
8272 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008273 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 if (wp->w_alist != &global_alist)
8275 for (i = 0; i < WARGCOUNT(wp); ++i)
8276 if (WARGLIST(wp)[i].ae_fname != NULL)
8277 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278}
8279#endif
8280
8281/*
8282 * ":preserve".
8283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008285ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008287 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 ml_preserve(curbuf, TRUE);
8289}
8290
8291/*
8292 * ":recover".
8293 */
8294 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008295ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296{
8297 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8298 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008299 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8300 | CCGD_MULTWIN
8301 | (eap->forceit ? CCGD_FORCEIT : 0)
8302 | CCGD_EXCMD)
8303
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 && (*eap->arg == NUL
8305 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8306 ml_recover();
8307 recoverymode = FALSE;
8308}
8309
8310/*
8311 * Command modifier used in a wrong way.
8312 */
8313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008314ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315{
8316 eap->errmsg = e_invcmd;
8317}
8318
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319/*
8320 * :sview [+command] file split window with new file, read-only
8321 * :split [[+command] file] split window with current or new file
8322 * :vsplit [[+command] file] split window vertically with current or new file
8323 * :new [[+command] file] split window with no or new file
8324 * :vnew [[+command] file] split vertically window with no or new file
8325 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008326 *
8327 * :tabedit open new Tab page with empty window
8328 * :tabedit [+command] file open new Tab page and edit "file"
8329 * :tabnew [[+command] file] just like :tabedit
8330 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 */
8332 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008333ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008335 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008336#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008338#endif
8339#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008341#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02008342 int use_tab = eap->cmdidx == CMD_tabedit
8343 || eap->cmdidx == CMD_tabfind
8344 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345
Bram Moolenaar4033c552017-09-16 20:54:51 +02008346#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349
Bram Moolenaar4033c552017-09-16 20:54:51 +02008350#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008352 * quickfix windows. But it's OK when doing ":tab split". */
8353 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 {
8355 if (eap->cmdidx == CMD_split)
8356 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 if (eap->cmdidx == CMD_vsplit)
8358 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361
Bram Moolenaar4033c552017-09-16 20:54:51 +02008362#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008363 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {
8365 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8366 FNAME_MESS, TRUE, curbuf->b_ffname);
8367 if (fname == NULL)
8368 goto theend;
8369 eap->arg = fname;
8370 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008371# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008372 else
8373# endif
8374#endif
8375#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 && eap->cmdidx != CMD_new)
8379 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008380 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008381# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008382 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008383# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008384 au_has_group((char_u *)"FileExplorer"))
8385 {
8386 /* No browsing supported but we do have the file explorer:
8387 * Edit the directory. */
8388 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8389 eap->arg = (char_u *)".";
8390 }
8391 else
8392 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02008393 fname = do_browse(0, (char_u *)(use_tab
8394 ? _("Edit File in new tab page")
8395 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008397 if (fname == NULL)
8398 goto theend;
8399 eap->arg = fname;
8400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 }
8402 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008405 /*
8406 * Either open new tab page or split the window.
8407 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02008408 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008409 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008410 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8411 : eap->addr_count == 0 ? 0
8412 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008413 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008414 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008415
8416 /* set the alternate buffer for the window we came from */
8417 if (curwin != old_curwin
8418 && win_valid(old_curwin)
8419 && old_curwin->w_buffer != curbuf
8420 && !cmdmod.keepalt)
8421 old_curwin->w_alt_fnum = curbuf->b_fnum;
8422 }
8423 }
8424 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8426 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 /* Reset 'scrollbind' when editing another file, but keep it when
8428 * doing ":split" without arguments. */
8429 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008430# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008432# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008434 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 else
8436 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 do_exedit(eap, old_curwin);
8438 }
8439
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008440# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008442# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008444# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445theend:
8446 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008447# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008449
8450/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008451 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008452 */
8453 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008454tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008455{
8456 exarg_T ea;
8457
8458 vim_memset(&ea, 0, sizeof(ea));
8459 ea.cmdidx = CMD_tabnew;
8460 ea.cmd = (char_u *)"tabn";
8461 ea.arg = (char_u *)"";
8462 ex_splitview(&ea);
8463}
8464
8465/*
8466 * :tabnext command
8467 */
8468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008469ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008470{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008471 int tab_number;
8472
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008473 switch (eap->cmdidx)
8474 {
8475 case CMD_tabfirst:
8476 case CMD_tabrewind:
8477 goto_tabpage(1);
8478 break;
8479 case CMD_tablast:
8480 goto_tabpage(9999);
8481 break;
8482 case CMD_tabprevious:
8483 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008484 if (eap->arg && *eap->arg != NUL)
8485 {
8486 char_u *p = eap->arg;
8487 char_u *p_save = p;
8488
8489 tab_number = getdigits(&p);
8490 if (p == p_save || *p_save == '-' || *p != NUL
8491 || tab_number == 0)
8492 {
8493 /* No numbers as argument. */
8494 eap->errmsg = e_invarg;
8495 return;
8496 }
8497 }
8498 else
8499 {
8500 if (eap->addr_count == 0)
8501 tab_number = 1;
8502 else
8503 {
8504 tab_number = eap->line2;
8505 if (tab_number < 1)
8506 {
8507 eap->errmsg = e_invrange;
8508 return;
8509 }
8510 }
8511 }
8512 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008513 break;
8514 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008515 tab_number = get_tabpage_arg(eap);
8516 if (eap->errmsg == NULL)
8517 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008518 break;
8519 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008520}
8521
8522/*
8523 * :tabmove command
8524 */
8525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008526ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008527{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008528 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008529
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008530 tab_number = get_tabpage_arg(eap);
8531 if (eap->errmsg == NULL)
8532 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008533}
8534
8535/*
8536 * :tabs command: List tabs and their contents.
8537 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008538 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008539ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008540{
8541 tabpage_T *tp;
8542 win_T *wp;
8543 int tabcount = 1;
8544
8545 msg_start();
8546 msg_scroll = TRUE;
8547 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8548 {
8549 msg_putchar('\n');
8550 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008551 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008552 out_flush(); /* output one line at a time */
8553 ui_breakcheck();
8554
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008555 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008556 wp = firstwin;
8557 else
8558 wp = tp->tp_firstwin;
8559 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8560 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008561 msg_putchar('\n');
8562 msg_putchar(wp == curwin ? '>' : ' ');
8563 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008564 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8565 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008566 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008567 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008568 else
8569 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8570 IObuff, IOSIZE, TRUE);
8571 msg_outtrans(IObuff);
8572 out_flush(); /* output one line at a time */
8573 ui_breakcheck();
8574 }
8575 }
8576}
8577
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578/*
8579 * ":mode": Set screen mode.
8580 * If no argument given, just get the screen size and redraw.
8581 */
8582 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008583ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584{
8585 if (*eap->arg == NUL)
8586 shell_resized();
8587 else
8588 mch_screenmode(eap->arg);
8589}
8590
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591/*
8592 * ":resize".
8593 * set, increment or decrement current window height
8594 */
8595 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008596ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597{
8598 int n;
8599 win_T *wp = curwin;
8600
8601 if (eap->addr_count > 0)
8602 {
8603 n = eap->line2;
8604 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8605 ;
8606 }
8607
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008608# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008610# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 if (cmdmod.split & WSP_VERT)
8613 {
8614 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008615 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8617 n = 9999;
8618 win_setwidth_win((int)n, wp);
8619 }
8620 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 {
8622 if (*eap->arg == '-' || *eap->arg == '+')
8623 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008624 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 n = 9999;
8626 win_setheight_win((int)n, wp);
8627 }
8628}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629
8630/*
8631 * ":find [+command] <file>" command.
8632 */
8633 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008634ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635{
8636#ifdef FEAT_SEARCHPATH
8637 char_u *fname;
8638 int count;
8639
8640 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8641 TRUE, curbuf->b_ffname);
8642 if (eap->addr_count > 0)
8643 {
8644 /* Repeat finding the file "count" times. This matters when it
8645 * appears several times in the path. */
8646 count = eap->line2;
8647 while (fname != NULL && --count > 0)
8648 {
8649 vim_free(fname);
8650 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8651 FALSE, curbuf->b_ffname);
8652 }
8653 }
8654
8655 if (fname != NULL)
8656 {
8657 eap->arg = fname;
8658#endif
8659 do_exedit(eap, NULL);
8660#ifdef FEAT_SEARCHPATH
8661 vim_free(fname);
8662 }
8663#endif
8664}
8665
8666/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008667 * ":open" simulation: for now just work like ":visual".
8668 */
8669 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008670ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008671{
8672 regmatch_T regmatch;
8673 char_u *p;
8674
8675 curwin->w_cursor.lnum = eap->line2;
8676 beginline(BL_SOL | BL_FIX);
8677 if (*eap->arg == '/')
8678 {
8679 /* ":open /pattern/": put cursor in column found with pattern */
8680 ++eap->arg;
8681 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8682 *p = NUL;
8683 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8684 if (regmatch.regprog != NULL)
8685 {
8686 regmatch.rm_ic = p_ic;
8687 p = ml_get_curline();
8688 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008689 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008690 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008691 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008692 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008693 }
8694 /* Move to the NUL, ignore any other arguments. */
8695 eap->arg += STRLEN(eap->arg);
8696 }
8697 check_cursor();
8698
8699 eap->cmdidx = CMD_visual;
8700 do_exedit(eap, NULL);
8701}
8702
8703/*
8704 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 */
8706 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008707ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708{
8709 do_exedit(eap, NULL);
8710}
8711
8712/*
8713 * ":edit <file>" command and alikes.
8714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008716do_exedit(
8717 exarg_T *eap,
8718 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719{
8720 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008722 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723
8724 /*
8725 * ":vi" command ends Ex mode.
8726 */
8727 if (exmode_active && (eap->cmdidx == CMD_visual
8728 || eap->cmdidx == CMD_view))
8729 {
8730 exmode_active = FALSE;
8731 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008732 {
8733 /* Special case: ":global/pat/visual\NLvi-commands" */
8734 if (global_busy)
8735 {
8736 int rd = RedrawingDisabled;
8737 int nwr = no_wait_return;
8738 int ms = msg_scroll;
8739#ifdef FEAT_GUI
8740 int he = hold_gui_events;
8741#endif
8742
8743 if (eap->nextcmd != NULL)
8744 {
8745 stuffReadbuff(eap->nextcmd);
8746 eap->nextcmd = NULL;
8747 }
8748
8749 if (exmode_was != EXMODE_VIM)
8750 settmode(TMODE_RAW);
8751 RedrawingDisabled = 0;
8752 no_wait_return = 0;
8753 need_wait_return = FALSE;
8754 msg_scroll = 0;
8755#ifdef FEAT_GUI
8756 hold_gui_events = 0;
8757#endif
8758 must_redraw = CLEAR;
8759
8760 main_loop(FALSE, TRUE);
8761
8762 RedrawingDisabled = rd;
8763 no_wait_return = nwr;
8764 msg_scroll = ms;
8765#ifdef FEAT_GUI
8766 hold_gui_events = he;
8767#endif
8768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 }
8772
8773 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008774 || eap->cmdidx == CMD_tabnew
8775 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008776 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008778 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 setpcmark();
8780 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008781 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8782 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008784 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 || *eap->arg != NUL
8786#ifdef FEAT_BROWSE
8787 || cmdmod.browse
8788#endif
8789 )
8790 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008791 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8792 * can bring us here, others are stopped earlier. */
8793 if (*eap->arg != NUL && curbuf_locked())
8794 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008795
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 n = readonlymode;
8797 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8798 readonlymode = TRUE;
8799 else if (eap->cmdidx == CMD_enew)
8800 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8801 empty buffer */
8802 setpcmark();
8803 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8804 NULL, eap,
8805 /* ":edit" goes to first line if Vi compatible */
8806 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8807 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8808 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008809 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008811 /* after a split we can use an existing buffer */
8812 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008814 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 {
8816 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 if (old_curwin != NULL)
8818 {
8819 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008820 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008822#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008823 cleanup_T cs;
8824
8825 /* Reset the error/interrupt/exception state here so that
8826 * aborting() returns FALSE when closing a window. */
8827 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008828#endif
8829#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008831#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008832 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008833
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008834#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008835 /* Restore the error/interrupt/exception state if not
8836 * discarded by a new aborting error, interrupt, or
8837 * uncaught exception. */
8838 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 }
8841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 }
8843 else if (readonlymode && curbuf->b_nwindows == 1)
8844 {
8845 /* When editing an already visited buffer, 'readonly' won't be set
8846 * but the previous value is kept. With ":view" and ":sview" we
8847 * want the file to be readonly, except when another window is
8848 * editing the same buffer. */
8849 curbuf->b_p_ro = TRUE;
8850 }
8851 readonlymode = n;
8852 }
8853 else
8854 {
8855 if (eap->do_ecmd_cmd != NULL)
8856 do_cmdline_cmd(eap->do_ecmd_cmd);
8857#ifdef FEAT_TITLE
8858 n = curwin->w_arg_idx_invalid;
8859#endif
8860 check_arg_idx(curwin);
8861#ifdef FEAT_TITLE
8862 if (n != curwin->w_arg_idx_invalid)
8863 maketitle();
8864#endif
8865 }
8866
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 /*
8868 * if ":split file" worked, set alternate file name in old window to new
8869 * file
8870 */
8871 if (old_curwin != NULL
8872 && *eap->arg != NUL
8873 && curwin != old_curwin
8874 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008875 && old_curwin->w_buffer != curbuf
8876 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878
8879 ex_no_reprint = TRUE;
8880}
8881
8882#ifndef FEAT_GUI
8883/*
8884 * ":gui" and ":gvim" when there is no GUI.
8885 */
8886 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008887ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888{
8889 eap->errmsg = e_nogvim;
8890}
8891#endif
8892
Bram Moolenaar4f974752019-02-17 17:44:42 +01008893#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008895ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896{
8897 gui_make_tearoff(eap->arg);
8898}
8899#endif
8900
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008901#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8902 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008904ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008906# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8907 if (gui.in_use)
8908 gui_make_popup(eap->arg, eap->forceit);
8909# ifdef FEAT_TERM_POPUP_MENU
8910 else
8911# endif
8912# endif
8913# ifdef FEAT_TERM_POPUP_MENU
8914 pum_make_popup(eap->arg, eap->forceit);
8915# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916}
8917#endif
8918
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008920ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921{
8922 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008923 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01008925 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926}
8927
8928/*
8929 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8930 * offset.
8931 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8932 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008934ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008937 win_T *save_curwin = curwin;
8938 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 long topline;
8940 long y;
8941 linenr_T old_linenr = curwin->w_cursor.lnum;
8942
8943 setpcmark();
8944
8945 /*
8946 * determine max topline
8947 */
8948 if (curwin->w_p_scb)
8949 {
8950 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008951 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 {
8953 if (wp->w_p_scb && wp->w_buffer)
8954 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01008955 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 if (topline > y)
8957 topline = y;
8958 }
8959 }
8960 if (topline < 1)
8961 topline = 1;
8962 }
8963 else
8964 {
8965 topline = 1;
8966 }
8967
8968
8969 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008970 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008972 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 {
8974 if (curwin->w_p_scb)
8975 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008976 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 y = topline - curwin->w_topline;
8978 if (y > 0)
8979 scrollup(y, TRUE);
8980 else
8981 scrolldown(-y, TRUE);
8982 curwin->w_scbind_pos = topline;
8983 redraw_later(VALID);
8984 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 }
8987 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008988 curwin = save_curwin;
8989 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 if (curwin->w_p_scb)
8991 {
8992 did_syncbind = TRUE;
8993 checkpcmark();
8994 if (old_linenr != curwin->w_cursor.lnum)
8995 {
8996 char_u ctrl_o[2];
8997
8998 ctrl_o[0] = Ctrl_O;
8999 ctrl_o[1] = 0;
9000 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
9001 }
9002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003}
9004
9005
9006 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009007ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008{
Bram Moolenaardf177f62005-02-22 08:39:57 +00009009 int i;
9010 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
9011 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012
9013 if (eap->usefilter) /* :r!cmd */
9014 do_bang(1, eap, FALSE, FALSE, TRUE);
9015 else
9016 {
9017 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
9018 return;
9019
9020#ifdef FEAT_BROWSE
9021 if (cmdmod.browse)
9022 {
9023 char_u *browseFile;
9024
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009025 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 NULL, NULL, NULL, curbuf);
9027 if (browseFile != NULL)
9028 {
9029 i = readfile(browseFile, NULL,
9030 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9031 vim_free(browseFile);
9032 }
9033 else
9034 i = OK;
9035 }
9036 else
9037#endif
9038 if (*eap->arg == NUL)
9039 {
9040 if (check_fname() == FAIL) /* check for no file name */
9041 return;
9042 i = readfile(curbuf->b_ffname, curbuf->b_fname,
9043 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9044 }
9045 else
9046 {
9047 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
9048 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
9049 i = readfile(eap->arg, NULL,
9050 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9051
9052 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01009053 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009055#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 if (!aborting())
9057#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009058 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 }
9060 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009061 {
9062 if (empty && exmode_active)
9063 {
9064 /* Delete the empty line that remains. Historically ex does
9065 * this but vi doesn't. */
9066 if (eap->line2 == 0)
9067 lnum = curbuf->b_ml.ml_line_count;
9068 else
9069 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009070 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009071 {
9072 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009073 if (curwin->w_cursor.lnum > 1
9074 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009075 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009076 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009077 }
9078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 }
9082}
9083
Bram Moolenaarea408852005-06-25 22:49:46 +00009084static char_u *prev_dir = NULL;
9085
9086#if defined(EXITFREE) || defined(PROTO)
9087 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009088free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009089{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009090 VIM_CLEAR(prev_dir);
9091 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00009092}
9093#endif
9094
Bram Moolenaarf4258302013-06-02 18:20:17 +02009095/*
9096 * Deal with the side effects of changing the current directory.
9097 * When "local" is TRUE then this was after an ":lcd" command.
9098 */
9099 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009100post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009101{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009102 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009103 if (local)
9104 {
9105 /* If still in global directory, need to remember current
9106 * directory as global directory. */
9107 if (globaldir == NULL && prev_dir != NULL)
9108 globaldir = vim_strsave(prev_dir);
9109 /* Remember this local directory for the window. */
9110 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9111 curwin->w_localdir = vim_strsave(NameBuff);
9112 }
9113 else
9114 {
9115 /* We are now in the global directory, no need to remember its
9116 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01009117 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009118 }
9119
9120 shorten_fnames(TRUE);
9121}
9122
Bram Moolenaarea408852005-06-25 22:49:46 +00009123
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124/*
9125 * ":cd", ":lcd", ":chdir" and ":lchdir".
9126 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009127 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009128ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009130 char_u *new_dir;
9131 char_u *tofree;
9132 int dir_differs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133
9134 new_dir = eap->arg;
9135#if !defined(UNIX) && !defined(VMS)
9136 /* for non-UNIX ":cd" means: print current directory */
9137 if (*new_dir == NUL)
9138 ex_pwd(NULL);
9139 else
9140#endif
9141 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009142 if (allbuf_locked())
9143 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009144 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9145 && !eap->forceit)
9146 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009147 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009148 return;
9149 }
9150
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 /* ":cd -": Change to previous directory */
9152 if (STRCMP(new_dir, "-") == 0)
9153 {
9154 if (prev_dir == NULL)
9155 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009156 emsg(_("E186: No previous directory"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 return;
9158 }
9159 new_dir = prev_dir;
9160 }
9161
9162 /* Save current directory for next ":cd -" */
9163 tofree = prev_dir;
9164 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9165 prev_dir = vim_strsave(NameBuff);
9166 else
9167 prev_dir = NULL;
9168
9169#if defined(UNIX) || defined(VMS)
9170 /* for UNIX ":cd" means: go to home directory */
9171 if (*new_dir == NUL)
9172 {
9173 /* use NameBuff for home directory name */
9174# ifdef VMS
9175 char_u *p;
9176
9177 p = mch_getenv((char_u *)"SYS$LOGIN");
9178 if (p == NULL || *p == NUL) /* empty is the same as not set */
9179 NameBuff[0] = NUL;
9180 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009181 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182# else
9183 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9184# endif
9185 new_dir = NameBuff;
9186 }
9187#endif
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009188 dir_differs = new_dir == NULL || prev_dir == NULL
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01009189 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009190 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009191 emsg(_(e_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 else
9193 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009194 int is_local_chdir = eap->cmdidx == CMD_lcd
9195 || eap->cmdidx == CMD_lchdir;
9196
9197 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198
9199 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009200 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 ex_pwd(eap);
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009202
9203 if (dir_differs)
9204 apply_autocmds(EVENT_DIRCHANGED,
9205 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9206 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 }
9208 vim_free(tofree);
9209 }
9210}
9211
9212/*
9213 * ":pwd".
9214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009216ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217{
9218 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9219 {
9220#ifdef BACKSLASH_IN_FILENAME
9221 slash_adjust(NameBuff);
9222#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01009223 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 }
9225 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009226 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227}
9228
9229/*
9230 * ":=".
9231 */
9232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009233ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009235 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009236 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237}
9238
9239 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009240ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009242 int n;
9243 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244
9245 if (cursor_valid())
9246 {
9247 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9248 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009249 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009251
9252 len = eap->line2;
9253 switch (*eap->arg)
9254 {
9255 case 'm': break;
9256 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009257 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009258 }
9259 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260}
9261
9262/*
9263 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9264 */
9265 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009266do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267{
9268 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009269 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270
9271 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02009272 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01009273 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009275 wait_now = msec - done > 1000L ? 1000L : msec - done;
9276#ifdef FEAT_TIMERS
9277 {
9278 long due_time = check_due_timer();
9279
9280 if (due_time > 0 && due_time < wait_now)
9281 wait_now = due_time;
9282 }
9283#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009284#ifdef FEAT_JOB_CHANNEL
9285 if (has_any_channel() && wait_now > 100L)
9286 wait_now = 100L;
9287#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009288 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009289#ifdef FEAT_JOB_CHANNEL
9290 if (has_any_channel())
9291 ui_breakcheck_force(TRUE);
9292 else
9293#endif
9294 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009295#ifdef MESSAGE_QUEUE
9296 /* Process the netbeans and clientserver messages that may have been
9297 * received in the call to ui_breakcheck() when the GUI is in use. This
9298 * may occur when running a test case. */
9299 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02009302
9303 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
9304 // input buffer, otherwise a following call to input() fails.
9305 if (got_int)
9306 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307}
9308
9309 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009310do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311{
9312 int mode;
9313 char_u *cmdp;
9314
9315 cmdp = eap->cmd;
9316 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9317
9318 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9319 eap->arg, mode, isabbrev))
9320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009321 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009323 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 break;
9325 }
9326}
9327
9328/*
9329 * ":winsize" command (obsolete).
9330 */
9331 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009332ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333{
9334 int w, h;
9335 char_u *arg = eap->arg;
9336 char_u *p;
9337
9338 w = getdigits(&arg);
9339 arg = skipwhite(arg);
9340 p = arg;
9341 h = getdigits(&arg);
9342 if (*p != NUL && *arg == NUL)
9343 set_shellsize(w, h, TRUE);
9344 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009345 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346}
9347
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009349ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350{
9351 int xchar = NUL;
9352 char_u *p;
9353
9354 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9355 {
9356 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9357 if (eap->arg[1] == NUL)
9358 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009359 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 return;
9361 }
9362 xchar = eap->arg[1];
9363 p = eap->arg + 2;
9364 }
9365 else
9366 p = eap->arg + 1;
9367
9368 eap->nextcmd = check_nextcmd(p);
9369 p = skipwhite(p);
9370 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009371 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009372 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 {
9374 /* Pass flags on for ":vertical wincmd ]". */
9375 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009376 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9378 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009379 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 }
9381}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382
Bram Moolenaar843ee412004-06-30 16:16:41 +00009383#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384/*
9385 * ":winpos".
9386 */
9387 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009388ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389{
9390 int x, y;
9391 char_u *arg = eap->arg;
9392 char_u *p;
9393
9394 if (*arg == NUL)
9395 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009396# if defined(FEAT_GUI) || defined(MSWIN)
9397# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009399# else
9400 if (mch_get_winpos(&x, &y) != FAIL)
9401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 {
9403 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01009404 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 }
9406 else
9407# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009408 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 }
9410 else
9411 {
9412 x = getdigits(&arg);
9413 arg = skipwhite(arg);
9414 p = arg;
9415 y = getdigits(&arg);
9416 if (*p == NUL || *arg != NUL)
9417 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009418 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 return;
9420 }
9421# ifdef FEAT_GUI
9422 if (gui.in_use)
9423 gui_mch_set_winpos(x, y);
9424 else if (gui.starting)
9425 {
9426 /* Remember the coordinates for when the window is opened. */
9427 gui_win_x = x;
9428 gui_win_y = y;
9429 }
9430# ifdef HAVE_TGETENT
9431 else
9432# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009433# else
9434# ifdef MSWIN
9435 mch_set_winpos(x, y);
9436# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437# endif
9438# ifdef HAVE_TGETENT
9439 if (*T_CWP)
9440 term_set_winpos(x, y);
9441# endif
9442 }
9443}
9444#endif
9445
9446/*
9447 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9448 */
9449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009450ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451{
9452 oparg_T oa;
9453
9454 clear_oparg(&oa);
9455 oa.regname = eap->regname;
9456 oa.start.lnum = eap->line1;
9457 oa.end.lnum = eap->line2;
9458 oa.line_count = eap->line2 - eap->line1 + 1;
9459 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9462 {
9463 setpcmark();
9464 curwin->w_cursor.lnum = eap->line1;
9465 beginline(BL_SOL | BL_FIX);
9466 }
9467
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009468 if (VIsual_active)
9469 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009470
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 switch (eap->cmdidx)
9472 {
9473 case CMD_delete:
9474 oa.op_type = OP_DELETE;
9475 op_delete(&oa);
9476 break;
9477
9478 case CMD_yank:
9479 oa.op_type = OP_YANK;
9480 (void)op_yank(&oa, FALSE, TRUE);
9481 break;
9482
9483 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009484 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009486 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9487#else
9488 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009490 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 oa.op_type = OP_RSHIFT;
9492 else
9493 oa.op_type = OP_LSHIFT;
9494 op_shift(&oa, FALSE, eap->amount);
9495 break;
9496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009498 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499}
9500
9501/*
9502 * ":put".
9503 */
9504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009505ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506{
9507 /* ":0put" works like ":1put!". */
9508 if (eap->line2 == 0)
9509 {
9510 eap->line2 = 1;
9511 eap->forceit = TRUE;
9512 }
9513 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009514 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9515 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516}
9517
9518/*
9519 * Handle ":copy" and ":move".
9520 */
9521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009522ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523{
9524 long n;
9525
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02009526 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 if (eap->arg == NULL) /* error detected */
9528 {
9529 eap->nextcmd = NULL;
9530 return;
9531 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009532 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533
9534 /*
9535 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9536 */
9537 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9538 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009539 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 return;
9541 }
9542
9543 if (eap->cmdidx == CMD_move)
9544 {
9545 if (do_move(eap->line1, eap->line2, n) == FAIL)
9546 return;
9547 }
9548 else
9549 ex_copy(eap->line1, eap->line2, n);
9550 u_clearline();
9551 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009552 ex_may_print(eap);
9553}
9554
9555/*
9556 * Print the current line if flags were given to the Ex command.
9557 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009558 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009559ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009560{
9561 if (eap->flags != 0)
9562 {
9563 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9564 (eap->flags & EXFLAG_LIST));
9565 ex_no_reprint = TRUE;
9566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567}
9568
9569/*
9570 * ":smagic" and ":snomagic".
9571 */
9572 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009573ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574{
9575 int magic_save = p_magic;
9576
9577 p_magic = (eap->cmdidx == CMD_smagic);
9578 do_sub(eap);
9579 p_magic = magic_save;
9580}
9581
9582/*
9583 * ":join".
9584 */
9585 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009586ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587{
9588 curwin->w_cursor.lnum = eap->line1;
9589 if (eap->line1 == eap->line2)
9590 {
9591 if (eap->addr_count >= 2) /* :2,2join does nothing */
9592 return;
9593 if (eap->line2 == curbuf->b_ml.ml_line_count)
9594 {
9595 beep_flush();
9596 return;
9597 }
9598 ++eap->line2;
9599 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009600 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009602 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603}
9604
9605/*
9606 * ":[addr]@r" or ":[addr]*r": execute register
9607 */
9608 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009609ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610{
9611 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009612 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613
9614 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009615 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616
9617#ifdef USE_ON_FLY_SCROLL
9618 dont_scroll = TRUE; /* disallow scrolling here */
9619#endif
9620
9621 /* get the register name. No name means to use the previous one */
9622 c = *eap->arg;
9623 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9624 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009625 /* Put the register in the typeahead buffer with the "silent" flag. */
9626 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9627 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009628 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 else
9632 {
9633 int save_efr = exec_from_reg;
9634
9635 exec_from_reg = TRUE;
9636
9637 /*
9638 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009639 * Continue until the stuff buffer is empty and all added characters
9640 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009642 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9644
9645 exec_from_reg = save_efr;
9646 }
9647}
9648
9649/*
9650 * ":!".
9651 */
9652 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009653ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654{
9655 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9656}
9657
9658/*
9659 * ":undo".
9660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009662ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009664 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009665 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009666 else
9667 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668}
9669
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009670#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009671 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009672ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009673{
9674 char_u hash[UNDO_HASH_SIZE];
9675
9676 u_compute_hash(hash);
9677 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9678}
9679
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009680 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009681ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009682{
9683 char_u hash[UNDO_HASH_SIZE];
9684
9685 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009686 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009687}
9688#endif
9689
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690/*
9691 * ":redo".
9692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009694ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695{
9696 u_redo(1);
9697}
9698
9699/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009700 * ":earlier" and ":later".
9701 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009702 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009703ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009704{
9705 long count = 0;
9706 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009707 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009708 char_u *p = eap->arg;
9709
9710 if (*p == NUL)
9711 count = 1;
9712 else if (isdigit(*p))
9713 {
9714 count = getdigits(&p);
9715 switch (*p)
9716 {
9717 case 's': ++p; sec = TRUE; break;
9718 case 'm': ++p; sec = TRUE; count *= 60; break;
9719 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009720 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9721 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009722 }
9723 }
9724
9725 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009726 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009727 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009728 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9729 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009730}
9731
9732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 * ":redir": start/stop redirection.
9734 */
9735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009736ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737{
9738 char *mode;
9739 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009740 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741
Bram Moolenaarba768492016-07-08 15:32:54 +02009742#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009743 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009744 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009745 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009746 return;
9747 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009748#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009749
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 if (STRICMP(eap->arg, "END") == 0)
9751 close_redir();
9752 else
9753 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009754 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009756 ++arg;
9757 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009759 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 mode = "a";
9761 }
9762 else
9763 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009764 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765
9766 close_redir();
9767
9768 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009769 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 if (fname == NULL)
9771 return;
9772#ifdef FEAT_BROWSE
9773 if (cmdmod.browse)
9774 {
9775 char_u *browseFile;
9776
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009777 browseFile = do_browse(BROWSE_SAVE,
9778 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009779 fname, NULL, NULL,
9780 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 if (browseFile == NULL)
9782 return; /* operation cancelled */
9783 vim_free(fname);
9784 fname = browseFile;
9785 eap->forceit = TRUE; /* since dialog already asked */
9786 }
9787#endif
9788
9789 redir_fd = open_exfile(fname, eap->forceit, mode);
9790 vim_free(fname);
9791 }
9792#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009793 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 {
9795 /* redirect to a register a-z (resp. A-Z for appending) */
9796 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009797 ++arg;
9798 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009800 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009801 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009803 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009805 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009806 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009807 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009808 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009810 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009811 if (*arg == '>')
9812 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009813 /* Make register empty when not using @A-@Z and the
9814 * command is valid. */
9815 if (*arg == NUL && !isupper(redir_reg))
9816 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009818 }
9819 if (*arg != NUL)
9820 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009821 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009822 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009823 }
9824 }
9825 else if (*arg == '=' && arg[1] == '>')
9826 {
9827 int append;
9828
9829 /* redirect to a variable */
9830 close_redir();
9831 arg += 2;
9832
9833 if (*arg == '>')
9834 {
9835 ++arg;
9836 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 }
9838 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009839 append = FALSE;
9840
9841 if (var_redir_start(skipwhite(arg), append) == OK)
9842 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 }
9844#endif
9845
9846 /* TODO: redirect to a buffer */
9847
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009849 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009851
9852 /* Make sure redirection is not off. Can happen for cmdline completion
9853 * that indirectly invokes a command to catch its output. */
9854 if (redir_fd != NULL
9855#ifdef FEAT_EVAL
9856 || redir_reg || redir_vname
9857#endif
9858 )
9859 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860}
9861
9862/*
9863 * ":redraw": force redraw
9864 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009865 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009866ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867{
9868 int r = RedrawingDisabled;
9869 int p = p_lz;
9870
9871 RedrawingDisabled = 0;
9872 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009873 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009875 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876#ifdef FEAT_TITLE
9877 if (need_maketitle)
9878 maketitle();
9879#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01009880#if defined(MSWIN) && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01009881 resize_console_buf();
9882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 RedrawingDisabled = r;
9884 p_lz = p;
9885
9886 /* Reset msg_didout, so that a message that's there is overwritten. */
9887 msg_didout = FALSE;
9888 msg_col = 0;
9889
Bram Moolenaar56667a52013-07-17 11:54:28 +02009890 /* No need to wait after an intentional redraw. */
9891 need_wait_return = FALSE;
9892
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 out_flush();
9894}
9895
9896/*
9897 * ":redrawstatus": force redraw of status line(s)
9898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009900ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 int r = RedrawingDisabled;
9903 int p = p_lz;
9904
9905 RedrawingDisabled = 0;
9906 p_lz = FALSE;
9907 if (eap->forceit)
9908 status_redraw_all();
9909 else
9910 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009911 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 RedrawingDisabled = r;
9913 p_lz = p;
9914 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915}
9916
Bram Moolenaare12bab32019-01-08 22:02:56 +01009917/*
9918 * ":redrawtabline": force redraw of the tabline
9919 */
9920 static void
9921ex_redrawtabline(exarg_T *eap UNUSED)
9922{
9923 int r = RedrawingDisabled;
9924 int p = p_lz;
9925
9926 RedrawingDisabled = 0;
9927 p_lz = FALSE;
9928
9929 draw_tabline();
9930
9931 RedrawingDisabled = r;
9932 p_lz = p;
9933 out_flush();
9934}
9935
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009937close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938{
9939 if (redir_fd != NULL)
9940 {
9941 fclose(redir_fd);
9942 redir_fd = NULL;
9943 }
9944#ifdef FEAT_EVAL
9945 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009946 if (redir_vname)
9947 {
9948 var_redir_stop();
9949 redir_vname = 0;
9950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951#endif
9952}
9953
9954#if defined(FEAT_SESSION) && defined(USE_CRNL)
9955# define MKSESSION_NL
9956static int mksession_nl = FALSE; /* use NL only in put_eol() */
9957#endif
9958
9959/*
9960 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9961 */
9962 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009963ex_mkrc(
9964 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965{
9966 FILE *fd;
9967 int failed = FALSE;
9968 char_u *fname;
9969#ifdef FEAT_BROWSE
9970 char_u *browseFile = NULL;
9971#endif
9972#ifdef FEAT_SESSION
9973 int view_session = FALSE;
9974 int using_vdir = FALSE; /* using 'viewdir'? */
9975 char_u *viewFile = NULL;
9976 unsigned *flagp;
9977#endif
9978
9979 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9980 {
9981#ifdef FEAT_SESSION
9982 view_session = TRUE;
9983#else
9984 ex_ni(eap);
9985 return;
9986#endif
9987 }
9988
9989#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009990 /* Use the short file name until ":lcd" is used. We also don't use the
9991 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009992 did_lcd = FALSE;
9993
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9995 if (eap->cmdidx == CMD_mkview
9996 && (*eap->arg == NUL
9997 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9998 {
9999 eap->forceit = TRUE;
10000 fname = get_view_file(*eap->arg);
10001 if (fname == NULL)
10002 return;
10003 viewFile = fname;
10004 using_vdir = TRUE;
10005 }
10006 else
10007#endif
10008 if (*eap->arg != NUL)
10009 fname = eap->arg;
10010 else if (eap->cmdidx == CMD_mkvimrc)
10011 fname = (char_u *)VIMRC_FILE;
10012#ifdef FEAT_SESSION
10013 else if (eap->cmdidx == CMD_mksession)
10014 fname = (char_u *)SESSION_FILE;
10015#endif
10016 else
10017 fname = (char_u *)EXRC_FILE;
10018
10019#ifdef FEAT_BROWSE
10020 if (cmdmod.browse)
10021 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +000010022 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023# ifdef FEAT_SESSION
10024 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
10025 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
10026# endif
10027 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +020010028 fname, (char_u *)"vim", NULL,
10029 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 if (browseFile == NULL)
10031 goto theend;
10032 fname = browseFile;
10033 eap->forceit = TRUE; /* since dialog already asked */
10034 }
10035#endif
10036
10037#if defined(FEAT_SESSION) && defined(vim_mkdir)
10038 /* When using 'viewdir' may have to create the directory. */
10039 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +000010040 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041#endif
10042
10043 fd = open_exfile(fname, eap->forceit, WRITEBIN);
10044 if (fd != NULL)
10045 {
10046#ifdef FEAT_SESSION
10047 if (eap->cmdidx == CMD_mkview)
10048 flagp = &vop_flags;
10049 else
10050 flagp = &ssop_flags;
10051#endif
10052
10053#ifdef MKSESSION_NL
10054 /* "unix" in 'sessionoptions': use NL line separator */
10055 if (view_session && (*flagp & SSOP_UNIX))
10056 mksession_nl = TRUE;
10057#endif
10058
10059 /* Write the version command for :mkvimrc */
10060 if (eap->cmdidx == CMD_mkvimrc)
10061 (void)put_line(fd, "version 6.0");
10062
10063#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010064 if (eap->cmdidx == CMD_mksession)
10065 {
10066 if (put_line(fd, "let SessionLoad = 1") == FAIL)
10067 failed = TRUE;
10068 }
10069
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 if (eap->cmdidx != CMD_mkview)
10071#endif
10072 {
10073 /* Write setting 'compatible' first, because it has side effects.
10074 * For that same reason only do it when needed. */
10075 if (p_cp)
10076 (void)put_line(fd, "if !&cp | set cp | endif");
10077 else
10078 (void)put_line(fd, "if &cp | set nocp | endif");
10079 }
10080
10081#ifdef FEAT_SESSION
10082 if (!view_session
10083 || (eap->cmdidx == CMD_mksession
10084 && (*flagp & SSOP_OPTIONS)))
10085#endif
10086 failed |= (makemap(fd, NULL) == FAIL
10087 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10088
10089#ifdef FEAT_SESSION
10090 if (!failed && view_session)
10091 {
10092 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10093 failed = TRUE;
10094 if (eap->cmdidx == CMD_mksession)
10095 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010096 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097
Bram Moolenaard9462e32011-04-11 21:35:11 +020010098 dirnow = alloc(MAXPATHL);
10099 if (dirnow == NULL)
10100 failed = TRUE;
10101 else
10102 {
10103 /*
10104 * Change to session file's dir.
10105 */
10106 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010108 *dirnow = NUL;
10109 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10110 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +010010111 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010112 shorten_fnames(TRUE);
10113 }
10114 else if (*dirnow != NUL
10115 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10116 {
10117 if (mch_chdir((char *)globaldir) == 0)
10118 shorten_fnames(TRUE);
10119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120
Bram Moolenaard9462e32011-04-11 21:35:11 +020010121 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122
Bram Moolenaard9462e32011-04-11 21:35:11 +020010123 /* restore original dir */
10124 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010126 {
10127 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010128 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +020010129 shorten_fnames(TRUE);
10130 }
10131 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 }
10133 }
10134 else
10135 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010136 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10137 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 }
10139 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10140 == FAIL)
10141 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +010010142# ifdef FEAT_SEARCH_EXTRA
10143 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
10144 failed = TRUE;
10145# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010146 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10147 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010148 if (eap->cmdidx == CMD_mksession)
10149 {
10150 if (put_line(fd, "unlet SessionLoad") == FAIL)
10151 failed = TRUE;
10152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 }
10154#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010155 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10156 failed = TRUE;
10157
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 failed |= fclose(fd);
10159
10160 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010161 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10163 else if (eap->cmdidx == CMD_mksession)
10164 {
10165 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010166 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167
Bram Moolenaard9462e32011-04-11 21:35:11 +020010168 tbuf = alloc(MAXPATHL);
10169 if (tbuf != NULL)
10170 {
10171 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10172 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10173 vim_free(tbuf);
10174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 }
10176#endif
10177#ifdef MKSESSION_NL
10178 mksession_nl = FALSE;
10179#endif
10180 }
10181
10182#ifdef FEAT_BROWSE
10183theend:
10184 vim_free(browseFile);
10185#endif
10186#ifdef FEAT_SESSION
10187 vim_free(viewFile);
10188#endif
10189}
10190
Bram Moolenaardf177f62005-02-22 08:39:57 +000010191#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10192 || defined(PROTO)
10193 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010194vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010195{
10196 if (vim_mkdir(name, prot) != 0)
10197 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010198 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +000010199 return FAIL;
10200 }
10201 return OK;
10202}
10203#endif
10204
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205/*
10206 * Open a file for writing for an Ex command, with some checks.
10207 * Return file descriptor, or NULL on failure.
10208 */
10209 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010210open_exfile(
10211 char_u *fname,
10212 int forceit,
10213 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214{
10215 FILE *fd;
10216
10217#ifdef UNIX
10218 /* with Unix it is possible to open a directory */
10219 if (mch_isdir(fname))
10220 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010221 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 return NULL;
10223 }
10224#endif
10225 if (!forceit && *mode != 'a' && vim_fexists(fname))
10226 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010227 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 return NULL;
10229 }
10230
10231 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010232 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233
10234 return fd;
10235}
10236
10237/*
10238 * ":mark" and ":k".
10239 */
10240 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010241ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242{
10243 pos_T pos;
10244
10245 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010246 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010248 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 else
10250 {
10251 pos = curwin->w_cursor; /* save curwin->w_cursor */
10252 curwin->w_cursor.lnum = eap->line2;
10253 beginline(BL_WHITE | BL_FIX);
10254 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010255 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10257 }
10258}
10259
10260/*
10261 * Update w_topline, w_leftcol and the cursor position.
10262 */
10263 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010264update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265{
10266 check_cursor(); /* put cursor on valid line */
10267 update_topline();
10268 if (!curwin->w_p_wrap)
10269 validate_cursor();
10270 update_curswant();
10271}
10272
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010274 * Save the current State and go to Normal mode.
10275 * Return TRUE if the typeahead could be saved.
10276 */
10277 int
10278save_current_state(save_state_T *sst)
10279{
10280 sst->save_msg_scroll = msg_scroll;
10281 sst->save_restart_edit = restart_edit;
10282 sst->save_msg_didout = msg_didout;
10283 sst->save_State = State;
10284 sst->save_insertmode = p_im;
10285 sst->save_finish_op = finish_op;
10286 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +010010287 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010288
10289 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10290 restart_edit = 0; /* don't go to Insert mode */
10291 p_im = FALSE; /* don't use 'insertmode' */
10292
10293 /*
10294 * Save the current typeahead. This is required to allow using ":normal"
10295 * from an event handler and makes sure we don't hang when the argument
10296 * ends with half a command.
10297 */
10298 save_typeahead(&sst->tabuf);
10299 return sst->tabuf.typebuf_valid;
10300}
10301
10302 void
10303restore_current_state(save_state_T *sst)
10304{
10305 /* Restore the previous typeahead. */
10306 restore_typeahead(&sst->tabuf);
10307
10308 msg_scroll = sst->save_msg_scroll;
10309 restart_edit = sst->save_restart_edit;
10310 p_im = sst->save_insertmode;
10311 finish_op = sst->save_finish_op;
10312 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +010010313 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010314 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10315
10316 /* Restore the state (needed when called from a function executed for
10317 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10318 State = sst->save_State;
10319#ifdef CURSOR_SHAPE
10320 ui_cursor_shape(); /* may show different cursor shape */
10321#endif
10322}
10323
10324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 * ":normal[!] {commands}": Execute normal mode commands.
10326 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010327 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010328ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010330 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 char_u *arg = NULL;
10332 int l;
10333 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010335 if (ex_normal_lock > 0)
10336 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010337 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010338 return;
10339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 if (ex_normal_busy >= p_mmd)
10341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010342 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 return;
10344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 /*
10347 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10348 * this for the K_SPECIAL leading byte, otherwise special keys will not
10349 * work.
10350 */
10351 if (has_mbyte)
10352 {
10353 int len = 0;
10354
10355 /* Count the number of characters to be escaped. */
10356 for (p = eap->arg; *p != NUL; ++p)
10357 {
Bram Moolenaar13505972019-01-24 15:04:48 +010010358#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 if (*p == CSI) /* leadbyte CSI */
10360 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +010010361#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010362 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +010010364#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +010010366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 )
10368 len += 2;
10369 }
10370 if (len > 0)
10371 {
10372 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10373 if (arg != NULL)
10374 {
10375 len = 0;
10376 for (p = eap->arg; *p != NUL; ++p)
10377 {
10378 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +010010379#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 if (*p == CSI)
10381 {
10382 arg[len++] = KS_EXTRA;
10383 arg[len++] = (int)KE_CSI;
10384 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010385#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010386 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 {
10388 arg[len++] = *++p;
10389 if (*p == K_SPECIAL)
10390 {
10391 arg[len++] = KS_SPECIAL;
10392 arg[len++] = KE_FILLER;
10393 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010394#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 else if (*p == CSI)
10396 {
10397 arg[len++] = KS_EXTRA;
10398 arg[len++] = (int)KE_CSI;
10399 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 }
10402 arg[len] = NUL;
10403 }
10404 }
10405 }
10406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010408 ++ex_normal_busy;
10409 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 {
10411 /*
10412 * Repeat the :normal command for each line in the range. When no
10413 * range given, execute it just once, without positioning the cursor
10414 * first.
10415 */
10416 do
10417 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418 if (eap->addr_count != 0)
10419 {
10420 curwin->w_cursor.lnum = eap->line1++;
10421 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010422 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423 }
10424
Bram Moolenaar13505972019-01-24 15:04:48 +010010425 exec_normal_cmd(arg != NULL
10426 ? arg
10427 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 }
10429 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10430 }
10431
10432 /* Might not return to the main loop when in an event handler. */
10433 update_topline_cursor();
10434
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010435 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010437#ifdef FEAT_MOUSE
10438 setmouse();
10439#endif
10440#ifdef CURSOR_SHAPE
10441 ui_cursor_shape(); /* may show different cursor shape */
10442#endif
10443
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445}
10446
10447/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010448 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 */
10450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010451ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010453 if (eap->forceit)
10454 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010455 /* cursor line can be zero on startup */
10456 if (!curwin->w_cursor.lnum)
10457 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010458 coladvance((colnr_T)MAXCOL);
10459 curwin->w_curswant = MAXCOL;
10460 curwin->w_set_curswant = FALSE;
10461 }
10462
Bram Moolenaara40c5002005-01-09 21:16:21 +000010463 /* Ignore the command when already in Insert mode. Inserting an
10464 * expression register that invokes a function can do this. */
10465 if (State & INSERT)
10466 return;
10467
Bram Moolenaar64969662005-12-14 21:59:55 +000010468 if (eap->cmdidx == CMD_startinsert)
10469 restart_edit = 'a';
10470 else if (eap->cmdidx == CMD_startreplace)
10471 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010473 restart_edit = 'V';
10474
10475 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010477 if (eap->cmdidx == CMD_startinsert)
10478 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 curwin->w_curswant = 0; /* avoid MAXCOL */
10480 }
10481}
10482
10483/*
10484 * ":stopinsert"
10485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010487ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488{
10489 restart_edit = 0;
10490 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010491 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010494/*
10495 * Execute normal mode command "cmd".
10496 * "remap" can be REMAP_NONE or REMAP_YES.
10497 */
10498 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010499exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010500{
Bram Moolenaar25281632016-01-21 23:32:32 +010010501 /* Stuff the argument into the typeahead buffer. */
10502 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010503 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +010010504}
Bram Moolenaar25281632016-01-21 23:32:32 +010010505
Bram Moolenaar25281632016-01-21 23:32:32 +010010506/*
10507 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010508 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +010010509 */
10510 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010511exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +010010512{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010513 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +020010514 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010515
Bram Moolenaar905dd902019-04-07 14:21:47 +020010516 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
10517 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010518 clear_oparg(&oa);
10519 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010520 while ((!stuff_empty()
10521 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +020010522 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010523 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010524 {
10525 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010526#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010527 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010528 && oa.op_type == OP_NOP && oa.regname == NUL
10529 && !VIsual_active)
10530 {
10531 /* If terminal_loop() returns OK we got a key that is handled
10532 * in Normal model. With FAIL we first need to position the
10533 * cursor and the screen needs to be redrawn. */
10534 if (terminal_loop(TRUE) == OK)
10535 normal_cmd(&oa, TRUE);
10536 }
10537 else
10538#endif
10539 /* execute a Normal mode cmd */
10540 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010541 }
10542}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010543
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544#ifdef FEAT_FIND_ID
10545 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010546ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547{
10548 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10549 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10550 (linenr_T)1, (linenr_T)MAXLNUM);
10551}
10552
Bram Moolenaar4033c552017-09-16 20:54:51 +020010553#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554/*
10555 * ":psearch"
10556 */
10557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010558ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559{
10560 g_do_tagpreview = p_pvh;
10561 ex_findpat(eap);
10562 g_do_tagpreview = 0;
10563}
10564#endif
10565
10566 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010567ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568{
10569 int whole = TRUE;
10570 long n;
10571 char_u *p;
10572 int action;
10573
10574 switch (cmdnames[eap->cmdidx].cmd_name[2])
10575 {
10576 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10577 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10578 action = ACTION_GOTO;
10579 else
10580 action = ACTION_SHOW;
10581 break;
10582 case 'i': /* ":ilist" and ":dlist" */
10583 action = ACTION_SHOW_ALL;
10584 break;
10585 case 'u': /* ":ijump" and ":djump" */
10586 action = ACTION_GOTO;
10587 break;
10588 default: /* ":isplit" and ":dsplit" */
10589 action = ACTION_SPLIT;
10590 break;
10591 }
10592
10593 n = 1;
10594 if (vim_isdigit(*eap->arg)) /* get count */
10595 {
10596 n = getdigits(&eap->arg);
10597 eap->arg = skipwhite(eap->arg);
10598 }
10599 if (*eap->arg == '/') /* Match regexp, not just whole words */
10600 {
10601 whole = FALSE;
10602 ++eap->arg;
10603 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10604 if (*p)
10605 {
10606 *p++ = NUL;
10607 p = skipwhite(p);
10608
10609 /* Check for trailing illegal characters */
10610 if (!ends_excmd(*p))
10611 eap->errmsg = e_trailing;
10612 else
10613 eap->nextcmd = check_nextcmd(p);
10614 }
10615 }
10616 if (!eap->skip)
10617 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10618 whole, !eap->forceit,
10619 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10620 n, action, eap->line1, eap->line2);
10621}
10622#endif
10623
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624
Bram Moolenaar4033c552017-09-16 20:54:51 +020010625#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626/*
10627 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10628 */
10629 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010630ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010632 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10634}
10635
10636/*
10637 * ":pedit"
10638 */
10639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010640ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641{
10642 win_T *curwin_save = curwin;
10643
10644 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010645 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010646 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 do_exedit(eap, NULL);
10648 keep_help_flag = FALSE;
10649 if (curwin != curwin_save && win_valid(curwin_save))
10650 {
10651 /* Return cursor to where we were */
10652 validate_cursor();
10653 redraw_later(VALID);
10654 win_enter(curwin_save, TRUE);
10655 }
10656 g_do_tagpreview = 0;
10657}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659
10660/*
10661 * ":stag", ":stselect" and ":stjump".
10662 */
10663 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010664ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665{
10666 postponed_split = -1;
10667 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010668 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10670 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010671 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673
10674/*
10675 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10676 */
10677 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010678ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679{
10680 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10681}
10682
10683 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010684ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685{
10686 int cmd;
10687
10688 switch (name[1])
10689 {
10690 case 'j': cmd = DT_JUMP; /* ":tjump" */
10691 break;
10692 case 's': cmd = DT_SELECT; /* ":tselect" */
10693 break;
10694 case 'p': cmd = DT_PREV; /* ":tprevious" */
10695 break;
10696 case 'N': cmd = DT_PREV; /* ":tNext" */
10697 break;
10698 case 'n': cmd = DT_NEXT; /* ":tnext" */
10699 break;
10700 case 'o': cmd = DT_POP; /* ":pop" */
10701 break;
10702 case 'f': /* ":tfirst" */
10703 case 'r': cmd = DT_FIRST; /* ":trewind" */
10704 break;
10705 case 'l': cmd = DT_LAST; /* ":tlast" */
10706 break;
10707 default: /* ":tag" */
10708#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010709 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010711 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712 return;
10713 }
10714#endif
10715 cmd = DT_TAG;
10716 break;
10717 }
10718
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010719 if (name[0] == 'l')
10720 {
10721#ifndef FEAT_QUICKFIX
10722 ex_ni(eap);
10723 return;
10724#else
10725 cmd = DT_LTAG;
10726#endif
10727 }
10728
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10730 eap->forceit, TRUE);
10731}
10732
10733/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010734 * Check "str" for starting with a special cmdline variable.
10735 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10736 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10737 */
10738 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010739find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010740{
10741 int len;
10742 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010743 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010744 "%",
10745#define SPEC_PERC 0
10746 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010747#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010748 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010749#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010750 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010751#define SPEC_CCWORD (SPEC_CWORD + 1)
10752 "<cexpr>", /* expr under cursor */
10753#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010754 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010755#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010756 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010757#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010758 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010759#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010760 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010761#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010762 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010763#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010764 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010765#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010766 "<sflnum>", /* script file line number */
10767#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010768#ifdef FEAT_CLIENTSERVER
10769 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010770# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010771#endif
10772 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010773
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010774 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010775 {
10776 len = (int)STRLEN(spec_str[i]);
10777 if (STRNCMP(src, spec_str[i], len) == 0)
10778 {
10779 *usedlen = len;
10780 return i;
10781 }
10782 }
10783 return -1;
10784}
10785
10786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 * Evaluate cmdline variables.
10788 *
10789 * change '%' to curbuf->b_ffname
10790 * '#' to curwin->w_altfile
10791 * '<cword>' to word under the cursor
10792 * '<cWORD>' to WORD under the cursor
10793 * '<cfile>' to path name under the cursor
10794 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010795 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796 * '<afile>' to file name for autocommand
10797 * '<abuf>' to buffer number for autocommand
10798 * '<amatch>' to matching name for autocommand
10799 *
10800 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10801 * "" for error without a message) and NULL is returned.
10802 * Returns an allocated string if a valid match was found.
10803 * Returns NULL if no match was found. "usedlen" then still contains the
10804 * number of characters to skip.
10805 */
10806 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010807eval_vars(
10808 char_u *src, /* pointer into commandline */
10809 char_u *srcstart, /* beginning of valid memory for src */
10810 int *usedlen, /* characters after src that are used */
10811 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010812 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010813 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010814 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815{
10816 int i;
10817 char_u *s;
10818 char_u *result;
10819 char_u *resultbuf = NULL;
10820 int resultlen;
10821 buf_T *buf;
10822 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10823 int spec_idx;
10824#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +020010825 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 int skip_mod = FALSE;
10827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829
10830 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010831 if (escaped != NULL)
10832 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833
10834 /*
10835 * Check if there is something to do.
10836 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010837 spec_idx = find_cmdline_var(src, usedlen);
10838 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 {
10840 *usedlen = 1;
10841 return NULL;
10842 }
10843
10844 /*
10845 * Skip when preceded with a backslash "\%" and "\#".
10846 * Note: In "\\%" the % is also not recognized!
10847 */
10848 if (src > srcstart && src[-1] == '\\')
10849 {
10850 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010851 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 return NULL;
10853 }
10854
10855 /*
10856 * word or WORD under cursor
10857 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010858 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10859 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010861 resultlen = find_ident_under_cursor(&result,
10862 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10863 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10864 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 if (resultlen == 0)
10866 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010867 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 return NULL;
10869 }
10870 }
10871
10872 /*
10873 * '#': Alternate file name
10874 * '%': Current file name
10875 * File name under the cursor
10876 * File name for autocommand
10877 * and following modifiers
10878 */
10879 else
10880 {
10881 switch (spec_idx)
10882 {
10883 case SPEC_PERC: /* '%': current file */
10884 if (curbuf->b_fname == NULL)
10885 {
10886 result = (char_u *)"";
10887 valid = 0; /* Must have ":p:h" to be valid */
10888 }
10889 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010892#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010893 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010894#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 break;
10897
10898 case SPEC_HASH: /* '#' or "#99": alternate file */
10899 if (src[1] == '#') /* "##": the argument list */
10900 {
10901 result = arg_all();
10902 resultbuf = result;
10903 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010904 if (escaped != NULL)
10905 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906#ifdef FEAT_MODIFY_FNAME
10907 skip_mod = TRUE;
10908#endif
10909 break;
10910 }
10911 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010912 if (*s == '<') /* "#<99" uses v:oldfiles */
10913 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010915 if (s == src + 2 && src[1] == '-')
10916 /* just a minus sign, don't skip over it */
10917 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918 *usedlen = (int)(s - src); /* length of what we expand */
10919
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010920 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010922 if (*usedlen < 2)
10923 {
10924 /* Should we give an error message for #<text? */
10925 *usedlen = 1;
10926 return NULL;
10927 }
10928#ifdef FEAT_EVAL
10929 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10930 (long)i);
10931 if (result == NULL)
10932 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010933 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +000010934 return NULL;
10935 }
10936#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +010010937 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 }
10941 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010942 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010943 if (i == 0 && src[1] == '<' && *usedlen > 1)
10944 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010945 buf = buflist_findnr(i);
10946 if (buf == NULL)
10947 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010948 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +000010949 return NULL;
10950 }
10951 if (lnump != NULL)
10952 *lnump = ECMD_LAST;
10953 if (buf->b_fname == NULL)
10954 {
10955 result = (char_u *)"";
10956 valid = 0; /* Must have ":p:h" to be valid */
10957 }
10958 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010959 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010960 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010961#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010962 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010963#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010964 }
Bram Moolenaard812df62008-11-09 12:46:09 +000010965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 break;
10967
10968#ifdef FEAT_SEARCHPATH
10969 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010970 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 if (result == NULL)
10972 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010973 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 return NULL;
10975 }
10976 resultbuf = result; /* remember allocated string */
10977 break;
10978#endif
10979
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 case SPEC_AFILE: /* file name for autocommand */
10981 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010982 if (result != NULL && !autocmd_fname_full)
10983 {
10984 /* Still need to turn the fname into a full path. It is
10985 * postponed to avoid a delay when <afile> is not used. */
10986 autocmd_fname_full = TRUE;
10987 result = FullName_save(autocmd_fname, FALSE);
10988 vim_free(autocmd_fname);
10989 autocmd_fname = result;
10990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991 if (result == NULL)
10992 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010993 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 return NULL;
10995 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010996 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 break;
10998
10999 case SPEC_ABUF: /* buffer number for autocommand */
11000 if (autocmd_bufnr <= 0)
11001 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011002 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 return NULL;
11004 }
11005 sprintf((char *)strbuf, "%d", autocmd_bufnr);
11006 result = strbuf;
11007 break;
11008
11009 case SPEC_AMATCH: /* match name for autocommand */
11010 result = autocmd_match;
11011 if (result == NULL)
11012 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011013 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 return NULL;
11015 }
11016 break;
11017
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 case SPEC_SFILE: /* file name for ":so" command */
11019 result = sourcing_name;
11020 if (result == NULL)
11021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011022 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 return NULL;
11024 }
11025 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011026
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010011027 case SPEC_SLNUM: /* line in file for ":so" command */
11028 if (sourcing_name == NULL || sourcing_lnum == 0)
11029 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011030 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010011031 return NULL;
11032 }
11033 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
11034 result = strbuf;
11035 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011036
11037#ifdef FEAT_EVAL
11038 case SPEC_SFLNUM: /* line in script file */
11039 if (current_sctx.sc_lnum + sourcing_lnum == 0)
11040 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011041 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011042 return NULL;
11043 }
11044 sprintf((char *)strbuf, "%ld",
11045 (long)(current_sctx.sc_lnum + sourcing_lnum));
11046 result = strbuf;
11047 break;
11048#endif
11049
11050#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011052 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
11053 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054 result = strbuf;
11055 break;
11056#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011057
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020011058 default:
11059 result = (char_u *)""; /* avoid gcc warning */
11060 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 }
11062
11063 resultlen = (int)STRLEN(result); /* length of new string */
11064 if (src[*usedlen] == '<') /* remove the file name extension */
11065 {
11066 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 resultlen = (int)(s - result);
11069 }
11070#ifdef FEAT_MODIFY_FNAME
11071 else if (!skip_mod)
11072 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020011073 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 &resultlen);
11075 if (result == NULL)
11076 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011077 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 return NULL;
11079 }
11080 }
11081#endif
11082 }
11083
11084 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
11085 {
11086 if (valid != VALID_HEAD + VALID_PATH)
11087 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011088 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011090 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 result = NULL;
11092 }
11093 else
11094 result = vim_strnsave(result, resultlen);
11095 vim_free(resultbuf);
11096 return result;
11097}
11098
11099/*
11100 * Concatenate all files in the argument list, separated by spaces, and return
11101 * it in one allocated string.
11102 * Spaces and backslashes in the file names are escaped with a backslash.
11103 * Returns NULL when out of memory.
11104 */
11105 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011106arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107{
11108 int len;
11109 int idx;
11110 char_u *retval = NULL;
11111 char_u *p;
11112
11113 /*
11114 * Do this loop two times:
11115 * first time: compute the total length
11116 * second time: concatenate the names
11117 */
11118 for (;;)
11119 {
11120 len = 0;
11121 for (idx = 0; idx < ARGCOUNT; ++idx)
11122 {
11123 p = alist_name(&ARGLIST[idx]);
11124 if (p != NULL)
11125 {
11126 if (len > 0)
11127 {
11128 /* insert a space in between names */
11129 if (retval != NULL)
11130 retval[len] = ' ';
11131 ++len;
11132 }
11133 for ( ; *p != NUL; ++p)
11134 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011135 if (*p == ' '
11136#ifndef BACKSLASH_IN_FILENAME
11137 || *p == '\\'
11138#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +020011139 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 {
11141 /* insert a backslash */
11142 if (retval != NULL)
11143 retval[len] = '\\';
11144 ++len;
11145 }
11146 if (retval != NULL)
11147 retval[len] = *p;
11148 ++len;
11149 }
11150 }
11151 }
11152
11153 /* second time: break here */
11154 if (retval != NULL)
11155 {
11156 retval[len] = NUL;
11157 break;
11158 }
11159
11160 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011161 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162 if (retval == NULL)
11163 break;
11164 }
11165
11166 return retval;
11167}
11168
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169/*
11170 * Expand the <sfile> string in "arg".
11171 *
11172 * Returns an allocated string, or NULL for any error.
11173 */
11174 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011175expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011177 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 int len;
11179 char_u *result;
11180 char_u *newres;
11181 char_u *repl;
11182 int srclen;
11183 char_u *p;
11184
11185 result = vim_strsave(arg);
11186 if (result == NULL)
11187 return NULL;
11188
11189 for (p = result; *p; )
11190 {
11191 if (STRNCMP(p, "<sfile>", 7) != 0)
11192 ++p;
11193 else
11194 {
11195 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011196 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 if (errormsg != NULL)
11198 {
11199 if (*errormsg)
11200 emsg(errormsg);
11201 vim_free(result);
11202 return NULL;
11203 }
11204 if (repl == NULL) /* no match (cannot happen) */
11205 {
11206 p += srclen;
11207 continue;
11208 }
11209 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11210 newres = alloc(len);
11211 if (newres == NULL)
11212 {
11213 vim_free(repl);
11214 vim_free(result);
11215 return NULL;
11216 }
11217 mch_memmove(newres, result, (size_t)(p - result));
11218 STRCPY(newres + (p - result), repl);
11219 len = (int)STRLEN(newres);
11220 STRCAT(newres, p + srclen);
11221 vim_free(repl);
11222 vim_free(result);
11223 result = newres;
11224 p = newres + len; /* continue after the match */
11225 }
11226 }
11227
11228 return result;
11229}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230
11231#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011232static int ses_winsizes(FILE *fd, int restore_size,
11233 win_T *tab_firstwin);
11234static int ses_win_rec(FILE *fd, frame_T *fr);
11235static frame_T *ses_skipframe(frame_T *fr);
11236static int ses_do_frame(frame_T *fr);
11237static int ses_do_win(win_T *wp);
11238static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11239static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011240static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241
11242/*
11243 * Write openfile commands for the current buffers to an .exrc file.
11244 * Return FAIL on error, OK otherwise.
11245 */
11246 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011247makeopens(
11248 FILE *fd,
11249 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250{
11251 buf_T *buf;
11252 int only_save_windows = TRUE;
11253 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 int restore_size = TRUE;
11255 win_T *wp;
11256 char_u *sname;
11257 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011258 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011259 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011260 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011261 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011262 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011263 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264
11265 if (ssop_flags & SSOP_BUFFERS)
11266 only_save_windows = FALSE; /* Save ALL buffers */
11267
11268 /*
11269 * Begin by setting the this_session variable, and then other
11270 * sessionable variables.
11271 */
11272#ifdef FEAT_EVAL
11273 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11274 return FAIL;
11275 if (ssop_flags & SSOP_GLOBALS)
11276 if (store_session_globals(fd) == FAIL)
11277 return FAIL;
11278#endif
11279
11280 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011281 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 */
11283 if (put_line(fd, "silent only") == FAIL)
11284 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011285 if ((ssop_flags & SSOP_TABPAGES)
11286 && put_line(fd, "silent tabonly") == FAIL)
11287 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288
11289 /*
11290 * Now a :cd command to the session directory or the current directory
11291 */
11292 if (ssop_flags & SSOP_SESDIR)
11293 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011294 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11295 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 return FAIL;
11297 }
11298 else if (ssop_flags & SSOP_CURDIR)
11299 {
11300 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11301 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011302 || fputs("cd ", fd) < 0
11303 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11304 || put_eol(fd) == FAIL)
11305 {
11306 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309 vim_free(sname);
11310 }
11311
11312 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011313 * If there is an empty, unnamed buffer we will wipe it out later.
11314 * Remember the buffer number.
11315 */
11316 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11317 return FAIL;
11318 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11319 return FAIL;
11320 if (put_line(fd, "endif") == FAIL)
11321 return FAIL;
11322
11323 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324 * Now save the current files, current buffer first.
11325 */
11326 if (put_line(fd, "set shortmess=aoO") == FAIL)
11327 return FAIL;
11328
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011330 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11332 return FAIL;
11333
11334 if (ssop_flags & SSOP_RESIZE)
11335 {
11336 /* Note: after the restore we still check it worked!*/
11337 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11338 || put_eol(fd) == FAIL)
11339 return FAIL;
11340 }
11341
11342#ifdef FEAT_GUI
11343 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11344 {
11345 int x, y;
11346
11347 if (gui_mch_get_winpos(&x, &y) == OK)
11348 {
11349 /* Note: after the restore we still check it worked!*/
11350 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11351 return FAIL;
11352 }
11353 }
11354#endif
11355
11356 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011357 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11358 * will be displayed when creating the next tab. That resizes the windows
11359 * in the first tab, which may cause problems. Set 'showtabline' to 2
11360 * temporarily to avoid that.
11361 */
11362 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11363 {
11364 if (put_line(fd, "set stal=2") == FAIL)
11365 return FAIL;
11366 restore_stal = TRUE;
11367 }
11368
11369 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011370 * May repeat putting Windows for each tab, when "tabpages" is in
11371 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011372 * Don't use goto_tabpage(), it may change directory and trigger
11373 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011375 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011376 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011377 if ((ssop_flags & SSOP_TABPAGES))
11378 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011379 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011380
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011381 // Similar to ses_win_rec() below, populate the tab pages first so
11382 // later local options won't be copied to the new tabs.
11383 FOR_ALL_TABPAGES(tp)
11384 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011385 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011386 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011387 return FAIL;
11388 }
Bram Moolenaar18144c82006-04-12 21:52:12 +000011389 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011391 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +020011392 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011393
Bram Moolenaar18144c82006-04-12 21:52:12 +000011394 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011396 tabpage_T *tp = find_tabpage(tabnr);
11397
11398 if (tp == NULL)
11399 break; /* done all tab pages */
11400 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011401 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011402 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011403 tab_topframe = topframe;
11404 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011405 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011406 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011407 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011408 tab_topframe = tp->tp_topframe;
11409 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011410 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011411 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413
Bram Moolenaar18144c82006-04-12 21:52:12 +000011414 /*
11415 * Before creating the window layout, try loading one file. If this
11416 * is aborted we don't end up with a number of useless windows.
11417 * This may have side effects! (e.g., compressed or network file).
11418 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011419 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011420 {
11421 if (ses_do_win(wp)
11422 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011423 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011424#ifdef FEAT_QUICKFIX
11425 && !bt_nofile(wp->w_buffer)
11426#endif
11427 )
11428 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011429 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
11430 return FAIL;
11431 need_tabnext = FALSE;
11432
11433 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011434 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11435 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011436 return FAIL;
11437 if (!wp->w_arg_idx_invalid)
11438 edited_win = wp;
11439 break;
11440 }
11441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011443 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011444 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011445 return FAIL;
11446
Bram Moolenaar18144c82006-04-12 21:52:12 +000011447 /*
11448 * Save current window layout.
11449 */
11450 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011452 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011454 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11455 return FAIL;
11456 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11457 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458
Bram Moolenaar18144c82006-04-12 21:52:12 +000011459 /*
11460 * Check if window sizes can be restored (no windows omitted).
11461 * Remember the window number of the current window after restoring.
11462 */
11463 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011464 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011465 {
11466 if (ses_do_win(wp))
11467 ++nr;
11468 else
11469 restore_size = FALSE;
11470 if (curwin == wp)
11471 cnr = nr;
11472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473
Bram Moolenaar18144c82006-04-12 21:52:12 +000011474 /* Go to the first window. */
11475 if (put_line(fd, "wincmd t") == FAIL)
11476 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011477
Bram Moolenaar18144c82006-04-12 21:52:12 +000011478 /*
11479 * If more than one window, see if sizes can be restored.
11480 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11481 * resized when moving between windows.
11482 * Do this before restoring the view, so that the topline and the
11483 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011484 * winminheight and winminwidth need to be set to avoid an error if the
11485 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011486 */
Bram Moolenaar19834012018-06-12 17:03:39 +020011487 if (put_line(fd, "set winminheight=0") == FAIL
11488 || put_line(fd, "set winheight=1") == FAIL
11489 || put_line(fd, "set winminwidth=0") == FAIL
11490 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011491 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011492 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011493 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494
Bram Moolenaar18144c82006-04-12 21:52:12 +000011495 /*
11496 * Restore the view of the window (options, file, cursor, etc.).
11497 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011498 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011499 {
11500 if (!ses_do_win(wp))
11501 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011502 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11503 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011504 return FAIL;
11505 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11506 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011507 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011508 }
11509
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011510 /* The argument index in the first tab page is zero, need to set it in
11511 * each window. For further tab pages it's the window where we do
11512 * "tabedit". */
11513 cur_arg_idx = next_arg_idx;
11514
Bram Moolenaar18144c82006-04-12 21:52:12 +000011515 /*
11516 * Restore cursor to the current window if it's not the first one.
11517 */
11518 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11519 || put_eol(fd) == FAIL))
11520 return FAIL;
11521
11522 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011523 * Restore window sizes again after jumping around in windows, because
11524 * the current window has a minimum size while others may not.
11525 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011526 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011527 return FAIL;
11528
Bram Moolenaar18144c82006-04-12 21:52:12 +000011529 /* Don't continue in another tab page when doing only the current one
11530 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011531 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011532 break;
11533 }
11534
11535 if (ssop_flags & SSOP_TABPAGES)
11536 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011537 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11538 || put_eol(fd) == FAIL)
11539 return FAIL;
11540 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011541 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11542 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011543
Bram Moolenaard39e2752019-01-26 20:07:38 +010011544 // Now put the remaining buffers into the buffer list.
11545 // This is near the end, so that when 'hidden' is set we don't create extra
11546 // buffers. If the buffer was already created with another command the
11547 // ":badd" will have no effect.
11548 FOR_ALL_BUFFERS(buf)
11549 {
11550 if (!(only_save_windows && buf->b_nwindows == 0)
11551 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11552#ifdef FEAT_TERMINAL
11553 // Skip terminal buffers: finished ones are not useful, others
11554 // will be resurrected and result in a new buffer.
11555 && !bt_terminal(buf)
11556#endif
11557 && buf->b_fname != NULL
11558 && buf->b_p_bl)
11559 {
11560 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11561 : buf->b_wininfo->wi_fpos.lnum) < 0
11562 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
11563 return FAIL;
11564 }
11565 }
11566
Bram Moolenaar9c102382006-05-03 21:26:49 +000011567 /*
11568 * Wipe out an empty unnamed buffer we started in.
11569 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011570 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011571 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011572 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011573 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011574 return FAIL;
11575 if (put_line(fd, "endif") == FAIL)
11576 return FAIL;
11577 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11578 return FAIL;
11579
11580 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11581 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11582 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11583 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011584 /* Re-apply 'winminheight' and 'winminwidth'. */
11585 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11586 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11587 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588
11589 /*
11590 * Lastly, execute the x.vim file if it exists.
11591 */
11592 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11593 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011594 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595 || put_line(fd, "endif") == FAIL)
11596 return FAIL;
11597
11598 return OK;
11599}
11600
11601 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011602ses_winsizes(
11603 FILE *fd,
11604 int restore_size,
11605 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606{
11607 int n = 0;
11608 win_T *wp;
11609
11610 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11611 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011612 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613 {
11614 if (!ses_do_win(wp))
11615 continue;
11616 ++n;
11617
11618 /* restore height when not full height */
11619 if (wp->w_height + wp->w_status_height < topframe->fr_height
11620 && (fprintf(fd,
11621 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11622 n, (long)wp->w_height, Rows / 2, Rows) < 0
11623 || put_eol(fd) == FAIL))
11624 return FAIL;
11625
11626 /* restore width when not full width */
11627 if (wp->w_width < Columns && (fprintf(fd,
11628 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11629 n, (long)wp->w_width, Columns / 2, Columns) < 0
11630 || put_eol(fd) == FAIL))
11631 return FAIL;
11632 }
11633 }
11634 else
11635 {
11636 /* Just equalise window sizes */
11637 if (put_line(fd, "wincmd =") == FAIL)
11638 return FAIL;
11639 }
11640 return OK;
11641}
11642
11643/*
11644 * Write commands to "fd" to recursively create windows for frame "fr",
11645 * horizontally and vertically split.
11646 * After the commands the last window in the frame is the current window.
11647 * Returns FAIL when writing the commands to "fd" fails.
11648 */
11649 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011650ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011651{
11652 frame_T *frc;
11653 int count = 0;
11654
11655 if (fr->fr_layout != FR_LEAF)
11656 {
11657 /* Find first frame that's not skipped and then create a window for
11658 * each following one (first frame is already there). */
11659 frc = ses_skipframe(fr->fr_child);
11660 if (frc != NULL)
11661 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11662 {
11663 /* Make window as big as possible so that we have lots of room
11664 * to split. */
11665 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11666 || put_line(fd, fr->fr_layout == FR_COL
11667 ? "split" : "vsplit") == FAIL)
11668 return FAIL;
11669 ++count;
11670 }
11671
11672 /* Go back to the first window. */
11673 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11674 ? "%dwincmd k" : "%dwincmd h", count) < 0
11675 || put_eol(fd) == FAIL))
11676 return FAIL;
11677
11678 /* Recursively create frames/windows in each window of this column or
11679 * row. */
11680 frc = ses_skipframe(fr->fr_child);
11681 while (frc != NULL)
11682 {
11683 ses_win_rec(fd, frc);
11684 frc = ses_skipframe(frc->fr_next);
11685 /* Go to next window. */
11686 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11687 return FAIL;
11688 }
11689 }
11690 return OK;
11691}
11692
11693/*
11694 * Skip frames that don't contain windows we want to save in the Session.
11695 * Returns NULL when there none.
11696 */
11697 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011698ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699{
11700 frame_T *frc;
11701
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011702 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703 if (ses_do_frame(frc))
11704 break;
11705 return frc;
11706}
11707
11708/*
11709 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11710 * the Session.
11711 */
11712 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011713ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714{
11715 frame_T *frc;
11716
11717 if (fr->fr_layout == FR_LEAF)
11718 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011719 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720 if (ses_do_frame(frc))
11721 return TRUE;
11722 return FALSE;
11723}
11724
11725/*
11726 * Return non-zero if window "wp" is to be stored in the Session.
11727 */
11728 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011729ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011731#ifdef FEAT_TERMINAL
11732 if (bt_terminal(wp->w_buffer))
11733 return !term_is_finished(wp->w_buffer)
11734 && (ssop_flags & SSOP_TERMINAL)
11735 && term_should_restore(wp->w_buffer);
11736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 if (wp->w_buffer->b_fname == NULL
11738#ifdef FEAT_QUICKFIX
11739 /* When 'buftype' is "nofile" can't restore the window contents. */
11740 || bt_nofile(wp->w_buffer)
11741#endif
11742 )
11743 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011744 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011745 return (ssop_flags & SSOP_HELP);
11746 return TRUE;
11747}
11748
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011749 static int
11750put_view_curpos(FILE *fd, win_T *wp, char *spaces)
11751{
11752 int r;
11753
11754 if (wp->w_curswant == MAXCOL)
11755 r = fprintf(fd, "%snormal! $", spaces);
11756 else
11757 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
11758 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
11759}
11760
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761/*
11762 * Write commands to "fd" to restore the view of a window.
11763 * Caller must make sure 'scrolloff' is zero.
11764 */
11765 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011766put_view(
11767 FILE *fd,
11768 win_T *wp,
11769 int add_edit, /* add ":edit" command to view */
11770 unsigned *flagp, /* vop_flags or ssop_flags */
11771 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011772 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773{
11774 win_T *save_curwin;
11775 int f;
11776 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011777 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778
11779 /* Always restore cursor position for ":mksession". For ":mkview" only
11780 * when 'viewoptions' contains "cursor". */
11781 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11782
11783 /*
11784 * Local argument list.
11785 */
11786 if (wp->w_alist == &global_alist)
11787 {
11788 if (put_line(fd, "argglobal") == FAIL)
11789 return FAIL;
11790 }
11791 else
11792 {
11793 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11794 flagp == &vop_flags
11795 || !(*flagp & SSOP_CURDIR)
11796 || wp->w_localdir != NULL, flagp) == FAIL)
11797 return FAIL;
11798 }
11799
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011800 /* Only when part of a session: restore the argument index. Some
11801 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011802 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011803 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011805 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806 || put_eol(fd) == FAIL)
11807 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011808 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 }
11810
11811 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011812 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011814# ifdef FEAT_TERMINAL
11815 if (bt_terminal(wp->w_buffer))
11816 {
11817 if (term_write_session(fd, wp) == FAIL)
11818 return FAIL;
11819 }
11820 else
11821# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822 /*
11823 * Load the file.
11824 */
11825 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011826# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011828# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829 )
11830 {
11831 /*
11832 * Editing a file in this buffer: use ":edit file".
11833 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011834 *
11835 * Note, if a buffer for that file already exists, use :badd to
11836 * edit that buffer, to not lose folding information (:edit resets
11837 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838 */
Bram Moolenaarad36a352019-01-24 13:34:42 +010011839 if (fputs("if bufexists(\"", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011840 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
Bram Moolenaarad36a352019-01-24 13:34:42 +010011841 || fputs("\") | buffer ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011842 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11843 || fputs(" | else | edit ", fd) < 0
11844 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11845 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011846 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847 return FAIL;
11848 }
11849 else
11850 {
11851 /* No file in this buffer, just make it empty. */
11852 if (put_line(fd, "enew") == FAIL)
11853 return FAIL;
11854#ifdef FEAT_QUICKFIX
11855 if (wp->w_buffer->b_ffname != NULL)
11856 {
11857 /* The buffer does have a name, but it's not a file name. */
11858 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011859 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860 return FAIL;
11861 }
11862#endif
11863 do_cursor = FALSE;
11864 }
11865 }
11866
11867 /*
11868 * Local mappings and abbreviations.
11869 */
11870 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11871 && makemap(fd, wp->w_buffer) == FAIL)
11872 return FAIL;
11873
11874 /*
11875 * Local options. Need to go to the window temporarily.
11876 * Store only local values when using ":mkview" and when ":mksession" is
11877 * used and 'sessionoptions' doesn't include "options".
11878 * Some folding options are always stored when "folds" is included,
11879 * otherwise the folds would not be restored correctly.
11880 */
11881 save_curwin = curwin;
11882 curwin = wp;
11883 curbuf = curwin->w_buffer;
11884 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11885 f = makeset(fd, OPT_LOCAL,
11886 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11887#ifdef FEAT_FOLDING
11888 else if (*flagp & SSOP_FOLDS)
11889 f = makefoldset(fd);
11890#endif
11891 else
11892 f = OK;
11893 curwin = save_curwin;
11894 curbuf = curwin->w_buffer;
11895 if (f == FAIL)
11896 return FAIL;
11897
11898#ifdef FEAT_FOLDING
11899 /*
11900 * Save Folds when 'buftype' is empty and for help files.
11901 */
11902 if ((*flagp & SSOP_FOLDS)
11903 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020011904 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905 {
11906 if (put_folds(fd, wp) == FAIL)
11907 return FAIL;
11908 }
11909#endif
11910
11911 /*
11912 * Set the cursor after creating folds, since that moves the cursor.
11913 */
11914 if (do_cursor)
11915 {
11916
11917 /* Restore the cursor line in the file and relatively in the
11918 * window. Don't use "G", it changes the jumplist. */
11919 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11920 (long)wp->w_cursor.lnum,
11921 (long)(wp->w_cursor.lnum - wp->w_topline),
11922 (long)wp->w_height / 2, (long)wp->w_height) < 0
11923 || put_eol(fd) == FAIL
11924 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11925 || put_line(fd, "exe s:l") == FAIL
11926 || put_line(fd, "normal! zt") == FAIL
11927 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11928 || put_eol(fd) == FAIL)
11929 return FAIL;
11930 /* Restore the cursor column and left offset when not wrapping. */
11931 if (wp->w_cursor.col == 0)
11932 {
11933 if (put_line(fd, "normal! 0") == FAIL)
11934 return FAIL;
11935 }
11936 else
11937 {
11938 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11939 {
11940 if (fprintf(fd,
11941 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011942 (long)wp->w_virtcol + 1,
11943 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944 (long)wp->w_width / 2, (long)wp->w_width) < 0
11945 || put_eol(fd) == FAIL
11946 || put_line(fd, "if s:c > 0") == FAIL
11947 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011948 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11949 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950 || put_eol(fd) == FAIL
11951 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011952 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953 || put_line(fd, "endif") == FAIL)
11954 return FAIL;
11955 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011956 else if (put_view_curpos(fd, wp, "") == FAIL)
11957 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958 }
11959 }
11960
11961 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011962 * Local directory, if the current flag is not view options or the "curdir"
11963 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011965 if (wp->w_localdir != NULL
11966 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967 {
11968 if (fputs("lcd ", fd) < 0
11969 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11970 || put_eol(fd) == FAIL)
11971 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011972 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973 }
11974
11975 return OK;
11976}
11977
11978/*
11979 * Write an argument list to the session file.
11980 * Returns FAIL if writing fails.
11981 */
11982 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011983ses_arglist(
11984 FILE *fd,
11985 char *cmd,
11986 garray_T *gap,
11987 int fullname, /* TRUE: use full path name */
11988 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989{
11990 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011991 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 char_u *s;
11993
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011994 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11995 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010011996 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997 return FAIL;
11998 for (i = 0; i < gap->ga_len; ++i)
11999 {
12000 /* NULL file names are skipped (only happens when out of memory). */
12001 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
12002 if (s != NULL)
12003 {
12004 if (fullname)
12005 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020012006 buf = alloc(MAXPATHL);
12007 if (buf != NULL)
12008 {
12009 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
12010 s = buf;
12011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010012013 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010012014 || ses_put_fname(fd, s, flagp) == FAIL
12015 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020012016 {
12017 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012019 }
12020 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021 }
12022 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010012023 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024}
12025
12026/*
12027 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012028 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 * Returns FAIL if writing fails.
12030 */
12031 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012032ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033{
12034 char_u *name;
12035
12036 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012037 * the session file will be sourced.
12038 * Don't do this for ":mkview", we don't know the current directory.
12039 * Don't do this after ":lcd", we don't keep track of what the current
12040 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041 if (buf->b_sfname != NULL
12042 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012043 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000012044#ifdef FEAT_AUTOCHDIR
12045 && !p_acd
12046#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012047 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048 name = buf->b_sfname;
12049 else
12050 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012051 if (ses_put_fname(fd, name, flagp) == FAIL
12052 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053 return FAIL;
12054 return OK;
12055}
12056
12057/*
12058 * Write a file name to the session file.
12059 * Takes care of the "slash" option in 'sessionoptions' and escapes special
12060 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012061 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062 */
12063 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012064ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065{
12066 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012067 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069
12070 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012071 if (sname == NULL)
12072 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012074 if (*flagp & SSOP_SLASH)
12075 {
12076 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010012077 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012078 if (*p == '\\')
12079 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012081
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020012082 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012083 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012085 if (p == NULL)
12086 return FAIL;
12087
12088 /* write the result */
12089 if (fputs((char *)p, fd) < 0)
12090 retval = FAIL;
12091
12092 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093 return retval;
12094}
12095
12096/*
12097 * ":loadview [nr]"
12098 */
12099 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012100ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101{
12102 char_u *fname;
12103
12104 fname = get_view_file(*eap->arg);
12105 if (fname != NULL)
12106 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012107 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108 vim_free(fname);
12109 }
12110}
12111
12112/*
12113 * Get the name of the view file for the current buffer.
12114 */
12115 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012116get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117{
12118 int len = 0;
12119 char_u *p, *s;
12120 char_u *retval;
12121 char_u *sname;
12122
12123 if (curbuf->b_ffname == NULL)
12124 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012125 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126 return NULL;
12127 }
12128 sname = home_replace_save(NULL, curbuf->b_ffname);
12129 if (sname == NULL)
12130 return NULL;
12131
12132 /*
12133 * We want a file name without separators, because we're not going to make
12134 * a directory.
12135 * "normal" path separator -> "=+"
12136 * "=" -> "=="
12137 * ":" path separator -> "=-"
12138 */
12139 for (p = sname; *p; ++p)
12140 if (*p == '=' || vim_ispathsep(*p))
12141 ++len;
12142 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
12143 if (retval != NULL)
12144 {
12145 STRCPY(retval, p_vdir);
12146 add_pathsep(retval);
12147 s = retval + STRLEN(retval);
12148 for (p = sname; *p; ++p)
12149 {
12150 if (*p == '=')
12151 {
12152 *s++ = '=';
12153 *s++ = '=';
12154 }
12155 else if (vim_ispathsep(*p))
12156 {
12157 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020012158#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159 if (*p == ':')
12160 *s++ = '-';
12161 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012163 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164 }
12165 else
12166 *s++ = *p;
12167 }
12168 *s++ = '=';
12169 *s++ = c;
12170 STRCPY(s, ".vim");
12171 }
12172
12173 vim_free(sname);
12174 return retval;
12175}
12176
12177#endif /* FEAT_SESSION */
12178
12179/*
12180 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12181 * Return FAIL for a write error.
12182 */
12183 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012184put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185{
12186 if (
12187#ifdef USE_CRNL
12188 (
12189# ifdef MKSESSION_NL
12190 !mksession_nl &&
12191# endif
12192 (putc('\r', fd) < 0)) ||
12193#endif
12194 (putc('\n', fd) < 0))
12195 return FAIL;
12196 return OK;
12197}
12198
12199/*
12200 * Write a line to "fd".
12201 * Return FAIL for a write error.
12202 */
12203 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012204put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205{
12206 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12207 return FAIL;
12208 return OK;
12209}
12210
12211#ifdef FEAT_VIMINFO
12212/*
12213 * ":rviminfo" and ":wviminfo".
12214 */
12215 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012216ex_viminfo(
12217 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218{
12219 char_u *save_viminfo;
12220
12221 save_viminfo = p_viminfo;
12222 if (*p_viminfo == NUL)
12223 p_viminfo = (char_u *)"'100";
12224 if (eap->cmdidx == CMD_rviminfo)
12225 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012226 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12227 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012228 emsg(_("E195: Cannot open viminfo file for reading"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229 }
12230 else
12231 write_viminfo(eap->arg, eap->forceit);
12232 p_viminfo = save_viminfo;
12233}
12234#endif
12235
12236#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012237/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012238 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012239 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012242dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244 if (fname == NULL)
12245 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012246 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247}
12248#endif
12249
12250/*
12251 * ":behave {mswin,xterm}"
12252 */
12253 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012254ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255{
12256 if (STRCMP(eap->arg, "mswin") == 0)
12257 {
12258 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12259 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12260 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12261 set_option_value((char_u *)"keymodel", 0L,
12262 (char_u *)"startsel,stopsel", 0);
12263 }
12264 else if (STRCMP(eap->arg, "xterm") == 0)
12265 {
12266 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12267 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12268 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12269 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12270 }
12271 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012272 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273}
12274
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012275#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12276/*
12277 * Function given to ExpandGeneric() to obtain the possible arguments of the
12278 * ":behave {mswin,xterm}" command.
12279 */
12280 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012281get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012282{
12283 if (idx == 0)
12284 return (char_u *)"mswin";
12285 if (idx == 1)
12286 return (char_u *)"xterm";
12287 return NULL;
12288}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012289
12290/*
12291 * Function given to ExpandGeneric() to obtain the possible arguments of the
12292 * ":messages {clear}" command.
12293 */
12294 char_u *
12295get_messages_arg(expand_T *xp UNUSED, int idx)
12296{
12297 if (idx == 0)
12298 return (char_u *)"clear";
12299 return NULL;
12300}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012301#endif
12302
Bram Moolenaar113e1072019-01-20 15:30:40 +010012303#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012304 char_u *
12305get_mapclear_arg(expand_T *xp UNUSED, int idx)
12306{
12307 if (idx == 0)
12308 return (char_u *)"<buffer>";
12309 return NULL;
12310}
Bram Moolenaar113e1072019-01-20 15:30:40 +010012311#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012312
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313static int filetype_detect = FALSE;
12314static int filetype_plugin = FALSE;
12315static int filetype_indent = FALSE;
12316
12317/*
12318 * ":filetype [plugin] [indent] {on,off,detect}"
12319 * on: Load the filetype.vim file to install autocommands for file types.
12320 * off: Load the ftoff.vim file to remove all autocommands for file types.
12321 * plugin on: load filetype.vim and ftplugin.vim
12322 * plugin off: load ftplugof.vim
12323 * indent on: load filetype.vim and indent.vim
12324 * indent off: load indoff.vim
12325 */
12326 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012327ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328{
12329 char_u *arg = eap->arg;
12330 int plugin = FALSE;
12331 int indent = FALSE;
12332
12333 if (*eap->arg == NUL)
12334 {
12335 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012336 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337 filetype_detect ? "ON" : "OFF",
12338 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12339 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12340 return;
12341 }
12342
12343 /* Accept "plugin" and "indent" in any order. */
12344 for (;;)
12345 {
12346 if (STRNCMP(arg, "plugin", 6) == 0)
12347 {
12348 plugin = TRUE;
12349 arg = skipwhite(arg + 6);
12350 continue;
12351 }
12352 if (STRNCMP(arg, "indent", 6) == 0)
12353 {
12354 indent = TRUE;
12355 arg = skipwhite(arg + 6);
12356 continue;
12357 }
12358 break;
12359 }
12360 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12361 {
12362 if (*arg == 'o' || !filetype_detect)
12363 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012364 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365 filetype_detect = TRUE;
12366 if (plugin)
12367 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012368 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369 filetype_plugin = TRUE;
12370 }
12371 if (indent)
12372 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012373 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374 filetype_indent = TRUE;
12375 }
12376 }
12377 if (*arg == 'd')
12378 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012379 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012380 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381 }
12382 }
12383 else if (STRCMP(arg, "off") == 0)
12384 {
12385 if (plugin || indent)
12386 {
12387 if (plugin)
12388 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012389 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390 filetype_plugin = FALSE;
12391 }
12392 if (indent)
12393 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012394 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395 filetype_indent = FALSE;
12396 }
12397 }
12398 else
12399 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012400 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401 filetype_detect = FALSE;
12402 }
12403 }
12404 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012405 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406}
12407
12408/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012409 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410 */
12411 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012412ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413{
12414 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012415 {
12416 char_u *arg = eap->arg;
12417
12418 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12419 arg += 9;
12420
12421 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12422 if (arg != eap->arg)
12423 did_filetype = FALSE;
12424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012428ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429{
12430#ifdef FEAT_DIGRAPHS
12431 if (*eap->arg != NUL)
12432 putdigraph(eap->arg);
12433 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010012434 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012436 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437#endif
12438}
12439
12440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012441ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442{
12443 int flags = 0;
12444
12445 if (eap->cmdidx == CMD_setlocal)
12446 flags = OPT_LOCAL;
12447 else if (eap->cmdidx == CMD_setglobal)
12448 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012449#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450 if (cmdmod.browse && flags == 0)
12451 ex_options(eap);
12452 else
12453#endif
12454 (void)do_set(eap->arg, flags);
12455}
12456
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012457#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
12458 void
12459set_no_hlsearch(int flag)
12460{
12461 no_hlsearch = flag;
12462# ifdef FEAT_EVAL
12463 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
12464# endif
12465}
12466
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467/*
12468 * ":nohlsearch"
12469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012471ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012473 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012474 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475}
12476
12477/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012478 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479 * Sets nextcmd to the start of the next command, if any. Also called when
12480 * skipping commands to find the next command.
12481 */
12482 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012483ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484{
12485 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012486 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487 char_u *end;
12488 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012489 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012490
12491 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012492 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012493 else
12494 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012495 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012496 return;
12497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498
12499 /* First clear any old pattern. */
12500 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012501 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502
12503 if (ends_excmd(*eap->arg))
12504 end = eap->arg;
12505 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012506 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507 end = eap->arg + 4;
12508 else
12509 {
12510 p = skiptowhite(eap->arg);
12511 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012512 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513 p = skipwhite(p);
12514 if (*p == NUL)
12515 {
12516 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012517 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012518 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519 return;
12520 }
12521 end = skip_regexp(p + 1, *p, TRUE, NULL);
12522 if (!eap->skip)
12523 {
12524 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12525 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012526 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527 eap->errmsg = e_trailing;
12528 return;
12529 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012530 if (*end != *p)
12531 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012532 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012533 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012534 return;
12535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536
12537 c = *end;
12538 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012539 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012540 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012541 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542 }
12543 }
12544 eap->nextcmd = find_nextcmd(end);
12545}
12546#endif
12547
12548#ifdef FEAT_CRYPT
12549/*
12550 * ":X": Get crypt key
12551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012553ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012555 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012556 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557}
12558#endif
12559
12560#ifdef FEAT_FOLDING
12561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012562ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563{
12564 if (foldManualAllowed(TRUE))
12565 foldCreate(eap->line1, eap->line2);
12566}
12567
12568 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012569ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012570{
12571 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12572 eap->forceit, FALSE);
12573}
12574
12575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012576ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577{
12578 linenr_T lnum;
12579
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012580#ifdef FEAT_CLIPBOARD
12581 start_global_changes();
12582#endif
12583
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584 /* First set the marks for all lines closed/open. */
12585 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12586 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12587 ml_setmarked(lnum);
12588
12589 /* Execute the command on the marked lines. */
12590 global_exe(eap->arg);
12591 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012592#ifdef FEAT_CLIPBOARD
12593 end_global_changes();
12594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595}
12596#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012597
Bram Moolenaar39665952018-08-15 20:59:48 +020012598#ifdef FEAT_QUICKFIX
12599/*
12600 * Returns TRUE if the supplied Ex cmdidx is for a location list command
12601 * instead of a quickfix command.
12602 */
12603 int
12604is_loclist_cmd(int cmdidx)
12605{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020012606 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020012607 return FALSE;
12608 return cmdnames[cmdidx].cmd_name[0] == 'l';
12609}
12610#endif
12611
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012612# if defined(FEAT_TIMERS) || defined(PROTO)
12613 int
12614get_pressedreturn(void)
12615{
12616 return ex_pressedreturn;
12617}
12618
12619 void
12620set_pressedreturn(int val)
12621{
12622 ex_pressedreturn = val;
12623}
12624#endif