blob: fc86f9eab7a09727b873eab725001e63339ed485 [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
180#if defined(FEAT_GUI_W32) && 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;
1702 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001703 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
1705 vim_memset(&ea, 0, sizeof(ea));
1706 ea.line1 = 1;
1707 ea.line2 = 1;
1708#ifdef FEAT_EVAL
1709 ++ex_nesting_level;
1710#endif
1711
Bram Moolenaar21691f82012-12-05 19:13:18 +01001712 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 if (quitmore
1714#ifdef FEAT_EVAL
1715 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001716 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001717#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001718 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001719 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 --quitmore;
1721
1722 /*
1723 * Reset browse, confirm, etc.. They are restored when returning, for
1724 * recursive calls.
1725 */
1726 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001728 /* "#!anything" is handled like a comment. */
1729 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1730 goto doend;
1731
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001732/*
1733 * 1. Skip comment lines and leading white space and colons.
1734 * 2. Handle command modifiers.
1735 */
1736 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001738 ea.cmdlinep = cmdlinep;
1739 ea.getline = fgetline;
1740 ea.cookie = cookie;
1741#ifdef FEAT_EVAL
1742 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001744 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001745 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001747 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
1749#ifdef FEAT_EVAL
1750 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1751 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1752#else
1753 ea.skip = (if_level > 0);
1754#endif
1755
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001757 * 3. Skip over the range to find the command. Let "p" point to after it.
1758 *
1759 * We need the command to know what kind of range it uses.
1760 */
1761 cmd = ea.cmd;
1762 ea.cmd = skip_range(ea.cmd, NULL);
1763 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1764 ea.cmd = skipwhite(ea.cmd + 1);
1765 p = find_command(&ea, NULL);
1766
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001767#ifdef FEAT_EVAL
1768# ifdef FEAT_PROFILE
1769 // Count this line for profiling if skip is TRUE.
1770 if (do_profiling == PROF_YES
1771 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1772 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1773 {
1774 int skip = did_emsg || got_int || did_throw;
1775
1776 if (ea.cmdidx == CMD_catch)
1777 skip = !skip && !(cstack->cs_idx >= 0
1778 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1779 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1780 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1781 skip = skip || !(cstack->cs_idx >= 0
1782 && !(cstack->cs_flags[cstack->cs_idx]
1783 & (CSF_ACTIVE | CSF_TRUE)));
1784 else if (ea.cmdidx == CMD_finally)
1785 skip = FALSE;
1786 else if (ea.cmdidx != CMD_endif
1787 && ea.cmdidx != CMD_endfor
1788 && ea.cmdidx != CMD_endtry
1789 && ea.cmdidx != CMD_endwhile)
1790 skip = ea.skip;
1791
1792 if (!skip)
1793 {
1794 if (getline_equal(fgetline, cookie, get_func_line))
1795 func_line_exec(getline_cookie(fgetline, cookie));
1796 else if (getline_equal(fgetline, cookie, getsourceline))
1797 script_line_exec();
1798 }
1799 }
1800# endif
1801
1802 /* May go to debug mode. If this happens and the ">quit" debug command is
1803 * used, throw an interrupt exception and skip the next command. */
1804 dbg_check_breakpoint(&ea);
1805 if (!ea.skip && got_int)
1806 {
1807 ea.skip = TRUE;
1808 (void)do_intthrow(cstack);
1809 }
1810#endif
1811
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001812/*
1813 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 *
1815 * where 'addr' is:
1816 *
1817 * % (entire file)
1818 * $ [+-NUM]
1819 * 'x [+-NUM] (where x denotes a currently defined mark)
1820 * . [+-NUM]
1821 * [+-NUM]..
1822 * NUM
1823 *
1824 * The ea.cmd pointer is updated to point to the first character following the
1825 * range spec. If an initial address is found, but no second, the upper bound
1826 * is equal to the lower.
1827 */
1828
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01001829 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001830 if (!IS_USER_CMDIDX(ea.cmdidx))
1831 {
1832 if (ea.cmdidx != CMD_SIZE)
1833 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1834 else
1835 ea.addr_type = ADDR_LINES;
1836
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001837 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01001838 if (ea.cmdidx == CMD_wincmd && p != NULL)
1839 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001840 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001841
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001842 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001843 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001844 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001847 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 */
1849
1850 /*
1851 * Skip ':' and any white space
1852 */
1853 ea.cmd = skipwhite(ea.cmd);
1854 while (*ea.cmd == ':')
1855 ea.cmd = skipwhite(ea.cmd + 1);
1856
1857 /*
1858 * If we got a line, but no command, then go to the line.
1859 * If we find a '|' or '\n' we set ea.nextcmd.
1860 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001861 if (*ea.cmd == NUL || *ea.cmd == '"'
1862 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 {
1864 /*
1865 * strange vi behaviour:
1866 * ":3" jumps to line 3
1867 * ":3|..." prints line 3
1868 * ":|" prints current line
1869 */
1870 if (ea.skip) /* skip this if inside :if */
1871 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001872 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 {
1874 ea.cmdidx = CMD_print;
1875 ea.argt = RANGE+COUNT+TRLBAR;
1876 if ((errormsg = invalid_range(&ea)) == NULL)
1877 {
1878 correct_range(&ea);
1879 ex_print(&ea);
1880 }
1881 }
1882 else if (ea.addr_count != 0)
1883 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001884 if (ea.line2 > curbuf->b_ml.ml_line_count)
1885 {
1886 /* With '-' in 'cpoptions' a line number past the file is an
1887 * error, otherwise put it at the end of the file. */
1888 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1889 ea.line2 = -1;
1890 else
1891 ea.line2 = curbuf->b_ml.ml_line_count;
1892 }
1893
1894 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001895 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 else
1897 {
1898 if (ea.line2 == 0)
1899 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 else
1901 curwin->w_cursor.lnum = ea.line2;
1902 beginline(BL_SOL | BL_FIX);
1903 }
1904 }
1905 goto doend;
1906 }
1907
Bram Moolenaard5005162014-08-22 23:05:54 +02001908 /* If this looks like an undefined user command and there are CmdUndefined
1909 * autocommands defined, trigger the matching autocommands. */
1910 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1911 && ASCII_ISUPPER(*ea.cmd)
1912 && has_cmdundefined())
1913 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001914 int ret;
1915
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001916 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001917 while (ASCII_ISALNUM(*p))
1918 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001919 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001920 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1921 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001922 /* If the autocommands did something and didn't cause an error, try
1923 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001924 p = (ret
1925#ifdef FEAT_EVAL
1926 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001927#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001928 ) ? find_command(&ea, NULL) : ea.cmd;
1929 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001930
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931#ifdef FEAT_USR_CMDS
1932 if (p == NULL)
1933 {
1934 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001935 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 goto doend;
1937 }
1938 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001939 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1940 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
1942 errormsg = uc_fun_cmd();
1943 goto doend;
1944 }
1945#endif
1946 if (ea.cmdidx == CMD_SIZE)
1947 {
1948 if (!ea.skip)
1949 {
1950 STRCPY(IObuff, _("E492: Not an editor command"));
1951 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001952 {
1953 /* If the modifier was parsed OK the error must be in the
1954 * following command */
1955 if (after_modifier != NULL)
1956 append_command(after_modifier);
1957 else
1958 append_command(*cmdlinep);
1959 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001960 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001961 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 }
1963 goto doend;
1964 }
1965
Bram Moolenaar958636c2014-10-21 20:01:58 +02001966 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1967 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001968#ifdef HAVE_EX_SCRIPT_NI
1969 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1970#endif
1971 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972
1973#ifndef FEAT_EVAL
1974 /*
1975 * When the expression evaluation is disabled, recognize the ":if" and
1976 * ":endif" commands and ignore everything in between it.
1977 */
1978 if (ea.cmdidx == CMD_if)
1979 ++if_level;
1980 if (if_level)
1981 {
1982 if (ea.cmdidx == CMD_endif)
1983 --if_level;
1984 goto doend;
1985 }
1986
1987#endif
1988
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001989 /* forced commands */
1990 if (*p == '!' && ea.cmdidx != CMD_substitute
1991 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
1993 ++p;
1994 ea.forceit = TRUE;
1995 }
1996 else
1997 ea.forceit = FALSE;
1998
1999/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002000 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002002 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002003 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004
2005 if (!ea.skip)
2006 {
2007#ifdef HAVE_SANDBOX
2008 if (sandbox != 0 && !(ea.argt & SBOXOK))
2009 {
2010 /* Command not allowed in sandbox. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002011 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 goto doend;
2013 }
2014#endif
2015 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2016 {
2017 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002018 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 goto doend;
2020 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002021
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002022 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002023 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002025 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002026 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 goto doend;
2028 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002029
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002030 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02002031 * Do allow ":checktime" (it is postponed).
2032 * Do allow ":edit" (check for an argument later).
2033 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002034 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002035 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002036 && ea.cmdidx != CMD_edit
2037 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002038 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002039 && curbuf_locked())
2040 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
2042 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2043 {
2044 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002045 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 goto doend;
2047 }
2048 }
2049
2050 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2051 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002052 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 goto doend;
2054 }
2055
2056 /*
2057 * Don't complain about the range if it is not used
2058 * (could happen if line_count is accidentally set to 0).
2059 */
2060 if (!ea.skip && !ni)
2061 {
2062 /*
2063 * If the range is backwards, ask for confirmation and, if given, swap
2064 * ea.line1 & ea.line2 so it's forwards again.
2065 * When global command is busy, don't ask, will fail below.
2066 */
2067 if (!global_busy && ea.line1 > ea.line2)
2068 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002069 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002071 if (sourcing || exmode_active)
2072 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002073 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002074 goto doend;
2075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 if (ask_yesno((char_u *)
2077 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002078 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 }
2080 lnum = ea.line1;
2081 ea.line1 = ea.line2;
2082 ea.line2 = lnum;
2083 }
2084 if ((errormsg = invalid_range(&ea)) != NULL)
2085 goto doend;
2086 }
2087
2088 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2089 ea.line2 = 1;
2090
2091 correct_range(&ea);
2092
2093#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002094 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2095 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 {
2097 /* Put the first line at the start of a closed fold, put the last line
2098 * at the end of a closed fold. */
2099 (void)hasFolding(ea.line1, &ea.line1, NULL);
2100 (void)hasFolding(ea.line2, NULL, &ea.line2);
2101 }
2102#endif
2103
2104#ifdef FEAT_QUICKFIX
2105 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002106 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 * option here, so things like % get expanded.
2108 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002109 p = replace_makeprg(&ea, p, cmdlinep);
2110 if (p == NULL)
2111 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112#endif
2113
2114 /*
2115 * Skip to start of argument.
2116 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2117 */
2118 if (ea.cmdidx == CMD_bang)
2119 ea.arg = p;
2120 else
2121 ea.arg = skipwhite(p);
2122
Bram Moolenaar379fb762018-08-30 15:58:28 +02002123 // ":file" cannot be run with an argument when "curbuf_lock" is set
2124 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2125 goto doend;
2126
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 /*
2128 * Check for "++opt=val" argument.
2129 * Must be first, allow ":w ++enc=utf8 !cmd"
2130 */
2131 if (ea.argt & ARGOPT)
2132 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2133 if (getargopt(&ea) == FAIL && !ni)
2134 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002135 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 goto doend;
2137 }
2138
2139 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2140 {
2141 if (*ea.arg == '>') /* append */
2142 {
2143 if (*++ea.arg != '>') /* typed wrong */
2144 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002145 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 goto doend;
2147 }
2148 ea.arg = skipwhite(ea.arg + 1);
2149 ea.append = TRUE;
2150 }
2151 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2152 {
2153 ++ea.arg;
2154 ea.usefilter = TRUE;
2155 }
2156 }
2157
2158 if (ea.cmdidx == CMD_read)
2159 {
2160 if (ea.forceit)
2161 {
2162 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2163 ea.forceit = FALSE;
2164 }
2165 else if (*ea.arg == '!') /* :r !filter */
2166 {
2167 ++ea.arg;
2168 ea.usefilter = TRUE;
2169 }
2170 }
2171
2172 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2173 {
2174 ea.amount = 1;
2175 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2176 {
2177 ++ea.arg;
2178 ++ea.amount;
2179 }
2180 ea.arg = skipwhite(ea.arg);
2181 }
2182
2183 /*
2184 * Check for "+command" argument, before checking for next command.
2185 * Don't do this for ":read !cmd" and ":write !cmd".
2186 */
2187 if ((ea.argt & EDITCMD) && !ea.usefilter)
2188 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2189
2190 /*
2191 * Check for '|' to separate commands and '"' to start comments.
2192 * Don't do this for ":read !cmd" and ":write !cmd".
2193 */
2194 if ((ea.argt & TRLBAR) && !ea.usefilter)
2195 separate_nextcmd(&ea);
2196
2197 /*
2198 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002199 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2200 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002202 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002203 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002204 || ea.cmdidx == CMD_global
2205 || ea.cmdidx == CMD_vglobal
2206 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 {
2208 for (p = ea.arg; *p; ++p)
2209 {
2210 /* Remove one backslash before a newline, so that it's possible to
2211 * pass a newline to the shell and also a newline that is preceded
2212 * with a backslash. This makes it impossible to end a shell
2213 * command in a backslash, but that doesn't appear useful.
2214 * Halving the number of backslashes is incompatible with previous
2215 * versions. */
2216 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002217 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 else if (*p == '\n')
2219 {
2220 ea.nextcmd = p + 1;
2221 *p = NUL;
2222 break;
2223 }
2224 }
2225 }
2226
2227 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2228 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002229 buf_T *buf;
2230
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002232 switch (ea.addr_type)
2233 {
2234 case ADDR_LINES:
2235 ea.line2 = curbuf->b_ml.ml_line_count;
2236 break;
2237 case ADDR_LOADED_BUFFERS:
2238 buf = firstbuf;
2239 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2240 buf = buf->b_next;
2241 ea.line1 = buf->b_fnum;
2242 buf = lastbuf;
2243 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2244 buf = buf->b_prev;
2245 ea.line2 = buf->b_fnum;
2246 break;
2247 case ADDR_BUFFERS:
2248 ea.line1 = firstbuf->b_fnum;
2249 ea.line2 = lastbuf->b_fnum;
2250 break;
2251 case ADDR_WINDOWS:
2252 ea.line2 = LAST_WIN_NR;
2253 break;
2254 case ADDR_TABS:
2255 ea.line2 = LAST_TAB_NR;
2256 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002257 case ADDR_TABS_RELATIVE:
2258 ea.line2 = 1;
2259 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002260 case ADDR_ARGUMENTS:
2261 if (ARGCOUNT == 0)
2262 ea.line1 = ea.line2 = 0;
2263 else
2264 ea.line2 = ARGCOUNT;
2265 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002266#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002267 case ADDR_QUICKFIX:
2268 ea.line2 = qf_get_size(&ea);
2269 if (ea.line2 == 0)
2270 ea.line2 = 1;
2271 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002272#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
2275
2276 /* accept numbered register only when no count allowed (:put) */
2277 if ( (ea.argt & REGSTR)
2278 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002279 /* Do not allow register = for user commands */
2280 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2282 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002283#ifndef FEAT_CLIPBOARD
2284 /* check these explicitly for a more specific error message */
2285 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002287 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002288 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 }
2290#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002291 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2292 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002293 {
2294 ea.regname = *ea.arg++;
2295#ifdef FEAT_EVAL
2296 /* for '=' register: accept the rest of the line as an expression */
2297 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2298 {
2299 set_expr_line(vim_strsave(ea.arg));
2300 ea.arg += STRLEN(ea.arg);
2301 }
2302#endif
2303 ea.arg = skipwhite(ea.arg);
2304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 }
2306
2307 /*
2308 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2309 * count, it's a buffer name.
2310 */
2311 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2312 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002313 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
2315 n = getdigits(&ea.arg);
2316 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002317 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002319 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 goto doend;
2321 }
2322 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2323 {
2324 ea.line2 = n;
2325 if (ea.addr_count == 0)
2326 ea.addr_count = 1;
2327 }
2328 else
2329 {
2330 ea.line1 = ea.line2;
2331 ea.line2 += n - 1;
2332 ++ea.addr_count;
2333 /*
2334 * Be vi compatible: no error message for out of range.
2335 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002336 if (ea.addr_type == ADDR_LINES
2337 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 ea.line2 = curbuf->b_ml.ml_line_count;
2339 }
2340 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002341
2342 /*
2343 * Check for flags: 'l', 'p' and '#'.
2344 */
2345 if (ea.argt & EXFLAGS)
2346 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002348 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002349 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002351 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 goto doend;
2353 }
2354
2355 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002357 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 goto doend;
2359 }
2360
2361#ifdef FEAT_EVAL
2362 /*
2363 * Skip the command when it's not going to be executed.
2364 * The commands like :if, :endif, etc. always need to be executed.
2365 * Also make an exception for commands that handle a trailing command
2366 * themselves.
2367 */
2368 if (ea.skip)
2369 {
2370 switch (ea.cmdidx)
2371 {
2372 /* commands that need evaluation */
2373 case CMD_while:
2374 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002375 case CMD_for:
2376 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 case CMD_if:
2378 case CMD_elseif:
2379 case CMD_else:
2380 case CMD_endif:
2381 case CMD_try:
2382 case CMD_catch:
2383 case CMD_finally:
2384 case CMD_endtry:
2385 case CMD_function:
2386 break;
2387
2388 /* Commands that handle '|' themselves. Check: A command should
2389 * either have the TRLBAR flag, appear in this list or appear in
2390 * the list at ":help :bar". */
2391 case CMD_aboveleft:
2392 case CMD_and:
2393 case CMD_belowright:
2394 case CMD_botright:
2395 case CMD_browse:
2396 case CMD_call:
2397 case CMD_confirm:
2398 case CMD_delfunction:
2399 case CMD_djump:
2400 case CMD_dlist:
2401 case CMD_dsearch:
2402 case CMD_dsplit:
2403 case CMD_echo:
2404 case CMD_echoerr:
2405 case CMD_echomsg:
2406 case CMD_echon:
2407 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002408 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 case CMD_help:
2410 case CMD_hide:
2411 case CMD_ijump:
2412 case CMD_ilist:
2413 case CMD_isearch:
2414 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002415 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 case CMD_keepjumps:
2417 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002418 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 case CMD_leftabove:
2420 case CMD_let:
2421 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002422 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002424 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002425 case CMD_noautocmd:
2426 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 case CMD_perl:
2428 case CMD_psearch:
2429 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002430 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002431 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 case CMD_return:
2433 case CMD_rightbelow:
2434 case CMD_ruby:
2435 case CMD_silent:
2436 case CMD_smagic:
2437 case CMD_snomagic:
2438 case CMD_substitute:
2439 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002440 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 case CMD_tcl:
2442 case CMD_throw:
2443 case CMD_tilde:
2444 case CMD_topleft:
2445 case CMD_unlet:
2446 case CMD_verbose:
2447 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002448 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 break;
2450
2451 default: goto doend;
2452 }
2453 }
2454#endif
2455
2456 if (ea.argt & XFILE)
2457 {
2458 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2459 goto doend;
2460 }
2461
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 /*
2463 * Accept buffer name. Cannot be used at the same time with a buffer
2464 * number. Don't do this for a user command.
2465 */
2466 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002467 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 {
2469 /*
2470 * :bdelete, :bwipeout and :bunload take several arguments, separated
2471 * by spaces: find next space (skipping over escaped characters).
2472 * The others take one argument: ignore trailing spaces.
2473 */
2474 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2475 || ea.cmdidx == CMD_bunload)
2476 p = skiptowhite_esc(ea.arg);
2477 else
2478 {
2479 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002480 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 --p;
2482 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002483 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2484 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 if (ea.line2 < 0) /* failed */
2486 goto doend;
2487 ea.addr_count = 1;
2488 ea.arg = skipwhite(p);
2489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
Bram Moolenaar2be57332018-02-13 18:05:18 +01002491 /* The :try command saves the emsg_silent flag, reset it here when
2492 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002493 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002494 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002495 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002496 if (emsg_silent < 0)
2497 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002498 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002499 }
2500
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002502 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504
2505#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002506 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 {
2508 /*
2509 * Execute a user-defined command.
2510 */
2511 do_ucmd(&ea);
2512 }
2513 else
2514#endif
2515 {
2516 /*
2517 * Call the function to execute the command.
2518 */
2519 ea.errmsg = NULL;
2520 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2521 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002522 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 }
2524
2525#ifdef FEAT_EVAL
2526 /*
2527 * If the command just executed called do_cmdline(), any throw or ":return"
2528 * or ":finish" encountered there must also check the cstack of the still
2529 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2530 * exception, or reanimate a returned function or finished script file and
2531 * return or finish it again.
2532 */
2533 if (need_rethrow)
2534 do_throw(cstack);
2535 else if (check_cstack)
2536 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002537 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002539 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 && current_func_returned())
2541 do_return(&ea, TRUE, FALSE, NULL);
2542 }
2543 need_rethrow = check_cstack = FALSE;
2544#endif
2545
2546doend:
2547 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002548 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002550 curwin->w_cursor.col = 0;
2551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552
2553 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2554 {
2555 if (sourcing)
2556 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002557 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 {
2559 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002560 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002562 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 }
2564 emsg(errormsg);
2565 }
2566#ifdef FEAT_EVAL
2567 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002568 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2569 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570#endif
2571
Bram Moolenaareffed932018-08-14 13:38:17 +02002572 if (ea.verbose_save >= 0)
2573 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002574
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002575 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 cmdmod = save_cmdmod;
2577
Bram Moolenaareffed932018-08-14 13:38:17 +02002578 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 {
2580 /* messages could be enabled for a serious error, need to check if the
2581 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002582 if (!did_emsg || msg_silent > ea.save_msg_silent)
2583 msg_silent = ea.save_msg_silent;
2584 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if (emsg_silent < 0)
2586 emsg_silent = 0;
2587 /* Restore msg_scroll, it's set by file I/O commands, even when no
2588 * message is actually displayed. */
2589 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002590
2591 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2592 * somewhere in the line. Put it back in the first column. */
2593 if (redirecting())
2594 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 }
2596
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002597#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002598 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002599 --sandbox;
2600#endif
2601
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2603 ea.nextcmd = NULL;
2604
2605#ifdef FEAT_EVAL
2606 --ex_nesting_level;
2607#endif
2608
2609 return ea.nextcmd;
2610}
2611#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002612 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613#endif
2614
2615/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002616 * Parse and skip over command modifiers:
2617 * - update eap->cmd
2618 * - store flags in "cmdmod".
2619 * - Set ex_pressedreturn for an empty command line.
2620 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002621 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002622 * - set p_verbose for ":verbose"
2623 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002624 * When "skip_only" is TRUE the global variables are not changed, except for
2625 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002626 * Return FAIL when the command is not to be executed.
2627 * May set "errormsg" to an error message.
2628 */
2629 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002630parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002631{
2632 char_u *p;
2633
2634 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2635 eap->verbose_save = -1;
2636 eap->save_msg_silent = -1;
2637
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002638 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002639 for (;;)
2640 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002641 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2642 ++eap->cmd;
2643
2644 /* in ex mode, an empty line works like :+ */
2645 if (*eap->cmd == NUL && exmode_active
2646 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2647 || getline_equal(eap->getline, eap->cookie, getexline))
2648 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2649 {
2650 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002651 if (!skip_only)
2652 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002653 }
2654
2655 /* ignore comment and empty lines */
2656 if (*eap->cmd == '"')
2657 return FAIL;
2658 if (*eap->cmd == NUL)
2659 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002660 if (!skip_only)
2661 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002662 return FAIL;
2663 }
2664
Bram Moolenaareffed932018-08-14 13:38:17 +02002665 p = skip_range(eap->cmd, NULL);
2666 switch (*p)
2667 {
2668 /* When adding an entry, also modify cmd_exists(). */
2669 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2670 break;
2671 cmdmod.split |= WSP_ABOVE;
2672 continue;
2673
2674 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2675 {
2676 cmdmod.split |= WSP_BELOW;
2677 continue;
2678 }
2679 if (checkforcmd(&eap->cmd, "browse", 3))
2680 {
2681#ifdef FEAT_BROWSE_CMD
2682 cmdmod.browse = TRUE;
2683#endif
2684 continue;
2685 }
2686 if (!checkforcmd(&eap->cmd, "botright", 2))
2687 break;
2688 cmdmod.split |= WSP_BOT;
2689 continue;
2690
2691 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2692 break;
2693#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2694 cmdmod.confirm = TRUE;
2695#endif
2696 continue;
2697
2698 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2699 {
2700 cmdmod.keepmarks = TRUE;
2701 continue;
2702 }
2703 if (checkforcmd(&eap->cmd, "keepalt", 5))
2704 {
2705 cmdmod.keepalt = TRUE;
2706 continue;
2707 }
2708 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2709 {
2710 cmdmod.keeppatterns = TRUE;
2711 continue;
2712 }
2713 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2714 break;
2715 cmdmod.keepjumps = TRUE;
2716 continue;
2717
2718 case 'f': /* only accept ":filter {pat} cmd" */
2719 {
2720 char_u *reg_pat;
2721
2722 if (!checkforcmd(&p, "filter", 4)
2723 || *p == NUL || ends_excmd(*p))
2724 break;
2725 if (*p == '!')
2726 {
2727 cmdmod.filter_force = TRUE;
2728 p = skipwhite(p + 1);
2729 if (*p == NUL || ends_excmd(*p))
2730 break;
2731 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002732 if (skip_only)
2733 p = skip_vimgrep_pat(p, NULL, NULL);
2734 else
2735 // NOTE: This puts a NUL after the pattern.
2736 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002737 if (p == NULL || *p == NUL)
2738 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002739 if (!skip_only)
2740 {
2741 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002742 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002743 if (cmdmod.filter_regmatch.regprog == NULL)
2744 break;
2745 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002746 eap->cmd = p;
2747 continue;
2748 }
2749
2750 /* ":hide" and ":hide | cmd" are not modifiers */
2751 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2752 || *p == NUL || ends_excmd(*p))
2753 break;
2754 eap->cmd = p;
2755 cmdmod.hide = TRUE;
2756 continue;
2757
2758 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2759 {
2760 cmdmod.lockmarks = TRUE;
2761 continue;
2762 }
2763
2764 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2765 break;
2766 cmdmod.split |= WSP_ABOVE;
2767 continue;
2768
2769 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2770 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002771 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002772 {
2773 /* Set 'eventignore' to "all". Restore the
2774 * existing option value later. */
2775 cmdmod.save_ei = vim_strsave(p_ei);
2776 set_string_option_direct((char_u *)"ei", -1,
2777 (char_u *)"all", OPT_FREE, SID_NONE);
2778 }
2779 continue;
2780 }
2781 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2782 break;
2783 cmdmod.noswapfile = TRUE;
2784 continue;
2785
2786 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2787 break;
2788 cmdmod.split |= WSP_BELOW;
2789 continue;
2790
2791 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2792 {
2793#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002794 if (!skip_only)
2795 {
2796 if (!eap->did_sandbox)
2797 ++sandbox;
2798 eap->did_sandbox = TRUE;
2799 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002800#endif
2801 continue;
2802 }
2803 if (!checkforcmd(&eap->cmd, "silent", 3))
2804 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002805 if (!skip_only)
2806 {
2807 if (eap->save_msg_silent == -1)
2808 eap->save_msg_silent = msg_silent;
2809 ++msg_silent;
2810 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002811 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2812 {
2813 /* ":silent!", but not "silent !cmd" */
2814 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002815 if (!skip_only)
2816 {
2817 ++emsg_silent;
2818 ++eap->did_esilent;
2819 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002820 }
2821 continue;
2822
2823 case 't': if (checkforcmd(&p, "tab", 3))
2824 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002825 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002826 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002827 long tabnr = get_address(eap, &eap->cmd,
2828 ADDR_TABS, eap->skip,
2829 skip_only, FALSE, 1);
2830 if (tabnr == MAXLNUM)
2831 cmdmod.tab = tabpage_index(curtab) + 1;
2832 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002833 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002834 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2835 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002836 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002837 return FAIL;
2838 }
2839 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002841 }
2842 eap->cmd = p;
2843 continue;
2844 }
2845 if (!checkforcmd(&eap->cmd, "topleft", 2))
2846 break;
2847 cmdmod.split |= WSP_TOP;
2848 continue;
2849
2850 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2851 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002852 if (!skip_only)
2853 {
2854 if (eap->save_msg_silent == -1)
2855 eap->save_msg_silent = msg_silent;
2856 msg_silent = 0;
2857 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002858 continue;
2859
2860 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2861 {
2862 cmdmod.split |= WSP_VERT;
2863 continue;
2864 }
2865 if (!checkforcmd(&p, "verbose", 4))
2866 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002867 if (!skip_only)
2868 {
2869 if (eap->verbose_save < 0)
2870 eap->verbose_save = p_verbose;
2871 if (vim_isdigit(*eap->cmd))
2872 p_verbose = atoi((char *)eap->cmd);
2873 else
2874 p_verbose = 1;
2875 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002876 eap->cmd = p;
2877 continue;
2878 }
2879 break;
2880 }
2881
2882 return OK;
2883}
2884
2885/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002886 * Free contents of "cmdmod".
2887 */
2888 static void
2889free_cmdmod(void)
2890{
2891 if (cmdmod.save_ei != NULL)
2892 {
2893 /* Restore 'eventignore' to the value before ":noautocmd". */
2894 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2895 OPT_FREE, SID_NONE);
2896 free_string_option(cmdmod.save_ei);
2897 }
2898
2899 if (cmdmod.filter_regmatch.regprog != NULL)
2900 vim_regfree(cmdmod.filter_regmatch.regprog);
2901}
2902
2903/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002904 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002905 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002906 * Return FAIL and set "errormsg" or return OK.
2907 */
2908 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002909parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002910{
2911 int address_count = 1;
2912 linenr_T lnum;
2913
2914 // Repeat for all ',' or ';' separated addresses.
2915 for (;;)
2916 {
2917 eap->line1 = eap->line2;
2918 switch (eap->addr_type)
2919 {
2920 case ADDR_LINES:
2921 // default is current line number
2922 eap->line2 = curwin->w_cursor.lnum;
2923 break;
2924 case ADDR_WINDOWS:
2925 eap->line2 = CURRENT_WIN_NR;
2926 break;
2927 case ADDR_ARGUMENTS:
2928 eap->line2 = curwin->w_arg_idx + 1;
2929 if (eap->line2 > ARGCOUNT)
2930 eap->line2 = ARGCOUNT;
2931 break;
2932 case ADDR_LOADED_BUFFERS:
2933 case ADDR_BUFFERS:
2934 eap->line2 = curbuf->b_fnum;
2935 break;
2936 case ADDR_TABS:
2937 eap->line2 = CURRENT_TAB_NR;
2938 break;
2939 case ADDR_TABS_RELATIVE:
2940 eap->line2 = 1;
2941 break;
2942#ifdef FEAT_QUICKFIX
2943 case ADDR_QUICKFIX:
2944 eap->line2 = qf_get_cur_valid_idx(eap);
2945 break;
2946#endif
2947 }
2948 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002949 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002950 eap->addr_count == 0, address_count++);
2951 if (eap->cmd == NULL) // error detected
2952 return FAIL;
2953 if (lnum == MAXLNUM)
2954 {
2955 if (*eap->cmd == '%') // '%' - all lines
2956 {
2957 ++eap->cmd;
2958 switch (eap->addr_type)
2959 {
2960 case ADDR_LINES:
2961 eap->line1 = 1;
2962 eap->line2 = curbuf->b_ml.ml_line_count;
2963 break;
2964 case ADDR_LOADED_BUFFERS:
2965 {
2966 buf_T *buf = firstbuf;
2967
2968 while (buf->b_next != NULL
2969 && buf->b_ml.ml_mfp == NULL)
2970 buf = buf->b_next;
2971 eap->line1 = buf->b_fnum;
2972 buf = lastbuf;
2973 while (buf->b_prev != NULL
2974 && buf->b_ml.ml_mfp == NULL)
2975 buf = buf->b_prev;
2976 eap->line2 = buf->b_fnum;
2977 break;
2978 }
2979 case ADDR_BUFFERS:
2980 eap->line1 = firstbuf->b_fnum;
2981 eap->line2 = lastbuf->b_fnum;
2982 break;
2983 case ADDR_WINDOWS:
2984 case ADDR_TABS:
2985 if (IS_USER_CMDIDX(eap->cmdidx))
2986 {
2987 eap->line1 = 1;
2988 eap->line2 = eap->addr_type == ADDR_WINDOWS
2989 ? LAST_WIN_NR : LAST_TAB_NR;
2990 }
2991 else
2992 {
2993 // there is no Vim command which uses '%' and
2994 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002995 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002996 return FAIL;
2997 }
2998 break;
2999 case ADDR_TABS_RELATIVE:
Bram Moolenaar51a74542018-12-02 18:21:49 +01003000 case ADDR_OTHER:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003001 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003002 return FAIL;
3003 case ADDR_ARGUMENTS:
3004 if (ARGCOUNT == 0)
3005 eap->line1 = eap->line2 = 0;
3006 else
3007 {
3008 eap->line1 = 1;
3009 eap->line2 = ARGCOUNT;
3010 }
3011 break;
3012#ifdef FEAT_QUICKFIX
3013 case ADDR_QUICKFIX:
3014 eap->line1 = 1;
3015 eap->line2 = qf_get_size(eap);
3016 if (eap->line2 == 0)
3017 eap->line2 = 1;
3018 break;
3019#endif
3020 }
3021 ++eap->addr_count;
3022 }
3023 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3024 {
3025 pos_T *fp;
3026
3027 // '*' - visual area
3028 if (eap->addr_type != ADDR_LINES)
3029 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003030 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003031 return FAIL;
3032 }
3033
3034 ++eap->cmd;
3035 if (!eap->skip)
3036 {
3037 fp = getmark('<', FALSE);
3038 if (check_mark(fp) == FAIL)
3039 return FAIL;
3040 eap->line1 = fp->lnum;
3041 fp = getmark('>', FALSE);
3042 if (check_mark(fp) == FAIL)
3043 return FAIL;
3044 eap->line2 = fp->lnum;
3045 ++eap->addr_count;
3046 }
3047 }
3048 }
3049 else
3050 eap->line2 = lnum;
3051 eap->addr_count++;
3052
3053 if (*eap->cmd == ';')
3054 {
3055 if (!eap->skip)
3056 {
3057 curwin->w_cursor.lnum = eap->line2;
3058 // don't leave the cursor on an illegal line or column
3059 check_cursor();
3060 }
3061 }
3062 else if (*eap->cmd != ',')
3063 break;
3064 ++eap->cmd;
3065 }
3066
3067 // One address given: set start and end lines.
3068 if (eap->addr_count == 1)
3069 {
3070 eap->line1 = eap->line2;
3071 // ... but only implicit: really no address given
3072 if (lnum == MAXLNUM)
3073 eap->addr_count = 0;
3074 }
3075 return OK;
3076}
3077
3078/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003079 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 * If there is a match advance "pp" to the argument and return TRUE.
3081 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003082 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003083checkforcmd(
3084 char_u **pp, /* start of command */
3085 char *cmd, /* name of command */
3086 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087{
3088 int i;
3089
3090 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003091 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 break;
3093 if (i >= len && !isalpha((*pp)[i]))
3094 {
3095 *pp = skipwhite(*pp + i);
3096 return TRUE;
3097 }
3098 return FALSE;
3099}
3100
3101/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003102 * Append "cmd" to the error message in IObuff.
3103 * Takes care of limiting the length and handling 0xa0, which would be
3104 * invisible otherwise.
3105 */
3106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003107append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003108{
3109 char_u *s = cmd;
3110 char_u *d;
3111
3112 STRCAT(IObuff, ": ");
3113 d = IObuff + STRLEN(IObuff);
3114 while (*s != NUL && d - IObuff < IOSIZE - 7)
3115 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003116 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003117 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003118 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003119 STRCPY(d, "<a0>");
3120 d += 4;
3121 }
3122 else
3123 MB_COPY_CHAR(s, d);
3124 }
3125 *d = NUL;
3126}
3127
3128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003130 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003132 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 * Returns NULL for an ambiguous user command.
3134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003136find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137{
3138 int len;
3139 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003140 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141
3142 /*
3143 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003144 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 * - the 'k' command can directly be followed by any character.
3146 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003147 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003149 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 */
3151 p = eap->cmd;
3152 if (*p == 'k')
3153 {
3154 eap->cmdidx = CMD_k;
3155 ++p;
3156 }
3157 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003158 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3159 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 || p[1] == 'g'
3161 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3162 || p[1] == 'I'
3163 || (p[1] == 'r' && p[2] != 'e')))
3164 {
3165 eap->cmdidx = CMD_substitute;
3166 ++p;
3167 }
3168 else
3169 {
3170 while (ASCII_ISALPHA(*p))
3171 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003172 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003173 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003174 while (ASCII_ISALNUM(*p))
3175 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003176
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 /* check for non-alpha command */
3178 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3179 ++p;
3180 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003181 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3182 {
3183 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3184 * :delete with the 'l' flag. Same for 'p'. */
3185 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003186 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003187 break;
3188 if (i == len - 1)
3189 {
3190 --len;
3191 if (p[-1] == 'l')
3192 eap->flags |= EXFLAG_LIST;
3193 else
3194 eap->flags |= EXFLAG_PRINT;
3195 }
3196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003198 if (ASCII_ISLOWER(eap->cmd[0]))
3199 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003200 int c1 = eap->cmd[0];
3201 int c2 = eap->cmd[1];
3202
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003203 if (command_count != (int)CMD_SIZE)
3204 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003205 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003206 getout(1);
3207 }
3208
3209 /* Use a precomputed index for fast look-up in cmdnames[]
3210 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003211 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3212 if (ASCII_ISLOWER(c2))
3213 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003216 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
3218 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3219 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3220 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3221 (size_t)len) == 0)
3222 {
3223#ifdef FEAT_EVAL
3224 if (full != NULL
3225 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3226 *full = TRUE;
3227#endif
3228 break;
3229 }
3230
3231#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003232 /* Look for a user defined command as a last resort. Let ":Print" be
3233 * overruled by a user defined command. */
3234 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3235 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003237 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 while (ASCII_ISALNUM(*p))
3239 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003240 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 }
3242#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003243 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 eap->cmdidx = CMD_SIZE;
3245 }
3246
3247 return p;
3248}
3249
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003250#ifdef FEAT_USR_CMDS
3251/*
3252 * Search for a user command that matches "eap->cmd".
3253 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3254 * Return a pointer to just after the command.
3255 * Return NULL if there is no matching command.
3256 */
3257 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003258find_ucmd(
3259 exarg_T *eap,
3260 char_u *p, /* end of the command (possibly including count) */
3261 int *full, /* set to TRUE for a full match */
3262 expand_T *xp, /* used for completion, NULL otherwise */
3263 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003264{
3265 int len = (int)(p - eap->cmd);
3266 int j, k, matchlen = 0;
3267 ucmd_T *uc;
3268 int found = FALSE;
3269 int possible = FALSE;
3270 char_u *cp, *np; /* Point into typed cmd and test name */
3271 garray_T *gap;
3272 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3273 only full match global is accepted. */
3274
3275 /*
3276 * Look for buffer-local user commands first, then global ones.
3277 */
3278 gap = &curbuf->b_ucmds;
3279 for (;;)
3280 {
3281 for (j = 0; j < gap->ga_len; ++j)
3282 {
3283 uc = USER_CMD_GA(gap, j);
3284 cp = eap->cmd;
3285 np = uc->uc_name;
3286 k = 0;
3287 while (k < len && *np != NUL && *cp++ == *np++)
3288 k++;
3289 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3290 {
3291 /* If finding a second match, the command is ambiguous. But
3292 * not if a buffer-local command wasn't a full match and a
3293 * global command is a full match. */
3294 if (k == len && found && *np != NUL)
3295 {
3296 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003297 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003298 amb_local = TRUE;
3299 }
3300
3301 if (!found || (k == len && *np == NUL))
3302 {
3303 /* If we matched up to a digit, then there could
3304 * be another command including the digit that we
3305 * should use instead.
3306 */
3307 if (k == len)
3308 found = TRUE;
3309 else
3310 possible = TRUE;
3311
3312 if (gap == &ucmds)
3313 eap->cmdidx = CMD_USER;
3314 else
3315 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003316 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003317 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003318 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003319
3320# ifdef FEAT_CMDL_COMPL
3321 if (compl != NULL)
3322 *compl = uc->uc_compl;
3323# ifdef FEAT_EVAL
3324 if (xp != NULL)
3325 {
3326 xp->xp_arg = uc->uc_compl_arg;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003327 xp->xp_script_ctx = uc->uc_script_ctx;
3328 xp->xp_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003329 }
3330# endif
3331# endif
3332 /* Do not search for further abbreviations
3333 * if this is an exact match. */
3334 matchlen = k;
3335 if (k == len && *np == NUL)
3336 {
3337 if (full != NULL)
3338 *full = TRUE;
3339 amb_local = FALSE;
3340 break;
3341 }
3342 }
3343 }
3344 }
3345
3346 /* Stop if we found a full match or searched all. */
3347 if (j < gap->ga_len || gap == &ucmds)
3348 break;
3349 gap = &ucmds;
3350 }
3351
3352 /* Only found ambiguous matches. */
3353 if (amb_local)
3354 {
3355 if (xp != NULL)
3356 xp->xp_context = EXPAND_UNSUCCESSFUL;
3357 return NULL;
3358 }
3359
3360 /* The match we found may be followed immediately by a number. Move "p"
3361 * back to point to it. */
3362 if (found || possible)
3363 return p + (matchlen - len);
3364 return p;
3365}
3366#endif
3367
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003369static struct cmdmod
3370{
3371 char *name;
3372 int minlen;
3373 int has_count; /* :123verbose :3tab */
3374} cmdmods[] = {
3375 {"aboveleft", 3, FALSE},
3376 {"belowright", 3, FALSE},
3377 {"botright", 2, FALSE},
3378 {"browse", 3, FALSE},
3379 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003380 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003381 {"hide", 3, FALSE},
3382 {"keepalt", 5, FALSE},
3383 {"keepjumps", 5, FALSE},
3384 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003385 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003386 {"leftabove", 5, FALSE},
3387 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003388 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003389 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003390 {"rightbelow", 6, FALSE},
3391 {"sandbox", 3, FALSE},
3392 {"silent", 3, FALSE},
3393 {"tab", 3, TRUE},
3394 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003395 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003396 {"verbose", 4, TRUE},
3397 {"vertical", 4, FALSE},
3398};
3399
3400/*
3401 * Return length of a command modifier (including optional count).
3402 * Return zero when it's not a modifier.
3403 */
3404 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003405modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003406{
3407 int i, j;
3408 char_u *p = cmd;
3409
3410 if (VIM_ISDIGIT(*cmd))
3411 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003412 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003413 {
3414 for (j = 0; p[j] != NUL; ++j)
3415 if (p[j] != cmdmods[i].name[j])
3416 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003417 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003418 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003419 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003420 }
3421 return 0;
3422}
3423
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424/*
3425 * Return > 0 if an Ex command "name" exists.
3426 * Return 2 if there is an exact match.
3427 * Return 3 if there is an ambiguous match.
3428 */
3429 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003430cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431{
3432 exarg_T ea;
3433 int full = FALSE;
3434 int i;
3435 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003436 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
3438 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003439 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 {
3441 for (j = 0; name[j] != NUL; ++j)
3442 if (name[j] != cmdmods[i].name[j])
3443 break;
3444 if (name[j] == NUL && j >= cmdmods[i].minlen)
3445 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3446 }
3447
Bram Moolenaara9587612006-05-04 21:47:50 +00003448 /* Check built-in commands and user defined commands.
3449 * For ":2match" and ":3match" we need to skip the number. */
3450 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003452 p = find_command(&ea, &full);
3453 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003455 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3456 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003457 if (*skipwhite(p) != NUL)
3458 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3460}
3461#endif
3462
3463/*
3464 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3465 * we don't need/want deleted. Maybe this could be done better if we didn't
3466 * repeat all this stuff. The only problem is that they may not stay
3467 * perfectly compatible with each other, but then the command line syntax
3468 * probably won't change that much -- webb.
3469 */
3470 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003471set_one_cmd_context(
3472 expand_T *xp,
3473 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474{
3475 char_u *p;
3476 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003477 int len = 0;
3478 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3480 int compl = EXPAND_NOTHING;
3481#endif
3482#ifdef FEAT_CMDL_COMPL
3483 int delim;
3484#endif
3485 int forceit = FALSE;
3486 int usefilter = FALSE; /* filter instead of file name */
3487
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003488 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 xp->xp_pattern = buff;
3490 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003491 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
3493/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003494 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 */
3496 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3497 ;
3498 xp->xp_pattern = cmd;
3499
3500 if (*cmd == NUL)
3501 return NULL;
3502 if (*cmd == '"') /* ignore comment lines */
3503 {
3504 xp->xp_context = EXPAND_NOTHING;
3505 return NULL;
3506 }
3507
3508/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003509 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 */
3511 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 xp->xp_pattern = cmd;
3513 if (*cmd == NUL)
3514 return NULL;
3515 if (*cmd == '"')
3516 {
3517 xp->xp_context = EXPAND_NOTHING;
3518 return NULL;
3519 }
3520
3521 if (*cmd == '|' || *cmd == '\n')
3522 return cmd + 1; /* There's another command */
3523
3524 /*
3525 * Isolate the command and search for it in the command table.
3526 * Exceptions:
3527 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003528 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3530 */
3531 if (*cmd == 'k' && cmd[1] != 'e')
3532 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003533 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 p = cmd + 1;
3535 }
3536 else
3537 {
3538 p = cmd;
3539 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3540 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003541 /* a user command may contain digits */
3542 if (ASCII_ISUPPER(cmd[0]))
3543 while (ASCII_ISALNUM(*p) || *p == '*')
3544 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003545 /* for python 3.x: ":py3*" commands completion */
3546 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003547 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003548 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003549 while (ASCII_ISALPHA(*p) || *p == '*')
3550 ++p;
3551 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003552 /* check for non-alpha command */
3553 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3554 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003555 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003557 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
3559 xp->xp_context = EXPAND_UNSUCCESSFUL;
3560 return NULL;
3561 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003562 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003563 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3564 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3565 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 break;
3567
3568#ifdef FEAT_USR_CMDS
3569 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3571 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572#endif
3573 }
3574
3575 /*
3576 * If the cursor is touching the command, and it ends in an alpha-numeric
3577 * character, complete the command name.
3578 */
3579 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3580 return NULL;
3581
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003582 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
3584 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3585 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003586 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 p = cmd + 1;
3588 }
3589#ifdef FEAT_USR_CMDS
3590 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3591 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003592 ea.cmd = cmd;
3593 p = find_ucmd(&ea, p, NULL, xp,
3594# if defined(FEAT_CMDL_COMPL)
3595 &compl
3596# else
3597 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003599 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003600 if (p == NULL)
3601 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603#endif
3604 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003605 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
3607 /* Not still touching the command and it was an illegal one */
3608 xp->xp_context = EXPAND_UNSUCCESSFUL;
3609 return NULL;
3610 }
3611
3612 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3613
3614 if (*p == '!') /* forced commands */
3615 {
3616 forceit = TRUE;
3617 ++p;
3618 }
3619
3620/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003621 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003623 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003624 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625
3626 arg = skipwhite(p);
3627
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003628 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 {
3630 if (*arg == '>') /* append */
3631 {
3632 if (*++arg == '>')
3633 ++arg;
3634 arg = skipwhite(arg);
3635 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003636 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 {
3638 ++arg;
3639 usefilter = TRUE;
3640 }
3641 }
3642
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003643 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 {
3645 usefilter = forceit; /* :r! filter if forced */
3646 if (*arg == '!') /* :r !filter */
3647 {
3648 ++arg;
3649 usefilter = TRUE;
3650 }
3651 }
3652
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003653 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 {
3655 while (*arg == *cmd) /* allow any number of '>' or '<' */
3656 ++arg;
3657 arg = skipwhite(arg);
3658 }
3659
3660 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003661 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
3663 /* Check if we're in the +command */
3664 p = arg + 1;
3665 arg = skip_cmd_arg(arg, FALSE);
3666
3667 /* Still touching the command after '+'? */
3668 if (*arg == NUL)
3669 return p;
3670
3671 /* Skip space(s) after +command to get to the real argument */
3672 arg = skipwhite(arg);
3673 }
3674
3675 /*
3676 * Check for '|' to separate commands and '"' to start comments.
3677 * Don't do this for ":read !cmd" and ":write !cmd".
3678 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003679 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 {
3681 p = arg;
3682 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003683 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 p += 2;
3685 while (*p)
3686 {
3687 if (*p == Ctrl_V)
3688 {
3689 if (p[1] != NUL)
3690 ++p;
3691 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003692 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 || *p == '|' || *p == '\n')
3694 {
3695 if (*(p - 1) != '\\')
3696 {
3697 if (*p == '|' || *p == '\n')
3698 return p + 1;
3699 return NULL; /* It's a comment */
3700 }
3701 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003702 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 }
3704 }
3705
3706 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003707 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 vim_strchr((char_u *)"|\"", *arg) == NULL)
3709 return NULL;
3710
3711 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003712 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003714 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003715 while (*p && p < buff + len)
3716 {
3717 if (*p == ' ' || *p == TAB)
3718 {
3719 /* argument starts after a space */
3720 xp->xp_pattern = ++p;
3721 }
3722 else
3723 {
3724 if (*p == '\\' && *(p + 1) != NUL)
3725 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003726 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003727 }
3728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003730 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003732 int c;
3733 int in_quote = FALSE;
3734 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
3736 /*
3737 * Allow spaces within back-quotes to count as part of the argument
3738 * being expanded.
3739 */
3740 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003741 p = xp->xp_pattern;
3742 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003744 if (has_mbyte)
3745 c = mb_ptr2char(p);
3746 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003747 c = *p;
3748 if (c == '\\' && p[1] != NUL)
3749 ++p;
3750 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 {
3752 if (!in_quote)
3753 {
3754 xp->xp_pattern = p;
3755 bow = p + 1;
3756 }
3757 in_quote = !in_quote;
3758 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003759 /* An argument can contain just about everything, except
3760 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003761 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003762#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003763 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003764#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003765 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003766 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003767 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003768 while (*p != NUL)
3769 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003770 if (has_mbyte)
3771 c = mb_ptr2char(p);
3772 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003773 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003774 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003775 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003776 if (has_mbyte)
3777 len = (*mb_ptr2len)(p);
3778 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003779 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003780 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003781 }
3782 if (in_quote)
3783 bow = p;
3784 else
3785 xp->xp_pattern = p;
3786 p -= len;
3787 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003788 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
3790
3791 /*
3792 * If we are still inside the quotes, and we passed a space, just
3793 * expand from there.
3794 */
3795 if (bow != NULL && in_quote)
3796 xp->xp_pattern = bow;
3797 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003798
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003799 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003800 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003801 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003802#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003803 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003804#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003805 /* When still after the command name expand executables. */
3806 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003807 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809
3810 /* Check for environment variable */
3811 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003812#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 || *xp->xp_pattern == '%'
3814#endif
3815 )
3816 {
3817 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3818 if (!vim_isIDc(*p))
3819 break;
3820 if (*p == NUL)
3821 {
3822 xp->xp_context = EXPAND_ENV_VARS;
3823 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003824#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3825 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003826 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003827 compl = EXPAND_ENV_VARS;
3828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
3830 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003831#if defined(FEAT_CMDL_COMPL)
3832 /* Check for user names */
3833 if (*xp->xp_pattern == '~')
3834 {
3835 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3836 ;
3837 /* Complete ~user only if it partially matches a user name.
3838 * A full match ~user<Tab> will be replaced by user's home
3839 * directory i.e. something like ~user<Tab> -> /home/user/ */
3840 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003841 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003842 {
3843 xp->xp_context = EXPAND_USER;
3844 ++xp->xp_pattern;
3845 }
3846 }
3847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849
3850/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003851 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003853 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003855 case CMD_find:
3856 case CMD_sfind:
3857 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003858 if (xp->xp_context == EXPAND_FILES)
3859 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003860 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 case CMD_cd:
3862 case CMD_chdir:
3863 case CMD_lcd:
3864 case CMD_lchdir:
3865 if (xp->xp_context == EXPAND_FILES)
3866 xp->xp_context = EXPAND_DIRECTORIES;
3867 break;
3868 case CMD_help:
3869 xp->xp_context = EXPAND_HELP;
3870 xp->xp_pattern = arg;
3871 break;
3872
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003873 /* Command modifiers: return the argument.
3874 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003876 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 case CMD_belowright:
3878 case CMD_botright:
3879 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003880 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003881 case CMD_cdo:
3882 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003884 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 case CMD_folddoclosed:
3886 case CMD_folddoopen:
3887 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003888 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 case CMD_keepjumps:
3890 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003891 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003892 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003894 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003896 case CMD_noautocmd:
3897 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003899 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003901 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003902 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 case CMD_topleft:
3904 case CMD_verbose:
3905 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003906 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 return arg;
3908
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003909 case CMD_filter:
3910 if (*arg != NUL)
3911 arg = skip_vimgrep_pat(arg, NULL, NULL);
3912 if (arg == NULL || *arg == NUL)
3913 {
3914 xp->xp_context = EXPAND_NOTHING;
3915 return NULL;
3916 }
3917 return skipwhite(arg);
3918
Bram Moolenaar4f688582007-07-24 12:34:30 +00003919#ifdef FEAT_CMDL_COMPL
3920# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 case CMD_match:
3922 if (*arg == NUL || !ends_excmd(*arg))
3923 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003924 /* also complete "None" */
3925 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 arg = skipwhite(skiptowhite(arg));
3927 if (*arg != NUL)
3928 {
3929 xp->xp_context = EXPAND_NOTHING;
3930 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3931 }
3932 }
3933 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003934# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936/*
3937 * All completion for the +cmdline_compl feature goes here.
3938 */
3939
3940# ifdef FEAT_USR_CMDS
3941 case CMD_command:
3942 /* Check for attributes */
3943 while (*arg == '-')
3944 {
3945 arg++; /* Skip "-" */
3946 p = skiptowhite(arg);
3947 if (*p == NUL)
3948 {
3949 /* Cursor is still in the attribute */
3950 p = vim_strchr(arg, '=');
3951 if (p == NULL)
3952 {
3953 /* No "=", so complete attribute names */
3954 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3955 xp->xp_pattern = arg;
3956 return NULL;
3957 }
3958
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003959 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 * their arguments as well.
3961 */
3962 if (STRNICMP(arg, "complete", p - arg) == 0)
3963 {
3964 xp->xp_context = EXPAND_USER_COMPLETE;
3965 xp->xp_pattern = p + 1;
3966 return NULL;
3967 }
3968 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3969 {
3970 xp->xp_context = EXPAND_USER_NARGS;
3971 xp->xp_pattern = p + 1;
3972 return NULL;
3973 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003974 else if (STRNICMP(arg, "addr", p - arg) == 0)
3975 {
3976 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3977 xp->xp_pattern = p + 1;
3978 return NULL;
3979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 return NULL;
3981 }
3982 arg = skipwhite(p);
3983 }
3984
3985 /* After the attributes comes the new command name */
3986 p = skiptowhite(arg);
3987 if (*p == NUL)
3988 {
3989 xp->xp_context = EXPAND_USER_COMMANDS;
3990 xp->xp_pattern = arg;
3991 break;
3992 }
3993
3994 /* And finally comes a normal command */
3995 return skipwhite(p);
3996
3997 case CMD_delcommand:
3998 xp->xp_context = EXPAND_USER_COMMANDS;
3999 xp->xp_pattern = arg;
4000 break;
4001# endif
4002
4003 case CMD_global:
4004 case CMD_vglobal:
4005 delim = *arg; /* get the delimiter */
4006 if (delim)
4007 ++arg; /* skip delimiter if there is one */
4008
4009 while (arg[0] != NUL && arg[0] != delim)
4010 {
4011 if (arg[0] == '\\' && arg[1] != NUL)
4012 ++arg;
4013 ++arg;
4014 }
4015 if (arg[0] != NUL)
4016 return arg + 1;
4017 break;
4018 case CMD_and:
4019 case CMD_substitute:
4020 delim = *arg;
4021 if (delim)
4022 {
4023 /* skip "from" part */
4024 ++arg;
4025 arg = skip_regexp(arg, delim, p_magic, NULL);
4026 }
4027 /* skip "to" part */
4028 while (arg[0] != NUL && arg[0] != delim)
4029 {
4030 if (arg[0] == '\\' && arg[1] != NUL)
4031 ++arg;
4032 ++arg;
4033 }
4034 if (arg[0] != NUL) /* skip delimiter */
4035 ++arg;
4036 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4037 ++arg;
4038 if (arg[0] != NUL)
4039 return arg;
4040 break;
4041 case CMD_isearch:
4042 case CMD_dsearch:
4043 case CMD_ilist:
4044 case CMD_dlist:
4045 case CMD_ijump:
4046 case CMD_psearch:
4047 case CMD_djump:
4048 case CMD_isplit:
4049 case CMD_dsplit:
4050 arg = skipwhite(skipdigits(arg)); /* skip count */
4051 if (*arg == '/') /* Match regexp, not just whole words */
4052 {
4053 for (++arg; *arg && *arg != '/'; arg++)
4054 if (*arg == '\\' && arg[1] != NUL)
4055 arg++;
4056 if (*arg)
4057 {
4058 arg = skipwhite(arg + 1);
4059
4060 /* Check for trailing illegal characters */
4061 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4062 xp->xp_context = EXPAND_NOTHING;
4063 else
4064 return arg;
4065 }
4066 }
4067 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004068
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 case CMD_autocmd:
4070 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004072 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 case CMD_set:
4075 set_context_in_set_cmd(xp, arg, 0);
4076 break;
4077 case CMD_setglobal:
4078 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4079 break;
4080 case CMD_setlocal:
4081 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4082 break;
4083 case CMD_tag:
4084 case CMD_stag:
4085 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004086 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 case CMD_tselect:
4088 case CMD_stselect:
4089 case CMD_ptselect:
4090 case CMD_tjump:
4091 case CMD_stjump:
4092 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004093 if (*p_wop != NUL)
4094 xp->xp_context = EXPAND_TAGS_LISTFILES;
4095 else
4096 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 xp->xp_pattern = arg;
4098 break;
4099 case CMD_augroup:
4100 xp->xp_context = EXPAND_AUGROUP;
4101 xp->xp_pattern = arg;
4102 break;
4103#ifdef FEAT_SYN_HL
4104 case CMD_syntax:
4105 set_context_in_syntax_cmd(xp, arg);
4106 break;
4107#endif
4108#ifdef FEAT_EVAL
4109 case CMD_let:
4110 case CMD_if:
4111 case CMD_elseif:
4112 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004113 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 case CMD_echo:
4115 case CMD_echon:
4116 case CMD_execute:
4117 case CMD_echomsg:
4118 case CMD_echoerr:
4119 case CMD_call:
4120 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004121 case CMD_cexpr:
4122 case CMD_caddexpr:
4123 case CMD_cgetexpr:
4124 case CMD_lexpr:
4125 case CMD_laddexpr:
4126 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004127 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 break;
4129
4130 case CMD_unlet:
4131 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4132 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02004133
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 xp->xp_context = EXPAND_USER_VARS;
4135 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02004136
4137 if (*xp->xp_pattern == '$')
4138 {
4139 xp->xp_context = EXPAND_ENV_VARS;
4140 ++xp->xp_pattern;
4141 }
4142
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 break;
4144
4145 case CMD_function:
4146 case CMD_delfunction:
4147 xp->xp_context = EXPAND_USER_FUNC;
4148 xp->xp_pattern = arg;
4149 break;
4150
4151 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004152 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 break;
4154#endif
4155 case CMD_highlight:
4156 set_context_in_highlight_cmd(xp, arg);
4157 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004158#ifdef FEAT_CSCOPE
4159 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004160 case CMD_lcscope:
4161 case CMD_scscope:
4162 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004163 break;
4164#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004165#ifdef FEAT_SIGNS
4166 case CMD_sign:
4167 set_context_in_sign_cmd(xp, arg);
4168 break;
4169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 case CMD_bdelete:
4171 case CMD_bwipeout:
4172 case CMD_bunload:
4173 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4174 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004175 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 case CMD_buffer:
4177 case CMD_sbuffer:
4178 case CMD_checktime:
4179 xp->xp_context = EXPAND_BUFFERS;
4180 xp->xp_pattern = arg;
4181 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182#ifdef FEAT_USR_CMDS
4183 case CMD_USER:
4184 case CMD_USER_BUF:
4185 if (compl != EXPAND_NOTHING)
4186 {
4187 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004188 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 {
4190# ifdef FEAT_MENU
4191 if (compl == EXPAND_MENUS)
4192 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4193# endif
4194 if (compl == EXPAND_COMMANDS)
4195 return arg;
4196 if (compl == EXPAND_MAPPINGS)
4197 return set_context_in_map_cmd(xp, (char_u *)"map",
4198 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004199 /* Find start of last argument. */
4200 p = arg;
4201 while (*p)
4202 {
4203 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004204 /* argument starts after a space */
4205 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004206 else if (*p == '\\' && *(p + 1) != NUL)
4207 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004208 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 xp->xp_pattern = arg;
4211 }
4212 xp->xp_context = compl;
4213 }
4214 break;
4215#endif
4216 case CMD_map: case CMD_noremap:
4217 case CMD_nmap: case CMD_nnoremap:
4218 case CMD_vmap: case CMD_vnoremap:
4219 case CMD_omap: case CMD_onoremap:
4220 case CMD_imap: case CMD_inoremap:
4221 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004222 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004223 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004224 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004225 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004227 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 case CMD_unmap:
4229 case CMD_nunmap:
4230 case CMD_vunmap:
4231 case CMD_ounmap:
4232 case CMD_iunmap:
4233 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004234 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004235 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004236 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004237 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004239 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004240 case CMD_mapclear:
4241 case CMD_nmapclear:
4242 case CMD_vmapclear:
4243 case CMD_omapclear:
4244 case CMD_imapclear:
4245 case CMD_cmapclear:
4246 case CMD_lmapclear:
4247 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004248 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004249 case CMD_xmapclear:
4250 xp->xp_context = EXPAND_MAPCLEAR;
4251 xp->xp_pattern = arg;
4252 break;
4253
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 case CMD_abbreviate: case CMD_noreabbrev:
4255 case CMD_cabbrev: case CMD_cnoreabbrev:
4256 case CMD_iabbrev: case CMD_inoreabbrev:
4257 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004258 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 case CMD_unabbreviate:
4260 case CMD_cunabbrev:
4261 case CMD_iunabbrev:
4262 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004263 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264#ifdef FEAT_MENU
4265 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4266 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4267 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4268 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4269 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4270 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4271 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004272 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 case CMD_tmenu: case CMD_tunmenu:
4274 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4275 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4276#endif
4277
4278 case CMD_colorscheme:
4279 xp->xp_context = EXPAND_COLORS;
4280 xp->xp_pattern = arg;
4281 break;
4282
4283 case CMD_compiler:
4284 xp->xp_context = EXPAND_COMPILER;
4285 xp->xp_pattern = arg;
4286 break;
4287
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004288 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004289 xp->xp_context = EXPAND_OWNSYNTAX;
4290 xp->xp_pattern = arg;
4291 break;
4292
4293 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004294 xp->xp_context = EXPAND_FILETYPE;
4295 xp->xp_pattern = arg;
4296 break;
4297
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004298 case CMD_packadd:
4299 xp->xp_context = EXPAND_PACKADD;
4300 xp->xp_pattern = arg;
4301 break;
4302
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004303#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004305 p = skiptowhite(arg);
4306 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 {
4308 xp->xp_context = EXPAND_LANGUAGE;
4309 xp->xp_pattern = arg;
4310 }
4311 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004312 {
4313 if ( STRNCMP(arg, "messages", p - arg) == 0
4314 || STRNCMP(arg, "ctype", p - arg) == 0
4315 || STRNCMP(arg, "time", p - arg) == 0)
4316 {
4317 xp->xp_context = EXPAND_LOCALES;
4318 xp->xp_pattern = skipwhite(p);
4319 }
4320 else
4321 xp->xp_context = EXPAND_NOTHING;
4322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 break;
4324#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004325#if defined(FEAT_PROFILE)
4326 case CMD_profile:
4327 set_context_in_profile_cmd(xp, arg);
4328 break;
4329#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004330 case CMD_behave:
4331 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004332 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004333 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004335 case CMD_messages:
4336 xp->xp_context = EXPAND_MESSAGES;
4337 xp->xp_pattern = arg;
4338 break;
4339
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004340#if defined(FEAT_CMDHIST)
4341 case CMD_history:
4342 xp->xp_context = EXPAND_HISTORY;
4343 xp->xp_pattern = arg;
4344 break;
4345#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004346#if defined(FEAT_PROFILE)
4347 case CMD_syntime:
4348 xp->xp_context = EXPAND_SYNTIME;
4349 xp->xp_pattern = arg;
4350 break;
4351#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004352
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004353 case CMD_argdelete:
4354 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4355 arg = xp->xp_pattern + 1;
4356 xp->xp_context = EXPAND_ARGLIST;
4357 xp->xp_pattern = arg;
4358 break;
4359
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360#endif /* FEAT_CMDL_COMPL */
4361
4362 default:
4363 break;
4364 }
4365 return NULL;
4366}
4367
4368/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004369 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 *
4371 * Backslashed delimiters after / or ? will be skipped, and commands will
4372 * not be expanded between /'s and ?'s or after "'".
4373 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004374 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 * Returns the "cmd" pointer advanced to beyond the range.
4376 */
4377 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004378skip_range(
4379 char_u *cmd,
4380 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004382 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004384 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004386 if (*cmd == '\\')
4387 {
4388 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4389 ++cmd;
4390 else
4391 break;
4392 }
4393 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 {
4395 if (*++cmd == NUL && ctx != NULL)
4396 *ctx = EXPAND_NOTHING;
4397 }
4398 else if (*cmd == '/' || *cmd == '?')
4399 {
4400 delim = *cmd++;
4401 while (*cmd != NUL && *cmd != delim)
4402 if (*cmd++ == '\\' && *cmd != NUL)
4403 ++cmd;
4404 if (*cmd == NUL && ctx != NULL)
4405 *ctx = EXPAND_NOTHING;
4406 }
4407 if (*cmd != NUL)
4408 ++cmd;
4409 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004410
4411 /* Skip ":" and white space. */
4412 while (*cmd == ':')
4413 cmd = skipwhite(cmd + 1);
4414
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 return cmd;
4416}
4417
4418/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004419 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 *
4421 * Set ptr to the next character after the part that was interpreted.
4422 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004423 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 *
4425 * Return MAXLNUM when no Ex address was found.
4426 */
4427 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004428get_address(
4429 exarg_T *eap UNUSED,
4430 char_u **ptr,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004431 int addr_type, // flag: one of ADDR_LINES, ...
4432 int skip, // only skip the address, don't use it
4433 int silent, // no errors or side effects
4434 int to_other_file, // flag: may jump to other file
4435 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436{
4437 int c;
4438 int i;
4439 long n;
4440 char_u *cmd;
4441 pos_T pos;
4442 pos_T *fp;
4443 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004444 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
4446 cmd = skipwhite(*ptr);
4447 lnum = MAXLNUM;
4448 do
4449 {
4450 switch (*cmd)
4451 {
4452 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004453 ++cmd;
4454 switch (addr_type)
4455 {
4456 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 lnum = curwin->w_cursor.lnum;
4458 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004459 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004460 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004461 break;
4462 case ADDR_ARGUMENTS:
4463 lnum = curwin->w_arg_idx + 1;
4464 break;
4465 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004466 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004467 lnum = curbuf->b_fnum;
4468 break;
4469 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004470 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004471 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004472 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004473 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004474 cmd = NULL;
4475 goto error;
4476 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004477#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004478 case ADDR_QUICKFIX:
4479 lnum = qf_get_cur_valid_idx(eap);
4480 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004481#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004482 }
4483 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484
4485 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004486 ++cmd;
4487 switch (addr_type)
4488 {
4489 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 lnum = curbuf->b_ml.ml_line_count;
4491 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004492 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004493 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004494 break;
4495 case ADDR_ARGUMENTS:
4496 lnum = ARGCOUNT;
4497 break;
4498 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004499 buf = lastbuf;
4500 while (buf->b_ml.ml_mfp == NULL)
4501 {
4502 if (buf->b_prev == NULL)
4503 break;
4504 buf = buf->b_prev;
4505 }
4506 lnum = buf->b_fnum;
4507 break;
4508 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004509 lnum = lastbuf->b_fnum;
4510 break;
4511 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004512 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004513 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004514 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004515 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004516 cmd = NULL;
4517 goto error;
4518 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004519#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004520 case ADDR_QUICKFIX:
4521 lnum = qf_get_size(eap);
4522 if (lnum == 0)
4523 lnum = 1;
4524 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004525#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004526 }
4527 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
4529 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004530 if (*++cmd == NUL)
4531 {
4532 cmd = NULL;
4533 goto error;
4534 }
4535 if (addr_type != ADDR_LINES)
4536 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004537 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004538 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004539 goto error;
4540 }
4541 if (skip)
4542 ++cmd;
4543 else
4544 {
4545 /* Only accept a mark in another file when it is
4546 * used by itself: ":'M". */
4547 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4548 ++cmd;
4549 if (fp == (pos_T *)-1)
4550 /* Jumped to another file. */
4551 lnum = curwin->w_cursor.lnum;
4552 else
4553 {
4554 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 {
4556 cmd = NULL;
4557 goto error;
4558 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004559 lnum = fp->lnum;
4560 }
4561 }
4562 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563
4564 case '/':
4565 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004566 c = *cmd++;
4567 if (addr_type != ADDR_LINES)
4568 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004569 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004570 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004571 goto error;
4572 }
4573 if (skip) /* skip "/pat/" */
4574 {
4575 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4576 if (*cmd == c)
4577 ++cmd;
4578 }
4579 else
4580 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004581 int flags;
4582
4583 pos = curwin->w_cursor; // save curwin->w_cursor
4584
4585 // When '/' or '?' follows another address, start from
4586 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004587 if (lnum != MAXLNUM)
4588 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004589
4590 // Start a forward search at the end of the line (unless
4591 // before the first line).
4592 // Start a backward search at the start of the line.
4593 // This makes sure we never match in the current
4594 // line, and can match anywhere in the
4595 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004596 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004597 curwin->w_cursor.col = MAXCOL;
4598 else
4599 curwin->w_cursor.col = 0;
4600 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004601 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4602 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004603 {
4604 curwin->w_cursor = pos;
4605 cmd = NULL;
4606 goto error;
4607 }
4608 lnum = curwin->w_cursor.lnum;
4609 curwin->w_cursor = pos;
4610 /* adjust command string pointer */
4611 cmd += searchcmdlen;
4612 }
4613 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
4615 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004616 ++cmd;
4617 if (addr_type != ADDR_LINES)
4618 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004619 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004620 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004621 goto error;
4622 }
4623 if (*cmd == '&')
4624 i = RE_SUBST;
4625 else if (*cmd == '?' || *cmd == '/')
4626 i = RE_SEARCH;
4627 else
4628 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004629 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004630 cmd = NULL;
4631 goto error;
4632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004634 if (!skip)
4635 {
4636 /*
4637 * When search follows another address, start from
4638 * there.
4639 */
4640 if (lnum != MAXLNUM)
4641 pos.lnum = lnum;
4642 else
4643 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004645 /*
4646 * Start the search just like for the above
4647 * do_search().
4648 */
4649 if (*cmd != '?')
4650 pos.col = MAXCOL;
4651 else
4652 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004653#ifdef FEAT_VIRTUALEDIT
4654 pos.coladd = 0;
4655#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004656 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004657 *cmd == '?' ? BACKWARD : FORWARD,
4658 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004659 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004660 lnum = pos.lnum;
4661 else
4662 {
4663 cmd = NULL;
4664 goto error;
4665 }
4666 }
4667 ++cmd;
4668 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669
4670 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004671 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4672 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 }
4674
4675 for (;;)
4676 {
4677 cmd = skipwhite(cmd);
4678 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4679 break;
4680
4681 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004682 {
4683 switch (addr_type)
4684 {
4685 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004686 /* "+1" is same as ".+1" */
4687 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004688 break;
4689 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004690 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004691 break;
4692 case ADDR_ARGUMENTS:
4693 lnum = curwin->w_arg_idx + 1;
4694 break;
4695 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004696 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004697 lnum = curbuf->b_fnum;
4698 break;
4699 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004700 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004701 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004702 case ADDR_TABS_RELATIVE:
4703 lnum = 1;
4704 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004705#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004706 case ADDR_QUICKFIX:
4707 lnum = qf_get_cur_valid_idx(eap);
4708 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004709#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004710 }
4711 }
4712
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 if (VIM_ISDIGIT(*cmd))
4714 i = '+'; /* "number" is same as "+number" */
4715 else
4716 i = *cmd++;
4717 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4718 n = 1;
4719 else
4720 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004721
4722 if (addr_type == ADDR_TABS_RELATIVE)
4723 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004724 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004725 cmd = NULL;
4726 goto error;
4727 }
4728 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004729 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004730 lnum = compute_buffer_local_count(
4731 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004733 {
4734#ifdef FEAT_FOLDING
4735 /* Relative line addressing, need to adjust for folded lines
4736 * now, but only do it after the first address. */
4737 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4738 && address_count >= 2)
4739 (void)hasFolding(lnum, NULL, &lnum);
4740#endif
4741 if (i == '-')
4742 lnum -= n;
4743 else
4744 lnum += n;
4745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 }
4747 } while (*cmd == '/' || *cmd == '?');
4748
4749error:
4750 *ptr = cmd;
4751 return lnum;
4752}
4753
4754/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004755 * Get flags from an Ex command argument.
4756 */
4757 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004758get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004759{
4760 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4761 {
4762 if (*eap->arg == 'l')
4763 eap->flags |= EXFLAG_LIST;
4764 else if (*eap->arg == 'p')
4765 eap->flags |= EXFLAG_PRINT;
4766 else
4767 eap->flags |= EXFLAG_NR;
4768 eap->arg = skipwhite(eap->arg + 1);
4769 }
4770}
4771
4772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 * Function called for command which is Not Implemented. NI!
4774 */
4775 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004776ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777{
4778 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004779 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780}
4781
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004782#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783/*
4784 * Function called for script command which is Not Implemented. NI!
4785 * Skips over ":perl <<EOF" constructs.
4786 */
4787 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004788ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789{
4790 if (!eap->skip)
4791 ex_ni(eap);
4792 else
4793 vim_free(script_get(eap, eap->arg));
4794}
4795#endif
4796
4797/*
4798 * Check range in Ex command for validity.
4799 * Return NULL when valid, error message when invalid.
4800 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004801 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004802invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004804 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 if ( eap->line1 < 0
4806 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004807 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004808 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004809
4810 if (eap->argt & RANGE)
4811 {
4812 switch(eap->addr_type)
4813 {
4814 case ADDR_LINES:
4815 if (!(eap->argt & NOTADR)
4816 && eap->line2 > curbuf->b_ml.ml_line_count
4817#ifdef FEAT_DIFF
4818 + (eap->cmdidx == CMD_diffget)
4819#endif
4820 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004821 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004822 break;
4823 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004824 /* add 1 if ARGCOUNT is 0 */
4825 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004826 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004827 break;
4828 case ADDR_BUFFERS:
4829 if (eap->line1 < firstbuf->b_fnum
4830 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004831 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004832 break;
4833 case ADDR_LOADED_BUFFERS:
4834 buf = firstbuf;
4835 while (buf->b_ml.ml_mfp == NULL)
4836 {
4837 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004838 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004839 buf = buf->b_next;
4840 }
4841 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004842 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004843 buf = lastbuf;
4844 while (buf->b_ml.ml_mfp == NULL)
4845 {
4846 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004847 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004848 buf = buf->b_prev;
4849 }
4850 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004851 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004852 break;
4853 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004854 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004855 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004856 break;
4857 case ADDR_TABS:
4858 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004859 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004860 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004861 case ADDR_TABS_RELATIVE:
4862 /* Do nothing */
4863 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004864#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004865 case ADDR_QUICKFIX:
4866 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004867 return _(e_invrange);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004868 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004869#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004870 }
4871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 return NULL;
4873}
4874
4875/*
4876 * Correct the range for zero line number, if required.
4877 */
4878 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004879correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880{
4881 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4882 {
4883 if (eap->line1 == 0)
4884 eap->line1 = 1;
4885 if (eap->line2 == 0)
4886 eap->line2 = 1;
4887 }
4888}
4889
Bram Moolenaar748bf032005-02-02 23:04:36 +00004890#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004891/*
4892 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4893 * pattern. Otherwise return eap->arg.
4894 */
4895 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004896skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004897{
4898 char_u *p = eap->arg;
4899
Bram Moolenaara37420f2006-02-04 22:37:47 +00004900 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4901 || eap->cmdidx == CMD_vimgrepadd
4902 || eap->cmdidx == CMD_lvimgrepadd
4903 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004904 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004905 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004906 if (p == NULL)
4907 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004908 }
4909 return p;
4910}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004911
4912/*
4913 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4914 * in the command line, so that things like % get expanded.
4915 */
4916 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004917replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004918{
4919 char_u *new_cmdline;
4920 char_u *program;
4921 char_u *pos;
4922 char_u *ptr;
4923 int len;
4924 int i;
4925
4926 /*
4927 * Don't do it when ":vimgrep" is used for ":grep".
4928 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004929 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4930 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4931 || eap->cmdidx == CMD_grepadd
4932 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004933 && !grep_internal(eap->cmdidx))
4934 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004935 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4936 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004937 {
4938 if (*curbuf->b_p_gp == NUL)
4939 program = p_gp;
4940 else
4941 program = curbuf->b_p_gp;
4942 }
4943 else
4944 {
4945 if (*curbuf->b_p_mp == NUL)
4946 program = p_mp;
4947 else
4948 program = curbuf->b_p_mp;
4949 }
4950
4951 p = skipwhite(p);
4952
4953 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4954 {
4955 /* replace $* by given arguments */
4956 i = 1;
4957 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4958 ++i;
4959 len = (int)STRLEN(p);
4960 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4961 if (new_cmdline == NULL)
4962 return NULL; /* out of memory */
4963 ptr = new_cmdline;
4964 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4965 {
4966 i = (int)(pos - program);
4967 STRNCPY(ptr, program, i);
4968 STRCPY(ptr += i, p);
4969 ptr += len;
4970 program = pos + 2;
4971 }
4972 STRCPY(ptr, program);
4973 }
4974 else
4975 {
4976 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4977 if (new_cmdline == NULL)
4978 return NULL; /* out of memory */
4979 STRCPY(new_cmdline, program);
4980 STRCAT(new_cmdline, " ");
4981 STRCAT(new_cmdline, p);
4982 }
4983 msg_make(p);
4984
4985 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4986 vim_free(*cmdlinep);
4987 *cmdlinep = new_cmdline;
4988 p = new_cmdline;
4989 }
4990 return p;
4991}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004992#endif
4993
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994/*
4995 * Expand file name in Ex command argument.
4996 * Return FAIL for failure, OK otherwise.
4997 */
4998 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004999expand_filename(
5000 exarg_T *eap,
5001 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005002 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003{
5004 int has_wildcards; /* need to expand wildcards */
5005 char_u *repl;
5006 int srclen;
5007 char_u *p;
5008 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005009 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010
Bram Moolenaar748bf032005-02-02 23:04:36 +00005011#ifdef FEAT_QUICKFIX
5012 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
5013 p = skip_grep_pat(eap);
5014#else
5015 p = eap->arg;
5016#endif
5017
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 /*
5019 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5020 * the file name contains a wildcard it should not cause expanding.
5021 * (it will be expanded anyway if there is a wildcard before replacing).
5022 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005023 has_wildcards = mch_has_wildcard(p);
5024 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005026#ifdef FEAT_EVAL
5027 /* Skip over `=expr`, wildcards in it are not expanded. */
5028 if (p[0] == '`' && p[1] == '=')
5029 {
5030 p += 2;
5031 (void)skip_expr(&p);
5032 if (*p == '`')
5033 ++p;
5034 continue;
5035 }
5036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 /*
5038 * Quick check if this cannot be the start of a special string.
5039 * Also removes backslash before '%', '#' and '<'.
5040 */
5041 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5042 {
5043 ++p;
5044 continue;
5045 }
5046
5047 /*
5048 * Try to find a match at this position.
5049 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005050 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5051 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 if (*errormsgp != NULL) /* error detected */
5053 return FAIL;
5054 if (repl == NULL) /* no match found */
5055 {
5056 p += srclen;
5057 continue;
5058 }
5059
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005060 /* Wildcards won't be expanded below, the replacement is taken
5061 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5062 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5063 {
5064 char_u *l = repl;
5065
5066 repl = expand_env_save(repl);
5067 vim_free(l);
5068 }
5069
Bram Moolenaar63b92542007-03-27 14:57:09 +00005070 /* Need to escape white space et al. with a backslash.
5071 * Don't do this for:
5072 * - replacement that already has been escaped: "##"
5073 * - shell commands (may have to use quotes instead).
5074 * - non-unix systems when there is a single argument (spaces don't
5075 * separate arguments then).
5076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005078 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 && eap->cmdidx != CMD_grep
5081 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005082 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005083 && eap->cmdidx != CMD_lgrep
5084 && eap->cmdidx != CMD_lgrepadd
5085 && eap->cmdidx != CMD_lmake
5086 && eap->cmdidx != CMD_make
5087 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088#ifndef UNIX
5089 && !(eap->argt & NOSPC)
5090#endif
5091 )
5092 {
5093 char_u *l;
5094#ifdef BACKSLASH_IN_FILENAME
5095 /* Don't escape a backslash here, because rem_backslash() doesn't
5096 * remove it later. */
5097 static char_u *nobslash = (char_u *)" \t\"|";
5098# define ESCAPE_CHARS nobslash
5099#else
5100# define ESCAPE_CHARS escape_chars
5101#endif
5102
5103 for (l = repl; *l; ++l)
5104 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5105 {
5106 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5107 if (l != NULL)
5108 {
5109 vim_free(repl);
5110 repl = l;
5111 }
5112 break;
5113 }
5114 }
5115
5116 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005117 if ((eap->usefilter || eap->cmdidx == CMD_bang
5118 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005119 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 {
5121 char_u *l;
5122
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005123 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 if (l != NULL)
5125 {
5126 vim_free(repl);
5127 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
5129 }
5130
5131 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5132 vim_free(repl);
5133 if (p == NULL)
5134 return FAIL;
5135 }
5136
5137 /*
5138 * One file argument: Expand wildcards.
5139 * Don't do this with ":r !command" or ":w !command".
5140 */
5141 if ((eap->argt & NOSPC) && !eap->usefilter)
5142 {
5143 /*
5144 * May do this twice:
5145 * 1. Replace environment variables.
5146 * 2. Replace any other wildcards, remove backslashes.
5147 */
5148 for (n = 1; n <= 2; ++n)
5149 {
5150 if (n == 2)
5151 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 /*
5153 * Halve the number of backslashes (this is Vi compatible).
5154 * For Unix and OS/2, when wildcards are expanded, this is
5155 * done by ExpandOne() below.
5156 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005157#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 if (!has_wildcards)
5159#endif
5160 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 }
5162
5163 if (has_wildcards)
5164 {
5165 if (n == 1)
5166 {
5167 /*
5168 * First loop: May expand environment variables. This
5169 * can be done much faster with expand_env() than with
5170 * something else (e.g., calling a shell).
5171 * After expanding environment variables, check again
5172 * if there are still wildcards present.
5173 */
5174 if (vim_strchr(eap->arg, '$') != NULL
5175 || vim_strchr(eap->arg, '~') != NULL)
5176 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005177 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005178 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 has_wildcards = mch_has_wildcard(NameBuff);
5180 p = NameBuff;
5181 }
5182 else
5183 p = NULL;
5184 }
5185 else /* n == 2 */
5186 {
5187 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005188 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189
5190 ExpandInit(&xpc);
5191 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005192 if (p_wic)
5193 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005195 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 if (p == NULL)
5197 return FAIL;
5198 }
5199 if (p != NULL)
5200 {
5201 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5202 p, cmdlinep);
5203 if (n == 2) /* p came from ExpandOne() */
5204 vim_free(p);
5205 }
5206 }
5207 }
5208 }
5209 return OK;
5210}
5211
5212/*
5213 * Replace part of the command line, keeping eap->cmd, eap->arg and
5214 * eap->nextcmd correct.
5215 * "src" points to the part that is to be replaced, of length "srclen".
5216 * "repl" is the replacement string.
5217 * Returns a pointer to the character after the replaced string.
5218 * Returns NULL for failure.
5219 */
5220 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005221repl_cmdline(
5222 exarg_T *eap,
5223 char_u *src,
5224 int srclen,
5225 char_u *repl,
5226 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227{
5228 int len;
5229 int i;
5230 char_u *new_cmdline;
5231
5232 /*
5233 * The new command line is build in new_cmdline[].
5234 * First allocate it.
5235 * Careful: a "+cmd" argument may have been NUL terminated.
5236 */
5237 len = (int)STRLEN(repl);
5238 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005239 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5241 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5242 return NULL; /* out of memory! */
5243
5244 /*
5245 * Copy the stuff before the expanded part.
5246 * Copy the expanded stuff.
5247 * Copy what came after the expanded part.
5248 * Copy the next commands, if there are any.
5249 */
5250 i = (int)(src - *cmdlinep); /* length of part before match */
5251 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005252
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 mch_memmove(new_cmdline + i, repl, (size_t)len);
5254 i += len; /* remember the end of the string */
5255 STRCPY(new_cmdline + i, src + srclen);
5256 src = new_cmdline + i; /* remember where to continue */
5257
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005258 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 {
5260 i = (int)STRLEN(new_cmdline) + 1;
5261 STRCPY(new_cmdline + i, eap->nextcmd);
5262 eap->nextcmd = new_cmdline + i;
5263 }
5264 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5265 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5266 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5267 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5268 vim_free(*cmdlinep);
5269 *cmdlinep = new_cmdline;
5270
5271 return src;
5272}
5273
5274/*
5275 * Check for '|' to separate commands and '"' to start comments.
5276 */
5277 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005278separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279{
5280 char_u *p;
5281
Bram Moolenaar86b68352004-12-27 21:59:20 +00005282#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005283 p = skip_grep_pat(eap);
5284#else
5285 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005286#endif
5287
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005288 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 {
5290 if (*p == Ctrl_V)
5291 {
5292 if (eap->argt & (USECTRLV | XFILE))
5293 ++p; /* skip CTRL-V and next char */
5294 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005295 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005296 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 if (*p == NUL) /* stop at NUL after CTRL-V */
5298 break;
5299 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005300
5301#ifdef FEAT_EVAL
5302 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005303 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005304 {
5305 p += 2;
5306 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005307 }
5308#endif
5309
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 /* Check for '"': start of comment or '|': next command */
5311 /* :@" and :*" do not start a comment!
5312 * :redir @" doesn't either. */
5313 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5314 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5315 || p != eap->arg)
5316 && (eap->cmdidx != CMD_redir
5317 || p != eap->arg + 1 || p[-1] != '@'))
5318 || *p == '|' || *p == '\n')
5319 {
5320 /*
5321 * We remove the '\' before the '|', unless USECTRLV is used
5322 * AND 'b' is present in 'cpoptions'.
5323 */
5324 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5325 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5326 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005327 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 --p;
5329 }
5330 else
5331 {
5332 eap->nextcmd = check_nextcmd(p);
5333 *p = NUL;
5334 break;
5335 }
5336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005338
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5340 del_trailing_spaces(eap->arg);
5341}
5342
5343/*
5344 * get + command from ex argument
5345 */
5346 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005347getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348{
5349 char_u *arg = *argp;
5350 char_u *command = NULL;
5351
5352 if (*arg == '+') /* +[command] */
5353 {
5354 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005355 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 command = dollar_command;
5357 else
5358 {
5359 command = arg;
5360 arg = skip_cmd_arg(command, TRUE);
5361 if (*arg != NUL)
5362 *arg++ = NUL; /* terminate command with NUL */
5363 }
5364
5365 arg = skipwhite(arg); /* skip over spaces */
5366 *argp = arg;
5367 }
5368 return command;
5369}
5370
5371/*
5372 * Find end of "+command" argument. Skip over "\ " and "\\".
5373 */
5374 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005375skip_cmd_arg(
5376 char_u *p,
5377 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378{
5379 while (*p && !vim_isspace(*p))
5380 {
5381 if (*p == '\\' && p[1] != NUL)
5382 {
5383 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005384 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 else
5386 ++p;
5387 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005388 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 }
5390 return p;
5391}
5392
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005393 int
5394get_bad_opt(char_u *p, exarg_T *eap)
5395{
5396 if (STRICMP(p, "keep") == 0)
5397 eap->bad_char = BAD_KEEP;
5398 else if (STRICMP(p, "drop") == 0)
5399 eap->bad_char = BAD_DROP;
5400 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5401 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005402 else
5403 return FAIL;
5404 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005405}
5406
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407/*
5408 * Get "++opt=arg" argument.
5409 * Return FAIL or OK.
5410 */
5411 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005412getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413{
5414 char_u *arg = eap->arg + 2;
5415 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005416 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418
5419 /* ":edit ++[no]bin[ary] file" */
5420 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5421 {
5422 if (*arg == 'n')
5423 {
5424 arg += 2;
5425 eap->force_bin = FORCE_NOBIN;
5426 }
5427 else
5428 eap->force_bin = FORCE_BIN;
5429 if (!checkforcmd(&arg, "binary", 3))
5430 return FAIL;
5431 eap->arg = skipwhite(arg);
5432 return OK;
5433 }
5434
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005435 /* ":read ++edit file" */
5436 if (STRNCMP(arg, "edit", 4) == 0)
5437 {
5438 eap->read_edit = TRUE;
5439 eap->arg = skipwhite(arg + 4);
5440 return OK;
5441 }
5442
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 if (STRNCMP(arg, "ff", 2) == 0)
5444 {
5445 arg += 2;
5446 pp = &eap->force_ff;
5447 }
5448 else if (STRNCMP(arg, "fileformat", 10) == 0)
5449 {
5450 arg += 10;
5451 pp = &eap->force_ff;
5452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 else if (STRNCMP(arg, "enc", 3) == 0)
5454 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005455 if (STRNCMP(arg, "encoding", 8) == 0)
5456 arg += 8;
5457 else
5458 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 pp = &eap->force_enc;
5460 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005461 else if (STRNCMP(arg, "bad", 3) == 0)
5462 {
5463 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005464 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466
5467 if (pp == NULL || *arg != '=')
5468 return FAIL;
5469
5470 ++arg;
5471 *pp = (int)(arg - eap->cmd);
5472 arg = skip_cmd_arg(arg, FALSE);
5473 eap->arg = skipwhite(arg);
5474 *arg = NUL;
5475
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 if (pp == &eap->force_ff)
5477 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5479 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005480 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005482 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 {
5484 /* Make 'fileencoding' lower case. */
5485 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5486 *p = TOLOWER_ASC(*p);
5487 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005488 else
5489 {
5490 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5491 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005492 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005493 return FAIL;
5494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495
5496 return OK;
5497}
5498
5499/*
5500 * ":abbreviate" and friends.
5501 */
5502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005503ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504{
5505 do_exmap(eap, TRUE); /* almost the same as mapping */
5506}
5507
5508/*
5509 * ":map" and friends.
5510 */
5511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005512ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
5514 /*
5515 * If we are sourcing .exrc or .vimrc in current directory we
5516 * print the mappings for security reasons.
5517 */
5518 if (secure)
5519 {
5520 secure = 2;
5521 msg_outtrans(eap->cmd);
5522 msg_putchar('\n');
5523 }
5524 do_exmap(eap, FALSE);
5525}
5526
5527/*
5528 * ":unmap" and friends.
5529 */
5530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005531ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532{
5533 do_exmap(eap, FALSE);
5534}
5535
5536/*
5537 * ":mapclear" and friends.
5538 */
5539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005540ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541{
5542 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5543}
5544
5545/*
5546 * ":abclear" and friends.
5547 */
5548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005549ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550{
5551 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5552}
5553
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005555ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
5557 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005558 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 * directory for security reasons.
5560 */
5561 if (secure)
5562 {
5563 secure = 2;
5564 eap->errmsg = e_curdir;
5565 }
5566 else if (eap->cmdidx == CMD_autocmd)
5567 do_autocmd(eap->arg, eap->forceit);
5568 else
5569 do_augroup(eap->arg, eap->forceit);
5570}
5571
5572/*
5573 * ":doautocmd": Apply the automatic commands to the current buffer.
5574 */
5575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005576ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005578 char_u *arg = eap->arg;
5579 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005580 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005581
Bram Moolenaar1610d052016-06-09 22:53:01 +02005582 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5583 /* Only when there is no <nomodeline>. */
5584 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005585 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588/*
5589 * :[N]bunload[!] [N] [bufname] unload buffer
5590 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5591 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5592 */
5593 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005594ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595{
5596 eap->errmsg = do_bufdel(
5597 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5598 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5599 : DOBUF_UNLOAD, eap->arg,
5600 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5601}
5602
5603/*
5604 * :[N]buffer [N] to buffer N
5605 * :[N]sbuffer [N] to buffer N
5606 */
5607 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005608ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609{
5610 if (*eap->arg)
5611 eap->errmsg = e_trailing;
5612 else
5613 {
5614 if (eap->addr_count == 0) /* default is current buffer */
5615 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5616 else
5617 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005618 if (eap->do_ecmd_cmd != NULL)
5619 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 }
5621}
5622
5623/*
5624 * :[N]bmodified [N] to next mod. buffer
5625 * :[N]sbmodified [N] to next mod. buffer
5626 */
5627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005628ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629{
5630 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005631 if (eap->do_ecmd_cmd != NULL)
5632 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633}
5634
5635/*
5636 * :[N]bnext [N] to next buffer
5637 * :[N]sbnext [N] split and to next buffer
5638 */
5639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005640ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641{
5642 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005643 if (eap->do_ecmd_cmd != NULL)
5644 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645}
5646
5647/*
5648 * :[N]bNext [N] to previous buffer
5649 * :[N]bprevious [N] to previous buffer
5650 * :[N]sbNext [N] split and to previous buffer
5651 * :[N]sbprevious [N] split and to previous buffer
5652 */
5653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005654ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655{
5656 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005657 if (eap->do_ecmd_cmd != NULL)
5658 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659}
5660
5661/*
5662 * :brewind to first buffer
5663 * :bfirst to first buffer
5664 * :sbrewind split and to first buffer
5665 * :sbfirst split and to first buffer
5666 */
5667 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005668ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669{
5670 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005671 if (eap->do_ecmd_cmd != NULL)
5672 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673}
5674
5675/*
5676 * :blast to last buffer
5677 * :sblast split and to last buffer
5678 */
5679 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005680ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681{
5682 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005683 if (eap->do_ecmd_cmd != NULL)
5684 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686
5687 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005688ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
5690 return (c == NUL || c == '|' || c == '"' || c == '\n');
5691}
5692
5693#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5694 || defined(PROTO)
5695/*
5696 * Return the next command, after the first '|' or '\n'.
5697 * Return NULL if not found.
5698 */
5699 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005700find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701{
5702 while (*p != '|' && *p != '\n')
5703 {
5704 if (*p == NUL)
5705 return NULL;
5706 ++p;
5707 }
5708 return (p + 1);
5709}
5710#endif
5711
5712/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005713 * Check if *p is a separator between Ex commands, skipping over white space.
5714 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 */
5716 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005717check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005719 char_u *s = skipwhite(p);
5720
5721 if (*s == '|' || *s == '\n')
5722 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 else
5724 return NULL;
5725}
5726
5727/*
5728 * - if there are more files to edit
5729 * - and this is the last window
5730 * - and forceit not used
5731 * - and not repeated twice on a row
5732 * return FAIL and give error message if 'message' TRUE
5733 * return OK otherwise
5734 */
5735 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005736check_more(
5737 int message, /* when FALSE check only, no messages */
5738 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
5740 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5741
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005742 if (!forceit && only_one_window()
5743 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 {
5745 if (message)
5746 {
5747#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5748 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5749 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005750 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005752 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5753 NGETTEXT("%d more file to edit. Quit anyway?",
5754 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5756 return OK;
5757 return FAIL;
5758 }
5759#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005760 semsg(NGETTEXT("E173: %d more file to edit",
5761 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 quitmore = 2; /* next try to quit is allowed */
5763 }
5764 return FAIL;
5765 }
5766 return OK;
5767}
5768
5769#ifdef FEAT_CMDL_COMPL
5770/*
5771 * Function given to ExpandGeneric() to obtain the list of command names.
5772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005774get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775{
5776 if (idx >= (int)CMD_SIZE)
5777# ifdef FEAT_USR_CMDS
5778 return get_user_command_name(idx);
5779# else
5780 return NULL;
5781# endif
5782 return cmdnames[idx].cmd_name;
5783}
5784#endif
5785
5786#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005788uc_add_command(
5789 char_u *name,
5790 size_t name_len,
5791 char_u *rep,
5792 long argt,
5793 long def,
5794 int flags,
5795 int compl,
5796 char_u *compl_arg,
5797 int addr_type,
5798 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
5800 ucmd_T *cmd = NULL;
5801 char_u *p;
5802 int i;
5803 int cmp = 1;
5804 char_u *rep_buf = NULL;
5805 garray_T *gap;
5806
Bram Moolenaar9c102382006-05-03 21:26:49 +00005807 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 if (rep_buf == NULL)
5809 {
5810 /* Can't replace termcodes - try using the string as is */
5811 rep_buf = vim_strsave(rep);
5812
5813 /* Give up if out of memory */
5814 if (rep_buf == NULL)
5815 return FAIL;
5816 }
5817
5818 /* get address of growarray: global or in curbuf */
5819 if (flags & UC_BUFFER)
5820 {
5821 gap = &curbuf->b_ucmds;
5822 if (gap->ga_itemsize == 0)
5823 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5824 }
5825 else
5826 gap = &ucmds;
5827
5828 /* Search for the command in the already defined commands. */
5829 for (i = 0; i < gap->ga_len; ++i)
5830 {
5831 size_t len;
5832
5833 cmd = USER_CMD_GA(gap, i);
5834 len = STRLEN(cmd->uc_name);
5835 cmp = STRNCMP(name, cmd->uc_name, name_len);
5836 if (cmp == 0)
5837 {
5838 if (name_len < len)
5839 cmp = -1;
5840 else if (name_len > len)
5841 cmp = 1;
5842 }
5843
5844 if (cmp == 0)
5845 {
Bram Moolenaar55d46912018-12-08 16:03:28 +01005846 // Command can be replaced with "command!" and when sourcing the
5847 // same script again, but only once.
5848 if (!force && (cmd->uc_script_ctx.sc_sid != current_sctx.sc_sid
5849 || cmd->uc_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005851 semsg(_("E174: Command already exists: add ! to replace it: %s"),
Bram Moolenaar55d46912018-12-08 16:03:28 +01005852 name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 goto fail;
5854 }
5855
Bram Moolenaard23a8232018-02-10 18:45:26 +01005856 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005857#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005858 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 break;
5861 }
5862
5863 /* Stop as soon as we pass the name to add */
5864 if (cmp < 0)
5865 break;
5866 }
5867
5868 /* Extend the array unless we're replacing an existing command */
5869 if (cmp != 0)
5870 {
5871 if (ga_grow(gap, 1) != OK)
5872 goto fail;
5873 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5874 goto fail;
5875
5876 cmd = USER_CMD_GA(gap, i);
5877 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5878
5879 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880
5881 cmd->uc_name = p;
5882 }
5883
5884 cmd->uc_rep = rep_buf;
5885 cmd->uc_argt = argt;
5886 cmd->uc_def = def;
5887 cmd->uc_compl = compl;
5888#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005889 cmd->uc_script_ctx = current_sctx;
5890 cmd->uc_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891# ifdef FEAT_CMDL_COMPL
5892 cmd->uc_compl_arg = compl_arg;
5893# endif
5894#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005895 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896
5897 return OK;
5898
5899fail:
5900 vim_free(rep_buf);
5901#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5902 vim_free(compl_arg);
5903#endif
5904 return FAIL;
5905}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005908#if defined(FEAT_USR_CMDS)
5909static struct
5910{
5911 int expand;
5912 char *name;
5913} addr_type_complete[] =
5914{
5915 {ADDR_ARGUMENTS, "arguments"},
5916 {ADDR_LINES, "lines"},
5917 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5918 {ADDR_TABS, "tabs"},
5919 {ADDR_BUFFERS, "buffers"},
5920 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005921 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaar51a74542018-12-02 18:21:49 +01005922 {ADDR_OTHER, "other"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005923 {-1, NULL}
5924};
5925#endif
5926
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005927#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928/*
5929 * List of names for completion for ":command" with the EXPAND_ flag.
5930 * Must be alphabetical for completion.
5931 */
5932static struct
5933{
5934 int expand;
5935 char *name;
5936} command_complete[] =
5937{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005938 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005940 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005942 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005944 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005945#if defined(FEAT_CSCOPE)
5946 {EXPAND_CSCOPE, "cscope"},
5947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5949 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005950 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951#endif
5952 {EXPAND_DIRECTORIES, "dir"},
5953 {EXPAND_ENV_VARS, "environment"},
5954 {EXPAND_EVENTS, "event"},
5955 {EXPAND_EXPRESSION, "expression"},
5956 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005957 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005958 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 {EXPAND_FUNCTIONS, "function"},
5960 {EXPAND_HELP, "help"},
5961 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005962#if defined(FEAT_CMDHIST)
5963 {EXPAND_HISTORY, "history"},
5964#endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005965#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005966 {EXPAND_LOCALES, "locale"},
5967#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005968 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 {EXPAND_MAPPINGS, "mapping"},
5970 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005971 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005972 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005973#if defined(FEAT_PROFILE)
5974 {EXPAND_SYNTIME, "syntime"},
5975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005977 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005978 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005979#if defined(FEAT_SIGNS)
5980 {EXPAND_SIGN, "sign"},
5981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 {EXPAND_TAGS, "tag"},
5983 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005984 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 {EXPAND_USER_VARS, "var"},
5986 {0, NULL}
5987};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005990#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005992uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993{
5994 int i, j;
5995 int found = FALSE;
5996 ucmd_T *cmd;
5997 int len;
5998 long a;
5999 garray_T *gap;
6000
6001 gap = &curbuf->b_ucmds;
6002 for (;;)
6003 {
6004 for (i = 0; i < gap->ga_len; ++i)
6005 {
6006 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006007 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008
Bram Moolenaard29459b2016-08-26 22:29:11 +02006009 /* Skip commands which don't match the requested prefix and
6010 * commands filtered out. */
6011 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6012 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 continue;
6014
6015 /* Put out the title first time */
6016 if (!found)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006017 msg_puts_title(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 found = TRUE;
6019 msg_putchar('\n');
6020 if (got_int)
6021 break;
6022
6023 /* Special cases */
6024 msg_putchar(a & BANG ? '!' : ' ');
6025 msg_putchar(a & REGSTR ? '"' : ' ');
6026 msg_putchar(gap != &ucmds ? 'b' : ' ');
6027 msg_putchar(' ');
6028
Bram Moolenaar8820b482017-03-16 17:23:31 +01006029 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 len = (int)STRLEN(cmd->uc_name) + 4;
6031
6032 do {
6033 msg_putchar(' ');
6034 ++len;
6035 } while (len < 16);
6036
6037 len = 0;
6038
6039 /* Arguments */
6040 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6041 {
6042 case 0: IObuff[len++] = '0'; break;
6043 case (EXTRA): IObuff[len++] = '*'; break;
6044 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6045 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6046 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6047 }
6048
6049 do {
6050 IObuff[len++] = ' ';
6051 } while (len < 5);
6052
6053 /* Range */
6054 if (a & (RANGE|COUNT))
6055 {
6056 if (a & COUNT)
6057 {
6058 /* -count=N */
6059 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6060 len += (int)STRLEN(IObuff + len);
6061 }
6062 else if (a & DFLALL)
6063 IObuff[len++] = '%';
6064 else if (cmd->uc_def >= 0)
6065 {
6066 /* -range=N */
6067 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6068 len += (int)STRLEN(IObuff + len);
6069 }
6070 else
6071 IObuff[len++] = '.';
6072 }
6073
6074 do {
6075 IObuff[len++] = ' ';
6076 } while (len < 11);
6077
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006078 /* Address Type */
6079 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6080 if (addr_type_complete[j].expand != ADDR_LINES
6081 && addr_type_complete[j].expand == cmd->uc_addr_type)
6082 {
6083 STRCPY(IObuff + len, addr_type_complete[j].name);
6084 len += (int)STRLEN(IObuff + len);
6085 break;
6086 }
6087
6088 do {
6089 IObuff[len++] = ' ';
6090 } while (len < 21);
6091
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 /* Completion */
6093 for (j = 0; command_complete[j].expand != 0; ++j)
6094 if (command_complete[j].expand == cmd->uc_compl)
6095 {
6096 STRCPY(IObuff + len, command_complete[j].name);
6097 len += (int)STRLEN(IObuff + len);
6098 break;
6099 }
6100
6101 do {
6102 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006103 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104
6105 IObuff[len] = '\0';
6106 msg_outtrans(IObuff);
6107
6108 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006109#ifdef FEAT_EVAL
6110 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006111 last_set_msg(cmd->uc_script_ctx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 out_flush();
6114 ui_breakcheck();
6115 if (got_int)
6116 break;
6117 }
6118 if (gap == &ucmds || i < gap->ga_len)
6119 break;
6120 gap = &ucmds;
6121 }
6122
6123 if (!found)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006124 msg(_("No user-defined commands found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125}
6126
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006127 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006128uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129{
6130 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6131 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6132 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6133 0xb9, 0x7f, 0};
6134 int i;
6135
6136 for (i = 0; fcmd[i]; ++i)
6137 IObuff[i] = fcmd[i] - 0x40;
6138 IObuff[i] = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006139 return (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140}
6141
6142 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006143uc_scan_attr(
6144 char_u *attr,
6145 size_t len,
6146 long *argt,
6147 long *def,
6148 int *flags,
6149 int *compl,
6150 char_u **compl_arg,
6151 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152{
6153 char_u *p;
6154
6155 if (len == 0)
6156 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006157 emsg(_("E175: No attribute specified"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 return FAIL;
6159 }
6160
6161 /* First, try the simple attributes (no arguments) */
6162 if (STRNICMP(attr, "bang", len) == 0)
6163 *argt |= BANG;
6164 else if (STRNICMP(attr, "buffer", len) == 0)
6165 *flags |= UC_BUFFER;
6166 else if (STRNICMP(attr, "register", len) == 0)
6167 *argt |= REGSTR;
6168 else if (STRNICMP(attr, "bar", len) == 0)
6169 *argt |= TRLBAR;
6170 else
6171 {
6172 int i;
6173 char_u *val = NULL;
6174 size_t vallen = 0;
6175 size_t attrlen = len;
6176
6177 /* Look for the attribute name - which is the part before any '=' */
6178 for (i = 0; i < (int)len; ++i)
6179 {
6180 if (attr[i] == '=')
6181 {
6182 val = &attr[i + 1];
6183 vallen = len - i - 1;
6184 attrlen = i;
6185 break;
6186 }
6187 }
6188
6189 if (STRNICMP(attr, "nargs", attrlen) == 0)
6190 {
6191 if (vallen == 1)
6192 {
6193 if (*val == '0')
6194 /* Do nothing - this is the default */;
6195 else if (*val == '1')
6196 *argt |= (EXTRA | NOSPC | NEEDARG);
6197 else if (*val == '*')
6198 *argt |= EXTRA;
6199 else if (*val == '?')
6200 *argt |= (EXTRA | NOSPC);
6201 else if (*val == '+')
6202 *argt |= (EXTRA | NEEDARG);
6203 else
6204 goto wrong_nargs;
6205 }
6206 else
6207 {
6208wrong_nargs:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006209 emsg(_("E176: Invalid number of arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 return FAIL;
6211 }
6212 }
6213 else if (STRNICMP(attr, "range", attrlen) == 0)
6214 {
6215 *argt |= RANGE;
6216 if (vallen == 1 && *val == '%')
6217 *argt |= DFLALL;
6218 else if (val != NULL)
6219 {
6220 p = val;
6221 if (*def >= 0)
6222 {
6223two_count:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006224 emsg(_("E177: Count cannot be specified twice"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 return FAIL;
6226 }
6227
6228 *def = getdigits(&p);
6229 *argt |= (ZEROR | NOTADR);
6230
6231 if (p != val + vallen || vallen == 0)
6232 {
6233invalid_count:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006234 emsg(_("E178: Invalid default value for count"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 return FAIL;
6236 }
6237 }
6238 }
6239 else if (STRNICMP(attr, "count", attrlen) == 0)
6240 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006241 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242
6243 if (val != NULL)
6244 {
6245 p = val;
6246 if (*def >= 0)
6247 goto two_count;
6248
6249 *def = getdigits(&p);
6250
6251 if (p != val + vallen)
6252 goto invalid_count;
6253 }
6254
6255 if (*def < 0)
6256 *def = 0;
6257 }
6258 else if (STRNICMP(attr, "complete", attrlen) == 0)
6259 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 if (val == NULL)
6261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006262 emsg(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 return FAIL;
6264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006266 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6267 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006270 else if (STRNICMP(attr, "addr", attrlen) == 0)
6271 {
6272 *argt |= RANGE;
6273 if (val == NULL)
6274 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006275 emsg(_("E179: argument required for -addr"));
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006276 return FAIL;
6277 }
6278 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6279 == FAIL)
6280 return FAIL;
6281 if (addr_type_arg != ADDR_LINES)
6282 *argt |= (ZEROR | NOTADR) ;
6283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 else
6285 {
6286 char_u ch = attr[len];
6287 attr[len] = '\0';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006288 semsg(_("E181: Invalid attribute: %s"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 attr[len] = ch;
6290 return FAIL;
6291 }
6292 }
6293
6294 return OK;
6295}
6296
Bram Moolenaara850a712009-01-28 14:42:59 +00006297/*
6298 * ":command ..."
6299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006301ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302{
6303 char_u *name;
6304 char_u *end;
6305 char_u *p;
6306 long argt = 0;
6307 long def = -1;
6308 int flags = 0;
6309 int compl = EXPAND_NOTHING;
6310 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006311 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006313 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314
6315 p = eap->arg;
6316
6317 /* Check for attributes */
6318 while (*p == '-')
6319 {
6320 ++p;
6321 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006322 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6323 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 == FAIL)
6325 return;
6326 p = skipwhite(end);
6327 }
6328
6329 /* Get the name (if any) and skip to the following argument */
6330 name = p;
6331 if (ASCII_ISALPHA(*p))
6332 while (ASCII_ISALNUM(*p))
6333 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006334 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006336 emsg(_("E182: Invalid command name"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 return;
6338 }
6339 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006340 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341
6342 /* If there is nothing after the name, and no attributes were specified,
6343 * we are listing commands
6344 */
6345 p = skipwhite(end);
6346 if (!has_attr && ends_excmd(*p))
6347 {
6348 uc_list(name, end - name);
6349 }
6350 else if (!ASCII_ISUPPER(*name))
6351 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006352 emsg(_("E183: User defined commands must start with an uppercase letter"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 return;
6354 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006355 else if ((name_len == 1 && *name == 'X')
6356 || (name_len <= 4
6357 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6358 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006359 emsg(_("E841: Reserved name, cannot be used for user defined command"));
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006360 return;
6361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 else
6363 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006364 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365}
6366
6367/*
6368 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 * Clear all user commands, global and for current buffer.
6370 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006371 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006372ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373{
6374 uc_clear(&ucmds);
6375 uc_clear(&curbuf->b_ucmds);
6376}
6377
6378/*
6379 * Clear all user commands for "gap".
6380 */
6381 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006382uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383{
6384 int i;
6385 ucmd_T *cmd;
6386
6387 for (i = 0; i < gap->ga_len; ++i)
6388 {
6389 cmd = USER_CMD_GA(gap, i);
6390 vim_free(cmd->uc_name);
6391 vim_free(cmd->uc_rep);
6392# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6393 vim_free(cmd->uc_compl_arg);
6394# endif
6395 }
6396 ga_clear(gap);
6397}
6398
6399 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006400ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401{
6402 int i = 0;
6403 ucmd_T *cmd = NULL;
6404 int cmp = -1;
6405 garray_T *gap;
6406
6407 gap = &curbuf->b_ucmds;
6408 for (;;)
6409 {
6410 for (i = 0; i < gap->ga_len; ++i)
6411 {
6412 cmd = USER_CMD_GA(gap, i);
6413 cmp = STRCMP(eap->arg, cmd->uc_name);
6414 if (cmp <= 0)
6415 break;
6416 }
6417 if (gap == &ucmds || cmp == 0)
6418 break;
6419 gap = &ucmds;
6420 }
6421
6422 if (cmp != 0)
6423 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006424 semsg(_("E184: No such user-defined command: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 return;
6426 }
6427
6428 vim_free(cmd->uc_name);
6429 vim_free(cmd->uc_rep);
6430# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6431 vim_free(cmd->uc_compl_arg);
6432# endif
6433
6434 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435
6436 if (i < gap->ga_len)
6437 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6438}
6439
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006440/*
6441 * split and quote args for <f-args>
6442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006444uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445{
6446 char_u *buf;
6447 char_u *p;
6448 char_u *q;
6449 int len;
6450
6451 /* Precalculate length */
6452 p = arg;
6453 len = 2; /* Initial and final quotes */
6454
6455 while (*p)
6456 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006457 if (p[0] == '\\' && p[1] == '\\')
6458 {
6459 len += 2;
6460 p += 2;
6461 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006462 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 {
6464 len += 1;
6465 p += 2;
6466 }
6467 else if (*p == '\\' || *p == '"')
6468 {
6469 len += 2;
6470 p += 1;
6471 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006472 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 {
6474 p = skipwhite(p);
6475 if (*p == NUL)
6476 break;
6477 len += 3; /* "," */
6478 }
6479 else
6480 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006481 int charlen = (*mb_ptr2len)(p);
Bram Moolenaar13505972019-01-24 15:04:48 +01006482
Bram Moolenaardfef1542012-07-10 19:25:10 +02006483 len += charlen;
6484 p += charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 }
6486 }
6487
6488 buf = alloc(len + 1);
6489 if (buf == NULL)
6490 {
6491 *lenp = 0;
6492 return buf;
6493 }
6494
6495 p = arg;
6496 q = buf;
6497 *q++ = '"';
6498 while (*p)
6499 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006500 if (p[0] == '\\' && p[1] == '\\')
6501 {
6502 *q++ = '\\';
6503 *q++ = '\\';
6504 p += 2;
6505 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006506 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 {
6508 *q++ = p[1];
6509 p += 2;
6510 }
6511 else if (*p == '\\' || *p == '"')
6512 {
6513 *q++ = '\\';
6514 *q++ = *p++;
6515 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006516 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 {
6518 p = skipwhite(p);
6519 if (*p == NUL)
6520 break;
6521 *q++ = '"';
6522 *q++ = ',';
6523 *q++ = '"';
6524 }
6525 else
6526 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006527 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 }
6529 }
6530 *q++ = '"';
6531 *q = 0;
6532
6533 *lenp = len;
6534 return buf;
6535}
6536
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006537 static size_t
6538add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6539{
6540 size_t result;
6541
6542 result = STRLEN(mod_str);
6543 if (*multi_mods)
6544 result += 1;
6545 if (buf != NULL)
6546 {
6547 if (*multi_mods)
6548 STRCAT(buf, " ");
6549 STRCAT(buf, mod_str);
6550 }
6551
6552 *multi_mods = 1;
6553
6554 return result;
6555}
6556
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557/*
6558 * Check for a <> code in a user command.
6559 * "code" points to the '<'. "len" the length of the <> (inclusive).
6560 * "buf" is where the result is to be added.
6561 * "split_buf" points to a buffer used for splitting, caller should free it.
6562 * "split_len" is the length of what "split_buf" contains.
6563 * Returns the length of the replacement, which has been added to "buf".
6564 * Returns -1 if there was no match, and only the "<" has been copied.
6565 */
6566 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006567uc_check_code(
6568 char_u *code,
6569 size_t len,
6570 char_u *buf,
6571 ucmd_T *cmd, /* the user command we're expanding */
6572 exarg_T *eap, /* ex arguments */
6573 char_u **split_buf,
6574 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575{
6576 size_t result = 0;
6577 char_u *p = code + 1;
6578 size_t l = len - 2;
6579 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006580 enum {
6581 ct_ARGS,
6582 ct_BANG,
6583 ct_COUNT,
6584 ct_LINE1,
6585 ct_LINE2,
6586 ct_RANGE,
6587 ct_MODS,
6588 ct_REGISTER,
6589 ct_LT,
6590 ct_NONE
6591 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592
6593 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6594 {
6595 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6596 p += 2;
6597 l -= 2;
6598 }
6599
Bram Moolenaar371d5402006-03-20 21:47:49 +00006600 ++l;
6601 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006603 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006605 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006607 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006609 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006611 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006613 else if (STRNICMP(p, "range>", l) == 0)
6614 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006615 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006617 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006619 else if (STRNICMP(p, "mods>", l) == 0)
6620 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621
6622 switch (type)
6623 {
6624 case ct_ARGS:
6625 /* Simple case first */
6626 if (*eap->arg == NUL)
6627 {
6628 if (quote == 1)
6629 {
6630 result = 2;
6631 if (buf != NULL)
6632 STRCPY(buf, "''");
6633 }
6634 else
6635 result = 0;
6636 break;
6637 }
6638
6639 /* When specified there is a single argument don't split it.
6640 * Works for ":Cmd %" when % is "a b c". */
6641 if ((eap->argt & NOSPC) && quote == 2)
6642 quote = 1;
6643
6644 switch (quote)
6645 {
6646 case 0: /* No quoting, no splitting */
6647 result = STRLEN(eap->arg);
6648 if (buf != NULL)
6649 STRCPY(buf, eap->arg);
6650 break;
6651 case 1: /* Quote, but don't split */
6652 result = STRLEN(eap->arg) + 2;
6653 for (p = eap->arg; *p; ++p)
6654 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006655 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6656 /* DBCS can contain \ in a trail byte, skip the
6657 * double-byte character. */
6658 ++p;
6659 else
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006660 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 ++result;
6662 }
6663
6664 if (buf != NULL)
6665 {
6666 *buf++ = '"';
6667 for (p = eap->arg; *p; ++p)
6668 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006669 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6670 /* DBCS can contain \ in a trail byte, copy the
6671 * double-byte character to avoid escaping. */
6672 *buf++ = *p++;
6673 else
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006674 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 *buf++ = '\\';
6676 *buf++ = *p;
6677 }
6678 *buf = '"';
6679 }
6680
6681 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006682 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 /* This is hard, so only do it once, and cache the result */
6684 if (*split_buf == NULL)
6685 *split_buf = uc_split_args(eap->arg, split_len);
6686
6687 result = *split_len;
6688 if (buf != NULL && result != 0)
6689 STRCPY(buf, *split_buf);
6690
6691 break;
6692 }
6693 break;
6694
6695 case ct_BANG:
6696 result = eap->forceit ? 1 : 0;
6697 if (quote)
6698 result += 2;
6699 if (buf != NULL)
6700 {
6701 if (quote)
6702 *buf++ = '"';
6703 if (eap->forceit)
6704 *buf++ = '!';
6705 if (quote)
6706 *buf = '"';
6707 }
6708 break;
6709
6710 case ct_LINE1:
6711 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006712 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 case ct_COUNT:
6714 {
6715 char num_buf[20];
6716 long num = (type == ct_LINE1) ? eap->line1 :
6717 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006718 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6720 size_t num_len;
6721
6722 sprintf(num_buf, "%ld", num);
6723 num_len = STRLEN(num_buf);
6724 result = num_len;
6725
6726 if (quote)
6727 result += 2;
6728
6729 if (buf != NULL)
6730 {
6731 if (quote)
6732 *buf++ = '"';
6733 STRCPY(buf, num_buf);
6734 buf += num_len;
6735 if (quote)
6736 *buf = '"';
6737 }
6738
6739 break;
6740 }
6741
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006742 case ct_MODS:
6743 {
6744 int multi_mods = 0;
6745 typedef struct {
6746 int *varp;
6747 char *name;
6748 } mod_entry_T;
6749 static mod_entry_T mod_entries[] = {
6750#ifdef FEAT_BROWSE_CMD
6751 {&cmdmod.browse, "browse"},
6752#endif
6753#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6754 {&cmdmod.confirm, "confirm"},
6755#endif
6756 {&cmdmod.hide, "hide"},
6757 {&cmdmod.keepalt, "keepalt"},
6758 {&cmdmod.keepjumps, "keepjumps"},
6759 {&cmdmod.keepmarks, "keepmarks"},
6760 {&cmdmod.keeppatterns, "keeppatterns"},
6761 {&cmdmod.lockmarks, "lockmarks"},
6762 {&cmdmod.noswapfile, "noswapfile"},
6763 {NULL, NULL}
6764 };
6765 int i;
6766
6767 result = quote ? 2 : 0;
6768 if (buf != NULL)
6769 {
6770 if (quote)
6771 *buf++ = '"';
6772 *buf = '\0';
6773 }
6774
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006775 /* :aboveleft and :leftabove */
6776 if (cmdmod.split & WSP_ABOVE)
6777 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6778 /* :belowright and :rightbelow */
6779 if (cmdmod.split & WSP_BELOW)
6780 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6781 /* :botright */
6782 if (cmdmod.split & WSP_BOT)
6783 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006784
6785 /* the modifiers that are simple flags */
6786 for (i = 0; mod_entries[i].varp != NULL; ++i)
6787 if (*mod_entries[i].varp)
6788 result += add_cmd_modifier(buf, mod_entries[i].name,
6789 &multi_mods);
6790
6791 /* TODO: How to support :noautocmd? */
6792#ifdef HAVE_SANDBOX
6793 /* TODO: How to support :sandbox?*/
6794#endif
6795 /* :silent */
6796 if (msg_silent > 0)
6797 result += add_cmd_modifier(buf,
6798 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006799 /* :tab */
6800 if (cmdmod.tab > 0)
6801 result += add_cmd_modifier(buf, "tab", &multi_mods);
6802 /* :topleft */
6803 if (cmdmod.split & WSP_TOP)
6804 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006805 /* TODO: How to support :unsilent?*/
6806 /* :verbose */
6807 if (p_verbose > 0)
6808 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006809 /* :vertical */
6810 if (cmdmod.split & WSP_VERT)
6811 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006812 if (quote && buf != NULL)
6813 {
6814 buf += result - 2;
6815 *buf = '"';
6816 }
6817 break;
6818 }
6819
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 case ct_REGISTER:
6821 result = eap->regname ? 1 : 0;
6822 if (quote)
6823 result += 2;
6824 if (buf != NULL)
6825 {
6826 if (quote)
6827 *buf++ = '\'';
6828 if (eap->regname)
6829 *buf++ = eap->regname;
6830 if (quote)
6831 *buf = '\'';
6832 }
6833 break;
6834
6835 case ct_LT:
6836 result = 1;
6837 if (buf != NULL)
6838 *buf = '<';
6839 break;
6840
6841 default:
6842 /* Not recognized: just copy the '<' and return -1. */
6843 result = (size_t)-1;
6844 if (buf != NULL)
6845 *buf = '<';
6846 break;
6847 }
6848
6849 return result;
6850}
6851
6852 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006853do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854{
6855 char_u *buf;
6856 char_u *p;
6857 char_u *q;
6858
6859 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006860 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006861 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 size_t len, totlen;
6863
6864 size_t split_len = 0;
6865 char_u *split_buf = NULL;
6866 ucmd_T *cmd;
6867#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006868 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869#endif
6870
6871 if (eap->cmdidx == CMD_USER)
6872 cmd = USER_CMD(eap->useridx);
6873 else
6874 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6875
6876 /*
6877 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006878 * First round: "buf" is NULL, compute length, allocate "buf".
6879 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 */
6881 buf = NULL;
6882 for (;;)
6883 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006884 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006885 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006887
6888 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006890 start = vim_strchr(p, '<');
6891 if (start != NULL)
6892 end = vim_strchr(start + 1, '>');
6893 if (buf != NULL)
6894 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006895 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6896 ;
6897 if (*ksp == K_SPECIAL
6898 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006899 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6900# ifdef FEAT_GUI
6901 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6902# endif
6903 ))
6904 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006905 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006906 * KS_SPECIAL KE_FILLER, like for mappings, but
6907 * do_cmdline() doesn't handle that, so convert it back.
6908 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6909 len = ksp - p;
6910 if (len > 0)
6911 {
6912 mch_memmove(q, p, len);
6913 q += len;
6914 }
6915 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6916 p = ksp + 3;
6917 continue;
6918 }
6919 }
6920
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006921 /* break if no <item> is found */
Bram Moolenaara850a712009-01-28 14:42:59 +00006922 if (start == NULL || end == NULL)
6923 break;
6924
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 /* Include the '>' */
6926 ++end;
6927
6928 /* Take everything up to the '<' */
6929 len = start - p;
6930 if (buf == NULL)
6931 totlen += len;
6932 else
6933 {
6934 mch_memmove(q, p, len);
6935 q += len;
6936 }
6937
6938 len = uc_check_code(start, end - start, q, cmd, eap,
6939 &split_buf, &split_len);
6940 if (len == (size_t)-1)
6941 {
6942 /* no match, continue after '<' */
6943 p = start + 1;
6944 len = 1;
6945 }
6946 else
6947 p = end;
6948 if (buf == NULL)
6949 totlen += len;
6950 else
6951 q += len;
6952 }
6953 if (buf != NULL) /* second time here, finished */
6954 {
6955 STRCPY(q, p);
6956 break;
6957 }
6958
6959 totlen += STRLEN(p); /* Add on the trailing characters */
6960 buf = alloc((unsigned)(totlen + 1));
6961 if (buf == NULL)
6962 {
6963 vim_free(split_buf);
6964 return;
6965 }
6966 }
6967
6968#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006969 current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970#endif
6971 (void)do_cmdline(buf, eap->getline, eap->cookie,
6972 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6973#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006974 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975#endif
6976 vim_free(buf);
6977 vim_free(split_buf);
6978}
6979
6980# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6981 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006982get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983{
6984 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6985}
6986
6987/*
6988 * Function given to ExpandGeneric() to obtain the list of user command names.
6989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006991get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992{
6993 if (idx < curbuf->b_ucmds.ga_len)
6994 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6995 idx -= curbuf->b_ucmds.ga_len;
6996 if (idx < ucmds.ga_len)
6997 return USER_CMD(idx)->uc_name;
6998 return NULL;
6999}
7000
7001/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007002 * Function given to ExpandGeneric() to obtain the list of user address type names.
7003 */
7004 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007005get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007006{
7007 return (char_u *)addr_type_complete[idx].name;
7008}
7009
7010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 * Function given to ExpandGeneric() to obtain the list of user command
7012 * attributes.
7013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007015get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016{
7017 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007018 {"addr", "bang", "bar", "buffer", "complete",
7019 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007021 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 return NULL;
7023 return (char_u *)user_cmd_flags[idx];
7024}
7025
7026/*
7027 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007030get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031{
7032 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7033
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007034 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 return NULL;
7036 return (char_u *)user_cmd_nargs[idx];
7037}
7038
7039/*
7040 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007043get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044{
7045 return (char_u *)command_complete[idx].name;
7046}
7047# endif /* FEAT_CMDL_COMPL */
7048
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007049/*
7050 * Parse address type argument
7051 */
7052 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007053parse_addr_type_arg(
7054 char_u *value,
7055 int vallen,
7056 long *argt,
7057 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007058{
7059 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007060
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007061 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7062 {
7063 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7064 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7065 if (a && b)
7066 {
7067 *addr_type_arg = addr_type_complete[i].expand;
7068 break;
7069 }
7070 }
7071
7072 if (addr_type_complete[i].expand == -1)
7073 {
7074 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007075
Bram Moolenaar1c465442017-03-12 20:10:05 +01007076 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007077 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007078 err[i] = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007079 semsg(_("E180: Invalid address type value: %s"), err);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007080 return FAIL;
7081 }
7082
7083 if (*addr_type_arg != ADDR_LINES)
7084 *argt |= NOTADR;
7085
7086 return OK;
7087}
7088
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089#endif /* FEAT_USR_CMDS */
7090
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007091#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7092/*
7093 * Parse a completion argument "value[vallen]".
7094 * The detected completion goes in "*complp", argument type in "*argt".
7095 * When there is an argument, for function and user defined completion, it's
7096 * copied to allocated memory and stored in "*compl_arg".
7097 * Returns FAIL if something is wrong.
7098 */
7099 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007100parse_compl_arg(
7101 char_u *value,
7102 int vallen,
7103 int *complp,
7104 long *argt,
7105 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007106{
7107 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007108# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007109 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007110# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007111 int i;
7112 int valend = vallen;
7113
7114 /* Look for any argument part - which is the part after any ',' */
7115 for (i = 0; i < vallen; ++i)
7116 {
7117 if (value[i] == ',')
7118 {
7119 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007120# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007121 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007122# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007123 valend = i;
7124 break;
7125 }
7126 }
7127
7128 for (i = 0; command_complete[i].expand != 0; ++i)
7129 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007130 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007131 && STRNCMP(value, command_complete[i].name, valend) == 0)
7132 {
7133 *complp = command_complete[i].expand;
7134 if (command_complete[i].expand == EXPAND_BUFFERS)
7135 *argt |= BUFNAME;
7136 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7137 || command_complete[i].expand == EXPAND_FILES)
7138 *argt |= XFILE;
7139 break;
7140 }
7141 }
7142
7143 if (command_complete[i].expand == 0)
7144 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007145 semsg(_("E180: Invalid complete value: %s"), value);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007146 return FAIL;
7147 }
7148
7149# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7150 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7151 && arg != NULL)
7152# else
7153 if (arg != NULL)
7154# endif
7155 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007156 emsg(_("E468: Completion argument only allowed for custom completion"));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007157 return FAIL;
7158 }
7159
7160# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7161 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7162 && arg == NULL)
7163 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007164 emsg(_("E467: Custom completion requires a function argument"));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007165 return FAIL;
7166 }
7167
7168 if (arg != NULL)
7169 *compl_arg = vim_strnsave(arg, (int)arglen);
7170# endif
7171 return OK;
7172}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007173
7174 int
7175cmdcomplete_str_to_type(char_u *complete_str)
7176{
7177 int i;
7178
7179 for (i = 0; command_complete[i].expand != 0; ++i)
7180 if (STRCMP(complete_str, command_complete[i].name) == 0)
7181 return command_complete[i].expand;
7182
7183 return EXPAND_NOTHING;
7184}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007185#endif
7186
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007188ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189{
Bram Moolenaare6850792010-05-14 15:28:44 +02007190 if (*eap->arg == NUL)
7191 {
7192#ifdef FEAT_EVAL
7193 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7194 char_u *p = NULL;
7195
7196 if (expr != NULL)
7197 {
7198 ++emsg_off;
7199 p = eval_to_string(expr, NULL, FALSE);
7200 --emsg_off;
7201 vim_free(expr);
7202 }
7203 if (p != NULL)
7204 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007205 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02007206 vim_free(p);
7207 }
7208 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007209 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02007210#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007211 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02007212#endif
7213 }
7214 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007215 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216}
7217
7218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007219ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220{
7221 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01007222 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 do_highlight(eap->arg, eap->forceit, FALSE);
7224}
7225
7226
7227/*
7228 * Call this function if we thought we were going to exit, but we won't
7229 * (because of an error). May need to restore the terminal mode.
7230 */
7231 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007232not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233{
7234 exiting = FALSE;
7235 settmode(TMODE_RAW);
7236}
7237
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007238 static int
7239before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7240{
7241 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7242
7243 /* Bail out when autocommands closed the window.
7244 * Refuse to quit when the buffer in the last window is being closed (can
7245 * only happen in autocommands). */
7246 if (!win_valid(wp)
7247 || curbuf_locked()
7248 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7249 return TRUE;
7250
7251 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7252 {
7253 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7254 /* Refuse to quit when locked or when the buffer in the last window is
7255 * being closed (can only happen in autocommands). */
7256 if (curbuf_locked()
7257 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7258 return TRUE;
7259 }
7260
7261 return FALSE;
7262}
7263
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007265 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007266 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 */
7268 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007269ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007271 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007272
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273#ifdef FEAT_CMDWIN
7274 if (cmdwin_type != 0)
7275 {
7276 cmdwin_result = Ctrl_C;
7277 return;
7278 }
7279#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007280 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007281 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007282 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007283 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007284 return;
7285 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007286 if (eap->addr_count > 0)
7287 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007288 int wnr = eap->line2;
7289
7290 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7291 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007292 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007293 }
7294 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007295 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007296
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007297 /* Refuse to quit when locked. */
7298 if (curbuf_locked())
7299 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007300
7301 /* Trigger QuitPre and maybe ExitPre */
7302 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007303 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304
7305#ifdef FEAT_NETBEANS_INTG
7306 netbeansForcedQuit = eap->forceit;
7307#endif
7308
7309 /*
7310 * If there are more files or windows we won't exit.
7311 */
7312 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7313 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007314 if ((!buf_hide(wp->w_buffer)
7315 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007316 | (eap->forceit ? CCGD_FORCEIT : 0)
7317 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007319 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 {
7321 not_exiting();
7322 }
7323 else
7324 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007325 /* quit last window
7326 * Note: only_one_window() returns true, even so a help window is
7327 * still open. In that case only quit, if no address has been
7328 * specified. Example:
7329 * :h|wincmd w|1q - don't quit
7330 * :h|wincmd w|q - quit
7331 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007332 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007334 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007335#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007339 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 }
7341}
7342
7343/*
7344 * ":cquit".
7345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007347ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348{
7349 getout(1); /* this does not always pass on the exit code to the Manx
7350 compiler. why? */
7351}
7352
7353/*
7354 * ":qall": try to quit all windows
7355 */
7356 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007357ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358{
7359# ifdef FEAT_CMDWIN
7360 if (cmdwin_type != 0)
7361 {
7362 if (eap->forceit)
7363 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7364 else
7365 cmdwin_result = K_XF2;
7366 return;
7367 }
7368# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007369
7370 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007371 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007372 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007373 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007374 return;
7375 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007376
7377 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007378 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007379
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007381 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 getout(0);
7383 not_exiting();
7384}
7385
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386/*
7387 * ":close": close current window, unless it is the last one
7388 */
7389 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007390ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007392 win_T *win;
7393 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007394#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007396 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007398#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007399 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007400 {
7401 if (eap->addr_count == 0)
7402 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02007403 else
7404 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007405 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007406 {
7407 winnr++;
7408 if (winnr == eap->line2)
7409 break;
7410 }
7411 if (win == NULL)
7412 win = lastwin;
7413 ex_win_close(eap->forceit, win, NULL);
7414 }
7415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416}
7417
Bram Moolenaar4033c552017-09-16 20:54:51 +02007418#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007419/*
7420 * ":pclose": Close any preview window.
7421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007423ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007424{
7425 win_T *win;
7426
Bram Moolenaar29323592016-07-24 22:04:11 +02007427 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007428 if (win->w_p_pvw)
7429 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007430 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007431 break;
7432 }
7433}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007434#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007435
Bram Moolenaarf740b292006-02-16 22:11:02 +00007436/*
7437 * Close window "win" and take care of handling closing the last window for a
7438 * modified buffer.
7439 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007441ex_win_close(
7442 int forceit,
7443 win_T *win,
7444 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445{
7446 int need_hide;
7447 buf_T *buf = win->w_buffer;
7448
7449 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007450 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007452#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 if ((p_confirm || cmdmod.confirm) && p_write)
7454 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007455 bufref_T bufref;
7456
7457 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007459 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 return;
7461 need_hide = FALSE;
7462 }
7463 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007466 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 return;
7468 }
7469 }
7470
Bram Moolenaar4033c552017-09-16 20:54:51 +02007471#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007473#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007474
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007476 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007477 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007478 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007479 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480}
7481
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007483 * Handle the argument for a tabpage related ex command.
7484 * Returns a tabpage number.
7485 * When an error is encountered then eap->errmsg is set.
7486 */
7487 static int
7488get_tabpage_arg(exarg_T *eap)
7489{
7490 int tab_number;
7491 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7492
7493 if (eap->arg && *eap->arg != NUL)
7494 {
7495 char_u *p = eap->arg;
7496 char_u *p_save;
7497 int relative = 0; /* argument +N/-N means: go to N places to the
7498 * right/left relative to the current position. */
7499
7500 if (*p == '-')
7501 {
7502 relative = -1;
7503 p++;
7504 }
7505 else if (*p == '+')
7506 {
7507 relative = 1;
7508 p++;
7509 }
7510
7511 p_save = p;
7512 tab_number = getdigits(&p);
7513
7514 if (relative == 0)
7515 {
7516 if (STRCMP(p, "$") == 0)
7517 tab_number = LAST_TAB_NR;
7518 else if (p == p_save || *p_save == '-' || *p != NUL
7519 || tab_number > LAST_TAB_NR)
7520 {
7521 /* No numbers as argument. */
7522 eap->errmsg = e_invarg;
7523 goto theend;
7524 }
7525 }
7526 else
7527 {
7528 if (*p_save == NUL)
7529 tab_number = 1;
7530 else if (p == p_save || *p_save == '-' || *p != NUL
7531 || tab_number == 0)
7532 {
7533 /* No numbers as argument. */
7534 eap->errmsg = e_invarg;
7535 goto theend;
7536 }
7537 tab_number = tab_number * relative + tabpage_index(curtab);
7538 if (!unaccept_arg0 && relative == -1)
7539 --tab_number;
7540 }
7541 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7542 eap->errmsg = e_invarg;
7543 }
7544 else if (eap->addr_count > 0)
7545 {
7546 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007547 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007548 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007549 tab_number = 0;
7550 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007551 else
7552 {
7553 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01007554 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01007555 {
7556 --tab_number;
7557 if (tab_number < unaccept_arg0)
7558 eap->errmsg = e_invarg;
7559 }
7560 }
7561 }
7562 else
7563 {
7564 switch (eap->cmdidx)
7565 {
7566 case CMD_tabnext:
7567 tab_number = tabpage_index(curtab) + 1;
7568 if (tab_number > LAST_TAB_NR)
7569 tab_number = 1;
7570 break;
7571 case CMD_tabmove:
7572 tab_number = LAST_TAB_NR;
7573 break;
7574 default:
7575 tab_number = tabpage_index(curtab);
7576 }
7577 }
7578
7579theend:
7580 return tab_number;
7581}
7582
7583/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007584 * ":tabclose": close current tab page, unless it is the last one.
7585 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 */
7587 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007588ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007590 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007591 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007592
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007593# ifdef FEAT_CMDWIN
7594 if (cmdwin_type != 0)
7595 cmdwin_result = K_IGNORE;
7596 else
7597# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007598 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007599 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007600 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007602 tab_number = get_tabpage_arg(eap);
7603 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007604 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007605 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007606 if (tp == NULL)
7607 {
7608 beep_flush();
7609 return;
7610 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007611 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007612 {
7613 tabpage_close_other(tp, eap->forceit);
7614 return;
7615 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007616 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007617 tabpage_close(eap->forceit);
7618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007620}
7621
7622/*
7623 * ":tabonly": close all tab pages except the current one
7624 */
7625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007626ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007627{
7628 tabpage_T *tp;
7629 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007630 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007631
7632# ifdef FEAT_CMDWIN
7633 if (cmdwin_type != 0)
7634 cmdwin_result = K_IGNORE;
7635 else
7636# endif
7637 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007638 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007639 else
7640 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007641 tab_number = get_tabpage_arg(eap);
7642 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007643 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007644 goto_tabpage(tab_number);
7645 /* Repeat this up to a 1000 times, because autocommands may
7646 * mess up the lists. */
7647 for (done = 0; done < 1000; ++done)
7648 {
7649 FOR_ALL_TABPAGES(tp)
7650 if (tp->tp_topframe != topframe)
7651 {
7652 tabpage_close_other(tp, eap->forceit);
7653 /* if we failed to close it quit */
7654 if (valid_tabpage(tp))
7655 done = 1000;
7656 /* start over, "tp" is now invalid */
7657 break;
7658 }
7659 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007660 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007661 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007662 }
7663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665
7666/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007667 * Close the current tab page.
7668 */
7669 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007670tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007671{
7672 /* First close all the windows but the current one. If that worked then
7673 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007674 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007675 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007676 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007677 ex_win_close(forceit, curwin, NULL);
7678# ifdef FEAT_GUI
7679 need_mouse_correct = TRUE;
7680# endif
7681}
7682
7683/*
7684 * Close tab page "tp", which is not the current tab page.
7685 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007686 * Also takes care of the tab pages line disappearing when closing the
7687 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007688 */
7689 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007690tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007691{
7692 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007693 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007694 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007695
7696 /* Limit to 1000 windows, autocommands may add a window while we close
7697 * one. OK, so I'm paranoid... */
7698 while (++done < 1000)
7699 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007700 wp = tp->tp_firstwin;
7701 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007702
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007703 /* Autocommands may delete the tab page under our fingers and we may
7704 * fail to close a window with a modified buffer. */
7705 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007706 break;
7707 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007708
Bram Moolenaar29323592016-07-24 22:04:11 +02007709 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007710
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007711 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007712 if (h != tabline_height())
7713 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007714}
7715
7716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 * ":only".
7718 */
7719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007720ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007722 win_T *wp;
7723 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724# ifdef FEAT_GUI
7725 need_mouse_correct = TRUE;
7726# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007727 if (eap->addr_count > 0)
7728 {
7729 wnr = eap->line2;
7730 for (wp = firstwin; --wnr > 0; )
7731 {
7732 if (wp->w_next == NULL)
7733 break;
7734 else
7735 wp = wp->w_next;
7736 }
7737 win_goto(wp);
7738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 close_others(TRUE, eap->forceit);
7740}
7741
7742/*
7743 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007744 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007746 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007747ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748{
7749 if (eap->addr_count == 0)
7750 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007751 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753
7754 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007755ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007757 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007758 if (!eap->skip)
7759 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007760#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007761 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007762#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007763 if (eap->addr_count == 0)
7764 win_close(curwin, FALSE); /* don't free buffer */
7765 else
7766 {
7767 int winnr = 0;
7768 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007769
Bram Moolenaar2256c992016-11-15 21:17:07 +01007770 FOR_ALL_WINDOWS(win)
7771 {
7772 winnr++;
7773 if (winnr == eap->line2)
7774 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007775 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007776 if (win == NULL)
7777 win = lastwin;
7778 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 }
7781}
7782
7783/*
7784 * ":stop" and ":suspend": Suspend Vim.
7785 */
7786 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007787ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788{
7789 /*
7790 * Disallow suspending for "rvim".
7791 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007792 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 {
7794 if (!eap->forceit)
7795 autowrite_all();
7796 windgoto((int)Rows - 1, 0);
7797 out_char('\n');
7798 out_flush();
7799 stoptermcap();
7800 out_flush(); /* needed for SUN to restore xterm buffer */
7801#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02007802 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803#endif
7804 ui_suspend(); /* call machine specific function */
7805#ifdef FEAT_TITLE
7806 maketitle();
7807 resettitle(); /* force updating the title */
7808#endif
7809 starttermcap();
7810 scroll_start(); /* scroll screen before redrawing */
7811 redraw_later_clear();
7812 shell_resized(); /* may have resized window */
7813 }
7814}
7815
7816/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007817 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 */
7819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007820ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821{
7822#ifdef FEAT_CMDWIN
7823 if (cmdwin_type != 0)
7824 {
7825 cmdwin_result = Ctrl_C;
7826 return;
7827 }
7828#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007829 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007830 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007831 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007832 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007833 return;
7834 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007835
7836 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007837 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838
7839 /*
7840 * if more files or windows we won't exit
7841 */
7842 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7843 exiting = TRUE;
7844 if ( ((eap->cmdidx == CMD_wq
7845 || curbufIsChanged())
7846 && do_write(eap) == FAIL)
7847 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007848 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 {
7850 not_exiting();
7851 }
7852 else
7853 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007856 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857# ifdef FEAT_GUI
7858 need_mouse_correct = TRUE;
7859# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007860 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007861 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 }
7863}
7864
7865/*
7866 * ":print", ":list", ":number".
7867 */
7868 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007869ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007871 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007872 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007873 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007875 for ( ;!got_int; ui_breakcheck())
7876 {
7877 print_line(eap->line1,
7878 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7879 || (eap->flags & EXFLAG_NR)),
7880 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7881 if (++eap->line1 > eap->line2)
7882 break;
7883 out_flush(); /* show one line at a time */
7884 }
7885 setpcmark();
7886 /* put cursor at last line */
7887 curwin->w_cursor.lnum = eap->line2;
7888 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 }
7890
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892}
7893
7894#ifdef FEAT_BYTEOFF
7895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007896ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897{
7898 goto_byte(eap->line2);
7899}
7900#endif
7901
7902/*
7903 * ":shell".
7904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007906ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907{
7908 do_shell(NULL, 0);
7909}
7910
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007911#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007913static int drop_busy = FALSE;
7914static int drop_filec;
7915static char_u **drop_filev = NULL;
7916static int drop_split;
7917static void (*drop_callback)(void *);
7918static void *drop_cookie;
7919
7920 static void
7921handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922{
7923 exarg_T ea;
7924 int save_msg_scroll = msg_scroll;
7925
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007926 // Setting the argument list may cause screen updates and being called
7927 // recursively. Avoid that by setting drop_busy.
7928 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929
7930 /* Check whether the current buffer is changed. If so, we will need
7931 * to split the current window or data could be lost.
7932 * We don't need to check if the 'hidden' option is set, as in this
7933 * case the buffer won't be lost.
7934 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007935 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 {
7937 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007938 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 --emsg_off;
7940 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007941 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 if (win_split(0, 0) == FAIL)
7944 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007945 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946
7947 /* When splitting the window, create a new alist. Otherwise the
7948 * existing one is overwritten. */
7949 alist_unlink(curwin->w_alist);
7950 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 }
7952
7953 /*
7954 * Set up the new argument list.
7955 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007956 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957
7958 /*
7959 * Move to the first file.
7960 */
7961 /* Fake up a minimal "next" command for do_argfile() */
7962 vim_memset(&ea, 0, sizeof(ea));
7963 ea.cmd = (char_u *)"next";
7964 do_argfile(&ea, 0);
7965
7966 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7967 * mode that is not needed here. */
7968 need_start_insertmode = FALSE;
7969
7970 /* Restore msg_scroll, otherwise a following command may cause scrolling
7971 * unexpectedly. The screen will be redrawn by the caller, thus
7972 * msg_scroll being set by displaying a message is irrelevant. */
7973 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007974
7975 if (drop_callback != NULL)
7976 drop_callback(drop_cookie);
7977
7978 drop_filev = NULL;
7979 drop_busy = FALSE;
7980}
7981
7982/*
7983 * Handle a file drop. The code is here because a drop is *nearly* like an
7984 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01007985 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007986 * code is very similar to :args and hence needs access to a lot of the static
7987 * functions in this file.
7988 *
7989 * The "filev" list must have been allocated using alloc(), as should each item
7990 * in the list. This function takes over responsibility for freeing the "filev"
7991 * list.
7992 */
7993 void
7994handle_drop(
7995 int filec, // the number of files dropped
7996 char_u **filev, // the list of files dropped
7997 int split, // force splitting the window
7998 void (*callback)(void *), // to be called after setting the argument
7999 // list
8000 void *cookie) // argument for "callback" (allocated)
8001{
8002 // Cannot handle recursive drops, finish the pending one.
8003 if (drop_busy)
8004 {
8005 FreeWild(filec, filev);
8006 vim_free(cookie);
8007 return;
8008 }
8009
8010 // When calling handle_drop() more than once in a row we only use the last
8011 // one.
8012 if (drop_filev != NULL)
8013 {
8014 FreeWild(drop_filec, drop_filev);
8015 vim_free(drop_cookie);
8016 }
8017
8018 drop_filec = filec;
8019 drop_filev = filev;
8020 drop_split = split;
8021 drop_callback = callback;
8022 drop_cookie = cookie;
8023
8024 // Postpone this when:
8025 // - editing the command line
8026 // - not possible to change the current buffer
8027 // - updating the screen
8028 // As it may change buffers and window structures that are in use and cause
8029 // freed memory to be used.
8030 if (text_locked() || curbuf_locked() || updating_screen)
8031 return;
8032
8033 handle_drop_internal();
8034}
8035
8036/*
8037 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
8038 * reset: Handle a postponed drop.
8039 */
8040 void
8041handle_any_postponed_drop(void)
8042{
8043 if (!drop_busy && drop_filev != NULL
8044 && !text_locked() && !curbuf_locked() && !updating_screen)
8045 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046}
8047#endif
8048
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049/*
8050 * Clear an argument list: free all file names and reset it to zero entries.
8051 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008052 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008053alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054{
8055 while (--al->al_ga.ga_len >= 0)
8056 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8057 ga_clear(&al->al_ga);
8058}
8059
8060/*
8061 * Init an argument list.
8062 */
8063 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008064alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065{
8066 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8067}
8068
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069/*
8070 * Remove a reference from an argument list.
8071 * Ignored when the argument list is the global one.
8072 * If the argument list is no longer used by any window, free it.
8073 */
8074 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008075alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076{
8077 if (al != &global_alist && --al->al_refcount <= 0)
8078 {
8079 alist_clear(al);
8080 vim_free(al);
8081 }
8082}
8083
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084/*
8085 * Create a new argument list and use it for the current window.
8086 */
8087 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008088alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089{
8090 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8091 if (curwin->w_alist == NULL)
8092 {
8093 curwin->w_alist = &global_alist;
8094 ++global_alist.al_refcount;
8095 }
8096 else
8097 {
8098 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008099 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 alist_init(curwin->w_alist);
8101 }
8102}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008104#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105/*
8106 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008107 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8108 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 */
8110 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008111alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112{
8113 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008114 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 char_u **new_arg_files;
8116 int new_arg_file_count;
8117 char_u *save_p_su = p_su;
8118 int i;
8119
8120 /* Don't use 'suffixes' here. This should work like the shell did the
8121 * expansion. Also, the vimrc file isn't read yet, thus the user
8122 * can't set the options. */
8123 p_su = empty_option;
8124 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8125 if (old_arg_files != NULL)
8126 {
8127 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008128 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8129 old_arg_count = GARGCOUNT;
8130 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008132 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 && new_arg_file_count > 0)
8134 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008135 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8136 TRUE, fnum_list, fnum_len);
8137 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 }
8140 p_su = save_p_su;
8141}
8142#endif
8143
8144/*
8145 * Set the argument list for the current window.
8146 * Takes over the allocated files[] and the allocated fnames in it.
8147 */
8148 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008149alist_set(
8150 alist_T *al,
8151 int count,
8152 char_u **files,
8153 int use_curbuf,
8154 int *fnum_list,
8155 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156{
8157 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008158 static int recursive = 0;
8159
8160 if (recursive)
8161 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008162 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008163 return;
8164 }
8165 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166
8167 alist_clear(al);
8168 if (ga_grow(&al->al_ga, count) == OK)
8169 {
8170 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008171 {
8172 if (got_int)
8173 {
8174 /* When adding many buffers this can take a long time. Allow
8175 * interrupting here. */
8176 while (i < count)
8177 vim_free(files[i++]);
8178 break;
8179 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008180
8181 /* May set buffer name of a buffer previously used for the
8182 * argument list, so that it's re-used by alist_add. */
8183 if (fnum_list != NULL && i < fnum_len)
8184 buf_set_name(fnum_list[i], files[i]);
8185
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008187 ui_breakcheck();
8188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 vim_free(files);
8190 }
8191 else
8192 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008195
8196 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197}
8198
8199/*
8200 * Add file "fname" to argument list "al".
8201 * "fname" must have been allocated and "al" must have been checked for room.
8202 */
8203 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008204alist_add(
8205 alist_T *al,
8206 char_u *fname,
8207 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208{
8209 if (fname == NULL) /* don't add NULL file names */
8210 return;
8211#ifdef BACKSLASH_IN_FILENAME
8212 slash_adjust(fname);
8213#endif
8214 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8215 if (set_fnum > 0)
8216 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8217 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8218 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219}
8220
8221#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8222/*
8223 * Adjust slashes in file names. Called after 'shellslash' was set.
8224 */
8225 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008226alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227{
8228 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008230 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231
8232 for (i = 0; i < GARGCOUNT; ++i)
8233 if (GARGLIST[i].ae_fname != NULL)
8234 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008235 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 if (wp->w_alist != &global_alist)
8237 for (i = 0; i < WARGCOUNT(wp); ++i)
8238 if (WARGLIST(wp)[i].ae_fname != NULL)
8239 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240}
8241#endif
8242
8243/*
8244 * ":preserve".
8245 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008247ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008249 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 ml_preserve(curbuf, TRUE);
8251}
8252
8253/*
8254 * ":recover".
8255 */
8256 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008257ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258{
8259 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8260 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008261 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8262 | CCGD_MULTWIN
8263 | (eap->forceit ? CCGD_FORCEIT : 0)
8264 | CCGD_EXCMD)
8265
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 && (*eap->arg == NUL
8267 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8268 ml_recover();
8269 recoverymode = FALSE;
8270}
8271
8272/*
8273 * Command modifier used in a wrong way.
8274 */
8275 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008276ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277{
8278 eap->errmsg = e_invcmd;
8279}
8280
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281/*
8282 * :sview [+command] file split window with new file, read-only
8283 * :split [[+command] file] split window with current or new file
8284 * :vsplit [[+command] file] split window vertically with current or new file
8285 * :new [[+command] file] split window with no or new file
8286 * :vnew [[+command] file] split vertically window with no or new file
8287 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008288 *
8289 * :tabedit open new Tab page with empty window
8290 * :tabedit [+command] file open new Tab page and edit "file"
8291 * :tabnew [[+command] file] just like :tabedit
8292 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 */
8294 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008295ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008297 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008298#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008300#endif
8301#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008303#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02008304 int use_tab = eap->cmdidx == CMD_tabedit
8305 || eap->cmdidx == CMD_tabfind
8306 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307
Bram Moolenaar4033c552017-09-16 20:54:51 +02008308#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311
Bram Moolenaar4033c552017-09-16 20:54:51 +02008312#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008314 * quickfix windows. But it's OK when doing ":tab split". */
8315 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 {
8317 if (eap->cmdidx == CMD_split)
8318 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 if (eap->cmdidx == CMD_vsplit)
8320 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323
Bram Moolenaar4033c552017-09-16 20:54:51 +02008324#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008325 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 {
8327 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8328 FNAME_MESS, TRUE, curbuf->b_ffname);
8329 if (fname == NULL)
8330 goto theend;
8331 eap->arg = fname;
8332 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008333# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008334 else
8335# endif
8336#endif
8337#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 && eap->cmdidx != CMD_new)
8341 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008342 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008343# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008344 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008345# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008346 au_has_group((char_u *)"FileExplorer"))
8347 {
8348 /* No browsing supported but we do have the file explorer:
8349 * Edit the directory. */
8350 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8351 eap->arg = (char_u *)".";
8352 }
8353 else
8354 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02008355 fname = do_browse(0, (char_u *)(use_tab
8356 ? _("Edit File in new tab page")
8357 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008359 if (fname == NULL)
8360 goto theend;
8361 eap->arg = fname;
8362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 }
8364 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008367 /*
8368 * Either open new tab page or split the window.
8369 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02008370 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008371 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008372 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8373 : eap->addr_count == 0 ? 0
8374 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008375 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008376 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008377
8378 /* set the alternate buffer for the window we came from */
8379 if (curwin != old_curwin
8380 && win_valid(old_curwin)
8381 && old_curwin->w_buffer != curbuf
8382 && !cmdmod.keepalt)
8383 old_curwin->w_alt_fnum = curbuf->b_fnum;
8384 }
8385 }
8386 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8388 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 /* Reset 'scrollbind' when editing another file, but keep it when
8390 * doing ":split" without arguments. */
8391 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008392# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008394# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008396 {
8397 RESET_BINDING(curwin);
8398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 else
8400 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 do_exedit(eap, old_curwin);
8402 }
8403
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008404# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008408# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409theend:
8410 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008411# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008413
8414/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008415 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008416 */
8417 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008418tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008419{
8420 exarg_T ea;
8421
8422 vim_memset(&ea, 0, sizeof(ea));
8423 ea.cmdidx = CMD_tabnew;
8424 ea.cmd = (char_u *)"tabn";
8425 ea.arg = (char_u *)"";
8426 ex_splitview(&ea);
8427}
8428
8429/*
8430 * :tabnext command
8431 */
8432 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008433ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008434{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008435 int tab_number;
8436
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008437 switch (eap->cmdidx)
8438 {
8439 case CMD_tabfirst:
8440 case CMD_tabrewind:
8441 goto_tabpage(1);
8442 break;
8443 case CMD_tablast:
8444 goto_tabpage(9999);
8445 break;
8446 case CMD_tabprevious:
8447 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008448 if (eap->arg && *eap->arg != NUL)
8449 {
8450 char_u *p = eap->arg;
8451 char_u *p_save = p;
8452
8453 tab_number = getdigits(&p);
8454 if (p == p_save || *p_save == '-' || *p != NUL
8455 || tab_number == 0)
8456 {
8457 /* No numbers as argument. */
8458 eap->errmsg = e_invarg;
8459 return;
8460 }
8461 }
8462 else
8463 {
8464 if (eap->addr_count == 0)
8465 tab_number = 1;
8466 else
8467 {
8468 tab_number = eap->line2;
8469 if (tab_number < 1)
8470 {
8471 eap->errmsg = e_invrange;
8472 return;
8473 }
8474 }
8475 }
8476 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008477 break;
8478 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008479 tab_number = get_tabpage_arg(eap);
8480 if (eap->errmsg == NULL)
8481 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008482 break;
8483 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008484}
8485
8486/*
8487 * :tabmove command
8488 */
8489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008490ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008491{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008492 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008493
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008494 tab_number = get_tabpage_arg(eap);
8495 if (eap->errmsg == NULL)
8496 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008497}
8498
8499/*
8500 * :tabs command: List tabs and their contents.
8501 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008503ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008504{
8505 tabpage_T *tp;
8506 win_T *wp;
8507 int tabcount = 1;
8508
8509 msg_start();
8510 msg_scroll = TRUE;
8511 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8512 {
8513 msg_putchar('\n');
8514 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008515 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008516 out_flush(); /* output one line at a time */
8517 ui_breakcheck();
8518
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008519 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008520 wp = firstwin;
8521 else
8522 wp = tp->tp_firstwin;
8523 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8524 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008525 msg_putchar('\n');
8526 msg_putchar(wp == curwin ? '>' : ' ');
8527 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008528 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8529 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008530 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008531 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008532 else
8533 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8534 IObuff, IOSIZE, TRUE);
8535 msg_outtrans(IObuff);
8536 out_flush(); /* output one line at a time */
8537 ui_breakcheck();
8538 }
8539 }
8540}
8541
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542/*
8543 * ":mode": Set screen mode.
8544 * If no argument given, just get the screen size and redraw.
8545 */
8546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008547ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548{
8549 if (*eap->arg == NUL)
8550 shell_resized();
8551 else
8552 mch_screenmode(eap->arg);
8553}
8554
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555/*
8556 * ":resize".
8557 * set, increment or decrement current window height
8558 */
8559 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008560ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561{
8562 int n;
8563 win_T *wp = curwin;
8564
8565 if (eap->addr_count > 0)
8566 {
8567 n = eap->line2;
8568 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8569 ;
8570 }
8571
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008572# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008574# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 if (cmdmod.split & WSP_VERT)
8577 {
8578 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008579 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8581 n = 9999;
8582 win_setwidth_win((int)n, wp);
8583 }
8584 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 {
8586 if (*eap->arg == '-' || *eap->arg == '+')
8587 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008588 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 n = 9999;
8590 win_setheight_win((int)n, wp);
8591 }
8592}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593
8594/*
8595 * ":find [+command] <file>" command.
8596 */
8597 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008598ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599{
8600#ifdef FEAT_SEARCHPATH
8601 char_u *fname;
8602 int count;
8603
8604 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8605 TRUE, curbuf->b_ffname);
8606 if (eap->addr_count > 0)
8607 {
8608 /* Repeat finding the file "count" times. This matters when it
8609 * appears several times in the path. */
8610 count = eap->line2;
8611 while (fname != NULL && --count > 0)
8612 {
8613 vim_free(fname);
8614 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8615 FALSE, curbuf->b_ffname);
8616 }
8617 }
8618
8619 if (fname != NULL)
8620 {
8621 eap->arg = fname;
8622#endif
8623 do_exedit(eap, NULL);
8624#ifdef FEAT_SEARCHPATH
8625 vim_free(fname);
8626 }
8627#endif
8628}
8629
8630/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008631 * ":open" simulation: for now just work like ":visual".
8632 */
8633 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008634ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008635{
8636 regmatch_T regmatch;
8637 char_u *p;
8638
8639 curwin->w_cursor.lnum = eap->line2;
8640 beginline(BL_SOL | BL_FIX);
8641 if (*eap->arg == '/')
8642 {
8643 /* ":open /pattern/": put cursor in column found with pattern */
8644 ++eap->arg;
8645 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8646 *p = NUL;
8647 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8648 if (regmatch.regprog != NULL)
8649 {
8650 regmatch.rm_ic = p_ic;
8651 p = ml_get_curline();
8652 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008653 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008654 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008655 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008656 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008657 }
8658 /* Move to the NUL, ignore any other arguments. */
8659 eap->arg += STRLEN(eap->arg);
8660 }
8661 check_cursor();
8662
8663 eap->cmdidx = CMD_visual;
8664 do_exedit(eap, NULL);
8665}
8666
8667/*
8668 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 */
8670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008671ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672{
8673 do_exedit(eap, NULL);
8674}
8675
8676/*
8677 * ":edit <file>" command and alikes.
8678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008680do_exedit(
8681 exarg_T *eap,
8682 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683{
8684 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008686 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687
8688 /*
8689 * ":vi" command ends Ex mode.
8690 */
8691 if (exmode_active && (eap->cmdidx == CMD_visual
8692 || eap->cmdidx == CMD_view))
8693 {
8694 exmode_active = FALSE;
8695 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008696 {
8697 /* Special case: ":global/pat/visual\NLvi-commands" */
8698 if (global_busy)
8699 {
8700 int rd = RedrawingDisabled;
8701 int nwr = no_wait_return;
8702 int ms = msg_scroll;
8703#ifdef FEAT_GUI
8704 int he = hold_gui_events;
8705#endif
8706
8707 if (eap->nextcmd != NULL)
8708 {
8709 stuffReadbuff(eap->nextcmd);
8710 eap->nextcmd = NULL;
8711 }
8712
8713 if (exmode_was != EXMODE_VIM)
8714 settmode(TMODE_RAW);
8715 RedrawingDisabled = 0;
8716 no_wait_return = 0;
8717 need_wait_return = FALSE;
8718 msg_scroll = 0;
8719#ifdef FEAT_GUI
8720 hold_gui_events = 0;
8721#endif
8722 must_redraw = CLEAR;
8723
8724 main_loop(FALSE, TRUE);
8725
8726 RedrawingDisabled = rd;
8727 no_wait_return = nwr;
8728 msg_scroll = ms;
8729#ifdef FEAT_GUI
8730 hold_gui_events = he;
8731#endif
8732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 }
8736
8737 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008738 || eap->cmdidx == CMD_tabnew
8739 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008740 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008742 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 setpcmark();
8744 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008745 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8746 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008748 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 || *eap->arg != NUL
8750#ifdef FEAT_BROWSE
8751 || cmdmod.browse
8752#endif
8753 )
8754 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008755 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8756 * can bring us here, others are stopped earlier. */
8757 if (*eap->arg != NUL && curbuf_locked())
8758 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008759
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 n = readonlymode;
8761 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8762 readonlymode = TRUE;
8763 else if (eap->cmdidx == CMD_enew)
8764 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8765 empty buffer */
8766 setpcmark();
8767 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8768 NULL, eap,
8769 /* ":edit" goes to first line if Vi compatible */
8770 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8771 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8772 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008773 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008775 /* after a split we can use an existing buffer */
8776 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008778 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 {
8780 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 if (old_curwin != NULL)
8782 {
8783 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008784 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008786#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008787 cleanup_T cs;
8788
8789 /* Reset the error/interrupt/exception state here so that
8790 * aborting() returns FALSE when closing a window. */
8791 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008792#endif
8793#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008795#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008796 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008797
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008798#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008799 /* Restore the error/interrupt/exception state if not
8800 * discarded by a new aborting error, interrupt, or
8801 * uncaught exception. */
8802 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 }
8805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 }
8807 else if (readonlymode && curbuf->b_nwindows == 1)
8808 {
8809 /* When editing an already visited buffer, 'readonly' won't be set
8810 * but the previous value is kept. With ":view" and ":sview" we
8811 * want the file to be readonly, except when another window is
8812 * editing the same buffer. */
8813 curbuf->b_p_ro = TRUE;
8814 }
8815 readonlymode = n;
8816 }
8817 else
8818 {
8819 if (eap->do_ecmd_cmd != NULL)
8820 do_cmdline_cmd(eap->do_ecmd_cmd);
8821#ifdef FEAT_TITLE
8822 n = curwin->w_arg_idx_invalid;
8823#endif
8824 check_arg_idx(curwin);
8825#ifdef FEAT_TITLE
8826 if (n != curwin->w_arg_idx_invalid)
8827 maketitle();
8828#endif
8829 }
8830
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 /*
8832 * if ":split file" worked, set alternate file name in old window to new
8833 * file
8834 */
8835 if (old_curwin != NULL
8836 && *eap->arg != NUL
8837 && curwin != old_curwin
8838 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008839 && old_curwin->w_buffer != curbuf
8840 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842
8843 ex_no_reprint = TRUE;
8844}
8845
8846#ifndef FEAT_GUI
8847/*
8848 * ":gui" and ":gvim" when there is no GUI.
8849 */
8850 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008851ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852{
8853 eap->errmsg = e_nogvim;
8854}
8855#endif
8856
8857#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8858 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008859ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860{
8861 gui_make_tearoff(eap->arg);
8862}
8863#endif
8864
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008865#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8866 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008868ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008870# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8871 if (gui.in_use)
8872 gui_make_popup(eap->arg, eap->forceit);
8873# ifdef FEAT_TERM_POPUP_MENU
8874 else
8875# endif
8876# endif
8877# ifdef FEAT_TERM_POPUP_MENU
8878 pum_make_popup(eap->arg, eap->forceit);
8879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880}
8881#endif
8882
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008884ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885{
8886 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008887 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01008889 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890}
8891
8892/*
8893 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8894 * offset.
8895 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008898ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008901 win_T *save_curwin = curwin;
8902 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 long topline;
8904 long y;
8905 linenr_T old_linenr = curwin->w_cursor.lnum;
8906
8907 setpcmark();
8908
8909 /*
8910 * determine max topline
8911 */
8912 if (curwin->w_p_scb)
8913 {
8914 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008915 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 {
8917 if (wp->w_p_scb && wp->w_buffer)
8918 {
8919 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8920 if (topline > y)
8921 topline = y;
8922 }
8923 }
8924 if (topline < 1)
8925 topline = 1;
8926 }
8927 else
8928 {
8929 topline = 1;
8930 }
8931
8932
8933 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008934 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008936 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 {
8938 if (curwin->w_p_scb)
8939 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008940 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 y = topline - curwin->w_topline;
8942 if (y > 0)
8943 scrollup(y, TRUE);
8944 else
8945 scrolldown(-y, TRUE);
8946 curwin->w_scbind_pos = topline;
8947 redraw_later(VALID);
8948 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 }
8951 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008952 curwin = save_curwin;
8953 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 if (curwin->w_p_scb)
8955 {
8956 did_syncbind = TRUE;
8957 checkpcmark();
8958 if (old_linenr != curwin->w_cursor.lnum)
8959 {
8960 char_u ctrl_o[2];
8961
8962 ctrl_o[0] = Ctrl_O;
8963 ctrl_o[1] = 0;
8964 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8965 }
8966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967}
8968
8969
8970 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008971ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008973 int i;
8974 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8975 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976
8977 if (eap->usefilter) /* :r!cmd */
8978 do_bang(1, eap, FALSE, FALSE, TRUE);
8979 else
8980 {
8981 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8982 return;
8983
8984#ifdef FEAT_BROWSE
8985 if (cmdmod.browse)
8986 {
8987 char_u *browseFile;
8988
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008989 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 NULL, NULL, NULL, curbuf);
8991 if (browseFile != NULL)
8992 {
8993 i = readfile(browseFile, NULL,
8994 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8995 vim_free(browseFile);
8996 }
8997 else
8998 i = OK;
8999 }
9000 else
9001#endif
9002 if (*eap->arg == NUL)
9003 {
9004 if (check_fname() == FAIL) /* check for no file name */
9005 return;
9006 i = readfile(curbuf->b_ffname, curbuf->b_fname,
9007 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9008 }
9009 else
9010 {
9011 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
9012 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
9013 i = readfile(eap->arg, NULL,
9014 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9015
9016 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01009017 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009019#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 if (!aborting())
9021#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009022 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 }
9024 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009025 {
9026 if (empty && exmode_active)
9027 {
9028 /* Delete the empty line that remains. Historically ex does
9029 * this but vi doesn't. */
9030 if (eap->line2 == 0)
9031 lnum = curbuf->b_ml.ml_line_count;
9032 else
9033 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009034 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009035 {
9036 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009037 if (curwin->w_cursor.lnum > 1
9038 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009039 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009040 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009041 }
9042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 }
9046}
9047
Bram Moolenaarea408852005-06-25 22:49:46 +00009048static char_u *prev_dir = NULL;
9049
9050#if defined(EXITFREE) || defined(PROTO)
9051 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009052free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009053{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009054 VIM_CLEAR(prev_dir);
9055 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00009056}
9057#endif
9058
Bram Moolenaarf4258302013-06-02 18:20:17 +02009059/*
9060 * Deal with the side effects of changing the current directory.
9061 * When "local" is TRUE then this was after an ":lcd" command.
9062 */
9063 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009064post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009065{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009066 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009067 if (local)
9068 {
9069 /* If still in global directory, need to remember current
9070 * directory as global directory. */
9071 if (globaldir == NULL && prev_dir != NULL)
9072 globaldir = vim_strsave(prev_dir);
9073 /* Remember this local directory for the window. */
9074 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9075 curwin->w_localdir = vim_strsave(NameBuff);
9076 }
9077 else
9078 {
9079 /* We are now in the global directory, no need to remember its
9080 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01009081 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009082 }
9083
9084 shorten_fnames(TRUE);
9085}
9086
Bram Moolenaarea408852005-06-25 22:49:46 +00009087
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088/*
9089 * ":cd", ":lcd", ":chdir" and ":lchdir".
9090 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009091 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009092ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009094 char_u *new_dir;
9095 char_u *tofree;
9096 int dir_differs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097
9098 new_dir = eap->arg;
9099#if !defined(UNIX) && !defined(VMS)
9100 /* for non-UNIX ":cd" means: print current directory */
9101 if (*new_dir == NUL)
9102 ex_pwd(NULL);
9103 else
9104#endif
9105 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009106 if (allbuf_locked())
9107 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009108 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9109 && !eap->forceit)
9110 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009111 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009112 return;
9113 }
9114
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 /* ":cd -": Change to previous directory */
9116 if (STRCMP(new_dir, "-") == 0)
9117 {
9118 if (prev_dir == NULL)
9119 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009120 emsg(_("E186: No previous directory"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 return;
9122 }
9123 new_dir = prev_dir;
9124 }
9125
9126 /* Save current directory for next ":cd -" */
9127 tofree = prev_dir;
9128 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9129 prev_dir = vim_strsave(NameBuff);
9130 else
9131 prev_dir = NULL;
9132
9133#if defined(UNIX) || defined(VMS)
9134 /* for UNIX ":cd" means: go to home directory */
9135 if (*new_dir == NUL)
9136 {
9137 /* use NameBuff for home directory name */
9138# ifdef VMS
9139 char_u *p;
9140
9141 p = mch_getenv((char_u *)"SYS$LOGIN");
9142 if (p == NULL || *p == NUL) /* empty is the same as not set */
9143 NameBuff[0] = NUL;
9144 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009145 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146# else
9147 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9148# endif
9149 new_dir = NameBuff;
9150 }
9151#endif
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009152 dir_differs = new_dir == NULL || prev_dir == NULL
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01009153 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009154 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009155 emsg(_(e_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 else
9157 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009158 int is_local_chdir = eap->cmdidx == CMD_lcd
9159 || eap->cmdidx == CMD_lchdir;
9160
9161 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162
9163 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009164 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 ex_pwd(eap);
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009166
9167 if (dir_differs)
9168 apply_autocmds(EVENT_DIRCHANGED,
9169 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9170 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 }
9172 vim_free(tofree);
9173 }
9174}
9175
9176/*
9177 * ":pwd".
9178 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009180ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181{
9182 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9183 {
9184#ifdef BACKSLASH_IN_FILENAME
9185 slash_adjust(NameBuff);
9186#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01009187 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 }
9189 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009190 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191}
9192
9193/*
9194 * ":=".
9195 */
9196 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009197ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009199 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009200 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201}
9202
9203 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009204ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009206 int n;
9207 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208
9209 if (cursor_valid())
9210 {
9211 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9212 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009213 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009215
9216 len = eap->line2;
9217 switch (*eap->arg)
9218 {
9219 case 'm': break;
9220 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009221 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009222 }
9223 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224}
9225
9226/*
9227 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9228 */
9229 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009230do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231{
9232 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009233 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234
9235 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02009236 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01009237 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009239 wait_now = msec - done > 1000L ? 1000L : msec - done;
9240#ifdef FEAT_TIMERS
9241 {
9242 long due_time = check_due_timer();
9243
9244 if (due_time > 0 && due_time < wait_now)
9245 wait_now = due_time;
9246 }
9247#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009248#ifdef FEAT_JOB_CHANNEL
9249 if (has_any_channel() && wait_now > 100L)
9250 wait_now = 100L;
9251#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009252 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009253#ifdef FEAT_JOB_CHANNEL
9254 if (has_any_channel())
9255 ui_breakcheck_force(TRUE);
9256 else
9257#endif
9258 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009259#ifdef MESSAGE_QUEUE
9260 /* Process the netbeans and clientserver messages that may have been
9261 * received in the call to ui_breakcheck() when the GUI is in use. This
9262 * may occur when running a test case. */
9263 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02009266
9267 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
9268 // input buffer, otherwise a following call to input() fails.
9269 if (got_int)
9270 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271}
9272
9273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009274do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275{
9276 int mode;
9277 char_u *cmdp;
9278
9279 cmdp = eap->cmd;
9280 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9281
9282 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9283 eap->arg, mode, isabbrev))
9284 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009285 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009287 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 break;
9289 }
9290}
9291
9292/*
9293 * ":winsize" command (obsolete).
9294 */
9295 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009296ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297{
9298 int w, h;
9299 char_u *arg = eap->arg;
9300 char_u *p;
9301
9302 w = getdigits(&arg);
9303 arg = skipwhite(arg);
9304 p = arg;
9305 h = getdigits(&arg);
9306 if (*p != NUL && *arg == NUL)
9307 set_shellsize(w, h, TRUE);
9308 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009309 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310}
9311
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009313ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314{
9315 int xchar = NUL;
9316 char_u *p;
9317
9318 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9319 {
9320 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9321 if (eap->arg[1] == NUL)
9322 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009323 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 return;
9325 }
9326 xchar = eap->arg[1];
9327 p = eap->arg + 2;
9328 }
9329 else
9330 p = eap->arg + 1;
9331
9332 eap->nextcmd = check_nextcmd(p);
9333 p = skipwhite(p);
9334 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009335 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009336 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 {
9338 /* Pass flags on for ":vertical wincmd ]". */
9339 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009340 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9342 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009343 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 }
9345}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346
Bram Moolenaar843ee412004-06-30 16:16:41 +00009347#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348/*
9349 * ":winpos".
9350 */
9351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009352ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353{
9354 int x, y;
9355 char_u *arg = eap->arg;
9356 char_u *p;
9357
9358 if (*arg == NUL)
9359 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009360# if defined(FEAT_GUI) || defined(MSWIN)
9361# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009363# else
9364 if (mch_get_winpos(&x, &y) != FAIL)
9365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 {
9367 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01009368 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 }
9370 else
9371# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009372 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 }
9374 else
9375 {
9376 x = getdigits(&arg);
9377 arg = skipwhite(arg);
9378 p = arg;
9379 y = getdigits(&arg);
9380 if (*p == NUL || *arg != NUL)
9381 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009382 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 return;
9384 }
9385# ifdef FEAT_GUI
9386 if (gui.in_use)
9387 gui_mch_set_winpos(x, y);
9388 else if (gui.starting)
9389 {
9390 /* Remember the coordinates for when the window is opened. */
9391 gui_win_x = x;
9392 gui_win_y = y;
9393 }
9394# ifdef HAVE_TGETENT
9395 else
9396# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009397# else
9398# ifdef MSWIN
9399 mch_set_winpos(x, y);
9400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401# endif
9402# ifdef HAVE_TGETENT
9403 if (*T_CWP)
9404 term_set_winpos(x, y);
9405# endif
9406 }
9407}
9408#endif
9409
9410/*
9411 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9412 */
9413 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009414ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415{
9416 oparg_T oa;
9417
9418 clear_oparg(&oa);
9419 oa.regname = eap->regname;
9420 oa.start.lnum = eap->line1;
9421 oa.end.lnum = eap->line2;
9422 oa.line_count = eap->line2 - eap->line1 + 1;
9423 oa.motion_type = MLINE;
9424#ifdef FEAT_VIRTUALEDIT
9425 virtual_op = FALSE;
9426#endif
9427 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9428 {
9429 setpcmark();
9430 curwin->w_cursor.lnum = eap->line1;
9431 beginline(BL_SOL | BL_FIX);
9432 }
9433
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009434 if (VIsual_active)
9435 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009436
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 switch (eap->cmdidx)
9438 {
9439 case CMD_delete:
9440 oa.op_type = OP_DELETE;
9441 op_delete(&oa);
9442 break;
9443
9444 case CMD_yank:
9445 oa.op_type = OP_YANK;
9446 (void)op_yank(&oa, FALSE, TRUE);
9447 break;
9448
9449 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009450 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009452 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9453#else
9454 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009456 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 oa.op_type = OP_RSHIFT;
9458 else
9459 oa.op_type = OP_LSHIFT;
9460 op_shift(&oa, FALSE, eap->amount);
9461 break;
9462 }
9463#ifdef FEAT_VIRTUALEDIT
9464 virtual_op = MAYBE;
9465#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009466 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467}
9468
9469/*
9470 * ":put".
9471 */
9472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009473ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474{
9475 /* ":0put" works like ":1put!". */
9476 if (eap->line2 == 0)
9477 {
9478 eap->line2 = 1;
9479 eap->forceit = TRUE;
9480 }
9481 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009482 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9483 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484}
9485
9486/*
9487 * Handle ":copy" and ":move".
9488 */
9489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009490ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491{
9492 long n;
9493
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02009494 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 if (eap->arg == NULL) /* error detected */
9496 {
9497 eap->nextcmd = NULL;
9498 return;
9499 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009500 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501
9502 /*
9503 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9504 */
9505 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9506 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009507 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 return;
9509 }
9510
9511 if (eap->cmdidx == CMD_move)
9512 {
9513 if (do_move(eap->line1, eap->line2, n) == FAIL)
9514 return;
9515 }
9516 else
9517 ex_copy(eap->line1, eap->line2, n);
9518 u_clearline();
9519 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009520 ex_may_print(eap);
9521}
9522
9523/*
9524 * Print the current line if flags were given to the Ex command.
9525 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009526 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009527ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009528{
9529 if (eap->flags != 0)
9530 {
9531 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9532 (eap->flags & EXFLAG_LIST));
9533 ex_no_reprint = TRUE;
9534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535}
9536
9537/*
9538 * ":smagic" and ":snomagic".
9539 */
9540 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009541ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542{
9543 int magic_save = p_magic;
9544
9545 p_magic = (eap->cmdidx == CMD_smagic);
9546 do_sub(eap);
9547 p_magic = magic_save;
9548}
9549
9550/*
9551 * ":join".
9552 */
9553 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009554ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555{
9556 curwin->w_cursor.lnum = eap->line1;
9557 if (eap->line1 == eap->line2)
9558 {
9559 if (eap->addr_count >= 2) /* :2,2join does nothing */
9560 return;
9561 if (eap->line2 == curbuf->b_ml.ml_line_count)
9562 {
9563 beep_flush();
9564 return;
9565 }
9566 ++eap->line2;
9567 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009568 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009570 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571}
9572
9573/*
9574 * ":[addr]@r" or ":[addr]*r": execute register
9575 */
9576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009577ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578{
9579 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009580 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581
9582 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009583 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584
9585#ifdef USE_ON_FLY_SCROLL
9586 dont_scroll = TRUE; /* disallow scrolling here */
9587#endif
9588
9589 /* get the register name. No name means to use the previous one */
9590 c = *eap->arg;
9591 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9592 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009593 /* Put the register in the typeahead buffer with the "silent" flag. */
9594 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9595 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009596 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 else
9600 {
9601 int save_efr = exec_from_reg;
9602
9603 exec_from_reg = TRUE;
9604
9605 /*
9606 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009607 * Continue until the stuff buffer is empty and all added characters
9608 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009610 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9612
9613 exec_from_reg = save_efr;
9614 }
9615}
9616
9617/*
9618 * ":!".
9619 */
9620 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009621ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622{
9623 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9624}
9625
9626/*
9627 * ":undo".
9628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009630ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009632 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009633 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009634 else
9635 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636}
9637
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009638#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009640ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009641{
9642 char_u hash[UNDO_HASH_SIZE];
9643
9644 u_compute_hash(hash);
9645 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9646}
9647
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009648 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009649ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009650{
9651 char_u hash[UNDO_HASH_SIZE];
9652
9653 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009654 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009655}
9656#endif
9657
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658/*
9659 * ":redo".
9660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009662ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663{
9664 u_redo(1);
9665}
9666
9667/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009668 * ":earlier" and ":later".
9669 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009671ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009672{
9673 long count = 0;
9674 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009675 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009676 char_u *p = eap->arg;
9677
9678 if (*p == NUL)
9679 count = 1;
9680 else if (isdigit(*p))
9681 {
9682 count = getdigits(&p);
9683 switch (*p)
9684 {
9685 case 's': ++p; sec = TRUE; break;
9686 case 'm': ++p; sec = TRUE; count *= 60; break;
9687 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009688 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9689 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009690 }
9691 }
9692
9693 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009694 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009695 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009696 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9697 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009698}
9699
9700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 * ":redir": start/stop redirection.
9702 */
9703 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009704ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705{
9706 char *mode;
9707 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009708 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709
Bram Moolenaarba768492016-07-08 15:32:54 +02009710#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009711 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009712 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009713 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009714 return;
9715 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009716#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009717
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 if (STRICMP(eap->arg, "END") == 0)
9719 close_redir();
9720 else
9721 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009722 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009724 ++arg;
9725 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009727 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 mode = "a";
9729 }
9730 else
9731 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009732 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733
9734 close_redir();
9735
9736 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009737 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 if (fname == NULL)
9739 return;
9740#ifdef FEAT_BROWSE
9741 if (cmdmod.browse)
9742 {
9743 char_u *browseFile;
9744
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009745 browseFile = do_browse(BROWSE_SAVE,
9746 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009747 fname, NULL, NULL,
9748 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 if (browseFile == NULL)
9750 return; /* operation cancelled */
9751 vim_free(fname);
9752 fname = browseFile;
9753 eap->forceit = TRUE; /* since dialog already asked */
9754 }
9755#endif
9756
9757 redir_fd = open_exfile(fname, eap->forceit, mode);
9758 vim_free(fname);
9759 }
9760#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009761 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 {
9763 /* redirect to a register a-z (resp. A-Z for appending) */
9764 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009765 ++arg;
9766 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009768 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009769 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009771 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009773 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009774 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009775 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009776 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009778 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009779 if (*arg == '>')
9780 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009781 /* Make register empty when not using @A-@Z and the
9782 * command is valid. */
9783 if (*arg == NUL && !isupper(redir_reg))
9784 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009786 }
9787 if (*arg != NUL)
9788 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009789 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009790 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009791 }
9792 }
9793 else if (*arg == '=' && arg[1] == '>')
9794 {
9795 int append;
9796
9797 /* redirect to a variable */
9798 close_redir();
9799 arg += 2;
9800
9801 if (*arg == '>')
9802 {
9803 ++arg;
9804 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 }
9806 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009807 append = FALSE;
9808
9809 if (var_redir_start(skipwhite(arg), append) == OK)
9810 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 }
9812#endif
9813
9814 /* TODO: redirect to a buffer */
9815
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009817 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009819
9820 /* Make sure redirection is not off. Can happen for cmdline completion
9821 * that indirectly invokes a command to catch its output. */
9822 if (redir_fd != NULL
9823#ifdef FEAT_EVAL
9824 || redir_reg || redir_vname
9825#endif
9826 )
9827 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828}
9829
9830/*
9831 * ":redraw": force redraw
9832 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009833 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009834ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835{
9836 int r = RedrawingDisabled;
9837 int p = p_lz;
9838
9839 RedrawingDisabled = 0;
9840 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009841 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009843 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844#ifdef FEAT_TITLE
9845 if (need_maketitle)
9846 maketitle();
9847#endif
9848 RedrawingDisabled = r;
9849 p_lz = p;
9850
9851 /* Reset msg_didout, so that a message that's there is overwritten. */
9852 msg_didout = FALSE;
9853 msg_col = 0;
9854
Bram Moolenaar56667a52013-07-17 11:54:28 +02009855 /* No need to wait after an intentional redraw. */
9856 need_wait_return = FALSE;
9857
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 out_flush();
9859}
9860
9861/*
9862 * ":redrawstatus": force redraw of status line(s)
9863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009865ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 int r = RedrawingDisabled;
9868 int p = p_lz;
9869
9870 RedrawingDisabled = 0;
9871 p_lz = FALSE;
9872 if (eap->forceit)
9873 status_redraw_all();
9874 else
9875 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009876 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 RedrawingDisabled = r;
9878 p_lz = p;
9879 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880}
9881
Bram Moolenaare12bab32019-01-08 22:02:56 +01009882/*
9883 * ":redrawtabline": force redraw of the tabline
9884 */
9885 static void
9886ex_redrawtabline(exarg_T *eap UNUSED)
9887{
9888 int r = RedrawingDisabled;
9889 int p = p_lz;
9890
9891 RedrawingDisabled = 0;
9892 p_lz = FALSE;
9893
9894 draw_tabline();
9895
9896 RedrawingDisabled = r;
9897 p_lz = p;
9898 out_flush();
9899}
9900
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009902close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903{
9904 if (redir_fd != NULL)
9905 {
9906 fclose(redir_fd);
9907 redir_fd = NULL;
9908 }
9909#ifdef FEAT_EVAL
9910 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009911 if (redir_vname)
9912 {
9913 var_redir_stop();
9914 redir_vname = 0;
9915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916#endif
9917}
9918
9919#if defined(FEAT_SESSION) && defined(USE_CRNL)
9920# define MKSESSION_NL
9921static int mksession_nl = FALSE; /* use NL only in put_eol() */
9922#endif
9923
9924/*
9925 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9926 */
9927 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009928ex_mkrc(
9929 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930{
9931 FILE *fd;
9932 int failed = FALSE;
9933 char_u *fname;
9934#ifdef FEAT_BROWSE
9935 char_u *browseFile = NULL;
9936#endif
9937#ifdef FEAT_SESSION
9938 int view_session = FALSE;
9939 int using_vdir = FALSE; /* using 'viewdir'? */
9940 char_u *viewFile = NULL;
9941 unsigned *flagp;
9942#endif
9943
9944 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9945 {
9946#ifdef FEAT_SESSION
9947 view_session = TRUE;
9948#else
9949 ex_ni(eap);
9950 return;
9951#endif
9952 }
9953
9954#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009955 /* Use the short file name until ":lcd" is used. We also don't use the
9956 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009957 did_lcd = FALSE;
9958
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9960 if (eap->cmdidx == CMD_mkview
9961 && (*eap->arg == NUL
9962 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9963 {
9964 eap->forceit = TRUE;
9965 fname = get_view_file(*eap->arg);
9966 if (fname == NULL)
9967 return;
9968 viewFile = fname;
9969 using_vdir = TRUE;
9970 }
9971 else
9972#endif
9973 if (*eap->arg != NUL)
9974 fname = eap->arg;
9975 else if (eap->cmdidx == CMD_mkvimrc)
9976 fname = (char_u *)VIMRC_FILE;
9977#ifdef FEAT_SESSION
9978 else if (eap->cmdidx == CMD_mksession)
9979 fname = (char_u *)SESSION_FILE;
9980#endif
9981 else
9982 fname = (char_u *)EXRC_FILE;
9983
9984#ifdef FEAT_BROWSE
9985 if (cmdmod.browse)
9986 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009987 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988# ifdef FEAT_SESSION
9989 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9990 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9991# endif
9992 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009993 fname, (char_u *)"vim", NULL,
9994 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 if (browseFile == NULL)
9996 goto theend;
9997 fname = browseFile;
9998 eap->forceit = TRUE; /* since dialog already asked */
9999 }
10000#endif
10001
10002#if defined(FEAT_SESSION) && defined(vim_mkdir)
10003 /* When using 'viewdir' may have to create the directory. */
10004 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +000010005 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006#endif
10007
10008 fd = open_exfile(fname, eap->forceit, WRITEBIN);
10009 if (fd != NULL)
10010 {
10011#ifdef FEAT_SESSION
10012 if (eap->cmdidx == CMD_mkview)
10013 flagp = &vop_flags;
10014 else
10015 flagp = &ssop_flags;
10016#endif
10017
10018#ifdef MKSESSION_NL
10019 /* "unix" in 'sessionoptions': use NL line separator */
10020 if (view_session && (*flagp & SSOP_UNIX))
10021 mksession_nl = TRUE;
10022#endif
10023
10024 /* Write the version command for :mkvimrc */
10025 if (eap->cmdidx == CMD_mkvimrc)
10026 (void)put_line(fd, "version 6.0");
10027
10028#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010029 if (eap->cmdidx == CMD_mksession)
10030 {
10031 if (put_line(fd, "let SessionLoad = 1") == FAIL)
10032 failed = TRUE;
10033 }
10034
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 if (eap->cmdidx != CMD_mkview)
10036#endif
10037 {
10038 /* Write setting 'compatible' first, because it has side effects.
10039 * For that same reason only do it when needed. */
10040 if (p_cp)
10041 (void)put_line(fd, "if !&cp | set cp | endif");
10042 else
10043 (void)put_line(fd, "if &cp | set nocp | endif");
10044 }
10045
10046#ifdef FEAT_SESSION
10047 if (!view_session
10048 || (eap->cmdidx == CMD_mksession
10049 && (*flagp & SSOP_OPTIONS)))
10050#endif
10051 failed |= (makemap(fd, NULL) == FAIL
10052 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10053
10054#ifdef FEAT_SESSION
10055 if (!failed && view_session)
10056 {
10057 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10058 failed = TRUE;
10059 if (eap->cmdidx == CMD_mksession)
10060 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010061 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062
Bram Moolenaard9462e32011-04-11 21:35:11 +020010063 dirnow = alloc(MAXPATHL);
10064 if (dirnow == NULL)
10065 failed = TRUE;
10066 else
10067 {
10068 /*
10069 * Change to session file's dir.
10070 */
10071 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010073 *dirnow = NUL;
10074 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10075 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +010010076 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010077 shorten_fnames(TRUE);
10078 }
10079 else if (*dirnow != NUL
10080 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10081 {
10082 if (mch_chdir((char *)globaldir) == 0)
10083 shorten_fnames(TRUE);
10084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085
Bram Moolenaard9462e32011-04-11 21:35:11 +020010086 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087
Bram Moolenaard9462e32011-04-11 21:35:11 +020010088 /* restore original dir */
10089 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010091 {
10092 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010093 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +020010094 shorten_fnames(TRUE);
10095 }
10096 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 }
10098 }
10099 else
10100 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010101 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10102 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 }
10104 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10105 == FAIL)
10106 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +010010107# ifdef FEAT_SEARCH_EXTRA
10108 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
10109 failed = TRUE;
10110# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010111 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10112 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010113 if (eap->cmdidx == CMD_mksession)
10114 {
10115 if (put_line(fd, "unlet SessionLoad") == FAIL)
10116 failed = TRUE;
10117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 }
10119#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010120 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10121 failed = TRUE;
10122
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 failed |= fclose(fd);
10124
10125 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010126 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10128 else if (eap->cmdidx == CMD_mksession)
10129 {
10130 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010131 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132
Bram Moolenaard9462e32011-04-11 21:35:11 +020010133 tbuf = alloc(MAXPATHL);
10134 if (tbuf != NULL)
10135 {
10136 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10137 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10138 vim_free(tbuf);
10139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 }
10141#endif
10142#ifdef MKSESSION_NL
10143 mksession_nl = FALSE;
10144#endif
10145 }
10146
10147#ifdef FEAT_BROWSE
10148theend:
10149 vim_free(browseFile);
10150#endif
10151#ifdef FEAT_SESSION
10152 vim_free(viewFile);
10153#endif
10154}
10155
Bram Moolenaardf177f62005-02-22 08:39:57 +000010156#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10157 || defined(PROTO)
10158 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010159vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010160{
10161 if (vim_mkdir(name, prot) != 0)
10162 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010163 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +000010164 return FAIL;
10165 }
10166 return OK;
10167}
10168#endif
10169
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170/*
10171 * Open a file for writing for an Ex command, with some checks.
10172 * Return file descriptor, or NULL on failure.
10173 */
10174 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010175open_exfile(
10176 char_u *fname,
10177 int forceit,
10178 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179{
10180 FILE *fd;
10181
10182#ifdef UNIX
10183 /* with Unix it is possible to open a directory */
10184 if (mch_isdir(fname))
10185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010186 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 return NULL;
10188 }
10189#endif
10190 if (!forceit && *mode != 'a' && vim_fexists(fname))
10191 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010192 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 return NULL;
10194 }
10195
10196 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010197 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198
10199 return fd;
10200}
10201
10202/*
10203 * ":mark" and ":k".
10204 */
10205 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010206ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207{
10208 pos_T pos;
10209
10210 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010211 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010213 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 else
10215 {
10216 pos = curwin->w_cursor; /* save curwin->w_cursor */
10217 curwin->w_cursor.lnum = eap->line2;
10218 beginline(BL_WHITE | BL_FIX);
10219 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010220 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10222 }
10223}
10224
10225/*
10226 * Update w_topline, w_leftcol and the cursor position.
10227 */
10228 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010229update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230{
10231 check_cursor(); /* put cursor on valid line */
10232 update_topline();
10233 if (!curwin->w_p_wrap)
10234 validate_cursor();
10235 update_curswant();
10236}
10237
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010239 * Save the current State and go to Normal mode.
10240 * Return TRUE if the typeahead could be saved.
10241 */
10242 int
10243save_current_state(save_state_T *sst)
10244{
10245 sst->save_msg_scroll = msg_scroll;
10246 sst->save_restart_edit = restart_edit;
10247 sst->save_msg_didout = msg_didout;
10248 sst->save_State = State;
10249 sst->save_insertmode = p_im;
10250 sst->save_finish_op = finish_op;
10251 sst->save_opcount = opcount;
10252
10253 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10254 restart_edit = 0; /* don't go to Insert mode */
10255 p_im = FALSE; /* don't use 'insertmode' */
10256
10257 /*
10258 * Save the current typeahead. This is required to allow using ":normal"
10259 * from an event handler and makes sure we don't hang when the argument
10260 * ends with half a command.
10261 */
10262 save_typeahead(&sst->tabuf);
10263 return sst->tabuf.typebuf_valid;
10264}
10265
10266 void
10267restore_current_state(save_state_T *sst)
10268{
10269 /* Restore the previous typeahead. */
10270 restore_typeahead(&sst->tabuf);
10271
10272 msg_scroll = sst->save_msg_scroll;
10273 restart_edit = sst->save_restart_edit;
10274 p_im = sst->save_insertmode;
10275 finish_op = sst->save_finish_op;
10276 opcount = sst->save_opcount;
10277 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10278
10279 /* Restore the state (needed when called from a function executed for
10280 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10281 State = sst->save_State;
10282#ifdef CURSOR_SHAPE
10283 ui_cursor_shape(); /* may show different cursor shape */
10284#endif
10285}
10286
10287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 * ":normal[!] {commands}": Execute normal mode commands.
10289 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010290 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010291ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010293 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 char_u *arg = NULL;
10295 int l;
10296 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010298 if (ex_normal_lock > 0)
10299 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010300 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010301 return;
10302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 if (ex_normal_busy >= p_mmd)
10304 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010305 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306 return;
10307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 /*
10310 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10311 * this for the K_SPECIAL leading byte, otherwise special keys will not
10312 * work.
10313 */
10314 if (has_mbyte)
10315 {
10316 int len = 0;
10317
10318 /* Count the number of characters to be escaped. */
10319 for (p = eap->arg; *p != NUL; ++p)
10320 {
Bram Moolenaar13505972019-01-24 15:04:48 +010010321#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 if (*p == CSI) /* leadbyte CSI */
10323 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +010010324#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010325 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +010010327#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +010010329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 )
10331 len += 2;
10332 }
10333 if (len > 0)
10334 {
10335 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10336 if (arg != NULL)
10337 {
10338 len = 0;
10339 for (p = eap->arg; *p != NUL; ++p)
10340 {
10341 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +010010342#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 if (*p == CSI)
10344 {
10345 arg[len++] = KS_EXTRA;
10346 arg[len++] = (int)KE_CSI;
10347 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010348#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010349 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 {
10351 arg[len++] = *++p;
10352 if (*p == K_SPECIAL)
10353 {
10354 arg[len++] = KS_SPECIAL;
10355 arg[len++] = KE_FILLER;
10356 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010357#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 else if (*p == CSI)
10359 {
10360 arg[len++] = KS_EXTRA;
10361 arg[len++] = (int)KE_CSI;
10362 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 }
10365 arg[len] = NUL;
10366 }
10367 }
10368 }
10369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010371 ++ex_normal_busy;
10372 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 {
10374 /*
10375 * Repeat the :normal command for each line in the range. When no
10376 * range given, execute it just once, without positioning the cursor
10377 * first.
10378 */
10379 do
10380 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 if (eap->addr_count != 0)
10382 {
10383 curwin->w_cursor.lnum = eap->line1++;
10384 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010385 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 }
10387
Bram Moolenaar13505972019-01-24 15:04:48 +010010388 exec_normal_cmd(arg != NULL
10389 ? arg
10390 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 }
10392 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10393 }
10394
10395 /* Might not return to the main loop when in an event handler. */
10396 update_topline_cursor();
10397
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010398 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010400#ifdef FEAT_MOUSE
10401 setmouse();
10402#endif
10403#ifdef CURSOR_SHAPE
10404 ui_cursor_shape(); /* may show different cursor shape */
10405#endif
10406
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408}
10409
10410/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010411 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412 */
10413 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010414ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010416 if (eap->forceit)
10417 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010418 /* cursor line can be zero on startup */
10419 if (!curwin->w_cursor.lnum)
10420 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010421 coladvance((colnr_T)MAXCOL);
10422 curwin->w_curswant = MAXCOL;
10423 curwin->w_set_curswant = FALSE;
10424 }
10425
Bram Moolenaara40c5002005-01-09 21:16:21 +000010426 /* Ignore the command when already in Insert mode. Inserting an
10427 * expression register that invokes a function can do this. */
10428 if (State & INSERT)
10429 return;
10430
Bram Moolenaar64969662005-12-14 21:59:55 +000010431 if (eap->cmdidx == CMD_startinsert)
10432 restart_edit = 'a';
10433 else if (eap->cmdidx == CMD_startreplace)
10434 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010436 restart_edit = 'V';
10437
10438 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010440 if (eap->cmdidx == CMD_startinsert)
10441 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 curwin->w_curswant = 0; /* avoid MAXCOL */
10443 }
10444}
10445
10446/*
10447 * ":stopinsert"
10448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010450ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451{
10452 restart_edit = 0;
10453 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010454 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010457/*
10458 * Execute normal mode command "cmd".
10459 * "remap" can be REMAP_NONE or REMAP_YES.
10460 */
10461 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010462exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010463{
Bram Moolenaar25281632016-01-21 23:32:32 +010010464 /* Stuff the argument into the typeahead buffer. */
10465 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010466 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +010010467}
Bram Moolenaar25281632016-01-21 23:32:32 +010010468
Bram Moolenaar25281632016-01-21 23:32:32 +010010469/*
10470 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010471 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +010010472 */
10473 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010474exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +010010475{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010476 oparg_T oa;
10477
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010478 clear_oparg(&oa);
10479 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010480 while ((!stuff_empty()
10481 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
10482 || (use_vpeekc && vpeekc() != NUL))
10483 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010484 {
10485 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010486#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010487 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010488 && oa.op_type == OP_NOP && oa.regname == NUL
10489 && !VIsual_active)
10490 {
10491 /* If terminal_loop() returns OK we got a key that is handled
10492 * in Normal model. With FAIL we first need to position the
10493 * cursor and the screen needs to be redrawn. */
10494 if (terminal_loop(TRUE) == OK)
10495 normal_cmd(&oa, TRUE);
10496 }
10497 else
10498#endif
10499 /* execute a Normal mode cmd */
10500 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010501 }
10502}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010503
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504#ifdef FEAT_FIND_ID
10505 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010506ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507{
10508 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10509 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10510 (linenr_T)1, (linenr_T)MAXLNUM);
10511}
10512
Bram Moolenaar4033c552017-09-16 20:54:51 +020010513#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514/*
10515 * ":psearch"
10516 */
10517 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010518ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519{
10520 g_do_tagpreview = p_pvh;
10521 ex_findpat(eap);
10522 g_do_tagpreview = 0;
10523}
10524#endif
10525
10526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010527ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528{
10529 int whole = TRUE;
10530 long n;
10531 char_u *p;
10532 int action;
10533
10534 switch (cmdnames[eap->cmdidx].cmd_name[2])
10535 {
10536 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10537 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10538 action = ACTION_GOTO;
10539 else
10540 action = ACTION_SHOW;
10541 break;
10542 case 'i': /* ":ilist" and ":dlist" */
10543 action = ACTION_SHOW_ALL;
10544 break;
10545 case 'u': /* ":ijump" and ":djump" */
10546 action = ACTION_GOTO;
10547 break;
10548 default: /* ":isplit" and ":dsplit" */
10549 action = ACTION_SPLIT;
10550 break;
10551 }
10552
10553 n = 1;
10554 if (vim_isdigit(*eap->arg)) /* get count */
10555 {
10556 n = getdigits(&eap->arg);
10557 eap->arg = skipwhite(eap->arg);
10558 }
10559 if (*eap->arg == '/') /* Match regexp, not just whole words */
10560 {
10561 whole = FALSE;
10562 ++eap->arg;
10563 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10564 if (*p)
10565 {
10566 *p++ = NUL;
10567 p = skipwhite(p);
10568
10569 /* Check for trailing illegal characters */
10570 if (!ends_excmd(*p))
10571 eap->errmsg = e_trailing;
10572 else
10573 eap->nextcmd = check_nextcmd(p);
10574 }
10575 }
10576 if (!eap->skip)
10577 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10578 whole, !eap->forceit,
10579 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10580 n, action, eap->line1, eap->line2);
10581}
10582#endif
10583
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584
Bram Moolenaar4033c552017-09-16 20:54:51 +020010585#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586/*
10587 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10588 */
10589 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010590ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010592 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10594}
10595
10596/*
10597 * ":pedit"
10598 */
10599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010600ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601{
10602 win_T *curwin_save = curwin;
10603
10604 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010605 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010606 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607 do_exedit(eap, NULL);
10608 keep_help_flag = FALSE;
10609 if (curwin != curwin_save && win_valid(curwin_save))
10610 {
10611 /* Return cursor to where we were */
10612 validate_cursor();
10613 redraw_later(VALID);
10614 win_enter(curwin_save, TRUE);
10615 }
10616 g_do_tagpreview = 0;
10617}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619
10620/*
10621 * ":stag", ":stselect" and ":stjump".
10622 */
10623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010624ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625{
10626 postponed_split = -1;
10627 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010628 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10630 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010631 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633
10634/*
10635 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10636 */
10637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010638ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639{
10640 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10641}
10642
10643 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010644ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645{
10646 int cmd;
10647
10648 switch (name[1])
10649 {
10650 case 'j': cmd = DT_JUMP; /* ":tjump" */
10651 break;
10652 case 's': cmd = DT_SELECT; /* ":tselect" */
10653 break;
10654 case 'p': cmd = DT_PREV; /* ":tprevious" */
10655 break;
10656 case 'N': cmd = DT_PREV; /* ":tNext" */
10657 break;
10658 case 'n': cmd = DT_NEXT; /* ":tnext" */
10659 break;
10660 case 'o': cmd = DT_POP; /* ":pop" */
10661 break;
10662 case 'f': /* ":tfirst" */
10663 case 'r': cmd = DT_FIRST; /* ":trewind" */
10664 break;
10665 case 'l': cmd = DT_LAST; /* ":tlast" */
10666 break;
10667 default: /* ":tag" */
10668#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010669 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010671 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 return;
10673 }
10674#endif
10675 cmd = DT_TAG;
10676 break;
10677 }
10678
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010679 if (name[0] == 'l')
10680 {
10681#ifndef FEAT_QUICKFIX
10682 ex_ni(eap);
10683 return;
10684#else
10685 cmd = DT_LTAG;
10686#endif
10687 }
10688
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10690 eap->forceit, TRUE);
10691}
10692
10693/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010694 * Check "str" for starting with a special cmdline variable.
10695 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10696 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10697 */
10698 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010699find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010700{
10701 int len;
10702 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010703 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010704 "%",
10705#define SPEC_PERC 0
10706 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010707#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010708 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010709#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010710 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010711#define SPEC_CCWORD (SPEC_CWORD + 1)
10712 "<cexpr>", /* expr under cursor */
10713#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010714 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010715#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010716 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010717#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010718 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010719#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010720 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010721#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010722 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010723#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010724 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010725#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010726 "<sflnum>", /* script file line number */
10727#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010728#ifdef FEAT_CLIENTSERVER
10729 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010730# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010731#endif
10732 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010733
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010734 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010735 {
10736 len = (int)STRLEN(spec_str[i]);
10737 if (STRNCMP(src, spec_str[i], len) == 0)
10738 {
10739 *usedlen = len;
10740 return i;
10741 }
10742 }
10743 return -1;
10744}
10745
10746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 * Evaluate cmdline variables.
10748 *
10749 * change '%' to curbuf->b_ffname
10750 * '#' to curwin->w_altfile
10751 * '<cword>' to word under the cursor
10752 * '<cWORD>' to WORD under the cursor
10753 * '<cfile>' to path name under the cursor
10754 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010755 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 * '<afile>' to file name for autocommand
10757 * '<abuf>' to buffer number for autocommand
10758 * '<amatch>' to matching name for autocommand
10759 *
10760 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10761 * "" for error without a message) and NULL is returned.
10762 * Returns an allocated string if a valid match was found.
10763 * Returns NULL if no match was found. "usedlen" then still contains the
10764 * number of characters to skip.
10765 */
10766 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010767eval_vars(
10768 char_u *src, /* pointer into commandline */
10769 char_u *srcstart, /* beginning of valid memory for src */
10770 int *usedlen, /* characters after src that are used */
10771 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010772 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010773 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010774 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775{
10776 int i;
10777 char_u *s;
10778 char_u *result;
10779 char_u *resultbuf = NULL;
10780 int resultlen;
10781 buf_T *buf;
10782 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10783 int spec_idx;
10784#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +020010785 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786 int skip_mod = FALSE;
10787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789
10790 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010791 if (escaped != NULL)
10792 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793
10794 /*
10795 * Check if there is something to do.
10796 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010797 spec_idx = find_cmdline_var(src, usedlen);
10798 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799 {
10800 *usedlen = 1;
10801 return NULL;
10802 }
10803
10804 /*
10805 * Skip when preceded with a backslash "\%" and "\#".
10806 * Note: In "\\%" the % is also not recognized!
10807 */
10808 if (src > srcstart && src[-1] == '\\')
10809 {
10810 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010811 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 return NULL;
10813 }
10814
10815 /*
10816 * word or WORD under cursor
10817 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010818 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10819 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010821 resultlen = find_ident_under_cursor(&result,
10822 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10823 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10824 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 if (resultlen == 0)
10826 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010827 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 return NULL;
10829 }
10830 }
10831
10832 /*
10833 * '#': Alternate file name
10834 * '%': Current file name
10835 * File name under the cursor
10836 * File name for autocommand
10837 * and following modifiers
10838 */
10839 else
10840 {
10841 switch (spec_idx)
10842 {
10843 case SPEC_PERC: /* '%': current file */
10844 if (curbuf->b_fname == NULL)
10845 {
10846 result = (char_u *)"";
10847 valid = 0; /* Must have ":p:h" to be valid */
10848 }
10849 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010850 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010852#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010853 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010854#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 break;
10857
10858 case SPEC_HASH: /* '#' or "#99": alternate file */
10859 if (src[1] == '#') /* "##": the argument list */
10860 {
10861 result = arg_all();
10862 resultbuf = result;
10863 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010864 if (escaped != NULL)
10865 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866#ifdef FEAT_MODIFY_FNAME
10867 skip_mod = TRUE;
10868#endif
10869 break;
10870 }
10871 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010872 if (*s == '<') /* "#<99" uses v:oldfiles */
10873 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010875 if (s == src + 2 && src[1] == '-')
10876 /* just a minus sign, don't skip over it */
10877 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 *usedlen = (int)(s - src); /* length of what we expand */
10879
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010880 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010882 if (*usedlen < 2)
10883 {
10884 /* Should we give an error message for #<text? */
10885 *usedlen = 1;
10886 return NULL;
10887 }
10888#ifdef FEAT_EVAL
10889 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10890 (long)i);
10891 if (result == NULL)
10892 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010893 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +000010894 return NULL;
10895 }
10896#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +010010897 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 }
10901 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010902 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010903 if (i == 0 && src[1] == '<' && *usedlen > 1)
10904 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010905 buf = buflist_findnr(i);
10906 if (buf == NULL)
10907 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010908 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +000010909 return NULL;
10910 }
10911 if (lnump != NULL)
10912 *lnump = ECMD_LAST;
10913 if (buf->b_fname == NULL)
10914 {
10915 result = (char_u *)"";
10916 valid = 0; /* Must have ":p:h" to be valid */
10917 }
10918 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010919 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010920 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010921#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010922 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010923#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010924 }
Bram Moolenaard812df62008-11-09 12:46:09 +000010925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 break;
10927
10928#ifdef FEAT_SEARCHPATH
10929 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010930 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 if (result == NULL)
10932 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010933 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 return NULL;
10935 }
10936 resultbuf = result; /* remember allocated string */
10937 break;
10938#endif
10939
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 case SPEC_AFILE: /* file name for autocommand */
10941 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010942 if (result != NULL && !autocmd_fname_full)
10943 {
10944 /* Still need to turn the fname into a full path. It is
10945 * postponed to avoid a delay when <afile> is not used. */
10946 autocmd_fname_full = TRUE;
10947 result = FullName_save(autocmd_fname, FALSE);
10948 vim_free(autocmd_fname);
10949 autocmd_fname = result;
10950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 if (result == NULL)
10952 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010953 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 return NULL;
10955 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010956 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 break;
10958
10959 case SPEC_ABUF: /* buffer number for autocommand */
10960 if (autocmd_bufnr <= 0)
10961 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010962 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 return NULL;
10964 }
10965 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10966 result = strbuf;
10967 break;
10968
10969 case SPEC_AMATCH: /* match name for autocommand */
10970 result = autocmd_match;
10971 if (result == NULL)
10972 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010973 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 return NULL;
10975 }
10976 break;
10977
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 case SPEC_SFILE: /* file name for ":so" command */
10979 result = sourcing_name;
10980 if (result == NULL)
10981 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010982 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 return NULL;
10984 }
10985 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010986
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010987 case SPEC_SLNUM: /* line in file for ":so" command */
10988 if (sourcing_name == NULL || sourcing_lnum == 0)
10989 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010990 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010991 return NULL;
10992 }
10993 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10994 result = strbuf;
10995 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010996
10997#ifdef FEAT_EVAL
10998 case SPEC_SFLNUM: /* line in script file */
10999 if (current_sctx.sc_lnum + sourcing_lnum == 0)
11000 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011001 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011002 return NULL;
11003 }
11004 sprintf((char *)strbuf, "%ld",
11005 (long)(current_sctx.sc_lnum + sourcing_lnum));
11006 result = strbuf;
11007 break;
11008#endif
11009
11010#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011012 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
11013 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 result = strbuf;
11015 break;
11016#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011017
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020011018 default:
11019 result = (char_u *)""; /* avoid gcc warning */
11020 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 }
11022
11023 resultlen = (int)STRLEN(result); /* length of new string */
11024 if (src[*usedlen] == '<') /* remove the file name extension */
11025 {
11026 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 resultlen = (int)(s - result);
11029 }
11030#ifdef FEAT_MODIFY_FNAME
11031 else if (!skip_mod)
11032 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020011033 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 &resultlen);
11035 if (result == NULL)
11036 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011037 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 return NULL;
11039 }
11040 }
11041#endif
11042 }
11043
11044 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
11045 {
11046 if (valid != VALID_HEAD + VALID_PATH)
11047 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011048 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011050 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 result = NULL;
11052 }
11053 else
11054 result = vim_strnsave(result, resultlen);
11055 vim_free(resultbuf);
11056 return result;
11057}
11058
11059/*
11060 * Concatenate all files in the argument list, separated by spaces, and return
11061 * it in one allocated string.
11062 * Spaces and backslashes in the file names are escaped with a backslash.
11063 * Returns NULL when out of memory.
11064 */
11065 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011066arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067{
11068 int len;
11069 int idx;
11070 char_u *retval = NULL;
11071 char_u *p;
11072
11073 /*
11074 * Do this loop two times:
11075 * first time: compute the total length
11076 * second time: concatenate the names
11077 */
11078 for (;;)
11079 {
11080 len = 0;
11081 for (idx = 0; idx < ARGCOUNT; ++idx)
11082 {
11083 p = alist_name(&ARGLIST[idx]);
11084 if (p != NULL)
11085 {
11086 if (len > 0)
11087 {
11088 /* insert a space in between names */
11089 if (retval != NULL)
11090 retval[len] = ' ';
11091 ++len;
11092 }
11093 for ( ; *p != NUL; ++p)
11094 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011095 if (*p == ' '
11096#ifndef BACKSLASH_IN_FILENAME
11097 || *p == '\\'
11098#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +020011099 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 {
11101 /* insert a backslash */
11102 if (retval != NULL)
11103 retval[len] = '\\';
11104 ++len;
11105 }
11106 if (retval != NULL)
11107 retval[len] = *p;
11108 ++len;
11109 }
11110 }
11111 }
11112
11113 /* second time: break here */
11114 if (retval != NULL)
11115 {
11116 retval[len] = NUL;
11117 break;
11118 }
11119
11120 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011121 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 if (retval == NULL)
11123 break;
11124 }
11125
11126 return retval;
11127}
11128
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129/*
11130 * Expand the <sfile> string in "arg".
11131 *
11132 * Returns an allocated string, or NULL for any error.
11133 */
11134 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011135expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011137 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138 int len;
11139 char_u *result;
11140 char_u *newres;
11141 char_u *repl;
11142 int srclen;
11143 char_u *p;
11144
11145 result = vim_strsave(arg);
11146 if (result == NULL)
11147 return NULL;
11148
11149 for (p = result; *p; )
11150 {
11151 if (STRNCMP(p, "<sfile>", 7) != 0)
11152 ++p;
11153 else
11154 {
11155 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011156 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157 if (errormsg != NULL)
11158 {
11159 if (*errormsg)
11160 emsg(errormsg);
11161 vim_free(result);
11162 return NULL;
11163 }
11164 if (repl == NULL) /* no match (cannot happen) */
11165 {
11166 p += srclen;
11167 continue;
11168 }
11169 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11170 newres = alloc(len);
11171 if (newres == NULL)
11172 {
11173 vim_free(repl);
11174 vim_free(result);
11175 return NULL;
11176 }
11177 mch_memmove(newres, result, (size_t)(p - result));
11178 STRCPY(newres + (p - result), repl);
11179 len = (int)STRLEN(newres);
11180 STRCAT(newres, p + srclen);
11181 vim_free(repl);
11182 vim_free(result);
11183 result = newres;
11184 p = newres + len; /* continue after the match */
11185 }
11186 }
11187
11188 return result;
11189}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190
11191#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011192static int ses_winsizes(FILE *fd, int restore_size,
11193 win_T *tab_firstwin);
11194static int ses_win_rec(FILE *fd, frame_T *fr);
11195static frame_T *ses_skipframe(frame_T *fr);
11196static int ses_do_frame(frame_T *fr);
11197static int ses_do_win(win_T *wp);
11198static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11199static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011200static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201
11202/*
11203 * Write openfile commands for the current buffers to an .exrc file.
11204 * Return FAIL on error, OK otherwise.
11205 */
11206 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011207makeopens(
11208 FILE *fd,
11209 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210{
11211 buf_T *buf;
11212 int only_save_windows = TRUE;
11213 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214 int restore_size = TRUE;
11215 win_T *wp;
11216 char_u *sname;
11217 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011218 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011219 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011220 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011221 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011222 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011223 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224
11225 if (ssop_flags & SSOP_BUFFERS)
11226 only_save_windows = FALSE; /* Save ALL buffers */
11227
11228 /*
11229 * Begin by setting the this_session variable, and then other
11230 * sessionable variables.
11231 */
11232#ifdef FEAT_EVAL
11233 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11234 return FAIL;
11235 if (ssop_flags & SSOP_GLOBALS)
11236 if (store_session_globals(fd) == FAIL)
11237 return FAIL;
11238#endif
11239
11240 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011241 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242 */
11243 if (put_line(fd, "silent only") == FAIL)
11244 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011245 if ((ssop_flags & SSOP_TABPAGES)
11246 && put_line(fd, "silent tabonly") == FAIL)
11247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248
11249 /*
11250 * Now a :cd command to the session directory or the current directory
11251 */
11252 if (ssop_flags & SSOP_SESDIR)
11253 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011254 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11255 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 return FAIL;
11257 }
11258 else if (ssop_flags & SSOP_CURDIR)
11259 {
11260 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11261 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011262 || fputs("cd ", fd) < 0
11263 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11264 || put_eol(fd) == FAIL)
11265 {
11266 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269 vim_free(sname);
11270 }
11271
11272 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011273 * If there is an empty, unnamed buffer we will wipe it out later.
11274 * Remember the buffer number.
11275 */
11276 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11277 return FAIL;
11278 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11279 return FAIL;
11280 if (put_line(fd, "endif") == FAIL)
11281 return FAIL;
11282
11283 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284 * Now save the current files, current buffer first.
11285 */
11286 if (put_line(fd, "set shortmess=aoO") == FAIL)
11287 return FAIL;
11288
11289 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011290 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291 {
11292 if (!(only_save_windows && buf->b_nwindows == 0)
11293 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011294#ifdef FEAT_TERMINAL
11295 /* skip terminal buffers: finished ones are not useful, others
11296 * will be resurrected and result in a new buffer */
11297 && !bt_terminal(buf)
11298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299 && buf->b_fname != NULL
11300 && buf->b_p_bl)
11301 {
11302 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11303 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011304 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 return FAIL;
11306 }
11307 }
11308
11309 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011310 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11312 return FAIL;
11313
11314 if (ssop_flags & SSOP_RESIZE)
11315 {
11316 /* Note: after the restore we still check it worked!*/
11317 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11318 || put_eol(fd) == FAIL)
11319 return FAIL;
11320 }
11321
11322#ifdef FEAT_GUI
11323 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11324 {
11325 int x, y;
11326
11327 if (gui_mch_get_winpos(&x, &y) == OK)
11328 {
11329 /* Note: after the restore we still check it worked!*/
11330 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11331 return FAIL;
11332 }
11333 }
11334#endif
11335
11336 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011337 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11338 * will be displayed when creating the next tab. That resizes the windows
11339 * in the first tab, which may cause problems. Set 'showtabline' to 2
11340 * temporarily to avoid that.
11341 */
11342 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11343 {
11344 if (put_line(fd, "set stal=2") == FAIL)
11345 return FAIL;
11346 restore_stal = TRUE;
11347 }
11348
11349 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011350 * May repeat putting Windows for each tab, when "tabpages" is in
11351 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011352 * Don't use goto_tabpage(), it may change directory and trigger
11353 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011355 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011356 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011357 if ((ssop_flags & SSOP_TABPAGES))
11358 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011359 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011360
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011361 // Similar to ses_win_rec() below, populate the tab pages first so
11362 // later local options won't be copied to the new tabs.
11363 FOR_ALL_TABPAGES(tp)
11364 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011365 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011366 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011367 return FAIL;
11368 }
Bram Moolenaar18144c82006-04-12 21:52:12 +000011369 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011371 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +020011372 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011373
Bram Moolenaar18144c82006-04-12 21:52:12 +000011374 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011376 tabpage_T *tp = find_tabpage(tabnr);
11377
11378 if (tp == NULL)
11379 break; /* done all tab pages */
11380 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011381 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011382 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011383 tab_topframe = topframe;
11384 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011385 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011386 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011387 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011388 tab_topframe = tp->tp_topframe;
11389 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011390 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011391 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393
Bram Moolenaar18144c82006-04-12 21:52:12 +000011394 /*
11395 * Before creating the window layout, try loading one file. If this
11396 * is aborted we don't end up with a number of useless windows.
11397 * This may have side effects! (e.g., compressed or network file).
11398 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011399 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011400 {
11401 if (ses_do_win(wp)
11402 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011403 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011404#ifdef FEAT_QUICKFIX
11405 && !bt_nofile(wp->w_buffer)
11406#endif
11407 )
11408 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011409 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
11410 return FAIL;
11411 need_tabnext = FALSE;
11412
11413 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011414 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11415 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011416 return FAIL;
11417 if (!wp->w_arg_idx_invalid)
11418 edited_win = wp;
11419 break;
11420 }
11421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011423 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011424 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011425 return FAIL;
11426
Bram Moolenaar18144c82006-04-12 21:52:12 +000011427 /*
11428 * Save current window layout.
11429 */
11430 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011432 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011434 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11435 return FAIL;
11436 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11437 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438
Bram Moolenaar18144c82006-04-12 21:52:12 +000011439 /*
11440 * Check if window sizes can be restored (no windows omitted).
11441 * Remember the window number of the current window after restoring.
11442 */
11443 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011444 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011445 {
11446 if (ses_do_win(wp))
11447 ++nr;
11448 else
11449 restore_size = FALSE;
11450 if (curwin == wp)
11451 cnr = nr;
11452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453
Bram Moolenaar18144c82006-04-12 21:52:12 +000011454 /* Go to the first window. */
11455 if (put_line(fd, "wincmd t") == FAIL)
11456 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011457
Bram Moolenaar18144c82006-04-12 21:52:12 +000011458 /*
11459 * If more than one window, see if sizes can be restored.
11460 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11461 * resized when moving between windows.
11462 * Do this before restoring the view, so that the topline and the
11463 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011464 * winminheight and winminwidth need to be set to avoid an error if the
11465 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011466 */
Bram Moolenaar19834012018-06-12 17:03:39 +020011467 if (put_line(fd, "set winminheight=0") == FAIL
11468 || put_line(fd, "set winheight=1") == FAIL
11469 || put_line(fd, "set winminwidth=0") == FAIL
11470 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011471 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011472 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011473 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474
Bram Moolenaar18144c82006-04-12 21:52:12 +000011475 /*
11476 * Restore the view of the window (options, file, cursor, etc.).
11477 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011478 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011479 {
11480 if (!ses_do_win(wp))
11481 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011482 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11483 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011484 return FAIL;
11485 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11486 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011487 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011488 }
11489
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011490 /* The argument index in the first tab page is zero, need to set it in
11491 * each window. For further tab pages it's the window where we do
11492 * "tabedit". */
11493 cur_arg_idx = next_arg_idx;
11494
Bram Moolenaar18144c82006-04-12 21:52:12 +000011495 /*
11496 * Restore cursor to the current window if it's not the first one.
11497 */
11498 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11499 || put_eol(fd) == FAIL))
11500 return FAIL;
11501
11502 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011503 * Restore window sizes again after jumping around in windows, because
11504 * the current window has a minimum size while others may not.
11505 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011506 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011507 return FAIL;
11508
Bram Moolenaar18144c82006-04-12 21:52:12 +000011509 /* Don't continue in another tab page when doing only the current one
11510 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011511 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011512 break;
11513 }
11514
11515 if (ssop_flags & SSOP_TABPAGES)
11516 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011517 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11518 || put_eol(fd) == FAIL)
11519 return FAIL;
11520 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011521 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11522 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011523
Bram Moolenaar9c102382006-05-03 21:26:49 +000011524 /*
11525 * Wipe out an empty unnamed buffer we started in.
11526 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011527 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011528 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011529 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011530 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011531 return FAIL;
11532 if (put_line(fd, "endif") == FAIL)
11533 return FAIL;
11534 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11535 return FAIL;
11536
11537 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11538 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11539 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11540 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011541 /* Re-apply 'winminheight' and 'winminwidth'. */
11542 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11543 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11544 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545
11546 /*
11547 * Lastly, execute the x.vim file if it exists.
11548 */
11549 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11550 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011551 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552 || put_line(fd, "endif") == FAIL)
11553 return FAIL;
11554
11555 return OK;
11556}
11557
11558 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011559ses_winsizes(
11560 FILE *fd,
11561 int restore_size,
11562 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563{
11564 int n = 0;
11565 win_T *wp;
11566
11567 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11568 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011569 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570 {
11571 if (!ses_do_win(wp))
11572 continue;
11573 ++n;
11574
11575 /* restore height when not full height */
11576 if (wp->w_height + wp->w_status_height < topframe->fr_height
11577 && (fprintf(fd,
11578 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11579 n, (long)wp->w_height, Rows / 2, Rows) < 0
11580 || put_eol(fd) == FAIL))
11581 return FAIL;
11582
11583 /* restore width when not full width */
11584 if (wp->w_width < Columns && (fprintf(fd,
11585 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11586 n, (long)wp->w_width, Columns / 2, Columns) < 0
11587 || put_eol(fd) == FAIL))
11588 return FAIL;
11589 }
11590 }
11591 else
11592 {
11593 /* Just equalise window sizes */
11594 if (put_line(fd, "wincmd =") == FAIL)
11595 return FAIL;
11596 }
11597 return OK;
11598}
11599
11600/*
11601 * Write commands to "fd" to recursively create windows for frame "fr",
11602 * horizontally and vertically split.
11603 * After the commands the last window in the frame is the current window.
11604 * Returns FAIL when writing the commands to "fd" fails.
11605 */
11606 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011607ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608{
11609 frame_T *frc;
11610 int count = 0;
11611
11612 if (fr->fr_layout != FR_LEAF)
11613 {
11614 /* Find first frame that's not skipped and then create a window for
11615 * each following one (first frame is already there). */
11616 frc = ses_skipframe(fr->fr_child);
11617 if (frc != NULL)
11618 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11619 {
11620 /* Make window as big as possible so that we have lots of room
11621 * to split. */
11622 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11623 || put_line(fd, fr->fr_layout == FR_COL
11624 ? "split" : "vsplit") == FAIL)
11625 return FAIL;
11626 ++count;
11627 }
11628
11629 /* Go back to the first window. */
11630 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11631 ? "%dwincmd k" : "%dwincmd h", count) < 0
11632 || put_eol(fd) == FAIL))
11633 return FAIL;
11634
11635 /* Recursively create frames/windows in each window of this column or
11636 * row. */
11637 frc = ses_skipframe(fr->fr_child);
11638 while (frc != NULL)
11639 {
11640 ses_win_rec(fd, frc);
11641 frc = ses_skipframe(frc->fr_next);
11642 /* Go to next window. */
11643 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11644 return FAIL;
11645 }
11646 }
11647 return OK;
11648}
11649
11650/*
11651 * Skip frames that don't contain windows we want to save in the Session.
11652 * Returns NULL when there none.
11653 */
11654 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011655ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656{
11657 frame_T *frc;
11658
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011659 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 if (ses_do_frame(frc))
11661 break;
11662 return frc;
11663}
11664
11665/*
11666 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11667 * the Session.
11668 */
11669 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011670ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671{
11672 frame_T *frc;
11673
11674 if (fr->fr_layout == FR_LEAF)
11675 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011676 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 if (ses_do_frame(frc))
11678 return TRUE;
11679 return FALSE;
11680}
11681
11682/*
11683 * Return non-zero if window "wp" is to be stored in the Session.
11684 */
11685 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011686ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011688#ifdef FEAT_TERMINAL
11689 if (bt_terminal(wp->w_buffer))
11690 return !term_is_finished(wp->w_buffer)
11691 && (ssop_flags & SSOP_TERMINAL)
11692 && term_should_restore(wp->w_buffer);
11693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694 if (wp->w_buffer->b_fname == NULL
11695#ifdef FEAT_QUICKFIX
11696 /* When 'buftype' is "nofile" can't restore the window contents. */
11697 || bt_nofile(wp->w_buffer)
11698#endif
11699 )
11700 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011701 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 return (ssop_flags & SSOP_HELP);
11703 return TRUE;
11704}
11705
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011706 static int
11707put_view_curpos(FILE *fd, win_T *wp, char *spaces)
11708{
11709 int r;
11710
11711 if (wp->w_curswant == MAXCOL)
11712 r = fprintf(fd, "%snormal! $", spaces);
11713 else
11714 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
11715 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
11716}
11717
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718/*
11719 * Write commands to "fd" to restore the view of a window.
11720 * Caller must make sure 'scrolloff' is zero.
11721 */
11722 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011723put_view(
11724 FILE *fd,
11725 win_T *wp,
11726 int add_edit, /* add ":edit" command to view */
11727 unsigned *flagp, /* vop_flags or ssop_flags */
11728 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011729 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730{
11731 win_T *save_curwin;
11732 int f;
11733 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011734 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735
11736 /* Always restore cursor position for ":mksession". For ":mkview" only
11737 * when 'viewoptions' contains "cursor". */
11738 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11739
11740 /*
11741 * Local argument list.
11742 */
11743 if (wp->w_alist == &global_alist)
11744 {
11745 if (put_line(fd, "argglobal") == FAIL)
11746 return FAIL;
11747 }
11748 else
11749 {
11750 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11751 flagp == &vop_flags
11752 || !(*flagp & SSOP_CURDIR)
11753 || wp->w_localdir != NULL, flagp) == FAIL)
11754 return FAIL;
11755 }
11756
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011757 /* Only when part of a session: restore the argument index. Some
11758 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011759 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011760 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011762 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763 || put_eol(fd) == FAIL)
11764 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011765 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766 }
11767
11768 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011769 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011771# ifdef FEAT_TERMINAL
11772 if (bt_terminal(wp->w_buffer))
11773 {
11774 if (term_write_session(fd, wp) == FAIL)
11775 return FAIL;
11776 }
11777 else
11778# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779 /*
11780 * Load the file.
11781 */
11782 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011783# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011785# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786 )
11787 {
11788 /*
11789 * Editing a file in this buffer: use ":edit file".
11790 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011791 *
11792 * Note, if a buffer for that file already exists, use :badd to
11793 * edit that buffer, to not lose folding information (:edit resets
11794 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 */
Bram Moolenaarad36a352019-01-24 13:34:42 +010011796 if (fputs("if bufexists(\"", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011797 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
Bram Moolenaarad36a352019-01-24 13:34:42 +010011798 || fputs("\") | buffer ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011799 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11800 || fputs(" | else | edit ", fd) < 0
11801 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11802 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011803 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804 return FAIL;
11805 }
11806 else
11807 {
11808 /* No file in this buffer, just make it empty. */
11809 if (put_line(fd, "enew") == FAIL)
11810 return FAIL;
11811#ifdef FEAT_QUICKFIX
11812 if (wp->w_buffer->b_ffname != NULL)
11813 {
11814 /* The buffer does have a name, but it's not a file name. */
11815 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011816 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817 return FAIL;
11818 }
11819#endif
11820 do_cursor = FALSE;
11821 }
11822 }
11823
11824 /*
11825 * Local mappings and abbreviations.
11826 */
11827 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11828 && makemap(fd, wp->w_buffer) == FAIL)
11829 return FAIL;
11830
11831 /*
11832 * Local options. Need to go to the window temporarily.
11833 * Store only local values when using ":mkview" and when ":mksession" is
11834 * used and 'sessionoptions' doesn't include "options".
11835 * Some folding options are always stored when "folds" is included,
11836 * otherwise the folds would not be restored correctly.
11837 */
11838 save_curwin = curwin;
11839 curwin = wp;
11840 curbuf = curwin->w_buffer;
11841 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11842 f = makeset(fd, OPT_LOCAL,
11843 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11844#ifdef FEAT_FOLDING
11845 else if (*flagp & SSOP_FOLDS)
11846 f = makefoldset(fd);
11847#endif
11848 else
11849 f = OK;
11850 curwin = save_curwin;
11851 curbuf = curwin->w_buffer;
11852 if (f == FAIL)
11853 return FAIL;
11854
11855#ifdef FEAT_FOLDING
11856 /*
11857 * Save Folds when 'buftype' is empty and for help files.
11858 */
11859 if ((*flagp & SSOP_FOLDS)
11860 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020011861 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862 {
11863 if (put_folds(fd, wp) == FAIL)
11864 return FAIL;
11865 }
11866#endif
11867
11868 /*
11869 * Set the cursor after creating folds, since that moves the cursor.
11870 */
11871 if (do_cursor)
11872 {
11873
11874 /* Restore the cursor line in the file and relatively in the
11875 * window. Don't use "G", it changes the jumplist. */
11876 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11877 (long)wp->w_cursor.lnum,
11878 (long)(wp->w_cursor.lnum - wp->w_topline),
11879 (long)wp->w_height / 2, (long)wp->w_height) < 0
11880 || put_eol(fd) == FAIL
11881 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11882 || put_line(fd, "exe s:l") == FAIL
11883 || put_line(fd, "normal! zt") == FAIL
11884 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11885 || put_eol(fd) == FAIL)
11886 return FAIL;
11887 /* Restore the cursor column and left offset when not wrapping. */
11888 if (wp->w_cursor.col == 0)
11889 {
11890 if (put_line(fd, "normal! 0") == FAIL)
11891 return FAIL;
11892 }
11893 else
11894 {
11895 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11896 {
11897 if (fprintf(fd,
11898 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011899 (long)wp->w_virtcol + 1,
11900 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901 (long)wp->w_width / 2, (long)wp->w_width) < 0
11902 || put_eol(fd) == FAIL
11903 || put_line(fd, "if s:c > 0") == FAIL
11904 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011905 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11906 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907 || put_eol(fd) == FAIL
11908 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011909 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910 || put_line(fd, "endif") == FAIL)
11911 return FAIL;
11912 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011913 else if (put_view_curpos(fd, wp, "") == FAIL)
11914 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915 }
11916 }
11917
11918 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011919 * Local directory, if the current flag is not view options or the "curdir"
11920 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011922 if (wp->w_localdir != NULL
11923 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924 {
11925 if (fputs("lcd ", fd) < 0
11926 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11927 || put_eol(fd) == FAIL)
11928 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011929 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930 }
11931
11932 return OK;
11933}
11934
11935/*
11936 * Write an argument list to the session file.
11937 * Returns FAIL if writing fails.
11938 */
11939 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011940ses_arglist(
11941 FILE *fd,
11942 char *cmd,
11943 garray_T *gap,
11944 int fullname, /* TRUE: use full path name */
11945 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946{
11947 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011948 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949 char_u *s;
11950
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011951 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11952 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010011953 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954 return FAIL;
11955 for (i = 0; i < gap->ga_len; ++i)
11956 {
11957 /* NULL file names are skipped (only happens when out of memory). */
11958 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11959 if (s != NULL)
11960 {
11961 if (fullname)
11962 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011963 buf = alloc(MAXPATHL);
11964 if (buf != NULL)
11965 {
11966 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11967 s = buf;
11968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011970 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011971 || ses_put_fname(fd, s, flagp) == FAIL
11972 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011973 {
11974 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011976 }
11977 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978 }
11979 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011980 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981}
11982
11983/*
11984 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011985 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986 * Returns FAIL if writing fails.
11987 */
11988 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011989ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990{
11991 char_u *name;
11992
11993 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011994 * the session file will be sourced.
11995 * Don't do this for ":mkview", we don't know the current directory.
11996 * Don't do this after ":lcd", we don't keep track of what the current
11997 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998 if (buf->b_sfname != NULL
11999 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012000 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000012001#ifdef FEAT_AUTOCHDIR
12002 && !p_acd
12003#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012004 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005 name = buf->b_sfname;
12006 else
12007 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012008 if (ses_put_fname(fd, name, flagp) == FAIL
12009 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010 return FAIL;
12011 return OK;
12012}
12013
12014/*
12015 * Write a file name to the session file.
12016 * Takes care of the "slash" option in 'sessionoptions' and escapes special
12017 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012018 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019 */
12020 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012021ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022{
12023 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012024 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026
12027 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012028 if (sname == NULL)
12029 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012031 if (*flagp & SSOP_SLASH)
12032 {
12033 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010012034 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012035 if (*p == '\\')
12036 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012038
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020012039 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012040 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012042 if (p == NULL)
12043 return FAIL;
12044
12045 /* write the result */
12046 if (fputs((char *)p, fd) < 0)
12047 retval = FAIL;
12048
12049 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 return retval;
12051}
12052
12053/*
12054 * ":loadview [nr]"
12055 */
12056 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012057ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058{
12059 char_u *fname;
12060
12061 fname = get_view_file(*eap->arg);
12062 if (fname != NULL)
12063 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012064 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065 vim_free(fname);
12066 }
12067}
12068
12069/*
12070 * Get the name of the view file for the current buffer.
12071 */
12072 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012073get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074{
12075 int len = 0;
12076 char_u *p, *s;
12077 char_u *retval;
12078 char_u *sname;
12079
12080 if (curbuf->b_ffname == NULL)
12081 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012082 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083 return NULL;
12084 }
12085 sname = home_replace_save(NULL, curbuf->b_ffname);
12086 if (sname == NULL)
12087 return NULL;
12088
12089 /*
12090 * We want a file name without separators, because we're not going to make
12091 * a directory.
12092 * "normal" path separator -> "=+"
12093 * "=" -> "=="
12094 * ":" path separator -> "=-"
12095 */
12096 for (p = sname; *p; ++p)
12097 if (*p == '=' || vim_ispathsep(*p))
12098 ++len;
12099 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
12100 if (retval != NULL)
12101 {
12102 STRCPY(retval, p_vdir);
12103 add_pathsep(retval);
12104 s = retval + STRLEN(retval);
12105 for (p = sname; *p; ++p)
12106 {
12107 if (*p == '=')
12108 {
12109 *s++ = '=';
12110 *s++ = '=';
12111 }
12112 else if (vim_ispathsep(*p))
12113 {
12114 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020012115#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116 if (*p == ':')
12117 *s++ = '-';
12118 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012120 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121 }
12122 else
12123 *s++ = *p;
12124 }
12125 *s++ = '=';
12126 *s++ = c;
12127 STRCPY(s, ".vim");
12128 }
12129
12130 vim_free(sname);
12131 return retval;
12132}
12133
12134#endif /* FEAT_SESSION */
12135
12136/*
12137 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12138 * Return FAIL for a write error.
12139 */
12140 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012141put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142{
12143 if (
12144#ifdef USE_CRNL
12145 (
12146# ifdef MKSESSION_NL
12147 !mksession_nl &&
12148# endif
12149 (putc('\r', fd) < 0)) ||
12150#endif
12151 (putc('\n', fd) < 0))
12152 return FAIL;
12153 return OK;
12154}
12155
12156/*
12157 * Write a line to "fd".
12158 * Return FAIL for a write error.
12159 */
12160 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012161put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162{
12163 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12164 return FAIL;
12165 return OK;
12166}
12167
12168#ifdef FEAT_VIMINFO
12169/*
12170 * ":rviminfo" and ":wviminfo".
12171 */
12172 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012173ex_viminfo(
12174 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175{
12176 char_u *save_viminfo;
12177
12178 save_viminfo = p_viminfo;
12179 if (*p_viminfo == NUL)
12180 p_viminfo = (char_u *)"'100";
12181 if (eap->cmdidx == CMD_rviminfo)
12182 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012183 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12184 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012185 emsg(_("E195: Cannot open viminfo file for reading"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186 }
12187 else
12188 write_viminfo(eap->arg, eap->forceit);
12189 p_viminfo = save_viminfo;
12190}
12191#endif
12192
12193#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012194/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012195 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012196 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012199dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201 if (fname == NULL)
12202 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012203 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204}
12205#endif
12206
12207/*
12208 * ":behave {mswin,xterm}"
12209 */
12210 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012211ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212{
12213 if (STRCMP(eap->arg, "mswin") == 0)
12214 {
12215 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12216 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12217 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12218 set_option_value((char_u *)"keymodel", 0L,
12219 (char_u *)"startsel,stopsel", 0);
12220 }
12221 else if (STRCMP(eap->arg, "xterm") == 0)
12222 {
12223 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12224 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12225 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12226 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12227 }
12228 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012229 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230}
12231
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012232#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12233/*
12234 * Function given to ExpandGeneric() to obtain the possible arguments of the
12235 * ":behave {mswin,xterm}" command.
12236 */
12237 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012238get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012239{
12240 if (idx == 0)
12241 return (char_u *)"mswin";
12242 if (idx == 1)
12243 return (char_u *)"xterm";
12244 return NULL;
12245}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012246
12247/*
12248 * Function given to ExpandGeneric() to obtain the possible arguments of the
12249 * ":messages {clear}" command.
12250 */
12251 char_u *
12252get_messages_arg(expand_T *xp UNUSED, int idx)
12253{
12254 if (idx == 0)
12255 return (char_u *)"clear";
12256 return NULL;
12257}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012258#endif
12259
Bram Moolenaar113e1072019-01-20 15:30:40 +010012260#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012261 char_u *
12262get_mapclear_arg(expand_T *xp UNUSED, int idx)
12263{
12264 if (idx == 0)
12265 return (char_u *)"<buffer>";
12266 return NULL;
12267}
Bram Moolenaar113e1072019-01-20 15:30:40 +010012268#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012269
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270static int filetype_detect = FALSE;
12271static int filetype_plugin = FALSE;
12272static int filetype_indent = FALSE;
12273
12274/*
12275 * ":filetype [plugin] [indent] {on,off,detect}"
12276 * on: Load the filetype.vim file to install autocommands for file types.
12277 * off: Load the ftoff.vim file to remove all autocommands for file types.
12278 * plugin on: load filetype.vim and ftplugin.vim
12279 * plugin off: load ftplugof.vim
12280 * indent on: load filetype.vim and indent.vim
12281 * indent off: load indoff.vim
12282 */
12283 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012284ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285{
12286 char_u *arg = eap->arg;
12287 int plugin = FALSE;
12288 int indent = FALSE;
12289
12290 if (*eap->arg == NUL)
12291 {
12292 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012293 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294 filetype_detect ? "ON" : "OFF",
12295 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12296 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12297 return;
12298 }
12299
12300 /* Accept "plugin" and "indent" in any order. */
12301 for (;;)
12302 {
12303 if (STRNCMP(arg, "plugin", 6) == 0)
12304 {
12305 plugin = TRUE;
12306 arg = skipwhite(arg + 6);
12307 continue;
12308 }
12309 if (STRNCMP(arg, "indent", 6) == 0)
12310 {
12311 indent = TRUE;
12312 arg = skipwhite(arg + 6);
12313 continue;
12314 }
12315 break;
12316 }
12317 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12318 {
12319 if (*arg == 'o' || !filetype_detect)
12320 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012321 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322 filetype_detect = TRUE;
12323 if (plugin)
12324 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012325 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326 filetype_plugin = TRUE;
12327 }
12328 if (indent)
12329 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012330 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 filetype_indent = TRUE;
12332 }
12333 }
12334 if (*arg == 'd')
12335 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012336 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012337 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338 }
12339 }
12340 else if (STRCMP(arg, "off") == 0)
12341 {
12342 if (plugin || indent)
12343 {
12344 if (plugin)
12345 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012346 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347 filetype_plugin = FALSE;
12348 }
12349 if (indent)
12350 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012351 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352 filetype_indent = FALSE;
12353 }
12354 }
12355 else
12356 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012357 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358 filetype_detect = FALSE;
12359 }
12360 }
12361 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012362 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363}
12364
12365/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012366 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367 */
12368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012369ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370{
12371 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012372 {
12373 char_u *arg = eap->arg;
12374
12375 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12376 arg += 9;
12377
12378 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12379 if (arg != eap->arg)
12380 did_filetype = FALSE;
12381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012385ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386{
12387#ifdef FEAT_DIGRAPHS
12388 if (*eap->arg != NUL)
12389 putdigraph(eap->arg);
12390 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010012391 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012393 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394#endif
12395}
12396
12397 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012398ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399{
12400 int flags = 0;
12401
12402 if (eap->cmdidx == CMD_setlocal)
12403 flags = OPT_LOCAL;
12404 else if (eap->cmdidx == CMD_setglobal)
12405 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012406#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407 if (cmdmod.browse && flags == 0)
12408 ex_options(eap);
12409 else
12410#endif
12411 (void)do_set(eap->arg, flags);
12412}
12413
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012414#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
12415 void
12416set_no_hlsearch(int flag)
12417{
12418 no_hlsearch = flag;
12419# ifdef FEAT_EVAL
12420 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
12421# endif
12422}
12423
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424/*
12425 * ":nohlsearch"
12426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012428ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012430 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012431 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432}
12433
12434/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012435 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436 * Sets nextcmd to the start of the next command, if any. Also called when
12437 * skipping commands to find the next command.
12438 */
12439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012440ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441{
12442 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012443 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444 char_u *end;
12445 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012446 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012447
12448 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012449 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012450 else
12451 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012452 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012453 return;
12454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455
12456 /* First clear any old pattern. */
12457 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012458 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459
12460 if (ends_excmd(*eap->arg))
12461 end = eap->arg;
12462 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012463 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012464 end = eap->arg + 4;
12465 else
12466 {
12467 p = skiptowhite(eap->arg);
12468 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012469 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470 p = skipwhite(p);
12471 if (*p == NUL)
12472 {
12473 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012474 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012475 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476 return;
12477 }
12478 end = skip_regexp(p + 1, *p, TRUE, NULL);
12479 if (!eap->skip)
12480 {
12481 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12482 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012483 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484 eap->errmsg = e_trailing;
12485 return;
12486 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012487 if (*end != *p)
12488 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012489 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012490 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012491 return;
12492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493
12494 c = *end;
12495 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012496 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012497 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012498 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499 }
12500 }
12501 eap->nextcmd = find_nextcmd(end);
12502}
12503#endif
12504
12505#ifdef FEAT_CRYPT
12506/*
12507 * ":X": Get crypt key
12508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012510ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012512 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012513 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514}
12515#endif
12516
12517#ifdef FEAT_FOLDING
12518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012519ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012520{
12521 if (foldManualAllowed(TRUE))
12522 foldCreate(eap->line1, eap->line2);
12523}
12524
12525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012526ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527{
12528 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12529 eap->forceit, FALSE);
12530}
12531
12532 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012533ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534{
12535 linenr_T lnum;
12536
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012537#ifdef FEAT_CLIPBOARD
12538 start_global_changes();
12539#endif
12540
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541 /* First set the marks for all lines closed/open. */
12542 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12543 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12544 ml_setmarked(lnum);
12545
12546 /* Execute the command on the marked lines. */
12547 global_exe(eap->arg);
12548 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012549#ifdef FEAT_CLIPBOARD
12550 end_global_changes();
12551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552}
12553#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012554
Bram Moolenaar39665952018-08-15 20:59:48 +020012555#ifdef FEAT_QUICKFIX
12556/*
12557 * Returns TRUE if the supplied Ex cmdidx is for a location list command
12558 * instead of a quickfix command.
12559 */
12560 int
12561is_loclist_cmd(int cmdidx)
12562{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020012563 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020012564 return FALSE;
12565 return cmdnames[cmdidx].cmd_name[0] == 'l';
12566}
12567#endif
12568
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012569# if defined(FEAT_TIMERS) || defined(PROTO)
12570 int
12571get_pressedreturn(void)
12572{
12573 return ex_pressedreturn;
12574}
12575
12576 void
12577set_pressedreturn(int val)
12578{
12579 ex_pressedreturn = val;
12580}
12581#endif