blob: dcfdcd4543949d2158d00c9e2bacab15c5998493 [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 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004654 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004655 *cmd == '?' ? BACKWARD : FORWARD,
4656 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004657 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004658 lnum = pos.lnum;
4659 else
4660 {
4661 cmd = NULL;
4662 goto error;
4663 }
4664 }
4665 ++cmd;
4666 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667
4668 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004669 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4670 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 }
4672
4673 for (;;)
4674 {
4675 cmd = skipwhite(cmd);
4676 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4677 break;
4678
4679 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004680 {
4681 switch (addr_type)
4682 {
4683 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004684 /* "+1" is same as ".+1" */
4685 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004686 break;
4687 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004688 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004689 break;
4690 case ADDR_ARGUMENTS:
4691 lnum = curwin->w_arg_idx + 1;
4692 break;
4693 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004694 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004695 lnum = curbuf->b_fnum;
4696 break;
4697 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004698 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004699 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004700 case ADDR_TABS_RELATIVE:
4701 lnum = 1;
4702 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004703#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004704 case ADDR_QUICKFIX:
4705 lnum = qf_get_cur_valid_idx(eap);
4706 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004707#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004708 }
4709 }
4710
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 if (VIM_ISDIGIT(*cmd))
4712 i = '+'; /* "number" is same as "+number" */
4713 else
4714 i = *cmd++;
4715 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4716 n = 1;
4717 else
4718 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004719
4720 if (addr_type == ADDR_TABS_RELATIVE)
4721 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004722 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004723 cmd = NULL;
4724 goto error;
4725 }
4726 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004727 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004728 lnum = compute_buffer_local_count(
4729 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004731 {
4732#ifdef FEAT_FOLDING
4733 /* Relative line addressing, need to adjust for folded lines
4734 * now, but only do it after the first address. */
4735 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4736 && address_count >= 2)
4737 (void)hasFolding(lnum, NULL, &lnum);
4738#endif
4739 if (i == '-')
4740 lnum -= n;
4741 else
4742 lnum += n;
4743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 }
4745 } while (*cmd == '/' || *cmd == '?');
4746
4747error:
4748 *ptr = cmd;
4749 return lnum;
4750}
4751
4752/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004753 * Get flags from an Ex command argument.
4754 */
4755 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004756get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004757{
4758 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4759 {
4760 if (*eap->arg == 'l')
4761 eap->flags |= EXFLAG_LIST;
4762 else if (*eap->arg == 'p')
4763 eap->flags |= EXFLAG_PRINT;
4764 else
4765 eap->flags |= EXFLAG_NR;
4766 eap->arg = skipwhite(eap->arg + 1);
4767 }
4768}
4769
4770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 * Function called for command which is Not Implemented. NI!
4772 */
4773 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004774ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
4776 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004777 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778}
4779
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004780#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781/*
4782 * Function called for script command which is Not Implemented. NI!
4783 * Skips over ":perl <<EOF" constructs.
4784 */
4785 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004786ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787{
4788 if (!eap->skip)
4789 ex_ni(eap);
4790 else
4791 vim_free(script_get(eap, eap->arg));
4792}
4793#endif
4794
4795/*
4796 * Check range in Ex command for validity.
4797 * Return NULL when valid, error message when invalid.
4798 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004799 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004800invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004802 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 if ( eap->line1 < 0
4804 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004805 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004806 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004807
4808 if (eap->argt & RANGE)
4809 {
4810 switch(eap->addr_type)
4811 {
4812 case ADDR_LINES:
4813 if (!(eap->argt & NOTADR)
4814 && eap->line2 > curbuf->b_ml.ml_line_count
4815#ifdef FEAT_DIFF
4816 + (eap->cmdidx == CMD_diffget)
4817#endif
4818 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004819 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004820 break;
4821 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004822 /* add 1 if ARGCOUNT is 0 */
4823 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004824 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004825 break;
4826 case ADDR_BUFFERS:
4827 if (eap->line1 < firstbuf->b_fnum
4828 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004829 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004830 break;
4831 case ADDR_LOADED_BUFFERS:
4832 buf = firstbuf;
4833 while (buf->b_ml.ml_mfp == NULL)
4834 {
4835 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004836 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004837 buf = buf->b_next;
4838 }
4839 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004840 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004841 buf = lastbuf;
4842 while (buf->b_ml.ml_mfp == NULL)
4843 {
4844 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004845 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004846 buf = buf->b_prev;
4847 }
4848 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004849 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004850 break;
4851 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004852 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004853 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004854 break;
4855 case ADDR_TABS:
4856 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004857 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004858 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004859 case ADDR_TABS_RELATIVE:
4860 /* Do nothing */
4861 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004862#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004863 case ADDR_QUICKFIX:
4864 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004865 return _(e_invrange);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004866 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004867#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004868 }
4869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 return NULL;
4871}
4872
4873/*
4874 * Correct the range for zero line number, if required.
4875 */
4876 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004877correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878{
4879 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4880 {
4881 if (eap->line1 == 0)
4882 eap->line1 = 1;
4883 if (eap->line2 == 0)
4884 eap->line2 = 1;
4885 }
4886}
4887
Bram Moolenaar748bf032005-02-02 23:04:36 +00004888#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004889/*
4890 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4891 * pattern. Otherwise return eap->arg.
4892 */
4893 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004894skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004895{
4896 char_u *p = eap->arg;
4897
Bram Moolenaara37420f2006-02-04 22:37:47 +00004898 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4899 || eap->cmdidx == CMD_vimgrepadd
4900 || eap->cmdidx == CMD_lvimgrepadd
4901 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004902 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004903 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004904 if (p == NULL)
4905 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004906 }
4907 return p;
4908}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004909
4910/*
4911 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4912 * in the command line, so that things like % get expanded.
4913 */
4914 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004915replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004916{
4917 char_u *new_cmdline;
4918 char_u *program;
4919 char_u *pos;
4920 char_u *ptr;
4921 int len;
4922 int i;
4923
4924 /*
4925 * Don't do it when ":vimgrep" is used for ":grep".
4926 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004927 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4928 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4929 || eap->cmdidx == CMD_grepadd
4930 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004931 && !grep_internal(eap->cmdidx))
4932 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004933 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4934 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004935 {
4936 if (*curbuf->b_p_gp == NUL)
4937 program = p_gp;
4938 else
4939 program = curbuf->b_p_gp;
4940 }
4941 else
4942 {
4943 if (*curbuf->b_p_mp == NUL)
4944 program = p_mp;
4945 else
4946 program = curbuf->b_p_mp;
4947 }
4948
4949 p = skipwhite(p);
4950
4951 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4952 {
4953 /* replace $* by given arguments */
4954 i = 1;
4955 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4956 ++i;
4957 len = (int)STRLEN(p);
4958 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4959 if (new_cmdline == NULL)
4960 return NULL; /* out of memory */
4961 ptr = new_cmdline;
4962 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4963 {
4964 i = (int)(pos - program);
4965 STRNCPY(ptr, program, i);
4966 STRCPY(ptr += i, p);
4967 ptr += len;
4968 program = pos + 2;
4969 }
4970 STRCPY(ptr, program);
4971 }
4972 else
4973 {
4974 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4975 if (new_cmdline == NULL)
4976 return NULL; /* out of memory */
4977 STRCPY(new_cmdline, program);
4978 STRCAT(new_cmdline, " ");
4979 STRCAT(new_cmdline, p);
4980 }
4981 msg_make(p);
4982
4983 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4984 vim_free(*cmdlinep);
4985 *cmdlinep = new_cmdline;
4986 p = new_cmdline;
4987 }
4988 return p;
4989}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004990#endif
4991
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992/*
4993 * Expand file name in Ex command argument.
4994 * Return FAIL for failure, OK otherwise.
4995 */
4996 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004997expand_filename(
4998 exarg_T *eap,
4999 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005000 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001{
5002 int has_wildcards; /* need to expand wildcards */
5003 char_u *repl;
5004 int srclen;
5005 char_u *p;
5006 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005007 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008
Bram Moolenaar748bf032005-02-02 23:04:36 +00005009#ifdef FEAT_QUICKFIX
5010 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
5011 p = skip_grep_pat(eap);
5012#else
5013 p = eap->arg;
5014#endif
5015
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 /*
5017 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5018 * the file name contains a wildcard it should not cause expanding.
5019 * (it will be expanded anyway if there is a wildcard before replacing).
5020 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005021 has_wildcards = mch_has_wildcard(p);
5022 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005024#ifdef FEAT_EVAL
5025 /* Skip over `=expr`, wildcards in it are not expanded. */
5026 if (p[0] == '`' && p[1] == '=')
5027 {
5028 p += 2;
5029 (void)skip_expr(&p);
5030 if (*p == '`')
5031 ++p;
5032 continue;
5033 }
5034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 /*
5036 * Quick check if this cannot be the start of a special string.
5037 * Also removes backslash before '%', '#' and '<'.
5038 */
5039 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5040 {
5041 ++p;
5042 continue;
5043 }
5044
5045 /*
5046 * Try to find a match at this position.
5047 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005048 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5049 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 if (*errormsgp != NULL) /* error detected */
5051 return FAIL;
5052 if (repl == NULL) /* no match found */
5053 {
5054 p += srclen;
5055 continue;
5056 }
5057
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005058 /* Wildcards won't be expanded below, the replacement is taken
5059 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5060 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5061 {
5062 char_u *l = repl;
5063
5064 repl = expand_env_save(repl);
5065 vim_free(l);
5066 }
5067
Bram Moolenaar63b92542007-03-27 14:57:09 +00005068 /* Need to escape white space et al. with a backslash.
5069 * Don't do this for:
5070 * - replacement that already has been escaped: "##"
5071 * - shell commands (may have to use quotes instead).
5072 * - non-unix systems when there is a single argument (spaces don't
5073 * separate arguments then).
5074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005076 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 && eap->cmdidx != CMD_grep
5079 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005080 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005081 && eap->cmdidx != CMD_lgrep
5082 && eap->cmdidx != CMD_lgrepadd
5083 && eap->cmdidx != CMD_lmake
5084 && eap->cmdidx != CMD_make
5085 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086#ifndef UNIX
5087 && !(eap->argt & NOSPC)
5088#endif
5089 )
5090 {
5091 char_u *l;
5092#ifdef BACKSLASH_IN_FILENAME
5093 /* Don't escape a backslash here, because rem_backslash() doesn't
5094 * remove it later. */
5095 static char_u *nobslash = (char_u *)" \t\"|";
5096# define ESCAPE_CHARS nobslash
5097#else
5098# define ESCAPE_CHARS escape_chars
5099#endif
5100
5101 for (l = repl; *l; ++l)
5102 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5103 {
5104 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5105 if (l != NULL)
5106 {
5107 vim_free(repl);
5108 repl = l;
5109 }
5110 break;
5111 }
5112 }
5113
5114 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005115 if ((eap->usefilter || eap->cmdidx == CMD_bang
5116 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005117 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
5119 char_u *l;
5120
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005121 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (l != NULL)
5123 {
5124 vim_free(repl);
5125 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
5127 }
5128
5129 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5130 vim_free(repl);
5131 if (p == NULL)
5132 return FAIL;
5133 }
5134
5135 /*
5136 * One file argument: Expand wildcards.
5137 * Don't do this with ":r !command" or ":w !command".
5138 */
5139 if ((eap->argt & NOSPC) && !eap->usefilter)
5140 {
5141 /*
5142 * May do this twice:
5143 * 1. Replace environment variables.
5144 * 2. Replace any other wildcards, remove backslashes.
5145 */
5146 for (n = 1; n <= 2; ++n)
5147 {
5148 if (n == 2)
5149 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 /*
5151 * Halve the number of backslashes (this is Vi compatible).
5152 * For Unix and OS/2, when wildcards are expanded, this is
5153 * done by ExpandOne() below.
5154 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005155#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 if (!has_wildcards)
5157#endif
5158 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 }
5160
5161 if (has_wildcards)
5162 {
5163 if (n == 1)
5164 {
5165 /*
5166 * First loop: May expand environment variables. This
5167 * can be done much faster with expand_env() than with
5168 * something else (e.g., calling a shell).
5169 * After expanding environment variables, check again
5170 * if there are still wildcards present.
5171 */
5172 if (vim_strchr(eap->arg, '$') != NULL
5173 || vim_strchr(eap->arg, '~') != NULL)
5174 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005175 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005176 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 has_wildcards = mch_has_wildcard(NameBuff);
5178 p = NameBuff;
5179 }
5180 else
5181 p = NULL;
5182 }
5183 else /* n == 2 */
5184 {
5185 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005186 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187
5188 ExpandInit(&xpc);
5189 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005190 if (p_wic)
5191 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005193 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 if (p == NULL)
5195 return FAIL;
5196 }
5197 if (p != NULL)
5198 {
5199 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5200 p, cmdlinep);
5201 if (n == 2) /* p came from ExpandOne() */
5202 vim_free(p);
5203 }
5204 }
5205 }
5206 }
5207 return OK;
5208}
5209
5210/*
5211 * Replace part of the command line, keeping eap->cmd, eap->arg and
5212 * eap->nextcmd correct.
5213 * "src" points to the part that is to be replaced, of length "srclen".
5214 * "repl" is the replacement string.
5215 * Returns a pointer to the character after the replaced string.
5216 * Returns NULL for failure.
5217 */
5218 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005219repl_cmdline(
5220 exarg_T *eap,
5221 char_u *src,
5222 int srclen,
5223 char_u *repl,
5224 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225{
5226 int len;
5227 int i;
5228 char_u *new_cmdline;
5229
5230 /*
5231 * The new command line is build in new_cmdline[].
5232 * First allocate it.
5233 * Careful: a "+cmd" argument may have been NUL terminated.
5234 */
5235 len = (int)STRLEN(repl);
5236 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005237 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5239 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5240 return NULL; /* out of memory! */
5241
5242 /*
5243 * Copy the stuff before the expanded part.
5244 * Copy the expanded stuff.
5245 * Copy what came after the expanded part.
5246 * Copy the next commands, if there are any.
5247 */
5248 i = (int)(src - *cmdlinep); /* length of part before match */
5249 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005250
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 mch_memmove(new_cmdline + i, repl, (size_t)len);
5252 i += len; /* remember the end of the string */
5253 STRCPY(new_cmdline + i, src + srclen);
5254 src = new_cmdline + i; /* remember where to continue */
5255
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005256 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 {
5258 i = (int)STRLEN(new_cmdline) + 1;
5259 STRCPY(new_cmdline + i, eap->nextcmd);
5260 eap->nextcmd = new_cmdline + i;
5261 }
5262 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5263 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5264 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5265 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5266 vim_free(*cmdlinep);
5267 *cmdlinep = new_cmdline;
5268
5269 return src;
5270}
5271
5272/*
5273 * Check for '|' to separate commands and '"' to start comments.
5274 */
5275 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005276separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277{
5278 char_u *p;
5279
Bram Moolenaar86b68352004-12-27 21:59:20 +00005280#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005281 p = skip_grep_pat(eap);
5282#else
5283 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005284#endif
5285
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005286 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 {
5288 if (*p == Ctrl_V)
5289 {
5290 if (eap->argt & (USECTRLV | XFILE))
5291 ++p; /* skip CTRL-V and next char */
5292 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005293 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005294 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 if (*p == NUL) /* stop at NUL after CTRL-V */
5296 break;
5297 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005298
5299#ifdef FEAT_EVAL
5300 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005301 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005302 {
5303 p += 2;
5304 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005305 }
5306#endif
5307
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 /* Check for '"': start of comment or '|': next command */
5309 /* :@" and :*" do not start a comment!
5310 * :redir @" doesn't either. */
5311 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5312 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5313 || p != eap->arg)
5314 && (eap->cmdidx != CMD_redir
5315 || p != eap->arg + 1 || p[-1] != '@'))
5316 || *p == '|' || *p == '\n')
5317 {
5318 /*
5319 * We remove the '\' before the '|', unless USECTRLV is used
5320 * AND 'b' is present in 'cpoptions'.
5321 */
5322 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5323 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5324 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005325 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 --p;
5327 }
5328 else
5329 {
5330 eap->nextcmd = check_nextcmd(p);
5331 *p = NUL;
5332 break;
5333 }
5334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005336
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5338 del_trailing_spaces(eap->arg);
5339}
5340
5341/*
5342 * get + command from ex argument
5343 */
5344 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005345getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
5347 char_u *arg = *argp;
5348 char_u *command = NULL;
5349
5350 if (*arg == '+') /* +[command] */
5351 {
5352 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005353 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 command = dollar_command;
5355 else
5356 {
5357 command = arg;
5358 arg = skip_cmd_arg(command, TRUE);
5359 if (*arg != NUL)
5360 *arg++ = NUL; /* terminate command with NUL */
5361 }
5362
5363 arg = skipwhite(arg); /* skip over spaces */
5364 *argp = arg;
5365 }
5366 return command;
5367}
5368
5369/*
5370 * Find end of "+command" argument. Skip over "\ " and "\\".
5371 */
5372 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005373skip_cmd_arg(
5374 char_u *p,
5375 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376{
5377 while (*p && !vim_isspace(*p))
5378 {
5379 if (*p == '\\' && p[1] != NUL)
5380 {
5381 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005382 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 else
5384 ++p;
5385 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005386 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388 return p;
5389}
5390
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005391 int
5392get_bad_opt(char_u *p, exarg_T *eap)
5393{
5394 if (STRICMP(p, "keep") == 0)
5395 eap->bad_char = BAD_KEEP;
5396 else if (STRICMP(p, "drop") == 0)
5397 eap->bad_char = BAD_DROP;
5398 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5399 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005400 else
5401 return FAIL;
5402 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005403}
5404
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405/*
5406 * Get "++opt=arg" argument.
5407 * Return FAIL or OK.
5408 */
5409 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005410getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411{
5412 char_u *arg = eap->arg + 2;
5413 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005414 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416
5417 /* ":edit ++[no]bin[ary] file" */
5418 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5419 {
5420 if (*arg == 'n')
5421 {
5422 arg += 2;
5423 eap->force_bin = FORCE_NOBIN;
5424 }
5425 else
5426 eap->force_bin = FORCE_BIN;
5427 if (!checkforcmd(&arg, "binary", 3))
5428 return FAIL;
5429 eap->arg = skipwhite(arg);
5430 return OK;
5431 }
5432
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005433 /* ":read ++edit file" */
5434 if (STRNCMP(arg, "edit", 4) == 0)
5435 {
5436 eap->read_edit = TRUE;
5437 eap->arg = skipwhite(arg + 4);
5438 return OK;
5439 }
5440
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 if (STRNCMP(arg, "ff", 2) == 0)
5442 {
5443 arg += 2;
5444 pp = &eap->force_ff;
5445 }
5446 else if (STRNCMP(arg, "fileformat", 10) == 0)
5447 {
5448 arg += 10;
5449 pp = &eap->force_ff;
5450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 else if (STRNCMP(arg, "enc", 3) == 0)
5452 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005453 if (STRNCMP(arg, "encoding", 8) == 0)
5454 arg += 8;
5455 else
5456 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 pp = &eap->force_enc;
5458 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005459 else if (STRNCMP(arg, "bad", 3) == 0)
5460 {
5461 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005462 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464
5465 if (pp == NULL || *arg != '=')
5466 return FAIL;
5467
5468 ++arg;
5469 *pp = (int)(arg - eap->cmd);
5470 arg = skip_cmd_arg(arg, FALSE);
5471 eap->arg = skipwhite(arg);
5472 *arg = NUL;
5473
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 if (pp == &eap->force_ff)
5475 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5477 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005478 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005480 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 {
5482 /* Make 'fileencoding' lower case. */
5483 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5484 *p = TOLOWER_ASC(*p);
5485 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005486 else
5487 {
5488 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5489 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005490 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005491 return FAIL;
5492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493
5494 return OK;
5495}
5496
5497/*
5498 * ":abbreviate" and friends.
5499 */
5500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005501ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502{
5503 do_exmap(eap, TRUE); /* almost the same as mapping */
5504}
5505
5506/*
5507 * ":map" and friends.
5508 */
5509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005510ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511{
5512 /*
5513 * If we are sourcing .exrc or .vimrc in current directory we
5514 * print the mappings for security reasons.
5515 */
5516 if (secure)
5517 {
5518 secure = 2;
5519 msg_outtrans(eap->cmd);
5520 msg_putchar('\n');
5521 }
5522 do_exmap(eap, FALSE);
5523}
5524
5525/*
5526 * ":unmap" and friends.
5527 */
5528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005529ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530{
5531 do_exmap(eap, FALSE);
5532}
5533
5534/*
5535 * ":mapclear" and friends.
5536 */
5537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005538ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539{
5540 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5541}
5542
5543/*
5544 * ":abclear" and friends.
5545 */
5546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005547ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548{
5549 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5550}
5551
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005553ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
5555 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005556 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 * directory for security reasons.
5558 */
5559 if (secure)
5560 {
5561 secure = 2;
5562 eap->errmsg = e_curdir;
5563 }
5564 else if (eap->cmdidx == CMD_autocmd)
5565 do_autocmd(eap->arg, eap->forceit);
5566 else
5567 do_augroup(eap->arg, eap->forceit);
5568}
5569
5570/*
5571 * ":doautocmd": Apply the automatic commands to the current buffer.
5572 */
5573 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005574ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005576 char_u *arg = eap->arg;
5577 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005578 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005579
Bram Moolenaar1610d052016-06-09 22:53:01 +02005580 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5581 /* Only when there is no <nomodeline>. */
5582 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005583 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586/*
5587 * :[N]bunload[!] [N] [bufname] unload buffer
5588 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5589 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5590 */
5591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005592ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593{
5594 eap->errmsg = do_bufdel(
5595 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5596 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5597 : DOBUF_UNLOAD, eap->arg,
5598 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5599}
5600
5601/*
5602 * :[N]buffer [N] to buffer N
5603 * :[N]sbuffer [N] to buffer N
5604 */
5605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005606ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607{
5608 if (*eap->arg)
5609 eap->errmsg = e_trailing;
5610 else
5611 {
5612 if (eap->addr_count == 0) /* default is current buffer */
5613 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5614 else
5615 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005616 if (eap->do_ecmd_cmd != NULL)
5617 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
5619}
5620
5621/*
5622 * :[N]bmodified [N] to next mod. buffer
5623 * :[N]sbmodified [N] to next mod. buffer
5624 */
5625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005626ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
5628 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005629 if (eap->do_ecmd_cmd != NULL)
5630 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631}
5632
5633/*
5634 * :[N]bnext [N] to next buffer
5635 * :[N]sbnext [N] split and to next buffer
5636 */
5637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005638ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639{
5640 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005641 if (eap->do_ecmd_cmd != NULL)
5642 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643}
5644
5645/*
5646 * :[N]bNext [N] to previous buffer
5647 * :[N]bprevious [N] to previous buffer
5648 * :[N]sbNext [N] split and to previous buffer
5649 * :[N]sbprevious [N] split and to previous buffer
5650 */
5651 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005652ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653{
5654 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005655 if (eap->do_ecmd_cmd != NULL)
5656 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657}
5658
5659/*
5660 * :brewind to first buffer
5661 * :bfirst to first buffer
5662 * :sbrewind split and to first buffer
5663 * :sbfirst split and to first buffer
5664 */
5665 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005666ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667{
5668 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005669 if (eap->do_ecmd_cmd != NULL)
5670 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671}
5672
5673/*
5674 * :blast to last buffer
5675 * :sblast split and to last buffer
5676 */
5677 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005678ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679{
5680 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005681 if (eap->do_ecmd_cmd != NULL)
5682 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684
5685 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005686ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687{
5688 return (c == NUL || c == '|' || c == '"' || c == '\n');
5689}
5690
5691#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5692 || defined(PROTO)
5693/*
5694 * Return the next command, after the first '|' or '\n'.
5695 * Return NULL if not found.
5696 */
5697 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005698find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
5700 while (*p != '|' && *p != '\n')
5701 {
5702 if (*p == NUL)
5703 return NULL;
5704 ++p;
5705 }
5706 return (p + 1);
5707}
5708#endif
5709
5710/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005711 * Check if *p is a separator between Ex commands, skipping over white space.
5712 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 */
5714 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005715check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005717 char_u *s = skipwhite(p);
5718
5719 if (*s == '|' || *s == '\n')
5720 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 else
5722 return NULL;
5723}
5724
5725/*
5726 * - if there are more files to edit
5727 * - and this is the last window
5728 * - and forceit not used
5729 * - and not repeated twice on a row
5730 * return FAIL and give error message if 'message' TRUE
5731 * return OK otherwise
5732 */
5733 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005734check_more(
5735 int message, /* when FALSE check only, no messages */
5736 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737{
5738 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5739
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005740 if (!forceit && only_one_window()
5741 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 {
5743 if (message)
5744 {
5745#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5746 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5747 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005748 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005750 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5751 NGETTEXT("%d more file to edit. Quit anyway?",
5752 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5754 return OK;
5755 return FAIL;
5756 }
5757#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005758 semsg(NGETTEXT("E173: %d more file to edit",
5759 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 quitmore = 2; /* next try to quit is allowed */
5761 }
5762 return FAIL;
5763 }
5764 return OK;
5765}
5766
5767#ifdef FEAT_CMDL_COMPL
5768/*
5769 * Function given to ExpandGeneric() to obtain the list of command names.
5770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005772get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773{
5774 if (idx >= (int)CMD_SIZE)
5775# ifdef FEAT_USR_CMDS
5776 return get_user_command_name(idx);
5777# else
5778 return NULL;
5779# endif
5780 return cmdnames[idx].cmd_name;
5781}
5782#endif
5783
5784#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005786uc_add_command(
5787 char_u *name,
5788 size_t name_len,
5789 char_u *rep,
5790 long argt,
5791 long def,
5792 int flags,
5793 int compl,
5794 char_u *compl_arg,
5795 int addr_type,
5796 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797{
5798 ucmd_T *cmd = NULL;
5799 char_u *p;
5800 int i;
5801 int cmp = 1;
5802 char_u *rep_buf = NULL;
5803 garray_T *gap;
5804
Bram Moolenaar9c102382006-05-03 21:26:49 +00005805 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 if (rep_buf == NULL)
5807 {
5808 /* Can't replace termcodes - try using the string as is */
5809 rep_buf = vim_strsave(rep);
5810
5811 /* Give up if out of memory */
5812 if (rep_buf == NULL)
5813 return FAIL;
5814 }
5815
5816 /* get address of growarray: global or in curbuf */
5817 if (flags & UC_BUFFER)
5818 {
5819 gap = &curbuf->b_ucmds;
5820 if (gap->ga_itemsize == 0)
5821 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5822 }
5823 else
5824 gap = &ucmds;
5825
5826 /* Search for the command in the already defined commands. */
5827 for (i = 0; i < gap->ga_len; ++i)
5828 {
5829 size_t len;
5830
5831 cmd = USER_CMD_GA(gap, i);
5832 len = STRLEN(cmd->uc_name);
5833 cmp = STRNCMP(name, cmd->uc_name, name_len);
5834 if (cmp == 0)
5835 {
5836 if (name_len < len)
5837 cmp = -1;
5838 else if (name_len > len)
5839 cmp = 1;
5840 }
5841
5842 if (cmp == 0)
5843 {
Bram Moolenaar55d46912018-12-08 16:03:28 +01005844 // Command can be replaced with "command!" and when sourcing the
5845 // same script again, but only once.
5846 if (!force && (cmd->uc_script_ctx.sc_sid != current_sctx.sc_sid
5847 || cmd->uc_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005849 semsg(_("E174: Command already exists: add ! to replace it: %s"),
Bram Moolenaar55d46912018-12-08 16:03:28 +01005850 name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 goto fail;
5852 }
5853
Bram Moolenaard23a8232018-02-10 18:45:26 +01005854 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005855#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005856 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 break;
5859 }
5860
5861 /* Stop as soon as we pass the name to add */
5862 if (cmp < 0)
5863 break;
5864 }
5865
5866 /* Extend the array unless we're replacing an existing command */
5867 if (cmp != 0)
5868 {
5869 if (ga_grow(gap, 1) != OK)
5870 goto fail;
5871 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5872 goto fail;
5873
5874 cmd = USER_CMD_GA(gap, i);
5875 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5876
5877 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878
5879 cmd->uc_name = p;
5880 }
5881
5882 cmd->uc_rep = rep_buf;
5883 cmd->uc_argt = argt;
5884 cmd->uc_def = def;
5885 cmd->uc_compl = compl;
5886#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005887 cmd->uc_script_ctx = current_sctx;
5888 cmd->uc_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889# ifdef FEAT_CMDL_COMPL
5890 cmd->uc_compl_arg = compl_arg;
5891# endif
5892#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005893 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894
5895 return OK;
5896
5897fail:
5898 vim_free(rep_buf);
5899#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5900 vim_free(compl_arg);
5901#endif
5902 return FAIL;
5903}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005906#if defined(FEAT_USR_CMDS)
5907static struct
5908{
5909 int expand;
5910 char *name;
5911} addr_type_complete[] =
5912{
5913 {ADDR_ARGUMENTS, "arguments"},
5914 {ADDR_LINES, "lines"},
5915 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5916 {ADDR_TABS, "tabs"},
5917 {ADDR_BUFFERS, "buffers"},
5918 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005919 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaar51a74542018-12-02 18:21:49 +01005920 {ADDR_OTHER, "other"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005921 {-1, NULL}
5922};
5923#endif
5924
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005925#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926/*
5927 * List of names for completion for ":command" with the EXPAND_ flag.
5928 * Must be alphabetical for completion.
5929 */
5930static struct
5931{
5932 int expand;
5933 char *name;
5934} command_complete[] =
5935{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005936 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005938 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005940 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005942 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005943#if defined(FEAT_CSCOPE)
5944 {EXPAND_CSCOPE, "cscope"},
5945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5947 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005948 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949#endif
5950 {EXPAND_DIRECTORIES, "dir"},
5951 {EXPAND_ENV_VARS, "environment"},
5952 {EXPAND_EVENTS, "event"},
5953 {EXPAND_EXPRESSION, "expression"},
5954 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005955 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005956 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 {EXPAND_FUNCTIONS, "function"},
5958 {EXPAND_HELP, "help"},
5959 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005960#if defined(FEAT_CMDHIST)
5961 {EXPAND_HISTORY, "history"},
5962#endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005963#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005964 {EXPAND_LOCALES, "locale"},
5965#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005966 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 {EXPAND_MAPPINGS, "mapping"},
5968 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005969 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005970 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005971#if defined(FEAT_PROFILE)
5972 {EXPAND_SYNTIME, "syntime"},
5973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005975 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005976 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005977#if defined(FEAT_SIGNS)
5978 {EXPAND_SIGN, "sign"},
5979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 {EXPAND_TAGS, "tag"},
5981 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005982 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 {EXPAND_USER_VARS, "var"},
5984 {0, NULL}
5985};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005988#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005990uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991{
5992 int i, j;
5993 int found = FALSE;
5994 ucmd_T *cmd;
5995 int len;
5996 long a;
5997 garray_T *gap;
5998
5999 gap = &curbuf->b_ucmds;
6000 for (;;)
6001 {
6002 for (i = 0; i < gap->ga_len; ++i)
6003 {
6004 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006005 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006
Bram Moolenaard29459b2016-08-26 22:29:11 +02006007 /* Skip commands which don't match the requested prefix and
6008 * commands filtered out. */
6009 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6010 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 continue;
6012
6013 /* Put out the title first time */
6014 if (!found)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006015 msg_puts_title(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 found = TRUE;
6017 msg_putchar('\n');
6018 if (got_int)
6019 break;
6020
6021 /* Special cases */
6022 msg_putchar(a & BANG ? '!' : ' ');
6023 msg_putchar(a & REGSTR ? '"' : ' ');
6024 msg_putchar(gap != &ucmds ? 'b' : ' ');
6025 msg_putchar(' ');
6026
Bram Moolenaar8820b482017-03-16 17:23:31 +01006027 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 len = (int)STRLEN(cmd->uc_name) + 4;
6029
6030 do {
6031 msg_putchar(' ');
6032 ++len;
6033 } while (len < 16);
6034
6035 len = 0;
6036
6037 /* Arguments */
6038 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6039 {
6040 case 0: IObuff[len++] = '0'; break;
6041 case (EXTRA): IObuff[len++] = '*'; break;
6042 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6043 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6044 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6045 }
6046
6047 do {
6048 IObuff[len++] = ' ';
6049 } while (len < 5);
6050
6051 /* Range */
6052 if (a & (RANGE|COUNT))
6053 {
6054 if (a & COUNT)
6055 {
6056 /* -count=N */
6057 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6058 len += (int)STRLEN(IObuff + len);
6059 }
6060 else if (a & DFLALL)
6061 IObuff[len++] = '%';
6062 else if (cmd->uc_def >= 0)
6063 {
6064 /* -range=N */
6065 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6066 len += (int)STRLEN(IObuff + len);
6067 }
6068 else
6069 IObuff[len++] = '.';
6070 }
6071
6072 do {
6073 IObuff[len++] = ' ';
6074 } while (len < 11);
6075
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006076 /* Address Type */
6077 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6078 if (addr_type_complete[j].expand != ADDR_LINES
6079 && addr_type_complete[j].expand == cmd->uc_addr_type)
6080 {
6081 STRCPY(IObuff + len, addr_type_complete[j].name);
6082 len += (int)STRLEN(IObuff + len);
6083 break;
6084 }
6085
6086 do {
6087 IObuff[len++] = ' ';
6088 } while (len < 21);
6089
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 /* Completion */
6091 for (j = 0; command_complete[j].expand != 0; ++j)
6092 if (command_complete[j].expand == cmd->uc_compl)
6093 {
6094 STRCPY(IObuff + len, command_complete[j].name);
6095 len += (int)STRLEN(IObuff + len);
6096 break;
6097 }
6098
6099 do {
6100 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006101 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102
6103 IObuff[len] = '\0';
6104 msg_outtrans(IObuff);
6105
6106 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006107#ifdef FEAT_EVAL
6108 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006109 last_set_msg(cmd->uc_script_ctx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 out_flush();
6112 ui_breakcheck();
6113 if (got_int)
6114 break;
6115 }
6116 if (gap == &ucmds || i < gap->ga_len)
6117 break;
6118 gap = &ucmds;
6119 }
6120
6121 if (!found)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006122 msg(_("No user-defined commands found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123}
6124
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006125 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006126uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127{
6128 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6129 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6130 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6131 0xb9, 0x7f, 0};
6132 int i;
6133
6134 for (i = 0; fcmd[i]; ++i)
6135 IObuff[i] = fcmd[i] - 0x40;
6136 IObuff[i] = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006137 return (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138}
6139
6140 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006141uc_scan_attr(
6142 char_u *attr,
6143 size_t len,
6144 long *argt,
6145 long *def,
6146 int *flags,
6147 int *compl,
6148 char_u **compl_arg,
6149 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150{
6151 char_u *p;
6152
6153 if (len == 0)
6154 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006155 emsg(_("E175: No attribute specified"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 return FAIL;
6157 }
6158
6159 /* First, try the simple attributes (no arguments) */
6160 if (STRNICMP(attr, "bang", len) == 0)
6161 *argt |= BANG;
6162 else if (STRNICMP(attr, "buffer", len) == 0)
6163 *flags |= UC_BUFFER;
6164 else if (STRNICMP(attr, "register", len) == 0)
6165 *argt |= REGSTR;
6166 else if (STRNICMP(attr, "bar", len) == 0)
6167 *argt |= TRLBAR;
6168 else
6169 {
6170 int i;
6171 char_u *val = NULL;
6172 size_t vallen = 0;
6173 size_t attrlen = len;
6174
6175 /* Look for the attribute name - which is the part before any '=' */
6176 for (i = 0; i < (int)len; ++i)
6177 {
6178 if (attr[i] == '=')
6179 {
6180 val = &attr[i + 1];
6181 vallen = len - i - 1;
6182 attrlen = i;
6183 break;
6184 }
6185 }
6186
6187 if (STRNICMP(attr, "nargs", attrlen) == 0)
6188 {
6189 if (vallen == 1)
6190 {
6191 if (*val == '0')
6192 /* Do nothing - this is the default */;
6193 else if (*val == '1')
6194 *argt |= (EXTRA | NOSPC | NEEDARG);
6195 else if (*val == '*')
6196 *argt |= EXTRA;
6197 else if (*val == '?')
6198 *argt |= (EXTRA | NOSPC);
6199 else if (*val == '+')
6200 *argt |= (EXTRA | NEEDARG);
6201 else
6202 goto wrong_nargs;
6203 }
6204 else
6205 {
6206wrong_nargs:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006207 emsg(_("E176: Invalid number of arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 return FAIL;
6209 }
6210 }
6211 else if (STRNICMP(attr, "range", attrlen) == 0)
6212 {
6213 *argt |= RANGE;
6214 if (vallen == 1 && *val == '%')
6215 *argt |= DFLALL;
6216 else if (val != NULL)
6217 {
6218 p = val;
6219 if (*def >= 0)
6220 {
6221two_count:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006222 emsg(_("E177: Count cannot be specified twice"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 return FAIL;
6224 }
6225
6226 *def = getdigits(&p);
6227 *argt |= (ZEROR | NOTADR);
6228
6229 if (p != val + vallen || vallen == 0)
6230 {
6231invalid_count:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006232 emsg(_("E178: Invalid default value for count"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 return FAIL;
6234 }
6235 }
6236 }
6237 else if (STRNICMP(attr, "count", attrlen) == 0)
6238 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006239 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240
6241 if (val != NULL)
6242 {
6243 p = val;
6244 if (*def >= 0)
6245 goto two_count;
6246
6247 *def = getdigits(&p);
6248
6249 if (p != val + vallen)
6250 goto invalid_count;
6251 }
6252
6253 if (*def < 0)
6254 *def = 0;
6255 }
6256 else if (STRNICMP(attr, "complete", attrlen) == 0)
6257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 if (val == NULL)
6259 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006260 emsg(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 return FAIL;
6262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006264 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6265 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006268 else if (STRNICMP(attr, "addr", attrlen) == 0)
6269 {
6270 *argt |= RANGE;
6271 if (val == NULL)
6272 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006273 emsg(_("E179: argument required for -addr"));
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006274 return FAIL;
6275 }
6276 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6277 == FAIL)
6278 return FAIL;
6279 if (addr_type_arg != ADDR_LINES)
6280 *argt |= (ZEROR | NOTADR) ;
6281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 else
6283 {
6284 char_u ch = attr[len];
6285 attr[len] = '\0';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006286 semsg(_("E181: Invalid attribute: %s"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 attr[len] = ch;
6288 return FAIL;
6289 }
6290 }
6291
6292 return OK;
6293}
6294
Bram Moolenaara850a712009-01-28 14:42:59 +00006295/*
6296 * ":command ..."
6297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006299ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300{
6301 char_u *name;
6302 char_u *end;
6303 char_u *p;
6304 long argt = 0;
6305 long def = -1;
6306 int flags = 0;
6307 int compl = EXPAND_NOTHING;
6308 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006309 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006311 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312
6313 p = eap->arg;
6314
6315 /* Check for attributes */
6316 while (*p == '-')
6317 {
6318 ++p;
6319 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006320 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6321 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 == FAIL)
6323 return;
6324 p = skipwhite(end);
6325 }
6326
6327 /* Get the name (if any) and skip to the following argument */
6328 name = p;
6329 if (ASCII_ISALPHA(*p))
6330 while (ASCII_ISALNUM(*p))
6331 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006332 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006334 emsg(_("E182: Invalid command name"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 return;
6336 }
6337 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006338 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339
6340 /* If there is nothing after the name, and no attributes were specified,
6341 * we are listing commands
6342 */
6343 p = skipwhite(end);
6344 if (!has_attr && ends_excmd(*p))
6345 {
6346 uc_list(name, end - name);
6347 }
6348 else if (!ASCII_ISUPPER(*name))
6349 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006350 emsg(_("E183: User defined commands must start with an uppercase letter"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 return;
6352 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006353 else if ((name_len == 1 && *name == 'X')
6354 || (name_len <= 4
6355 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006357 emsg(_("E841: Reserved name, cannot be used for user defined command"));
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006358 return;
6359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 else
6361 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006362 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363}
6364
6365/*
6366 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 * Clear all user commands, global and for current buffer.
6368 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006369 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006370ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371{
6372 uc_clear(&ucmds);
6373 uc_clear(&curbuf->b_ucmds);
6374}
6375
6376/*
6377 * Clear all user commands for "gap".
6378 */
6379 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006380uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381{
6382 int i;
6383 ucmd_T *cmd;
6384
6385 for (i = 0; i < gap->ga_len; ++i)
6386 {
6387 cmd = USER_CMD_GA(gap, i);
6388 vim_free(cmd->uc_name);
6389 vim_free(cmd->uc_rep);
6390# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6391 vim_free(cmd->uc_compl_arg);
6392# endif
6393 }
6394 ga_clear(gap);
6395}
6396
6397 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006398ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399{
6400 int i = 0;
6401 ucmd_T *cmd = NULL;
6402 int cmp = -1;
6403 garray_T *gap;
6404
6405 gap = &curbuf->b_ucmds;
6406 for (;;)
6407 {
6408 for (i = 0; i < gap->ga_len; ++i)
6409 {
6410 cmd = USER_CMD_GA(gap, i);
6411 cmp = STRCMP(eap->arg, cmd->uc_name);
6412 if (cmp <= 0)
6413 break;
6414 }
6415 if (gap == &ucmds || cmp == 0)
6416 break;
6417 gap = &ucmds;
6418 }
6419
6420 if (cmp != 0)
6421 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006422 semsg(_("E184: No such user-defined command: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 return;
6424 }
6425
6426 vim_free(cmd->uc_name);
6427 vim_free(cmd->uc_rep);
6428# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6429 vim_free(cmd->uc_compl_arg);
6430# endif
6431
6432 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433
6434 if (i < gap->ga_len)
6435 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6436}
6437
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006438/*
6439 * split and quote args for <f-args>
6440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006442uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
6444 char_u *buf;
6445 char_u *p;
6446 char_u *q;
6447 int len;
6448
6449 /* Precalculate length */
6450 p = arg;
6451 len = 2; /* Initial and final quotes */
6452
6453 while (*p)
6454 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006455 if (p[0] == '\\' && p[1] == '\\')
6456 {
6457 len += 2;
6458 p += 2;
6459 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006460 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 {
6462 len += 1;
6463 p += 2;
6464 }
6465 else if (*p == '\\' || *p == '"')
6466 {
6467 len += 2;
6468 p += 1;
6469 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006470 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 {
6472 p = skipwhite(p);
6473 if (*p == NUL)
6474 break;
6475 len += 3; /* "," */
6476 }
6477 else
6478 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006479 int charlen = (*mb_ptr2len)(p);
Bram Moolenaar13505972019-01-24 15:04:48 +01006480
Bram Moolenaardfef1542012-07-10 19:25:10 +02006481 len += charlen;
6482 p += charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 }
6484 }
6485
6486 buf = alloc(len + 1);
6487 if (buf == NULL)
6488 {
6489 *lenp = 0;
6490 return buf;
6491 }
6492
6493 p = arg;
6494 q = buf;
6495 *q++ = '"';
6496 while (*p)
6497 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006498 if (p[0] == '\\' && p[1] == '\\')
6499 {
6500 *q++ = '\\';
6501 *q++ = '\\';
6502 p += 2;
6503 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006504 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 {
6506 *q++ = p[1];
6507 p += 2;
6508 }
6509 else if (*p == '\\' || *p == '"')
6510 {
6511 *q++ = '\\';
6512 *q++ = *p++;
6513 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006514 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 {
6516 p = skipwhite(p);
6517 if (*p == NUL)
6518 break;
6519 *q++ = '"';
6520 *q++ = ',';
6521 *q++ = '"';
6522 }
6523 else
6524 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006525 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 }
6527 }
6528 *q++ = '"';
6529 *q = 0;
6530
6531 *lenp = len;
6532 return buf;
6533}
6534
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006535 static size_t
6536add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6537{
6538 size_t result;
6539
6540 result = STRLEN(mod_str);
6541 if (*multi_mods)
6542 result += 1;
6543 if (buf != NULL)
6544 {
6545 if (*multi_mods)
6546 STRCAT(buf, " ");
6547 STRCAT(buf, mod_str);
6548 }
6549
6550 *multi_mods = 1;
6551
6552 return result;
6553}
6554
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555/*
6556 * Check for a <> code in a user command.
6557 * "code" points to the '<'. "len" the length of the <> (inclusive).
6558 * "buf" is where the result is to be added.
6559 * "split_buf" points to a buffer used for splitting, caller should free it.
6560 * "split_len" is the length of what "split_buf" contains.
6561 * Returns the length of the replacement, which has been added to "buf".
6562 * Returns -1 if there was no match, and only the "<" has been copied.
6563 */
6564 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006565uc_check_code(
6566 char_u *code,
6567 size_t len,
6568 char_u *buf,
6569 ucmd_T *cmd, /* the user command we're expanding */
6570 exarg_T *eap, /* ex arguments */
6571 char_u **split_buf,
6572 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573{
6574 size_t result = 0;
6575 char_u *p = code + 1;
6576 size_t l = len - 2;
6577 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006578 enum {
6579 ct_ARGS,
6580 ct_BANG,
6581 ct_COUNT,
6582 ct_LINE1,
6583 ct_LINE2,
6584 ct_RANGE,
6585 ct_MODS,
6586 ct_REGISTER,
6587 ct_LT,
6588 ct_NONE
6589 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590
6591 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6592 {
6593 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6594 p += 2;
6595 l -= 2;
6596 }
6597
Bram Moolenaar371d5402006-03-20 21:47:49 +00006598 ++l;
6599 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006601 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006603 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006605 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006607 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006609 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006611 else if (STRNICMP(p, "range>", l) == 0)
6612 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006613 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006615 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006617 else if (STRNICMP(p, "mods>", l) == 0)
6618 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619
6620 switch (type)
6621 {
6622 case ct_ARGS:
6623 /* Simple case first */
6624 if (*eap->arg == NUL)
6625 {
6626 if (quote == 1)
6627 {
6628 result = 2;
6629 if (buf != NULL)
6630 STRCPY(buf, "''");
6631 }
6632 else
6633 result = 0;
6634 break;
6635 }
6636
6637 /* When specified there is a single argument don't split it.
6638 * Works for ":Cmd %" when % is "a b c". */
6639 if ((eap->argt & NOSPC) && quote == 2)
6640 quote = 1;
6641
6642 switch (quote)
6643 {
6644 case 0: /* No quoting, no splitting */
6645 result = STRLEN(eap->arg);
6646 if (buf != NULL)
6647 STRCPY(buf, eap->arg);
6648 break;
6649 case 1: /* Quote, but don't split */
6650 result = STRLEN(eap->arg) + 2;
6651 for (p = eap->arg; *p; ++p)
6652 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006653 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6654 /* DBCS can contain \ in a trail byte, skip the
6655 * double-byte character. */
6656 ++p;
6657 else
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006658 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 ++result;
6660 }
6661
6662 if (buf != NULL)
6663 {
6664 *buf++ = '"';
6665 for (p = eap->arg; *p; ++p)
6666 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006667 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6668 /* DBCS can contain \ in a trail byte, copy the
6669 * double-byte character to avoid escaping. */
6670 *buf++ = *p++;
6671 else
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006672 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 *buf++ = '\\';
6674 *buf++ = *p;
6675 }
6676 *buf = '"';
6677 }
6678
6679 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006680 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 /* This is hard, so only do it once, and cache the result */
6682 if (*split_buf == NULL)
6683 *split_buf = uc_split_args(eap->arg, split_len);
6684
6685 result = *split_len;
6686 if (buf != NULL && result != 0)
6687 STRCPY(buf, *split_buf);
6688
6689 break;
6690 }
6691 break;
6692
6693 case ct_BANG:
6694 result = eap->forceit ? 1 : 0;
6695 if (quote)
6696 result += 2;
6697 if (buf != NULL)
6698 {
6699 if (quote)
6700 *buf++ = '"';
6701 if (eap->forceit)
6702 *buf++ = '!';
6703 if (quote)
6704 *buf = '"';
6705 }
6706 break;
6707
6708 case ct_LINE1:
6709 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006710 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 case ct_COUNT:
6712 {
6713 char num_buf[20];
6714 long num = (type == ct_LINE1) ? eap->line1 :
6715 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006716 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6718 size_t num_len;
6719
6720 sprintf(num_buf, "%ld", num);
6721 num_len = STRLEN(num_buf);
6722 result = num_len;
6723
6724 if (quote)
6725 result += 2;
6726
6727 if (buf != NULL)
6728 {
6729 if (quote)
6730 *buf++ = '"';
6731 STRCPY(buf, num_buf);
6732 buf += num_len;
6733 if (quote)
6734 *buf = '"';
6735 }
6736
6737 break;
6738 }
6739
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006740 case ct_MODS:
6741 {
6742 int multi_mods = 0;
6743 typedef struct {
6744 int *varp;
6745 char *name;
6746 } mod_entry_T;
6747 static mod_entry_T mod_entries[] = {
6748#ifdef FEAT_BROWSE_CMD
6749 {&cmdmod.browse, "browse"},
6750#endif
6751#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6752 {&cmdmod.confirm, "confirm"},
6753#endif
6754 {&cmdmod.hide, "hide"},
6755 {&cmdmod.keepalt, "keepalt"},
6756 {&cmdmod.keepjumps, "keepjumps"},
6757 {&cmdmod.keepmarks, "keepmarks"},
6758 {&cmdmod.keeppatterns, "keeppatterns"},
6759 {&cmdmod.lockmarks, "lockmarks"},
6760 {&cmdmod.noswapfile, "noswapfile"},
6761 {NULL, NULL}
6762 };
6763 int i;
6764
6765 result = quote ? 2 : 0;
6766 if (buf != NULL)
6767 {
6768 if (quote)
6769 *buf++ = '"';
6770 *buf = '\0';
6771 }
6772
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006773 /* :aboveleft and :leftabove */
6774 if (cmdmod.split & WSP_ABOVE)
6775 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6776 /* :belowright and :rightbelow */
6777 if (cmdmod.split & WSP_BELOW)
6778 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6779 /* :botright */
6780 if (cmdmod.split & WSP_BOT)
6781 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006782
6783 /* the modifiers that are simple flags */
6784 for (i = 0; mod_entries[i].varp != NULL; ++i)
6785 if (*mod_entries[i].varp)
6786 result += add_cmd_modifier(buf, mod_entries[i].name,
6787 &multi_mods);
6788
6789 /* TODO: How to support :noautocmd? */
6790#ifdef HAVE_SANDBOX
6791 /* TODO: How to support :sandbox?*/
6792#endif
6793 /* :silent */
6794 if (msg_silent > 0)
6795 result += add_cmd_modifier(buf,
6796 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006797 /* :tab */
6798 if (cmdmod.tab > 0)
6799 result += add_cmd_modifier(buf, "tab", &multi_mods);
6800 /* :topleft */
6801 if (cmdmod.split & WSP_TOP)
6802 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006803 /* TODO: How to support :unsilent?*/
6804 /* :verbose */
6805 if (p_verbose > 0)
6806 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006807 /* :vertical */
6808 if (cmdmod.split & WSP_VERT)
6809 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006810 if (quote && buf != NULL)
6811 {
6812 buf += result - 2;
6813 *buf = '"';
6814 }
6815 break;
6816 }
6817
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 case ct_REGISTER:
6819 result = eap->regname ? 1 : 0;
6820 if (quote)
6821 result += 2;
6822 if (buf != NULL)
6823 {
6824 if (quote)
6825 *buf++ = '\'';
6826 if (eap->regname)
6827 *buf++ = eap->regname;
6828 if (quote)
6829 *buf = '\'';
6830 }
6831 break;
6832
6833 case ct_LT:
6834 result = 1;
6835 if (buf != NULL)
6836 *buf = '<';
6837 break;
6838
6839 default:
6840 /* Not recognized: just copy the '<' and return -1. */
6841 result = (size_t)-1;
6842 if (buf != NULL)
6843 *buf = '<';
6844 break;
6845 }
6846
6847 return result;
6848}
6849
6850 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006851do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852{
6853 char_u *buf;
6854 char_u *p;
6855 char_u *q;
6856
6857 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006858 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006859 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 size_t len, totlen;
6861
6862 size_t split_len = 0;
6863 char_u *split_buf = NULL;
6864 ucmd_T *cmd;
6865#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006866 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867#endif
6868
6869 if (eap->cmdidx == CMD_USER)
6870 cmd = USER_CMD(eap->useridx);
6871 else
6872 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6873
6874 /*
6875 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006876 * First round: "buf" is NULL, compute length, allocate "buf".
6877 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 */
6879 buf = NULL;
6880 for (;;)
6881 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006882 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006883 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006885
6886 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006888 start = vim_strchr(p, '<');
6889 if (start != NULL)
6890 end = vim_strchr(start + 1, '>');
6891 if (buf != NULL)
6892 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006893 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6894 ;
6895 if (*ksp == K_SPECIAL
6896 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006897 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6898# ifdef FEAT_GUI
6899 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6900# endif
6901 ))
6902 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006903 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006904 * KS_SPECIAL KE_FILLER, like for mappings, but
6905 * do_cmdline() doesn't handle that, so convert it back.
6906 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6907 len = ksp - p;
6908 if (len > 0)
6909 {
6910 mch_memmove(q, p, len);
6911 q += len;
6912 }
6913 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6914 p = ksp + 3;
6915 continue;
6916 }
6917 }
6918
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006919 /* break if no <item> is found */
Bram Moolenaara850a712009-01-28 14:42:59 +00006920 if (start == NULL || end == NULL)
6921 break;
6922
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 /* Include the '>' */
6924 ++end;
6925
6926 /* Take everything up to the '<' */
6927 len = start - p;
6928 if (buf == NULL)
6929 totlen += len;
6930 else
6931 {
6932 mch_memmove(q, p, len);
6933 q += len;
6934 }
6935
6936 len = uc_check_code(start, end - start, q, cmd, eap,
6937 &split_buf, &split_len);
6938 if (len == (size_t)-1)
6939 {
6940 /* no match, continue after '<' */
6941 p = start + 1;
6942 len = 1;
6943 }
6944 else
6945 p = end;
6946 if (buf == NULL)
6947 totlen += len;
6948 else
6949 q += len;
6950 }
6951 if (buf != NULL) /* second time here, finished */
6952 {
6953 STRCPY(q, p);
6954 break;
6955 }
6956
6957 totlen += STRLEN(p); /* Add on the trailing characters */
6958 buf = alloc((unsigned)(totlen + 1));
6959 if (buf == NULL)
6960 {
6961 vim_free(split_buf);
6962 return;
6963 }
6964 }
6965
6966#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006967 current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968#endif
6969 (void)do_cmdline(buf, eap->getline, eap->cookie,
6970 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6971#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006972 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973#endif
6974 vim_free(buf);
6975 vim_free(split_buf);
6976}
6977
6978# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6979 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006980get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981{
6982 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6983}
6984
6985/*
6986 * Function given to ExpandGeneric() to obtain the list of user command names.
6987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006989get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990{
6991 if (idx < curbuf->b_ucmds.ga_len)
6992 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6993 idx -= curbuf->b_ucmds.ga_len;
6994 if (idx < ucmds.ga_len)
6995 return USER_CMD(idx)->uc_name;
6996 return NULL;
6997}
6998
6999/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007000 * Function given to ExpandGeneric() to obtain the list of user address type names.
7001 */
7002 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007003get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007004{
7005 return (char_u *)addr_type_complete[idx].name;
7006}
7007
7008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 * Function given to ExpandGeneric() to obtain the list of user command
7010 * attributes.
7011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007013get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014{
7015 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007016 {"addr", "bang", "bar", "buffer", "complete",
7017 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007019 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 return NULL;
7021 return (char_u *)user_cmd_flags[idx];
7022}
7023
7024/*
7025 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007028get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029{
7030 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7031
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007032 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 return NULL;
7034 return (char_u *)user_cmd_nargs[idx];
7035}
7036
7037/*
7038 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007041get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
7043 return (char_u *)command_complete[idx].name;
7044}
7045# endif /* FEAT_CMDL_COMPL */
7046
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007047/*
7048 * Parse address type argument
7049 */
7050 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007051parse_addr_type_arg(
7052 char_u *value,
7053 int vallen,
7054 long *argt,
7055 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007056{
7057 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007058
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007059 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7060 {
7061 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7062 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7063 if (a && b)
7064 {
7065 *addr_type_arg = addr_type_complete[i].expand;
7066 break;
7067 }
7068 }
7069
7070 if (addr_type_complete[i].expand == -1)
7071 {
7072 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007073
Bram Moolenaar1c465442017-03-12 20:10:05 +01007074 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007075 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007076 err[i] = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007077 semsg(_("E180: Invalid address type value: %s"), err);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007078 return FAIL;
7079 }
7080
7081 if (*addr_type_arg != ADDR_LINES)
7082 *argt |= NOTADR;
7083
7084 return OK;
7085}
7086
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087#endif /* FEAT_USR_CMDS */
7088
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007089#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7090/*
7091 * Parse a completion argument "value[vallen]".
7092 * The detected completion goes in "*complp", argument type in "*argt".
7093 * When there is an argument, for function and user defined completion, it's
7094 * copied to allocated memory and stored in "*compl_arg".
7095 * Returns FAIL if something is wrong.
7096 */
7097 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007098parse_compl_arg(
7099 char_u *value,
7100 int vallen,
7101 int *complp,
7102 long *argt,
7103 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007104{
7105 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007106# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007107 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007108# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007109 int i;
7110 int valend = vallen;
7111
7112 /* Look for any argument part - which is the part after any ',' */
7113 for (i = 0; i < vallen; ++i)
7114 {
7115 if (value[i] == ',')
7116 {
7117 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007118# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007119 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007120# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007121 valend = i;
7122 break;
7123 }
7124 }
7125
7126 for (i = 0; command_complete[i].expand != 0; ++i)
7127 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007128 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007129 && STRNCMP(value, command_complete[i].name, valend) == 0)
7130 {
7131 *complp = command_complete[i].expand;
7132 if (command_complete[i].expand == EXPAND_BUFFERS)
7133 *argt |= BUFNAME;
7134 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7135 || command_complete[i].expand == EXPAND_FILES)
7136 *argt |= XFILE;
7137 break;
7138 }
7139 }
7140
7141 if (command_complete[i].expand == 0)
7142 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007143 semsg(_("E180: Invalid complete value: %s"), value);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007144 return FAIL;
7145 }
7146
7147# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7148 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7149 && arg != NULL)
7150# else
7151 if (arg != NULL)
7152# endif
7153 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007154 emsg(_("E468: Completion argument only allowed for custom completion"));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007155 return FAIL;
7156 }
7157
7158# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7159 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7160 && arg == NULL)
7161 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007162 emsg(_("E467: Custom completion requires a function argument"));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007163 return FAIL;
7164 }
7165
7166 if (arg != NULL)
7167 *compl_arg = vim_strnsave(arg, (int)arglen);
7168# endif
7169 return OK;
7170}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007171
7172 int
7173cmdcomplete_str_to_type(char_u *complete_str)
7174{
7175 int i;
7176
7177 for (i = 0; command_complete[i].expand != 0; ++i)
7178 if (STRCMP(complete_str, command_complete[i].name) == 0)
7179 return command_complete[i].expand;
7180
7181 return EXPAND_NOTHING;
7182}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007183#endif
7184
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007186ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187{
Bram Moolenaare6850792010-05-14 15:28:44 +02007188 if (*eap->arg == NUL)
7189 {
7190#ifdef FEAT_EVAL
7191 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7192 char_u *p = NULL;
7193
7194 if (expr != NULL)
7195 {
7196 ++emsg_off;
7197 p = eval_to_string(expr, NULL, FALSE);
7198 --emsg_off;
7199 vim_free(expr);
7200 }
7201 if (p != NULL)
7202 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007203 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02007204 vim_free(p);
7205 }
7206 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007207 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02007208#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007209 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02007210#endif
7211 }
7212 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007213 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214}
7215
7216 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007217ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218{
7219 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01007220 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 do_highlight(eap->arg, eap->forceit, FALSE);
7222}
7223
7224
7225/*
7226 * Call this function if we thought we were going to exit, but we won't
7227 * (because of an error). May need to restore the terminal mode.
7228 */
7229 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007230not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231{
7232 exiting = FALSE;
7233 settmode(TMODE_RAW);
7234}
7235
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007236 static int
7237before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7238{
7239 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7240
7241 /* Bail out when autocommands closed the window.
7242 * Refuse to quit when the buffer in the last window is being closed (can
7243 * only happen in autocommands). */
7244 if (!win_valid(wp)
7245 || curbuf_locked()
7246 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7247 return TRUE;
7248
7249 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7250 {
7251 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7252 /* Refuse to quit when locked or when the buffer in the last window is
7253 * being closed (can only happen in autocommands). */
7254 if (curbuf_locked()
7255 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7256 return TRUE;
7257 }
7258
7259 return FALSE;
7260}
7261
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007263 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007264 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 */
7266 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007267ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007269 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007270
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271#ifdef FEAT_CMDWIN
7272 if (cmdwin_type != 0)
7273 {
7274 cmdwin_result = Ctrl_C;
7275 return;
7276 }
7277#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007278 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007279 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007280 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007281 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007282 return;
7283 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007284 if (eap->addr_count > 0)
7285 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007286 int wnr = eap->line2;
7287
7288 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7289 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007290 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007291 }
7292 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007293 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007294
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007295 /* Refuse to quit when locked. */
7296 if (curbuf_locked())
7297 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007298
7299 /* Trigger QuitPre and maybe ExitPre */
7300 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007301 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302
7303#ifdef FEAT_NETBEANS_INTG
7304 netbeansForcedQuit = eap->forceit;
7305#endif
7306
7307 /*
7308 * If there are more files or windows we won't exit.
7309 */
7310 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7311 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007312 if ((!buf_hide(wp->w_buffer)
7313 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007314 | (eap->forceit ? CCGD_FORCEIT : 0)
7315 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007317 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 {
7319 not_exiting();
7320 }
7321 else
7322 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007323 /* quit last window
7324 * Note: only_one_window() returns true, even so a help window is
7325 * still open. In that case only quit, if no address has been
7326 * specified. Example:
7327 * :h|wincmd w|1q - don't quit
7328 * :h|wincmd w|q - quit
7329 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007330 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007332 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007333#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007337 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 }
7339}
7340
7341/*
7342 * ":cquit".
7343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007345ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346{
7347 getout(1); /* this does not always pass on the exit code to the Manx
7348 compiler. why? */
7349}
7350
7351/*
7352 * ":qall": try to quit all windows
7353 */
7354 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007355ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356{
7357# ifdef FEAT_CMDWIN
7358 if (cmdwin_type != 0)
7359 {
7360 if (eap->forceit)
7361 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7362 else
7363 cmdwin_result = K_XF2;
7364 return;
7365 }
7366# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007367
7368 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007369 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007370 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007371 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007372 return;
7373 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007374
7375 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007376 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007377
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007379 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 getout(0);
7381 not_exiting();
7382}
7383
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384/*
7385 * ":close": close current window, unless it is the last one
7386 */
7387 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007388ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007390 win_T *win;
7391 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007392#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007394 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007396#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007397 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007398 {
7399 if (eap->addr_count == 0)
7400 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02007401 else
7402 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007403 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007404 {
7405 winnr++;
7406 if (winnr == eap->line2)
7407 break;
7408 }
7409 if (win == NULL)
7410 win = lastwin;
7411 ex_win_close(eap->forceit, win, NULL);
7412 }
7413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414}
7415
Bram Moolenaar4033c552017-09-16 20:54:51 +02007416#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007417/*
7418 * ":pclose": Close any preview window.
7419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007421ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007422{
7423 win_T *win;
7424
Bram Moolenaar29323592016-07-24 22:04:11 +02007425 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007426 if (win->w_p_pvw)
7427 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007428 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007429 break;
7430 }
7431}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007432#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007433
Bram Moolenaarf740b292006-02-16 22:11:02 +00007434/*
7435 * Close window "win" and take care of handling closing the last window for a
7436 * modified buffer.
7437 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007439ex_win_close(
7440 int forceit,
7441 win_T *win,
7442 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443{
7444 int need_hide;
7445 buf_T *buf = win->w_buffer;
7446
7447 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007448 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007450#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 if ((p_confirm || cmdmod.confirm) && p_write)
7452 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007453 bufref_T bufref;
7454
7455 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007457 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 return;
7459 need_hide = FALSE;
7460 }
7461 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007464 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 return;
7466 }
7467 }
7468
Bram Moolenaar4033c552017-09-16 20:54:51 +02007469#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007471#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007472
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007474 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007475 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007476 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007477 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478}
7479
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007481 * Handle the argument for a tabpage related ex command.
7482 * Returns a tabpage number.
7483 * When an error is encountered then eap->errmsg is set.
7484 */
7485 static int
7486get_tabpage_arg(exarg_T *eap)
7487{
7488 int tab_number;
7489 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7490
7491 if (eap->arg && *eap->arg != NUL)
7492 {
7493 char_u *p = eap->arg;
7494 char_u *p_save;
7495 int relative = 0; /* argument +N/-N means: go to N places to the
7496 * right/left relative to the current position. */
7497
7498 if (*p == '-')
7499 {
7500 relative = -1;
7501 p++;
7502 }
7503 else if (*p == '+')
7504 {
7505 relative = 1;
7506 p++;
7507 }
7508
7509 p_save = p;
7510 tab_number = getdigits(&p);
7511
7512 if (relative == 0)
7513 {
7514 if (STRCMP(p, "$") == 0)
7515 tab_number = LAST_TAB_NR;
7516 else if (p == p_save || *p_save == '-' || *p != NUL
7517 || tab_number > LAST_TAB_NR)
7518 {
7519 /* No numbers as argument. */
7520 eap->errmsg = e_invarg;
7521 goto theend;
7522 }
7523 }
7524 else
7525 {
7526 if (*p_save == NUL)
7527 tab_number = 1;
7528 else if (p == p_save || *p_save == '-' || *p != NUL
7529 || tab_number == 0)
7530 {
7531 /* No numbers as argument. */
7532 eap->errmsg = e_invarg;
7533 goto theend;
7534 }
7535 tab_number = tab_number * relative + tabpage_index(curtab);
7536 if (!unaccept_arg0 && relative == -1)
7537 --tab_number;
7538 }
7539 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7540 eap->errmsg = e_invarg;
7541 }
7542 else if (eap->addr_count > 0)
7543 {
7544 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007545 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007546 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007547 tab_number = 0;
7548 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007549 else
7550 {
7551 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01007552 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01007553 {
7554 --tab_number;
7555 if (tab_number < unaccept_arg0)
7556 eap->errmsg = e_invarg;
7557 }
7558 }
7559 }
7560 else
7561 {
7562 switch (eap->cmdidx)
7563 {
7564 case CMD_tabnext:
7565 tab_number = tabpage_index(curtab) + 1;
7566 if (tab_number > LAST_TAB_NR)
7567 tab_number = 1;
7568 break;
7569 case CMD_tabmove:
7570 tab_number = LAST_TAB_NR;
7571 break;
7572 default:
7573 tab_number = tabpage_index(curtab);
7574 }
7575 }
7576
7577theend:
7578 return tab_number;
7579}
7580
7581/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007582 * ":tabclose": close current tab page, unless it is the last one.
7583 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 */
7585 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007586ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007588 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007589 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007590
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007591# ifdef FEAT_CMDWIN
7592 if (cmdwin_type != 0)
7593 cmdwin_result = K_IGNORE;
7594 else
7595# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007596 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007597 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007598 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007600 tab_number = get_tabpage_arg(eap);
7601 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007602 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007603 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007604 if (tp == NULL)
7605 {
7606 beep_flush();
7607 return;
7608 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007609 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007610 {
7611 tabpage_close_other(tp, eap->forceit);
7612 return;
7613 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007614 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007615 tabpage_close(eap->forceit);
7616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007618}
7619
7620/*
7621 * ":tabonly": close all tab pages except the current one
7622 */
7623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007624ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007625{
7626 tabpage_T *tp;
7627 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007628 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007629
7630# ifdef FEAT_CMDWIN
7631 if (cmdwin_type != 0)
7632 cmdwin_result = K_IGNORE;
7633 else
7634# endif
7635 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007636 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007637 else
7638 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007639 tab_number = get_tabpage_arg(eap);
7640 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007641 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007642 goto_tabpage(tab_number);
7643 /* Repeat this up to a 1000 times, because autocommands may
7644 * mess up the lists. */
7645 for (done = 0; done < 1000; ++done)
7646 {
7647 FOR_ALL_TABPAGES(tp)
7648 if (tp->tp_topframe != topframe)
7649 {
7650 tabpage_close_other(tp, eap->forceit);
7651 /* if we failed to close it quit */
7652 if (valid_tabpage(tp))
7653 done = 1000;
7654 /* start over, "tp" is now invalid */
7655 break;
7656 }
7657 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007658 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007659 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007660 }
7661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663
7664/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007665 * Close the current tab page.
7666 */
7667 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007668tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007669{
7670 /* First close all the windows but the current one. If that worked then
7671 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007672 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007673 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007674 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007675 ex_win_close(forceit, curwin, NULL);
7676# ifdef FEAT_GUI
7677 need_mouse_correct = TRUE;
7678# endif
7679}
7680
7681/*
7682 * Close tab page "tp", which is not the current tab page.
7683 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007684 * Also takes care of the tab pages line disappearing when closing the
7685 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007686 */
7687 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007688tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007689{
7690 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007691 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007692 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007693
7694 /* Limit to 1000 windows, autocommands may add a window while we close
7695 * one. OK, so I'm paranoid... */
7696 while (++done < 1000)
7697 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007698 wp = tp->tp_firstwin;
7699 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007700
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007701 /* Autocommands may delete the tab page under our fingers and we may
7702 * fail to close a window with a modified buffer. */
7703 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007704 break;
7705 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007706
Bram Moolenaar29323592016-07-24 22:04:11 +02007707 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007708
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007709 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007710 if (h != tabline_height())
7711 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007712}
7713
7714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 * ":only".
7716 */
7717 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007718ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007720 win_T *wp;
7721 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722# ifdef FEAT_GUI
7723 need_mouse_correct = TRUE;
7724# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007725 if (eap->addr_count > 0)
7726 {
7727 wnr = eap->line2;
7728 for (wp = firstwin; --wnr > 0; )
7729 {
7730 if (wp->w_next == NULL)
7731 break;
7732 else
7733 wp = wp->w_next;
7734 }
7735 win_goto(wp);
7736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 close_others(TRUE, eap->forceit);
7738}
7739
7740/*
7741 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007742 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007744 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007745ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746{
7747 if (eap->addr_count == 0)
7748 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007749 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751
7752 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007753ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007755 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007756 if (!eap->skip)
7757 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007758#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007759 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007760#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007761 if (eap->addr_count == 0)
7762 win_close(curwin, FALSE); /* don't free buffer */
7763 else
7764 {
7765 int winnr = 0;
7766 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007767
Bram Moolenaar2256c992016-11-15 21:17:07 +01007768 FOR_ALL_WINDOWS(win)
7769 {
7770 winnr++;
7771 if (winnr == eap->line2)
7772 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007773 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007774 if (win == NULL)
7775 win = lastwin;
7776 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 }
7779}
7780
7781/*
7782 * ":stop" and ":suspend": Suspend Vim.
7783 */
7784 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007785ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786{
7787 /*
7788 * Disallow suspending for "rvim".
7789 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007790 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 {
7792 if (!eap->forceit)
7793 autowrite_all();
7794 windgoto((int)Rows - 1, 0);
7795 out_char('\n');
7796 out_flush();
7797 stoptermcap();
7798 out_flush(); /* needed for SUN to restore xterm buffer */
7799#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02007800 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801#endif
7802 ui_suspend(); /* call machine specific function */
7803#ifdef FEAT_TITLE
7804 maketitle();
7805 resettitle(); /* force updating the title */
7806#endif
7807 starttermcap();
7808 scroll_start(); /* scroll screen before redrawing */
7809 redraw_later_clear();
7810 shell_resized(); /* may have resized window */
7811 }
7812}
7813
7814/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007815 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 */
7817 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007818ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819{
7820#ifdef FEAT_CMDWIN
7821 if (cmdwin_type != 0)
7822 {
7823 cmdwin_result = Ctrl_C;
7824 return;
7825 }
7826#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007827 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007828 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007829 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007830 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007831 return;
7832 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007833
7834 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007835 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836
7837 /*
7838 * if more files or windows we won't exit
7839 */
7840 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7841 exiting = TRUE;
7842 if ( ((eap->cmdidx == CMD_wq
7843 || curbufIsChanged())
7844 && do_write(eap) == FAIL)
7845 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007846 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {
7848 not_exiting();
7849 }
7850 else
7851 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007854 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855# ifdef FEAT_GUI
7856 need_mouse_correct = TRUE;
7857# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007858 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007859 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 }
7861}
7862
7863/*
7864 * ":print", ":list", ":number".
7865 */
7866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007867ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007869 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007870 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007871 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007873 for ( ;!got_int; ui_breakcheck())
7874 {
7875 print_line(eap->line1,
7876 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7877 || (eap->flags & EXFLAG_NR)),
7878 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7879 if (++eap->line1 > eap->line2)
7880 break;
7881 out_flush(); /* show one line at a time */
7882 }
7883 setpcmark();
7884 /* put cursor at last line */
7885 curwin->w_cursor.lnum = eap->line2;
7886 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 }
7888
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890}
7891
7892#ifdef FEAT_BYTEOFF
7893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007894ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895{
7896 goto_byte(eap->line2);
7897}
7898#endif
7899
7900/*
7901 * ":shell".
7902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007904ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905{
7906 do_shell(NULL, 0);
7907}
7908
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007909#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007911static int drop_busy = FALSE;
7912static int drop_filec;
7913static char_u **drop_filev = NULL;
7914static int drop_split;
7915static void (*drop_callback)(void *);
7916static void *drop_cookie;
7917
7918 static void
7919handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920{
7921 exarg_T ea;
7922 int save_msg_scroll = msg_scroll;
7923
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007924 // Setting the argument list may cause screen updates and being called
7925 // recursively. Avoid that by setting drop_busy.
7926 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927
7928 /* Check whether the current buffer is changed. If so, we will need
7929 * to split the current window or data could be lost.
7930 * We don't need to check if the 'hidden' option is set, as in this
7931 * case the buffer won't be lost.
7932 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007933 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 {
7935 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007936 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 --emsg_off;
7938 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007939 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 if (win_split(0, 0) == FAIL)
7942 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007943 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944
7945 /* When splitting the window, create a new alist. Otherwise the
7946 * existing one is overwritten. */
7947 alist_unlink(curwin->w_alist);
7948 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 }
7950
7951 /*
7952 * Set up the new argument list.
7953 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007954 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955
7956 /*
7957 * Move to the first file.
7958 */
7959 /* Fake up a minimal "next" command for do_argfile() */
7960 vim_memset(&ea, 0, sizeof(ea));
7961 ea.cmd = (char_u *)"next";
7962 do_argfile(&ea, 0);
7963
7964 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7965 * mode that is not needed here. */
7966 need_start_insertmode = FALSE;
7967
7968 /* Restore msg_scroll, otherwise a following command may cause scrolling
7969 * unexpectedly. The screen will be redrawn by the caller, thus
7970 * msg_scroll being set by displaying a message is irrelevant. */
7971 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007972
7973 if (drop_callback != NULL)
7974 drop_callback(drop_cookie);
7975
7976 drop_filev = NULL;
7977 drop_busy = FALSE;
7978}
7979
7980/*
7981 * Handle a file drop. The code is here because a drop is *nearly* like an
7982 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01007983 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007984 * code is very similar to :args and hence needs access to a lot of the static
7985 * functions in this file.
7986 *
7987 * The "filev" list must have been allocated using alloc(), as should each item
7988 * in the list. This function takes over responsibility for freeing the "filev"
7989 * list.
7990 */
7991 void
7992handle_drop(
7993 int filec, // the number of files dropped
7994 char_u **filev, // the list of files dropped
7995 int split, // force splitting the window
7996 void (*callback)(void *), // to be called after setting the argument
7997 // list
7998 void *cookie) // argument for "callback" (allocated)
7999{
8000 // Cannot handle recursive drops, finish the pending one.
8001 if (drop_busy)
8002 {
8003 FreeWild(filec, filev);
8004 vim_free(cookie);
8005 return;
8006 }
8007
8008 // When calling handle_drop() more than once in a row we only use the last
8009 // one.
8010 if (drop_filev != NULL)
8011 {
8012 FreeWild(drop_filec, drop_filev);
8013 vim_free(drop_cookie);
8014 }
8015
8016 drop_filec = filec;
8017 drop_filev = filev;
8018 drop_split = split;
8019 drop_callback = callback;
8020 drop_cookie = cookie;
8021
8022 // Postpone this when:
8023 // - editing the command line
8024 // - not possible to change the current buffer
8025 // - updating the screen
8026 // As it may change buffers and window structures that are in use and cause
8027 // freed memory to be used.
8028 if (text_locked() || curbuf_locked() || updating_screen)
8029 return;
8030
8031 handle_drop_internal();
8032}
8033
8034/*
8035 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
8036 * reset: Handle a postponed drop.
8037 */
8038 void
8039handle_any_postponed_drop(void)
8040{
8041 if (!drop_busy && drop_filev != NULL
8042 && !text_locked() && !curbuf_locked() && !updating_screen)
8043 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044}
8045#endif
8046
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047/*
8048 * Clear an argument list: free all file names and reset it to zero entries.
8049 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008050 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008051alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052{
8053 while (--al->al_ga.ga_len >= 0)
8054 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8055 ga_clear(&al->al_ga);
8056}
8057
8058/*
8059 * Init an argument list.
8060 */
8061 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008062alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063{
8064 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8065}
8066
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067/*
8068 * Remove a reference from an argument list.
8069 * Ignored when the argument list is the global one.
8070 * If the argument list is no longer used by any window, free it.
8071 */
8072 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008073alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074{
8075 if (al != &global_alist && --al->al_refcount <= 0)
8076 {
8077 alist_clear(al);
8078 vim_free(al);
8079 }
8080}
8081
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082/*
8083 * Create a new argument list and use it for the current window.
8084 */
8085 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008086alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087{
8088 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8089 if (curwin->w_alist == NULL)
8090 {
8091 curwin->w_alist = &global_alist;
8092 ++global_alist.al_refcount;
8093 }
8094 else
8095 {
8096 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008097 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 alist_init(curwin->w_alist);
8099 }
8100}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008102#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103/*
8104 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008105 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8106 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 */
8108 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008109alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110{
8111 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008112 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 char_u **new_arg_files;
8114 int new_arg_file_count;
8115 char_u *save_p_su = p_su;
8116 int i;
8117
8118 /* Don't use 'suffixes' here. This should work like the shell did the
8119 * expansion. Also, the vimrc file isn't read yet, thus the user
8120 * can't set the options. */
8121 p_su = empty_option;
8122 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8123 if (old_arg_files != NULL)
8124 {
8125 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008126 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8127 old_arg_count = GARGCOUNT;
8128 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008130 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 && new_arg_file_count > 0)
8132 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008133 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8134 TRUE, fnum_list, fnum_len);
8135 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 }
8138 p_su = save_p_su;
8139}
8140#endif
8141
8142/*
8143 * Set the argument list for the current window.
8144 * Takes over the allocated files[] and the allocated fnames in it.
8145 */
8146 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008147alist_set(
8148 alist_T *al,
8149 int count,
8150 char_u **files,
8151 int use_curbuf,
8152 int *fnum_list,
8153 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154{
8155 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008156 static int recursive = 0;
8157
8158 if (recursive)
8159 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008160 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008161 return;
8162 }
8163 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164
8165 alist_clear(al);
8166 if (ga_grow(&al->al_ga, count) == OK)
8167 {
8168 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008169 {
8170 if (got_int)
8171 {
8172 /* When adding many buffers this can take a long time. Allow
8173 * interrupting here. */
8174 while (i < count)
8175 vim_free(files[i++]);
8176 break;
8177 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008178
8179 /* May set buffer name of a buffer previously used for the
8180 * argument list, so that it's re-used by alist_add. */
8181 if (fnum_list != NULL && i < fnum_len)
8182 buf_set_name(fnum_list[i], files[i]);
8183
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008185 ui_breakcheck();
8186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 vim_free(files);
8188 }
8189 else
8190 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008193
8194 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195}
8196
8197/*
8198 * Add file "fname" to argument list "al".
8199 * "fname" must have been allocated and "al" must have been checked for room.
8200 */
8201 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008202alist_add(
8203 alist_T *al,
8204 char_u *fname,
8205 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206{
8207 if (fname == NULL) /* don't add NULL file names */
8208 return;
8209#ifdef BACKSLASH_IN_FILENAME
8210 slash_adjust(fname);
8211#endif
8212 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8213 if (set_fnum > 0)
8214 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8215 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8216 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217}
8218
8219#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8220/*
8221 * Adjust slashes in file names. Called after 'shellslash' was set.
8222 */
8223 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008224alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225{
8226 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008228 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229
8230 for (i = 0; i < GARGCOUNT; ++i)
8231 if (GARGLIST[i].ae_fname != NULL)
8232 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008233 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 if (wp->w_alist != &global_alist)
8235 for (i = 0; i < WARGCOUNT(wp); ++i)
8236 if (WARGLIST(wp)[i].ae_fname != NULL)
8237 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238}
8239#endif
8240
8241/*
8242 * ":preserve".
8243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008245ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008247 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 ml_preserve(curbuf, TRUE);
8249}
8250
8251/*
8252 * ":recover".
8253 */
8254 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008255ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256{
8257 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8258 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008259 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8260 | CCGD_MULTWIN
8261 | (eap->forceit ? CCGD_FORCEIT : 0)
8262 | CCGD_EXCMD)
8263
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 && (*eap->arg == NUL
8265 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8266 ml_recover();
8267 recoverymode = FALSE;
8268}
8269
8270/*
8271 * Command modifier used in a wrong way.
8272 */
8273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008274ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275{
8276 eap->errmsg = e_invcmd;
8277}
8278
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279/*
8280 * :sview [+command] file split window with new file, read-only
8281 * :split [[+command] file] split window with current or new file
8282 * :vsplit [[+command] file] split window vertically with current or new file
8283 * :new [[+command] file] split window with no or new file
8284 * :vnew [[+command] file] split vertically window with no or new file
8285 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008286 *
8287 * :tabedit open new Tab page with empty window
8288 * :tabedit [+command] file open new Tab page and edit "file"
8289 * :tabnew [[+command] file] just like :tabedit
8290 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 */
8292 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008293ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008295 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008296#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008298#endif
8299#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008301#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02008302 int use_tab = eap->cmdidx == CMD_tabedit
8303 || eap->cmdidx == CMD_tabfind
8304 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305
Bram Moolenaar4033c552017-09-16 20:54:51 +02008306#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309
Bram Moolenaar4033c552017-09-16 20:54:51 +02008310#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008312 * quickfix windows. But it's OK when doing ":tab split". */
8313 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 {
8315 if (eap->cmdidx == CMD_split)
8316 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 if (eap->cmdidx == CMD_vsplit)
8318 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321
Bram Moolenaar4033c552017-09-16 20:54:51 +02008322#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008323 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 {
8325 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8326 FNAME_MESS, TRUE, curbuf->b_ffname);
8327 if (fname == NULL)
8328 goto theend;
8329 eap->arg = fname;
8330 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008331# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008332 else
8333# endif
8334#endif
8335#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 && eap->cmdidx != CMD_new)
8339 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008340 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008341# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008342 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008343# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008344 au_has_group((char_u *)"FileExplorer"))
8345 {
8346 /* No browsing supported but we do have the file explorer:
8347 * Edit the directory. */
8348 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8349 eap->arg = (char_u *)".";
8350 }
8351 else
8352 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02008353 fname = do_browse(0, (char_u *)(use_tab
8354 ? _("Edit File in new tab page")
8355 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008357 if (fname == NULL)
8358 goto theend;
8359 eap->arg = fname;
8360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 }
8362 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008365 /*
8366 * Either open new tab page or split the window.
8367 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02008368 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008369 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008370 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8371 : eap->addr_count == 0 ? 0
8372 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008373 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008374 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008375
8376 /* set the alternate buffer for the window we came from */
8377 if (curwin != old_curwin
8378 && win_valid(old_curwin)
8379 && old_curwin->w_buffer != curbuf
8380 && !cmdmod.keepalt)
8381 old_curwin->w_alt_fnum = curbuf->b_fnum;
8382 }
8383 }
8384 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8386 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 /* Reset 'scrollbind' when editing another file, but keep it when
8388 * doing ":split" without arguments. */
8389 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008390# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008392# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008394 {
8395 RESET_BINDING(curwin);
8396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 else
8398 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 do_exedit(eap, old_curwin);
8400 }
8401
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008402# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008404# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008406# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407theend:
8408 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008409# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008411
8412/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008413 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008414 */
8415 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008416tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008417{
8418 exarg_T ea;
8419
8420 vim_memset(&ea, 0, sizeof(ea));
8421 ea.cmdidx = CMD_tabnew;
8422 ea.cmd = (char_u *)"tabn";
8423 ea.arg = (char_u *)"";
8424 ex_splitview(&ea);
8425}
8426
8427/*
8428 * :tabnext command
8429 */
8430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008431ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008432{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008433 int tab_number;
8434
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008435 switch (eap->cmdidx)
8436 {
8437 case CMD_tabfirst:
8438 case CMD_tabrewind:
8439 goto_tabpage(1);
8440 break;
8441 case CMD_tablast:
8442 goto_tabpage(9999);
8443 break;
8444 case CMD_tabprevious:
8445 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008446 if (eap->arg && *eap->arg != NUL)
8447 {
8448 char_u *p = eap->arg;
8449 char_u *p_save = p;
8450
8451 tab_number = getdigits(&p);
8452 if (p == p_save || *p_save == '-' || *p != NUL
8453 || tab_number == 0)
8454 {
8455 /* No numbers as argument. */
8456 eap->errmsg = e_invarg;
8457 return;
8458 }
8459 }
8460 else
8461 {
8462 if (eap->addr_count == 0)
8463 tab_number = 1;
8464 else
8465 {
8466 tab_number = eap->line2;
8467 if (tab_number < 1)
8468 {
8469 eap->errmsg = e_invrange;
8470 return;
8471 }
8472 }
8473 }
8474 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008475 break;
8476 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008477 tab_number = get_tabpage_arg(eap);
8478 if (eap->errmsg == NULL)
8479 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008480 break;
8481 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008482}
8483
8484/*
8485 * :tabmove command
8486 */
8487 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008488ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008489{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008490 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008491
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008492 tab_number = get_tabpage_arg(eap);
8493 if (eap->errmsg == NULL)
8494 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008495}
8496
8497/*
8498 * :tabs command: List tabs and their contents.
8499 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008501ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008502{
8503 tabpage_T *tp;
8504 win_T *wp;
8505 int tabcount = 1;
8506
8507 msg_start();
8508 msg_scroll = TRUE;
8509 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8510 {
8511 msg_putchar('\n');
8512 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008513 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008514 out_flush(); /* output one line at a time */
8515 ui_breakcheck();
8516
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008517 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008518 wp = firstwin;
8519 else
8520 wp = tp->tp_firstwin;
8521 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8522 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008523 msg_putchar('\n');
8524 msg_putchar(wp == curwin ? '>' : ' ');
8525 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008526 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8527 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008528 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008529 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008530 else
8531 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8532 IObuff, IOSIZE, TRUE);
8533 msg_outtrans(IObuff);
8534 out_flush(); /* output one line at a time */
8535 ui_breakcheck();
8536 }
8537 }
8538}
8539
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540/*
8541 * ":mode": Set screen mode.
8542 * If no argument given, just get the screen size and redraw.
8543 */
8544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008545ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546{
8547 if (*eap->arg == NUL)
8548 shell_resized();
8549 else
8550 mch_screenmode(eap->arg);
8551}
8552
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553/*
8554 * ":resize".
8555 * set, increment or decrement current window height
8556 */
8557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008558ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559{
8560 int n;
8561 win_T *wp = curwin;
8562
8563 if (eap->addr_count > 0)
8564 {
8565 n = eap->line2;
8566 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8567 ;
8568 }
8569
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008570# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 if (cmdmod.split & WSP_VERT)
8575 {
8576 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008577 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8579 n = 9999;
8580 win_setwidth_win((int)n, wp);
8581 }
8582 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 {
8584 if (*eap->arg == '-' || *eap->arg == '+')
8585 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008586 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 n = 9999;
8588 win_setheight_win((int)n, wp);
8589 }
8590}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591
8592/*
8593 * ":find [+command] <file>" command.
8594 */
8595 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008596ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597{
8598#ifdef FEAT_SEARCHPATH
8599 char_u *fname;
8600 int count;
8601
8602 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8603 TRUE, curbuf->b_ffname);
8604 if (eap->addr_count > 0)
8605 {
8606 /* Repeat finding the file "count" times. This matters when it
8607 * appears several times in the path. */
8608 count = eap->line2;
8609 while (fname != NULL && --count > 0)
8610 {
8611 vim_free(fname);
8612 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8613 FALSE, curbuf->b_ffname);
8614 }
8615 }
8616
8617 if (fname != NULL)
8618 {
8619 eap->arg = fname;
8620#endif
8621 do_exedit(eap, NULL);
8622#ifdef FEAT_SEARCHPATH
8623 vim_free(fname);
8624 }
8625#endif
8626}
8627
8628/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008629 * ":open" simulation: for now just work like ":visual".
8630 */
8631 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008632ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008633{
8634 regmatch_T regmatch;
8635 char_u *p;
8636
8637 curwin->w_cursor.lnum = eap->line2;
8638 beginline(BL_SOL | BL_FIX);
8639 if (*eap->arg == '/')
8640 {
8641 /* ":open /pattern/": put cursor in column found with pattern */
8642 ++eap->arg;
8643 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8644 *p = NUL;
8645 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8646 if (regmatch.regprog != NULL)
8647 {
8648 regmatch.rm_ic = p_ic;
8649 p = ml_get_curline();
8650 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008651 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008652 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008653 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008654 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008655 }
8656 /* Move to the NUL, ignore any other arguments. */
8657 eap->arg += STRLEN(eap->arg);
8658 }
8659 check_cursor();
8660
8661 eap->cmdidx = CMD_visual;
8662 do_exedit(eap, NULL);
8663}
8664
8665/*
8666 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 */
8668 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008669ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670{
8671 do_exedit(eap, NULL);
8672}
8673
8674/*
8675 * ":edit <file>" command and alikes.
8676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008678do_exedit(
8679 exarg_T *eap,
8680 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681{
8682 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008684 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685
8686 /*
8687 * ":vi" command ends Ex mode.
8688 */
8689 if (exmode_active && (eap->cmdidx == CMD_visual
8690 || eap->cmdidx == CMD_view))
8691 {
8692 exmode_active = FALSE;
8693 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008694 {
8695 /* Special case: ":global/pat/visual\NLvi-commands" */
8696 if (global_busy)
8697 {
8698 int rd = RedrawingDisabled;
8699 int nwr = no_wait_return;
8700 int ms = msg_scroll;
8701#ifdef FEAT_GUI
8702 int he = hold_gui_events;
8703#endif
8704
8705 if (eap->nextcmd != NULL)
8706 {
8707 stuffReadbuff(eap->nextcmd);
8708 eap->nextcmd = NULL;
8709 }
8710
8711 if (exmode_was != EXMODE_VIM)
8712 settmode(TMODE_RAW);
8713 RedrawingDisabled = 0;
8714 no_wait_return = 0;
8715 need_wait_return = FALSE;
8716 msg_scroll = 0;
8717#ifdef FEAT_GUI
8718 hold_gui_events = 0;
8719#endif
8720 must_redraw = CLEAR;
8721
8722 main_loop(FALSE, TRUE);
8723
8724 RedrawingDisabled = rd;
8725 no_wait_return = nwr;
8726 msg_scroll = ms;
8727#ifdef FEAT_GUI
8728 hold_gui_events = he;
8729#endif
8730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 }
8734
8735 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008736 || eap->cmdidx == CMD_tabnew
8737 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008738 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008740 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 setpcmark();
8742 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008743 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8744 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008746 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 || *eap->arg != NUL
8748#ifdef FEAT_BROWSE
8749 || cmdmod.browse
8750#endif
8751 )
8752 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008753 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8754 * can bring us here, others are stopped earlier. */
8755 if (*eap->arg != NUL && curbuf_locked())
8756 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008757
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 n = readonlymode;
8759 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8760 readonlymode = TRUE;
8761 else if (eap->cmdidx == CMD_enew)
8762 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8763 empty buffer */
8764 setpcmark();
8765 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8766 NULL, eap,
8767 /* ":edit" goes to first line if Vi compatible */
8768 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8769 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8770 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008771 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008773 /* after a split we can use an existing buffer */
8774 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008776 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 {
8778 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 if (old_curwin != NULL)
8780 {
8781 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008782 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008784#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008785 cleanup_T cs;
8786
8787 /* Reset the error/interrupt/exception state here so that
8788 * aborting() returns FALSE when closing a window. */
8789 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008790#endif
8791#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008793#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008794 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008795
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008796#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008797 /* Restore the error/interrupt/exception state if not
8798 * discarded by a new aborting error, interrupt, or
8799 * uncaught exception. */
8800 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 }
8803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 }
8805 else if (readonlymode && curbuf->b_nwindows == 1)
8806 {
8807 /* When editing an already visited buffer, 'readonly' won't be set
8808 * but the previous value is kept. With ":view" and ":sview" we
8809 * want the file to be readonly, except when another window is
8810 * editing the same buffer. */
8811 curbuf->b_p_ro = TRUE;
8812 }
8813 readonlymode = n;
8814 }
8815 else
8816 {
8817 if (eap->do_ecmd_cmd != NULL)
8818 do_cmdline_cmd(eap->do_ecmd_cmd);
8819#ifdef FEAT_TITLE
8820 n = curwin->w_arg_idx_invalid;
8821#endif
8822 check_arg_idx(curwin);
8823#ifdef FEAT_TITLE
8824 if (n != curwin->w_arg_idx_invalid)
8825 maketitle();
8826#endif
8827 }
8828
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 /*
8830 * if ":split file" worked, set alternate file name in old window to new
8831 * file
8832 */
8833 if (old_curwin != NULL
8834 && *eap->arg != NUL
8835 && curwin != old_curwin
8836 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008837 && old_curwin->w_buffer != curbuf
8838 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840
8841 ex_no_reprint = TRUE;
8842}
8843
8844#ifndef FEAT_GUI
8845/*
8846 * ":gui" and ":gvim" when there is no GUI.
8847 */
8848 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008849ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850{
8851 eap->errmsg = e_nogvim;
8852}
8853#endif
8854
8855#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8856 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008857ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858{
8859 gui_make_tearoff(eap->arg);
8860}
8861#endif
8862
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008863#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8864 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008866ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008868# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8869 if (gui.in_use)
8870 gui_make_popup(eap->arg, eap->forceit);
8871# ifdef FEAT_TERM_POPUP_MENU
8872 else
8873# endif
8874# endif
8875# ifdef FEAT_TERM_POPUP_MENU
8876 pum_make_popup(eap->arg, eap->forceit);
8877# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878}
8879#endif
8880
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008882ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883{
8884 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008885 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01008887 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888}
8889
8890/*
8891 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8892 * offset.
8893 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008896ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008899 win_T *save_curwin = curwin;
8900 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 long topline;
8902 long y;
8903 linenr_T old_linenr = curwin->w_cursor.lnum;
8904
8905 setpcmark();
8906
8907 /*
8908 * determine max topline
8909 */
8910 if (curwin->w_p_scb)
8911 {
8912 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008913 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 {
8915 if (wp->w_p_scb && wp->w_buffer)
8916 {
8917 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8918 if (topline > y)
8919 topline = y;
8920 }
8921 }
8922 if (topline < 1)
8923 topline = 1;
8924 }
8925 else
8926 {
8927 topline = 1;
8928 }
8929
8930
8931 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008932 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008934 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 {
8936 if (curwin->w_p_scb)
8937 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008938 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 y = topline - curwin->w_topline;
8940 if (y > 0)
8941 scrollup(y, TRUE);
8942 else
8943 scrolldown(-y, TRUE);
8944 curwin->w_scbind_pos = topline;
8945 redraw_later(VALID);
8946 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 }
8949 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008950 curwin = save_curwin;
8951 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 if (curwin->w_p_scb)
8953 {
8954 did_syncbind = TRUE;
8955 checkpcmark();
8956 if (old_linenr != curwin->w_cursor.lnum)
8957 {
8958 char_u ctrl_o[2];
8959
8960 ctrl_o[0] = Ctrl_O;
8961 ctrl_o[1] = 0;
8962 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8963 }
8964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965}
8966
8967
8968 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008969ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008971 int i;
8972 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8973 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974
8975 if (eap->usefilter) /* :r!cmd */
8976 do_bang(1, eap, FALSE, FALSE, TRUE);
8977 else
8978 {
8979 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8980 return;
8981
8982#ifdef FEAT_BROWSE
8983 if (cmdmod.browse)
8984 {
8985 char_u *browseFile;
8986
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008987 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 NULL, NULL, NULL, curbuf);
8989 if (browseFile != NULL)
8990 {
8991 i = readfile(browseFile, NULL,
8992 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8993 vim_free(browseFile);
8994 }
8995 else
8996 i = OK;
8997 }
8998 else
8999#endif
9000 if (*eap->arg == NUL)
9001 {
9002 if (check_fname() == FAIL) /* check for no file name */
9003 return;
9004 i = readfile(curbuf->b_ffname, curbuf->b_fname,
9005 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9006 }
9007 else
9008 {
9009 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
9010 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
9011 i = readfile(eap->arg, NULL,
9012 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9013
9014 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01009015 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009017#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 if (!aborting())
9019#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009020 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 }
9022 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009023 {
9024 if (empty && exmode_active)
9025 {
9026 /* Delete the empty line that remains. Historically ex does
9027 * this but vi doesn't. */
9028 if (eap->line2 == 0)
9029 lnum = curbuf->b_ml.ml_line_count;
9030 else
9031 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009032 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009033 {
9034 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009035 if (curwin->w_cursor.lnum > 1
9036 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009037 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009038 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009039 }
9040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 }
9044}
9045
Bram Moolenaarea408852005-06-25 22:49:46 +00009046static char_u *prev_dir = NULL;
9047
9048#if defined(EXITFREE) || defined(PROTO)
9049 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009050free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009051{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009052 VIM_CLEAR(prev_dir);
9053 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00009054}
9055#endif
9056
Bram Moolenaarf4258302013-06-02 18:20:17 +02009057/*
9058 * Deal with the side effects of changing the current directory.
9059 * When "local" is TRUE then this was after an ":lcd" command.
9060 */
9061 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009062post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009063{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009064 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009065 if (local)
9066 {
9067 /* If still in global directory, need to remember current
9068 * directory as global directory. */
9069 if (globaldir == NULL && prev_dir != NULL)
9070 globaldir = vim_strsave(prev_dir);
9071 /* Remember this local directory for the window. */
9072 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9073 curwin->w_localdir = vim_strsave(NameBuff);
9074 }
9075 else
9076 {
9077 /* We are now in the global directory, no need to remember its
9078 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01009079 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009080 }
9081
9082 shorten_fnames(TRUE);
9083}
9084
Bram Moolenaarea408852005-06-25 22:49:46 +00009085
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086/*
9087 * ":cd", ":lcd", ":chdir" and ":lchdir".
9088 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009089 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009090ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009092 char_u *new_dir;
9093 char_u *tofree;
9094 int dir_differs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095
9096 new_dir = eap->arg;
9097#if !defined(UNIX) && !defined(VMS)
9098 /* for non-UNIX ":cd" means: print current directory */
9099 if (*new_dir == NUL)
9100 ex_pwd(NULL);
9101 else
9102#endif
9103 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009104 if (allbuf_locked())
9105 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009106 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9107 && !eap->forceit)
9108 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009109 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009110 return;
9111 }
9112
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 /* ":cd -": Change to previous directory */
9114 if (STRCMP(new_dir, "-") == 0)
9115 {
9116 if (prev_dir == NULL)
9117 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009118 emsg(_("E186: No previous directory"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 return;
9120 }
9121 new_dir = prev_dir;
9122 }
9123
9124 /* Save current directory for next ":cd -" */
9125 tofree = prev_dir;
9126 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9127 prev_dir = vim_strsave(NameBuff);
9128 else
9129 prev_dir = NULL;
9130
9131#if defined(UNIX) || defined(VMS)
9132 /* for UNIX ":cd" means: go to home directory */
9133 if (*new_dir == NUL)
9134 {
9135 /* use NameBuff for home directory name */
9136# ifdef VMS
9137 char_u *p;
9138
9139 p = mch_getenv((char_u *)"SYS$LOGIN");
9140 if (p == NULL || *p == NUL) /* empty is the same as not set */
9141 NameBuff[0] = NUL;
9142 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009143 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144# else
9145 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9146# endif
9147 new_dir = NameBuff;
9148 }
9149#endif
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009150 dir_differs = new_dir == NULL || prev_dir == NULL
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01009151 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009152 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009153 emsg(_(e_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 else
9155 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009156 int is_local_chdir = eap->cmdidx == CMD_lcd
9157 || eap->cmdidx == CMD_lchdir;
9158
9159 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160
9161 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009162 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 ex_pwd(eap);
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009164
9165 if (dir_differs)
9166 apply_autocmds(EVENT_DIRCHANGED,
9167 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9168 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 }
9170 vim_free(tofree);
9171 }
9172}
9173
9174/*
9175 * ":pwd".
9176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009178ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179{
9180 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9181 {
9182#ifdef BACKSLASH_IN_FILENAME
9183 slash_adjust(NameBuff);
9184#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01009185 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 }
9187 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009188 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189}
9190
9191/*
9192 * ":=".
9193 */
9194 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009195ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009197 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009198 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199}
9200
9201 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009202ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009204 int n;
9205 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206
9207 if (cursor_valid())
9208 {
9209 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9210 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009211 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009213
9214 len = eap->line2;
9215 switch (*eap->arg)
9216 {
9217 case 'm': break;
9218 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009219 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009220 }
9221 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222}
9223
9224/*
9225 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9226 */
9227 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009228do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229{
9230 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009231 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232
9233 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02009234 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01009235 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009237 wait_now = msec - done > 1000L ? 1000L : msec - done;
9238#ifdef FEAT_TIMERS
9239 {
9240 long due_time = check_due_timer();
9241
9242 if (due_time > 0 && due_time < wait_now)
9243 wait_now = due_time;
9244 }
9245#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009246#ifdef FEAT_JOB_CHANNEL
9247 if (has_any_channel() && wait_now > 100L)
9248 wait_now = 100L;
9249#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009250 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009251#ifdef FEAT_JOB_CHANNEL
9252 if (has_any_channel())
9253 ui_breakcheck_force(TRUE);
9254 else
9255#endif
9256 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009257#ifdef MESSAGE_QUEUE
9258 /* Process the netbeans and clientserver messages that may have been
9259 * received in the call to ui_breakcheck() when the GUI is in use. This
9260 * may occur when running a test case. */
9261 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02009264
9265 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
9266 // input buffer, otherwise a following call to input() fails.
9267 if (got_int)
9268 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269}
9270
9271 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009272do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273{
9274 int mode;
9275 char_u *cmdp;
9276
9277 cmdp = eap->cmd;
9278 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9279
9280 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9281 eap->arg, mode, isabbrev))
9282 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009283 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009285 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 break;
9287 }
9288}
9289
9290/*
9291 * ":winsize" command (obsolete).
9292 */
9293 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009294ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295{
9296 int w, h;
9297 char_u *arg = eap->arg;
9298 char_u *p;
9299
9300 w = getdigits(&arg);
9301 arg = skipwhite(arg);
9302 p = arg;
9303 h = getdigits(&arg);
9304 if (*p != NUL && *arg == NUL)
9305 set_shellsize(w, h, TRUE);
9306 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009307 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308}
9309
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009311ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312{
9313 int xchar = NUL;
9314 char_u *p;
9315
9316 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9317 {
9318 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9319 if (eap->arg[1] == NUL)
9320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009321 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 return;
9323 }
9324 xchar = eap->arg[1];
9325 p = eap->arg + 2;
9326 }
9327 else
9328 p = eap->arg + 1;
9329
9330 eap->nextcmd = check_nextcmd(p);
9331 p = skipwhite(p);
9332 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009333 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009334 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 {
9336 /* Pass flags on for ":vertical wincmd ]". */
9337 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009338 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9340 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009341 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 }
9343}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344
Bram Moolenaar843ee412004-06-30 16:16:41 +00009345#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346/*
9347 * ":winpos".
9348 */
9349 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009350ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351{
9352 int x, y;
9353 char_u *arg = eap->arg;
9354 char_u *p;
9355
9356 if (*arg == NUL)
9357 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009358# if defined(FEAT_GUI) || defined(MSWIN)
9359# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009361# else
9362 if (mch_get_winpos(&x, &y) != FAIL)
9363# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 {
9365 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01009366 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 }
9368 else
9369# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009370 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 }
9372 else
9373 {
9374 x = getdigits(&arg);
9375 arg = skipwhite(arg);
9376 p = arg;
9377 y = getdigits(&arg);
9378 if (*p == NUL || *arg != NUL)
9379 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009380 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 return;
9382 }
9383# ifdef FEAT_GUI
9384 if (gui.in_use)
9385 gui_mch_set_winpos(x, y);
9386 else if (gui.starting)
9387 {
9388 /* Remember the coordinates for when the window is opened. */
9389 gui_win_x = x;
9390 gui_win_y = y;
9391 }
9392# ifdef HAVE_TGETENT
9393 else
9394# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009395# else
9396# ifdef MSWIN
9397 mch_set_winpos(x, y);
9398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399# endif
9400# ifdef HAVE_TGETENT
9401 if (*T_CWP)
9402 term_set_winpos(x, y);
9403# endif
9404 }
9405}
9406#endif
9407
9408/*
9409 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9410 */
9411 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009412ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413{
9414 oparg_T oa;
9415
9416 clear_oparg(&oa);
9417 oa.regname = eap->regname;
9418 oa.start.lnum = eap->line1;
9419 oa.end.lnum = eap->line2;
9420 oa.line_count = eap->line2 - eap->line1 + 1;
9421 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9424 {
9425 setpcmark();
9426 curwin->w_cursor.lnum = eap->line1;
9427 beginline(BL_SOL | BL_FIX);
9428 }
9429
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009430 if (VIsual_active)
9431 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009432
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 switch (eap->cmdidx)
9434 {
9435 case CMD_delete:
9436 oa.op_type = OP_DELETE;
9437 op_delete(&oa);
9438 break;
9439
9440 case CMD_yank:
9441 oa.op_type = OP_YANK;
9442 (void)op_yank(&oa, FALSE, TRUE);
9443 break;
9444
9445 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009446 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009448 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9449#else
9450 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009452 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 oa.op_type = OP_RSHIFT;
9454 else
9455 oa.op_type = OP_LSHIFT;
9456 op_shift(&oa, FALSE, eap->amount);
9457 break;
9458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009460 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461}
9462
9463/*
9464 * ":put".
9465 */
9466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009467ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468{
9469 /* ":0put" works like ":1put!". */
9470 if (eap->line2 == 0)
9471 {
9472 eap->line2 = 1;
9473 eap->forceit = TRUE;
9474 }
9475 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009476 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9477 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478}
9479
9480/*
9481 * Handle ":copy" and ":move".
9482 */
9483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009484ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485{
9486 long n;
9487
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02009488 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 if (eap->arg == NULL) /* error detected */
9490 {
9491 eap->nextcmd = NULL;
9492 return;
9493 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009494 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495
9496 /*
9497 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9498 */
9499 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9500 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009501 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 return;
9503 }
9504
9505 if (eap->cmdidx == CMD_move)
9506 {
9507 if (do_move(eap->line1, eap->line2, n) == FAIL)
9508 return;
9509 }
9510 else
9511 ex_copy(eap->line1, eap->line2, n);
9512 u_clearline();
9513 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009514 ex_may_print(eap);
9515}
9516
9517/*
9518 * Print the current line if flags were given to the Ex command.
9519 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009520 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009521ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009522{
9523 if (eap->flags != 0)
9524 {
9525 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9526 (eap->flags & EXFLAG_LIST));
9527 ex_no_reprint = TRUE;
9528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529}
9530
9531/*
9532 * ":smagic" and ":snomagic".
9533 */
9534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009535ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536{
9537 int magic_save = p_magic;
9538
9539 p_magic = (eap->cmdidx == CMD_smagic);
9540 do_sub(eap);
9541 p_magic = magic_save;
9542}
9543
9544/*
9545 * ":join".
9546 */
9547 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009548ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549{
9550 curwin->w_cursor.lnum = eap->line1;
9551 if (eap->line1 == eap->line2)
9552 {
9553 if (eap->addr_count >= 2) /* :2,2join does nothing */
9554 return;
9555 if (eap->line2 == curbuf->b_ml.ml_line_count)
9556 {
9557 beep_flush();
9558 return;
9559 }
9560 ++eap->line2;
9561 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009562 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009564 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565}
9566
9567/*
9568 * ":[addr]@r" or ":[addr]*r": execute register
9569 */
9570 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009571ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572{
9573 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009574 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575
9576 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009577 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578
9579#ifdef USE_ON_FLY_SCROLL
9580 dont_scroll = TRUE; /* disallow scrolling here */
9581#endif
9582
9583 /* get the register name. No name means to use the previous one */
9584 c = *eap->arg;
9585 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9586 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009587 /* Put the register in the typeahead buffer with the "silent" flag. */
9588 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9589 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 else
9594 {
9595 int save_efr = exec_from_reg;
9596
9597 exec_from_reg = TRUE;
9598
9599 /*
9600 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009601 * Continue until the stuff buffer is empty and all added characters
9602 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009604 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9606
9607 exec_from_reg = save_efr;
9608 }
9609}
9610
9611/*
9612 * ":!".
9613 */
9614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009615ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616{
9617 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9618}
9619
9620/*
9621 * ":undo".
9622 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009624ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009626 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009627 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009628 else
9629 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630}
9631
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009632#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009633 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009634ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009635{
9636 char_u hash[UNDO_HASH_SIZE];
9637
9638 u_compute_hash(hash);
9639 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9640}
9641
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009642 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009643ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009644{
9645 char_u hash[UNDO_HASH_SIZE];
9646
9647 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009648 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009649}
9650#endif
9651
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652/*
9653 * ":redo".
9654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009656ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657{
9658 u_redo(1);
9659}
9660
9661/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009662 * ":earlier" and ":later".
9663 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009664 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009665ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009666{
9667 long count = 0;
9668 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009669 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009670 char_u *p = eap->arg;
9671
9672 if (*p == NUL)
9673 count = 1;
9674 else if (isdigit(*p))
9675 {
9676 count = getdigits(&p);
9677 switch (*p)
9678 {
9679 case 's': ++p; sec = TRUE; break;
9680 case 'm': ++p; sec = TRUE; count *= 60; break;
9681 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009682 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9683 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009684 }
9685 }
9686
9687 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009688 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009689 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009690 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9691 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009692}
9693
9694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 * ":redir": start/stop redirection.
9696 */
9697 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009698ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699{
9700 char *mode;
9701 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009702 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703
Bram Moolenaarba768492016-07-08 15:32:54 +02009704#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009705 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009706 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009707 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009708 return;
9709 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009710#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009711
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 if (STRICMP(eap->arg, "END") == 0)
9713 close_redir();
9714 else
9715 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009716 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009718 ++arg;
9719 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009721 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 mode = "a";
9723 }
9724 else
9725 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009726 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727
9728 close_redir();
9729
9730 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009731 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 if (fname == NULL)
9733 return;
9734#ifdef FEAT_BROWSE
9735 if (cmdmod.browse)
9736 {
9737 char_u *browseFile;
9738
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009739 browseFile = do_browse(BROWSE_SAVE,
9740 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009741 fname, NULL, NULL,
9742 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 if (browseFile == NULL)
9744 return; /* operation cancelled */
9745 vim_free(fname);
9746 fname = browseFile;
9747 eap->forceit = TRUE; /* since dialog already asked */
9748 }
9749#endif
9750
9751 redir_fd = open_exfile(fname, eap->forceit, mode);
9752 vim_free(fname);
9753 }
9754#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009755 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 {
9757 /* redirect to a register a-z (resp. A-Z for appending) */
9758 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009759 ++arg;
9760 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009762 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009763 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009765 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009767 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009768 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009769 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009770 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009772 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009773 if (*arg == '>')
9774 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009775 /* Make register empty when not using @A-@Z and the
9776 * command is valid. */
9777 if (*arg == NUL && !isupper(redir_reg))
9778 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009780 }
9781 if (*arg != NUL)
9782 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009783 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009784 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009785 }
9786 }
9787 else if (*arg == '=' && arg[1] == '>')
9788 {
9789 int append;
9790
9791 /* redirect to a variable */
9792 close_redir();
9793 arg += 2;
9794
9795 if (*arg == '>')
9796 {
9797 ++arg;
9798 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 }
9800 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009801 append = FALSE;
9802
9803 if (var_redir_start(skipwhite(arg), append) == OK)
9804 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 }
9806#endif
9807
9808 /* TODO: redirect to a buffer */
9809
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009811 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009813
9814 /* Make sure redirection is not off. Can happen for cmdline completion
9815 * that indirectly invokes a command to catch its output. */
9816 if (redir_fd != NULL
9817#ifdef FEAT_EVAL
9818 || redir_reg || redir_vname
9819#endif
9820 )
9821 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822}
9823
9824/*
9825 * ":redraw": force redraw
9826 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009827 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009828ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829{
9830 int r = RedrawingDisabled;
9831 int p = p_lz;
9832
9833 RedrawingDisabled = 0;
9834 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009835 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009837 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838#ifdef FEAT_TITLE
9839 if (need_maketitle)
9840 maketitle();
9841#endif
9842 RedrawingDisabled = r;
9843 p_lz = p;
9844
9845 /* Reset msg_didout, so that a message that's there is overwritten. */
9846 msg_didout = FALSE;
9847 msg_col = 0;
9848
Bram Moolenaar56667a52013-07-17 11:54:28 +02009849 /* No need to wait after an intentional redraw. */
9850 need_wait_return = FALSE;
9851
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 out_flush();
9853}
9854
9855/*
9856 * ":redrawstatus": force redraw of status line(s)
9857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009859ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 int r = RedrawingDisabled;
9862 int p = p_lz;
9863
9864 RedrawingDisabled = 0;
9865 p_lz = FALSE;
9866 if (eap->forceit)
9867 status_redraw_all();
9868 else
9869 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009870 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 RedrawingDisabled = r;
9872 p_lz = p;
9873 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874}
9875
Bram Moolenaare12bab32019-01-08 22:02:56 +01009876/*
9877 * ":redrawtabline": force redraw of the tabline
9878 */
9879 static void
9880ex_redrawtabline(exarg_T *eap UNUSED)
9881{
9882 int r = RedrawingDisabled;
9883 int p = p_lz;
9884
9885 RedrawingDisabled = 0;
9886 p_lz = FALSE;
9887
9888 draw_tabline();
9889
9890 RedrawingDisabled = r;
9891 p_lz = p;
9892 out_flush();
9893}
9894
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009896close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897{
9898 if (redir_fd != NULL)
9899 {
9900 fclose(redir_fd);
9901 redir_fd = NULL;
9902 }
9903#ifdef FEAT_EVAL
9904 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009905 if (redir_vname)
9906 {
9907 var_redir_stop();
9908 redir_vname = 0;
9909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910#endif
9911}
9912
9913#if defined(FEAT_SESSION) && defined(USE_CRNL)
9914# define MKSESSION_NL
9915static int mksession_nl = FALSE; /* use NL only in put_eol() */
9916#endif
9917
9918/*
9919 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9920 */
9921 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009922ex_mkrc(
9923 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924{
9925 FILE *fd;
9926 int failed = FALSE;
9927 char_u *fname;
9928#ifdef FEAT_BROWSE
9929 char_u *browseFile = NULL;
9930#endif
9931#ifdef FEAT_SESSION
9932 int view_session = FALSE;
9933 int using_vdir = FALSE; /* using 'viewdir'? */
9934 char_u *viewFile = NULL;
9935 unsigned *flagp;
9936#endif
9937
9938 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9939 {
9940#ifdef FEAT_SESSION
9941 view_session = TRUE;
9942#else
9943 ex_ni(eap);
9944 return;
9945#endif
9946 }
9947
9948#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009949 /* Use the short file name until ":lcd" is used. We also don't use the
9950 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009951 did_lcd = FALSE;
9952
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9954 if (eap->cmdidx == CMD_mkview
9955 && (*eap->arg == NUL
9956 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9957 {
9958 eap->forceit = TRUE;
9959 fname = get_view_file(*eap->arg);
9960 if (fname == NULL)
9961 return;
9962 viewFile = fname;
9963 using_vdir = TRUE;
9964 }
9965 else
9966#endif
9967 if (*eap->arg != NUL)
9968 fname = eap->arg;
9969 else if (eap->cmdidx == CMD_mkvimrc)
9970 fname = (char_u *)VIMRC_FILE;
9971#ifdef FEAT_SESSION
9972 else if (eap->cmdidx == CMD_mksession)
9973 fname = (char_u *)SESSION_FILE;
9974#endif
9975 else
9976 fname = (char_u *)EXRC_FILE;
9977
9978#ifdef FEAT_BROWSE
9979 if (cmdmod.browse)
9980 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009981 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982# ifdef FEAT_SESSION
9983 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9984 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9985# endif
9986 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009987 fname, (char_u *)"vim", NULL,
9988 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 if (browseFile == NULL)
9990 goto theend;
9991 fname = browseFile;
9992 eap->forceit = TRUE; /* since dialog already asked */
9993 }
9994#endif
9995
9996#if defined(FEAT_SESSION) && defined(vim_mkdir)
9997 /* When using 'viewdir' may have to create the directory. */
9998 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009999 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000#endif
10001
10002 fd = open_exfile(fname, eap->forceit, WRITEBIN);
10003 if (fd != NULL)
10004 {
10005#ifdef FEAT_SESSION
10006 if (eap->cmdidx == CMD_mkview)
10007 flagp = &vop_flags;
10008 else
10009 flagp = &ssop_flags;
10010#endif
10011
10012#ifdef MKSESSION_NL
10013 /* "unix" in 'sessionoptions': use NL line separator */
10014 if (view_session && (*flagp & SSOP_UNIX))
10015 mksession_nl = TRUE;
10016#endif
10017
10018 /* Write the version command for :mkvimrc */
10019 if (eap->cmdidx == CMD_mkvimrc)
10020 (void)put_line(fd, "version 6.0");
10021
10022#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010023 if (eap->cmdidx == CMD_mksession)
10024 {
10025 if (put_line(fd, "let SessionLoad = 1") == FAIL)
10026 failed = TRUE;
10027 }
10028
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 if (eap->cmdidx != CMD_mkview)
10030#endif
10031 {
10032 /* Write setting 'compatible' first, because it has side effects.
10033 * For that same reason only do it when needed. */
10034 if (p_cp)
10035 (void)put_line(fd, "if !&cp | set cp | endif");
10036 else
10037 (void)put_line(fd, "if &cp | set nocp | endif");
10038 }
10039
10040#ifdef FEAT_SESSION
10041 if (!view_session
10042 || (eap->cmdidx == CMD_mksession
10043 && (*flagp & SSOP_OPTIONS)))
10044#endif
10045 failed |= (makemap(fd, NULL) == FAIL
10046 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10047
10048#ifdef FEAT_SESSION
10049 if (!failed && view_session)
10050 {
10051 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10052 failed = TRUE;
10053 if (eap->cmdidx == CMD_mksession)
10054 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010055 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056
Bram Moolenaard9462e32011-04-11 21:35:11 +020010057 dirnow = alloc(MAXPATHL);
10058 if (dirnow == NULL)
10059 failed = TRUE;
10060 else
10061 {
10062 /*
10063 * Change to session file's dir.
10064 */
10065 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010067 *dirnow = NUL;
10068 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10069 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +010010070 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010071 shorten_fnames(TRUE);
10072 }
10073 else if (*dirnow != NUL
10074 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10075 {
10076 if (mch_chdir((char *)globaldir) == 0)
10077 shorten_fnames(TRUE);
10078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079
Bram Moolenaard9462e32011-04-11 21:35:11 +020010080 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081
Bram Moolenaard9462e32011-04-11 21:35:11 +020010082 /* restore original dir */
10083 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010085 {
10086 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010087 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +020010088 shorten_fnames(TRUE);
10089 }
10090 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 }
10092 }
10093 else
10094 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010095 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10096 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 }
10098 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10099 == FAIL)
10100 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +010010101# ifdef FEAT_SEARCH_EXTRA
10102 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
10103 failed = TRUE;
10104# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010105 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10106 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010107 if (eap->cmdidx == CMD_mksession)
10108 {
10109 if (put_line(fd, "unlet SessionLoad") == FAIL)
10110 failed = TRUE;
10111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 }
10113#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010114 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10115 failed = TRUE;
10116
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 failed |= fclose(fd);
10118
10119 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010120 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10122 else if (eap->cmdidx == CMD_mksession)
10123 {
10124 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010125 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126
Bram Moolenaard9462e32011-04-11 21:35:11 +020010127 tbuf = alloc(MAXPATHL);
10128 if (tbuf != NULL)
10129 {
10130 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10131 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10132 vim_free(tbuf);
10133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 }
10135#endif
10136#ifdef MKSESSION_NL
10137 mksession_nl = FALSE;
10138#endif
10139 }
10140
10141#ifdef FEAT_BROWSE
10142theend:
10143 vim_free(browseFile);
10144#endif
10145#ifdef FEAT_SESSION
10146 vim_free(viewFile);
10147#endif
10148}
10149
Bram Moolenaardf177f62005-02-22 08:39:57 +000010150#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10151 || defined(PROTO)
10152 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010153vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010154{
10155 if (vim_mkdir(name, prot) != 0)
10156 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010157 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +000010158 return FAIL;
10159 }
10160 return OK;
10161}
10162#endif
10163
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164/*
10165 * Open a file for writing for an Ex command, with some checks.
10166 * Return file descriptor, or NULL on failure.
10167 */
10168 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010169open_exfile(
10170 char_u *fname,
10171 int forceit,
10172 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173{
10174 FILE *fd;
10175
10176#ifdef UNIX
10177 /* with Unix it is possible to open a directory */
10178 if (mch_isdir(fname))
10179 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010180 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 return NULL;
10182 }
10183#endif
10184 if (!forceit && *mode != 'a' && vim_fexists(fname))
10185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010186 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 return NULL;
10188 }
10189
10190 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010191 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192
10193 return fd;
10194}
10195
10196/*
10197 * ":mark" and ":k".
10198 */
10199 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010200ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201{
10202 pos_T pos;
10203
10204 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010205 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010207 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 else
10209 {
10210 pos = curwin->w_cursor; /* save curwin->w_cursor */
10211 curwin->w_cursor.lnum = eap->line2;
10212 beginline(BL_WHITE | BL_FIX);
10213 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010214 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10216 }
10217}
10218
10219/*
10220 * Update w_topline, w_leftcol and the cursor position.
10221 */
10222 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010223update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224{
10225 check_cursor(); /* put cursor on valid line */
10226 update_topline();
10227 if (!curwin->w_p_wrap)
10228 validate_cursor();
10229 update_curswant();
10230}
10231
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010233 * Save the current State and go to Normal mode.
10234 * Return TRUE if the typeahead could be saved.
10235 */
10236 int
10237save_current_state(save_state_T *sst)
10238{
10239 sst->save_msg_scroll = msg_scroll;
10240 sst->save_restart_edit = restart_edit;
10241 sst->save_msg_didout = msg_didout;
10242 sst->save_State = State;
10243 sst->save_insertmode = p_im;
10244 sst->save_finish_op = finish_op;
10245 sst->save_opcount = opcount;
10246
10247 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10248 restart_edit = 0; /* don't go to Insert mode */
10249 p_im = FALSE; /* don't use 'insertmode' */
10250
10251 /*
10252 * Save the current typeahead. This is required to allow using ":normal"
10253 * from an event handler and makes sure we don't hang when the argument
10254 * ends with half a command.
10255 */
10256 save_typeahead(&sst->tabuf);
10257 return sst->tabuf.typebuf_valid;
10258}
10259
10260 void
10261restore_current_state(save_state_T *sst)
10262{
10263 /* Restore the previous typeahead. */
10264 restore_typeahead(&sst->tabuf);
10265
10266 msg_scroll = sst->save_msg_scroll;
10267 restart_edit = sst->save_restart_edit;
10268 p_im = sst->save_insertmode;
10269 finish_op = sst->save_finish_op;
10270 opcount = sst->save_opcount;
10271 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10272
10273 /* Restore the state (needed when called from a function executed for
10274 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10275 State = sst->save_State;
10276#ifdef CURSOR_SHAPE
10277 ui_cursor_shape(); /* may show different cursor shape */
10278#endif
10279}
10280
10281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 * ":normal[!] {commands}": Execute normal mode commands.
10283 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010284 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010285ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010287 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 char_u *arg = NULL;
10289 int l;
10290 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010292 if (ex_normal_lock > 0)
10293 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010294 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010295 return;
10296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 if (ex_normal_busy >= p_mmd)
10298 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010299 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 return;
10301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 /*
10304 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10305 * this for the K_SPECIAL leading byte, otherwise special keys will not
10306 * work.
10307 */
10308 if (has_mbyte)
10309 {
10310 int len = 0;
10311
10312 /* Count the number of characters to be escaped. */
10313 for (p = eap->arg; *p != NUL; ++p)
10314 {
Bram Moolenaar13505972019-01-24 15:04:48 +010010315#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 if (*p == CSI) /* leadbyte CSI */
10317 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +010010318#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010319 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +010010321#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +010010323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 )
10325 len += 2;
10326 }
10327 if (len > 0)
10328 {
10329 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10330 if (arg != NULL)
10331 {
10332 len = 0;
10333 for (p = eap->arg; *p != NUL; ++p)
10334 {
10335 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +010010336#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 if (*p == CSI)
10338 {
10339 arg[len++] = KS_EXTRA;
10340 arg[len++] = (int)KE_CSI;
10341 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010342#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010343 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 {
10345 arg[len++] = *++p;
10346 if (*p == K_SPECIAL)
10347 {
10348 arg[len++] = KS_SPECIAL;
10349 arg[len++] = KE_FILLER;
10350 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010351#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 else if (*p == CSI)
10353 {
10354 arg[len++] = KS_EXTRA;
10355 arg[len++] = (int)KE_CSI;
10356 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 }
10359 arg[len] = NUL;
10360 }
10361 }
10362 }
10363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010365 ++ex_normal_busy;
10366 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 {
10368 /*
10369 * Repeat the :normal command for each line in the range. When no
10370 * range given, execute it just once, without positioning the cursor
10371 * first.
10372 */
10373 do
10374 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 if (eap->addr_count != 0)
10376 {
10377 curwin->w_cursor.lnum = eap->line1++;
10378 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010379 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 }
10381
Bram Moolenaar13505972019-01-24 15:04:48 +010010382 exec_normal_cmd(arg != NULL
10383 ? arg
10384 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 }
10386 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10387 }
10388
10389 /* Might not return to the main loop when in an event handler. */
10390 update_topline_cursor();
10391
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010392 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010394#ifdef FEAT_MOUSE
10395 setmouse();
10396#endif
10397#ifdef CURSOR_SHAPE
10398 ui_cursor_shape(); /* may show different cursor shape */
10399#endif
10400
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402}
10403
10404/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010405 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 */
10407 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010408ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010410 if (eap->forceit)
10411 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010412 /* cursor line can be zero on startup */
10413 if (!curwin->w_cursor.lnum)
10414 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010415 coladvance((colnr_T)MAXCOL);
10416 curwin->w_curswant = MAXCOL;
10417 curwin->w_set_curswant = FALSE;
10418 }
10419
Bram Moolenaara40c5002005-01-09 21:16:21 +000010420 /* Ignore the command when already in Insert mode. Inserting an
10421 * expression register that invokes a function can do this. */
10422 if (State & INSERT)
10423 return;
10424
Bram Moolenaar64969662005-12-14 21:59:55 +000010425 if (eap->cmdidx == CMD_startinsert)
10426 restart_edit = 'a';
10427 else if (eap->cmdidx == CMD_startreplace)
10428 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010430 restart_edit = 'V';
10431
10432 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010434 if (eap->cmdidx == CMD_startinsert)
10435 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 curwin->w_curswant = 0; /* avoid MAXCOL */
10437 }
10438}
10439
10440/*
10441 * ":stopinsert"
10442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010444ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445{
10446 restart_edit = 0;
10447 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010448 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010451/*
10452 * Execute normal mode command "cmd".
10453 * "remap" can be REMAP_NONE or REMAP_YES.
10454 */
10455 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010456exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010457{
Bram Moolenaar25281632016-01-21 23:32:32 +010010458 /* Stuff the argument into the typeahead buffer. */
10459 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010460 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +010010461}
Bram Moolenaar25281632016-01-21 23:32:32 +010010462
Bram Moolenaar25281632016-01-21 23:32:32 +010010463/*
10464 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010465 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +010010466 */
10467 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010468exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +010010469{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010470 oparg_T oa;
10471
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010472 clear_oparg(&oa);
10473 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010474 while ((!stuff_empty()
10475 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
10476 || (use_vpeekc && vpeekc() != NUL))
10477 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010478 {
10479 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010480#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010481 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010482 && oa.op_type == OP_NOP && oa.regname == NUL
10483 && !VIsual_active)
10484 {
10485 /* If terminal_loop() returns OK we got a key that is handled
10486 * in Normal model. With FAIL we first need to position the
10487 * cursor and the screen needs to be redrawn. */
10488 if (terminal_loop(TRUE) == OK)
10489 normal_cmd(&oa, TRUE);
10490 }
10491 else
10492#endif
10493 /* execute a Normal mode cmd */
10494 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010495 }
10496}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010497
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498#ifdef FEAT_FIND_ID
10499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010500ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501{
10502 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10503 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10504 (linenr_T)1, (linenr_T)MAXLNUM);
10505}
10506
Bram Moolenaar4033c552017-09-16 20:54:51 +020010507#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508/*
10509 * ":psearch"
10510 */
10511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010512ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513{
10514 g_do_tagpreview = p_pvh;
10515 ex_findpat(eap);
10516 g_do_tagpreview = 0;
10517}
10518#endif
10519
10520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010521ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522{
10523 int whole = TRUE;
10524 long n;
10525 char_u *p;
10526 int action;
10527
10528 switch (cmdnames[eap->cmdidx].cmd_name[2])
10529 {
10530 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10531 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10532 action = ACTION_GOTO;
10533 else
10534 action = ACTION_SHOW;
10535 break;
10536 case 'i': /* ":ilist" and ":dlist" */
10537 action = ACTION_SHOW_ALL;
10538 break;
10539 case 'u': /* ":ijump" and ":djump" */
10540 action = ACTION_GOTO;
10541 break;
10542 default: /* ":isplit" and ":dsplit" */
10543 action = ACTION_SPLIT;
10544 break;
10545 }
10546
10547 n = 1;
10548 if (vim_isdigit(*eap->arg)) /* get count */
10549 {
10550 n = getdigits(&eap->arg);
10551 eap->arg = skipwhite(eap->arg);
10552 }
10553 if (*eap->arg == '/') /* Match regexp, not just whole words */
10554 {
10555 whole = FALSE;
10556 ++eap->arg;
10557 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10558 if (*p)
10559 {
10560 *p++ = NUL;
10561 p = skipwhite(p);
10562
10563 /* Check for trailing illegal characters */
10564 if (!ends_excmd(*p))
10565 eap->errmsg = e_trailing;
10566 else
10567 eap->nextcmd = check_nextcmd(p);
10568 }
10569 }
10570 if (!eap->skip)
10571 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10572 whole, !eap->forceit,
10573 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10574 n, action, eap->line1, eap->line2);
10575}
10576#endif
10577
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578
Bram Moolenaar4033c552017-09-16 20:54:51 +020010579#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580/*
10581 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10582 */
10583 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010584ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010586 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10588}
10589
10590/*
10591 * ":pedit"
10592 */
10593 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010594ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595{
10596 win_T *curwin_save = curwin;
10597
10598 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010599 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010600 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601 do_exedit(eap, NULL);
10602 keep_help_flag = FALSE;
10603 if (curwin != curwin_save && win_valid(curwin_save))
10604 {
10605 /* Return cursor to where we were */
10606 validate_cursor();
10607 redraw_later(VALID);
10608 win_enter(curwin_save, TRUE);
10609 }
10610 g_do_tagpreview = 0;
10611}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613
10614/*
10615 * ":stag", ":stselect" and ":stjump".
10616 */
10617 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010618ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619{
10620 postponed_split = -1;
10621 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010622 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10624 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010625 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627
10628/*
10629 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10630 */
10631 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010632ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633{
10634 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10635}
10636
10637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010638ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639{
10640 int cmd;
10641
10642 switch (name[1])
10643 {
10644 case 'j': cmd = DT_JUMP; /* ":tjump" */
10645 break;
10646 case 's': cmd = DT_SELECT; /* ":tselect" */
10647 break;
10648 case 'p': cmd = DT_PREV; /* ":tprevious" */
10649 break;
10650 case 'N': cmd = DT_PREV; /* ":tNext" */
10651 break;
10652 case 'n': cmd = DT_NEXT; /* ":tnext" */
10653 break;
10654 case 'o': cmd = DT_POP; /* ":pop" */
10655 break;
10656 case 'f': /* ":tfirst" */
10657 case 'r': cmd = DT_FIRST; /* ":trewind" */
10658 break;
10659 case 'l': cmd = DT_LAST; /* ":tlast" */
10660 break;
10661 default: /* ":tag" */
10662#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010663 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010665 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 return;
10667 }
10668#endif
10669 cmd = DT_TAG;
10670 break;
10671 }
10672
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010673 if (name[0] == 'l')
10674 {
10675#ifndef FEAT_QUICKFIX
10676 ex_ni(eap);
10677 return;
10678#else
10679 cmd = DT_LTAG;
10680#endif
10681 }
10682
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10684 eap->forceit, TRUE);
10685}
10686
10687/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010688 * Check "str" for starting with a special cmdline variable.
10689 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10690 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10691 */
10692 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010693find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010694{
10695 int len;
10696 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010697 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010698 "%",
10699#define SPEC_PERC 0
10700 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010701#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010702 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010703#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010704 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010705#define SPEC_CCWORD (SPEC_CWORD + 1)
10706 "<cexpr>", /* expr under cursor */
10707#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010708 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010709#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010710 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010711#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010712 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010713#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010714 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010715#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010716 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010717#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010718 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010719#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010720 "<sflnum>", /* script file line number */
10721#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010722#ifdef FEAT_CLIENTSERVER
10723 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010724# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010725#endif
10726 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010727
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010728 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010729 {
10730 len = (int)STRLEN(spec_str[i]);
10731 if (STRNCMP(src, spec_str[i], len) == 0)
10732 {
10733 *usedlen = len;
10734 return i;
10735 }
10736 }
10737 return -1;
10738}
10739
10740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 * Evaluate cmdline variables.
10742 *
10743 * change '%' to curbuf->b_ffname
10744 * '#' to curwin->w_altfile
10745 * '<cword>' to word under the cursor
10746 * '<cWORD>' to WORD under the cursor
10747 * '<cfile>' to path name under the cursor
10748 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010749 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 * '<afile>' to file name for autocommand
10751 * '<abuf>' to buffer number for autocommand
10752 * '<amatch>' to matching name for autocommand
10753 *
10754 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10755 * "" for error without a message) and NULL is returned.
10756 * Returns an allocated string if a valid match was found.
10757 * Returns NULL if no match was found. "usedlen" then still contains the
10758 * number of characters to skip.
10759 */
10760 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010761eval_vars(
10762 char_u *src, /* pointer into commandline */
10763 char_u *srcstart, /* beginning of valid memory for src */
10764 int *usedlen, /* characters after src that are used */
10765 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010766 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010767 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010768 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769{
10770 int i;
10771 char_u *s;
10772 char_u *result;
10773 char_u *resultbuf = NULL;
10774 int resultlen;
10775 buf_T *buf;
10776 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10777 int spec_idx;
10778#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +020010779 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 int skip_mod = FALSE;
10781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783
10784 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010785 if (escaped != NULL)
10786 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787
10788 /*
10789 * Check if there is something to do.
10790 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010791 spec_idx = find_cmdline_var(src, usedlen);
10792 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 {
10794 *usedlen = 1;
10795 return NULL;
10796 }
10797
10798 /*
10799 * Skip when preceded with a backslash "\%" and "\#".
10800 * Note: In "\\%" the % is also not recognized!
10801 */
10802 if (src > srcstart && src[-1] == '\\')
10803 {
10804 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010805 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 return NULL;
10807 }
10808
10809 /*
10810 * word or WORD under cursor
10811 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010812 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10813 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010815 resultlen = find_ident_under_cursor(&result,
10816 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10817 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10818 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 if (resultlen == 0)
10820 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010821 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 return NULL;
10823 }
10824 }
10825
10826 /*
10827 * '#': Alternate file name
10828 * '%': Current file name
10829 * File name under the cursor
10830 * File name for autocommand
10831 * and following modifiers
10832 */
10833 else
10834 {
10835 switch (spec_idx)
10836 {
10837 case SPEC_PERC: /* '%': current file */
10838 if (curbuf->b_fname == NULL)
10839 {
10840 result = (char_u *)"";
10841 valid = 0; /* Must have ":p:h" to be valid */
10842 }
10843 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010846#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010847 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010848#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 break;
10851
10852 case SPEC_HASH: /* '#' or "#99": alternate file */
10853 if (src[1] == '#') /* "##": the argument list */
10854 {
10855 result = arg_all();
10856 resultbuf = result;
10857 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010858 if (escaped != NULL)
10859 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860#ifdef FEAT_MODIFY_FNAME
10861 skip_mod = TRUE;
10862#endif
10863 break;
10864 }
10865 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010866 if (*s == '<') /* "#<99" uses v:oldfiles */
10867 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010869 if (s == src + 2 && src[1] == '-')
10870 /* just a minus sign, don't skip over it */
10871 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 *usedlen = (int)(s - src); /* length of what we expand */
10873
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010874 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010876 if (*usedlen < 2)
10877 {
10878 /* Should we give an error message for #<text? */
10879 *usedlen = 1;
10880 return NULL;
10881 }
10882#ifdef FEAT_EVAL
10883 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10884 (long)i);
10885 if (result == NULL)
10886 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010887 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +000010888 return NULL;
10889 }
10890#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +010010891 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 }
10895 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010896 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010897 if (i == 0 && src[1] == '<' && *usedlen > 1)
10898 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010899 buf = buflist_findnr(i);
10900 if (buf == NULL)
10901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010902 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +000010903 return NULL;
10904 }
10905 if (lnump != NULL)
10906 *lnump = ECMD_LAST;
10907 if (buf->b_fname == NULL)
10908 {
10909 result = (char_u *)"";
10910 valid = 0; /* Must have ":p:h" to be valid */
10911 }
10912 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010913 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010914 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010915#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010916 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010917#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010918 }
Bram Moolenaard812df62008-11-09 12:46:09 +000010919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 break;
10921
10922#ifdef FEAT_SEARCHPATH
10923 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010924 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 if (result == NULL)
10926 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010927 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 return NULL;
10929 }
10930 resultbuf = result; /* remember allocated string */
10931 break;
10932#endif
10933
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 case SPEC_AFILE: /* file name for autocommand */
10935 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010936 if (result != NULL && !autocmd_fname_full)
10937 {
10938 /* Still need to turn the fname into a full path. It is
10939 * postponed to avoid a delay when <afile> is not used. */
10940 autocmd_fname_full = TRUE;
10941 result = FullName_save(autocmd_fname, FALSE);
10942 vim_free(autocmd_fname);
10943 autocmd_fname = result;
10944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 if (result == NULL)
10946 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010947 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 return NULL;
10949 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010950 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 break;
10952
10953 case SPEC_ABUF: /* buffer number for autocommand */
10954 if (autocmd_bufnr <= 0)
10955 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010956 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 return NULL;
10958 }
10959 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10960 result = strbuf;
10961 break;
10962
10963 case SPEC_AMATCH: /* match name for autocommand */
10964 result = autocmd_match;
10965 if (result == NULL)
10966 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010967 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 return NULL;
10969 }
10970 break;
10971
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 case SPEC_SFILE: /* file name for ":so" command */
10973 result = sourcing_name;
10974 if (result == NULL)
10975 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010976 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 return NULL;
10978 }
10979 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010980
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010981 case SPEC_SLNUM: /* line in file for ":so" command */
10982 if (sourcing_name == NULL || sourcing_lnum == 0)
10983 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010984 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010985 return NULL;
10986 }
10987 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10988 result = strbuf;
10989 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010990
10991#ifdef FEAT_EVAL
10992 case SPEC_SFLNUM: /* line in script file */
10993 if (current_sctx.sc_lnum + sourcing_lnum == 0)
10994 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010995 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010996 return NULL;
10997 }
10998 sprintf((char *)strbuf, "%ld",
10999 (long)(current_sctx.sc_lnum + sourcing_lnum));
11000 result = strbuf;
11001 break;
11002#endif
11003
11004#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011006 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
11007 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 result = strbuf;
11009 break;
11010#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011011
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020011012 default:
11013 result = (char_u *)""; /* avoid gcc warning */
11014 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 }
11016
11017 resultlen = (int)STRLEN(result); /* length of new string */
11018 if (src[*usedlen] == '<') /* remove the file name extension */
11019 {
11020 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 resultlen = (int)(s - result);
11023 }
11024#ifdef FEAT_MODIFY_FNAME
11025 else if (!skip_mod)
11026 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020011027 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 &resultlen);
11029 if (result == NULL)
11030 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011031 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 return NULL;
11033 }
11034 }
11035#endif
11036 }
11037
11038 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
11039 {
11040 if (valid != VALID_HEAD + VALID_PATH)
11041 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011042 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011044 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 result = NULL;
11046 }
11047 else
11048 result = vim_strnsave(result, resultlen);
11049 vim_free(resultbuf);
11050 return result;
11051}
11052
11053/*
11054 * Concatenate all files in the argument list, separated by spaces, and return
11055 * it in one allocated string.
11056 * Spaces and backslashes in the file names are escaped with a backslash.
11057 * Returns NULL when out of memory.
11058 */
11059 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011060arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061{
11062 int len;
11063 int idx;
11064 char_u *retval = NULL;
11065 char_u *p;
11066
11067 /*
11068 * Do this loop two times:
11069 * first time: compute the total length
11070 * second time: concatenate the names
11071 */
11072 for (;;)
11073 {
11074 len = 0;
11075 for (idx = 0; idx < ARGCOUNT; ++idx)
11076 {
11077 p = alist_name(&ARGLIST[idx]);
11078 if (p != NULL)
11079 {
11080 if (len > 0)
11081 {
11082 /* insert a space in between names */
11083 if (retval != NULL)
11084 retval[len] = ' ';
11085 ++len;
11086 }
11087 for ( ; *p != NUL; ++p)
11088 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011089 if (*p == ' '
11090#ifndef BACKSLASH_IN_FILENAME
11091 || *p == '\\'
11092#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +020011093 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 {
11095 /* insert a backslash */
11096 if (retval != NULL)
11097 retval[len] = '\\';
11098 ++len;
11099 }
11100 if (retval != NULL)
11101 retval[len] = *p;
11102 ++len;
11103 }
11104 }
11105 }
11106
11107 /* second time: break here */
11108 if (retval != NULL)
11109 {
11110 retval[len] = NUL;
11111 break;
11112 }
11113
11114 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011115 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116 if (retval == NULL)
11117 break;
11118 }
11119
11120 return retval;
11121}
11122
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123/*
11124 * Expand the <sfile> string in "arg".
11125 *
11126 * Returns an allocated string, or NULL for any error.
11127 */
11128 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011129expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011131 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132 int len;
11133 char_u *result;
11134 char_u *newres;
11135 char_u *repl;
11136 int srclen;
11137 char_u *p;
11138
11139 result = vim_strsave(arg);
11140 if (result == NULL)
11141 return NULL;
11142
11143 for (p = result; *p; )
11144 {
11145 if (STRNCMP(p, "<sfile>", 7) != 0)
11146 ++p;
11147 else
11148 {
11149 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011150 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 if (errormsg != NULL)
11152 {
11153 if (*errormsg)
11154 emsg(errormsg);
11155 vim_free(result);
11156 return NULL;
11157 }
11158 if (repl == NULL) /* no match (cannot happen) */
11159 {
11160 p += srclen;
11161 continue;
11162 }
11163 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11164 newres = alloc(len);
11165 if (newres == NULL)
11166 {
11167 vim_free(repl);
11168 vim_free(result);
11169 return NULL;
11170 }
11171 mch_memmove(newres, result, (size_t)(p - result));
11172 STRCPY(newres + (p - result), repl);
11173 len = (int)STRLEN(newres);
11174 STRCAT(newres, p + srclen);
11175 vim_free(repl);
11176 vim_free(result);
11177 result = newres;
11178 p = newres + len; /* continue after the match */
11179 }
11180 }
11181
11182 return result;
11183}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184
11185#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011186static int ses_winsizes(FILE *fd, int restore_size,
11187 win_T *tab_firstwin);
11188static int ses_win_rec(FILE *fd, frame_T *fr);
11189static frame_T *ses_skipframe(frame_T *fr);
11190static int ses_do_frame(frame_T *fr);
11191static int ses_do_win(win_T *wp);
11192static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11193static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011194static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195
11196/*
11197 * Write openfile commands for the current buffers to an .exrc file.
11198 * Return FAIL on error, OK otherwise.
11199 */
11200 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011201makeopens(
11202 FILE *fd,
11203 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204{
11205 buf_T *buf;
11206 int only_save_windows = TRUE;
11207 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 int restore_size = TRUE;
11209 win_T *wp;
11210 char_u *sname;
11211 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011212 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011213 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011214 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011215 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011216 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011217 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218
11219 if (ssop_flags & SSOP_BUFFERS)
11220 only_save_windows = FALSE; /* Save ALL buffers */
11221
11222 /*
11223 * Begin by setting the this_session variable, and then other
11224 * sessionable variables.
11225 */
11226#ifdef FEAT_EVAL
11227 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11228 return FAIL;
11229 if (ssop_flags & SSOP_GLOBALS)
11230 if (store_session_globals(fd) == FAIL)
11231 return FAIL;
11232#endif
11233
11234 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011235 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236 */
11237 if (put_line(fd, "silent only") == FAIL)
11238 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011239 if ((ssop_flags & SSOP_TABPAGES)
11240 && put_line(fd, "silent tabonly") == FAIL)
11241 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242
11243 /*
11244 * Now a :cd command to the session directory or the current directory
11245 */
11246 if (ssop_flags & SSOP_SESDIR)
11247 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011248 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11249 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 return FAIL;
11251 }
11252 else if (ssop_flags & SSOP_CURDIR)
11253 {
11254 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11255 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011256 || fputs("cd ", fd) < 0
11257 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11258 || put_eol(fd) == FAIL)
11259 {
11260 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 vim_free(sname);
11264 }
11265
11266 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011267 * If there is an empty, unnamed buffer we will wipe it out later.
11268 * Remember the buffer number.
11269 */
11270 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11271 return FAIL;
11272 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11273 return FAIL;
11274 if (put_line(fd, "endif") == FAIL)
11275 return FAIL;
11276
11277 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278 * Now save the current files, current buffer first.
11279 */
11280 if (put_line(fd, "set shortmess=aoO") == FAIL)
11281 return FAIL;
11282
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011284 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11286 return FAIL;
11287
11288 if (ssop_flags & SSOP_RESIZE)
11289 {
11290 /* Note: after the restore we still check it worked!*/
11291 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11292 || put_eol(fd) == FAIL)
11293 return FAIL;
11294 }
11295
11296#ifdef FEAT_GUI
11297 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11298 {
11299 int x, y;
11300
11301 if (gui_mch_get_winpos(&x, &y) == OK)
11302 {
11303 /* Note: after the restore we still check it worked!*/
11304 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11305 return FAIL;
11306 }
11307 }
11308#endif
11309
11310 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011311 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11312 * will be displayed when creating the next tab. That resizes the windows
11313 * in the first tab, which may cause problems. Set 'showtabline' to 2
11314 * temporarily to avoid that.
11315 */
11316 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11317 {
11318 if (put_line(fd, "set stal=2") == FAIL)
11319 return FAIL;
11320 restore_stal = TRUE;
11321 }
11322
11323 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011324 * May repeat putting Windows for each tab, when "tabpages" is in
11325 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011326 * Don't use goto_tabpage(), it may change directory and trigger
11327 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011329 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011330 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011331 if ((ssop_flags & SSOP_TABPAGES))
11332 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011333 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011334
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011335 // Similar to ses_win_rec() below, populate the tab pages first so
11336 // later local options won't be copied to the new tabs.
11337 FOR_ALL_TABPAGES(tp)
11338 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011339 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011340 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011341 return FAIL;
11342 }
Bram Moolenaar18144c82006-04-12 21:52:12 +000011343 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011345 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +020011346 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011347
Bram Moolenaar18144c82006-04-12 21:52:12 +000011348 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011350 tabpage_T *tp = find_tabpage(tabnr);
11351
11352 if (tp == NULL)
11353 break; /* done all tab pages */
11354 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011355 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011356 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011357 tab_topframe = topframe;
11358 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011359 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011360 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011361 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011362 tab_topframe = tp->tp_topframe;
11363 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011364 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011365 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367
Bram Moolenaar18144c82006-04-12 21:52:12 +000011368 /*
11369 * Before creating the window layout, try loading one file. If this
11370 * is aborted we don't end up with a number of useless windows.
11371 * This may have side effects! (e.g., compressed or network file).
11372 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011373 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011374 {
11375 if (ses_do_win(wp)
11376 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011377 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011378#ifdef FEAT_QUICKFIX
11379 && !bt_nofile(wp->w_buffer)
11380#endif
11381 )
11382 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011383 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
11384 return FAIL;
11385 need_tabnext = FALSE;
11386
11387 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011388 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11389 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011390 return FAIL;
11391 if (!wp->w_arg_idx_invalid)
11392 edited_win = wp;
11393 break;
11394 }
11395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011397 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011398 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011399 return FAIL;
11400
Bram Moolenaar18144c82006-04-12 21:52:12 +000011401 /*
11402 * Save current window layout.
11403 */
11404 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011406 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011408 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11409 return FAIL;
11410 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11411 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412
Bram Moolenaar18144c82006-04-12 21:52:12 +000011413 /*
11414 * Check if window sizes can be restored (no windows omitted).
11415 * Remember the window number of the current window after restoring.
11416 */
11417 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011418 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011419 {
11420 if (ses_do_win(wp))
11421 ++nr;
11422 else
11423 restore_size = FALSE;
11424 if (curwin == wp)
11425 cnr = nr;
11426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427
Bram Moolenaar18144c82006-04-12 21:52:12 +000011428 /* Go to the first window. */
11429 if (put_line(fd, "wincmd t") == FAIL)
11430 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011431
Bram Moolenaar18144c82006-04-12 21:52:12 +000011432 /*
11433 * If more than one window, see if sizes can be restored.
11434 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11435 * resized when moving between windows.
11436 * Do this before restoring the view, so that the topline and the
11437 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011438 * winminheight and winminwidth need to be set to avoid an error if the
11439 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011440 */
Bram Moolenaar19834012018-06-12 17:03:39 +020011441 if (put_line(fd, "set winminheight=0") == FAIL
11442 || put_line(fd, "set winheight=1") == FAIL
11443 || put_line(fd, "set winminwidth=0") == FAIL
11444 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011445 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011446 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011447 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448
Bram Moolenaar18144c82006-04-12 21:52:12 +000011449 /*
11450 * Restore the view of the window (options, file, cursor, etc.).
11451 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011452 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011453 {
11454 if (!ses_do_win(wp))
11455 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011456 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11457 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011458 return FAIL;
11459 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11460 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011461 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011462 }
11463
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011464 /* The argument index in the first tab page is zero, need to set it in
11465 * each window. For further tab pages it's the window where we do
11466 * "tabedit". */
11467 cur_arg_idx = next_arg_idx;
11468
Bram Moolenaar18144c82006-04-12 21:52:12 +000011469 /*
11470 * Restore cursor to the current window if it's not the first one.
11471 */
11472 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11473 || put_eol(fd) == FAIL))
11474 return FAIL;
11475
11476 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011477 * Restore window sizes again after jumping around in windows, because
11478 * the current window has a minimum size while others may not.
11479 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011480 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011481 return FAIL;
11482
Bram Moolenaar18144c82006-04-12 21:52:12 +000011483 /* Don't continue in another tab page when doing only the current one
11484 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011485 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011486 break;
11487 }
11488
11489 if (ssop_flags & SSOP_TABPAGES)
11490 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011491 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11492 || put_eol(fd) == FAIL)
11493 return FAIL;
11494 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011495 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11496 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011497
Bram Moolenaard39e2752019-01-26 20:07:38 +010011498 // Now put the remaining buffers into the buffer list.
11499 // This is near the end, so that when 'hidden' is set we don't create extra
11500 // buffers. If the buffer was already created with another command the
11501 // ":badd" will have no effect.
11502 FOR_ALL_BUFFERS(buf)
11503 {
11504 if (!(only_save_windows && buf->b_nwindows == 0)
11505 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11506#ifdef FEAT_TERMINAL
11507 // Skip terminal buffers: finished ones are not useful, others
11508 // will be resurrected and result in a new buffer.
11509 && !bt_terminal(buf)
11510#endif
11511 && buf->b_fname != NULL
11512 && buf->b_p_bl)
11513 {
11514 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11515 : buf->b_wininfo->wi_fpos.lnum) < 0
11516 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
11517 return FAIL;
11518 }
11519 }
11520
Bram Moolenaar9c102382006-05-03 21:26:49 +000011521 /*
11522 * Wipe out an empty unnamed buffer we started in.
11523 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011524 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011525 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011526 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011527 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011528 return FAIL;
11529 if (put_line(fd, "endif") == FAIL)
11530 return FAIL;
11531 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11532 return FAIL;
11533
11534 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11535 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11536 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11537 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011538 /* Re-apply 'winminheight' and 'winminwidth'. */
11539 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11540 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11541 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542
11543 /*
11544 * Lastly, execute the x.vim file if it exists.
11545 */
11546 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11547 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011548 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549 || put_line(fd, "endif") == FAIL)
11550 return FAIL;
11551
11552 return OK;
11553}
11554
11555 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011556ses_winsizes(
11557 FILE *fd,
11558 int restore_size,
11559 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560{
11561 int n = 0;
11562 win_T *wp;
11563
11564 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11565 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011566 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567 {
11568 if (!ses_do_win(wp))
11569 continue;
11570 ++n;
11571
11572 /* restore height when not full height */
11573 if (wp->w_height + wp->w_status_height < topframe->fr_height
11574 && (fprintf(fd,
11575 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11576 n, (long)wp->w_height, Rows / 2, Rows) < 0
11577 || put_eol(fd) == FAIL))
11578 return FAIL;
11579
11580 /* restore width when not full width */
11581 if (wp->w_width < Columns && (fprintf(fd,
11582 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11583 n, (long)wp->w_width, Columns / 2, Columns) < 0
11584 || put_eol(fd) == FAIL))
11585 return FAIL;
11586 }
11587 }
11588 else
11589 {
11590 /* Just equalise window sizes */
11591 if (put_line(fd, "wincmd =") == FAIL)
11592 return FAIL;
11593 }
11594 return OK;
11595}
11596
11597/*
11598 * Write commands to "fd" to recursively create windows for frame "fr",
11599 * horizontally and vertically split.
11600 * After the commands the last window in the frame is the current window.
11601 * Returns FAIL when writing the commands to "fd" fails.
11602 */
11603 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011604ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605{
11606 frame_T *frc;
11607 int count = 0;
11608
11609 if (fr->fr_layout != FR_LEAF)
11610 {
11611 /* Find first frame that's not skipped and then create a window for
11612 * each following one (first frame is already there). */
11613 frc = ses_skipframe(fr->fr_child);
11614 if (frc != NULL)
11615 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11616 {
11617 /* Make window as big as possible so that we have lots of room
11618 * to split. */
11619 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11620 || put_line(fd, fr->fr_layout == FR_COL
11621 ? "split" : "vsplit") == FAIL)
11622 return FAIL;
11623 ++count;
11624 }
11625
11626 /* Go back to the first window. */
11627 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11628 ? "%dwincmd k" : "%dwincmd h", count) < 0
11629 || put_eol(fd) == FAIL))
11630 return FAIL;
11631
11632 /* Recursively create frames/windows in each window of this column or
11633 * row. */
11634 frc = ses_skipframe(fr->fr_child);
11635 while (frc != NULL)
11636 {
11637 ses_win_rec(fd, frc);
11638 frc = ses_skipframe(frc->fr_next);
11639 /* Go to next window. */
11640 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11641 return FAIL;
11642 }
11643 }
11644 return OK;
11645}
11646
11647/*
11648 * Skip frames that don't contain windows we want to save in the Session.
11649 * Returns NULL when there none.
11650 */
11651 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011652ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653{
11654 frame_T *frc;
11655
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011656 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657 if (ses_do_frame(frc))
11658 break;
11659 return frc;
11660}
11661
11662/*
11663 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11664 * the Session.
11665 */
11666 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011667ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668{
11669 frame_T *frc;
11670
11671 if (fr->fr_layout == FR_LEAF)
11672 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011673 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674 if (ses_do_frame(frc))
11675 return TRUE;
11676 return FALSE;
11677}
11678
11679/*
11680 * Return non-zero if window "wp" is to be stored in the Session.
11681 */
11682 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011683ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011685#ifdef FEAT_TERMINAL
11686 if (bt_terminal(wp->w_buffer))
11687 return !term_is_finished(wp->w_buffer)
11688 && (ssop_flags & SSOP_TERMINAL)
11689 && term_should_restore(wp->w_buffer);
11690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691 if (wp->w_buffer->b_fname == NULL
11692#ifdef FEAT_QUICKFIX
11693 /* When 'buftype' is "nofile" can't restore the window contents. */
11694 || bt_nofile(wp->w_buffer)
11695#endif
11696 )
11697 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011698 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699 return (ssop_flags & SSOP_HELP);
11700 return TRUE;
11701}
11702
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011703 static int
11704put_view_curpos(FILE *fd, win_T *wp, char *spaces)
11705{
11706 int r;
11707
11708 if (wp->w_curswant == MAXCOL)
11709 r = fprintf(fd, "%snormal! $", spaces);
11710 else
11711 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
11712 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
11713}
11714
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715/*
11716 * Write commands to "fd" to restore the view of a window.
11717 * Caller must make sure 'scrolloff' is zero.
11718 */
11719 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011720put_view(
11721 FILE *fd,
11722 win_T *wp,
11723 int add_edit, /* add ":edit" command to view */
11724 unsigned *flagp, /* vop_flags or ssop_flags */
11725 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011726 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727{
11728 win_T *save_curwin;
11729 int f;
11730 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011731 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732
11733 /* Always restore cursor position for ":mksession". For ":mkview" only
11734 * when 'viewoptions' contains "cursor". */
11735 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11736
11737 /*
11738 * Local argument list.
11739 */
11740 if (wp->w_alist == &global_alist)
11741 {
11742 if (put_line(fd, "argglobal") == FAIL)
11743 return FAIL;
11744 }
11745 else
11746 {
11747 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11748 flagp == &vop_flags
11749 || !(*flagp & SSOP_CURDIR)
11750 || wp->w_localdir != NULL, flagp) == FAIL)
11751 return FAIL;
11752 }
11753
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011754 /* Only when part of a session: restore the argument index. Some
11755 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011756 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011757 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011759 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760 || put_eol(fd) == FAIL)
11761 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011762 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763 }
11764
11765 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011766 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011768# ifdef FEAT_TERMINAL
11769 if (bt_terminal(wp->w_buffer))
11770 {
11771 if (term_write_session(fd, wp) == FAIL)
11772 return FAIL;
11773 }
11774 else
11775# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776 /*
11777 * Load the file.
11778 */
11779 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011780# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783 )
11784 {
11785 /*
11786 * Editing a file in this buffer: use ":edit file".
11787 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011788 *
11789 * Note, if a buffer for that file already exists, use :badd to
11790 * edit that buffer, to not lose folding information (:edit resets
11791 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792 */
Bram Moolenaarad36a352019-01-24 13:34:42 +010011793 if (fputs("if bufexists(\"", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011794 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
Bram Moolenaarad36a352019-01-24 13:34:42 +010011795 || fputs("\") | buffer ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011796 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11797 || fputs(" | else | edit ", fd) < 0
11798 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11799 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011800 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801 return FAIL;
11802 }
11803 else
11804 {
11805 /* No file in this buffer, just make it empty. */
11806 if (put_line(fd, "enew") == FAIL)
11807 return FAIL;
11808#ifdef FEAT_QUICKFIX
11809 if (wp->w_buffer->b_ffname != NULL)
11810 {
11811 /* The buffer does have a name, but it's not a file name. */
11812 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011813 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814 return FAIL;
11815 }
11816#endif
11817 do_cursor = FALSE;
11818 }
11819 }
11820
11821 /*
11822 * Local mappings and abbreviations.
11823 */
11824 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11825 && makemap(fd, wp->w_buffer) == FAIL)
11826 return FAIL;
11827
11828 /*
11829 * Local options. Need to go to the window temporarily.
11830 * Store only local values when using ":mkview" and when ":mksession" is
11831 * used and 'sessionoptions' doesn't include "options".
11832 * Some folding options are always stored when "folds" is included,
11833 * otherwise the folds would not be restored correctly.
11834 */
11835 save_curwin = curwin;
11836 curwin = wp;
11837 curbuf = curwin->w_buffer;
11838 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11839 f = makeset(fd, OPT_LOCAL,
11840 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11841#ifdef FEAT_FOLDING
11842 else if (*flagp & SSOP_FOLDS)
11843 f = makefoldset(fd);
11844#endif
11845 else
11846 f = OK;
11847 curwin = save_curwin;
11848 curbuf = curwin->w_buffer;
11849 if (f == FAIL)
11850 return FAIL;
11851
11852#ifdef FEAT_FOLDING
11853 /*
11854 * Save Folds when 'buftype' is empty and for help files.
11855 */
11856 if ((*flagp & SSOP_FOLDS)
11857 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020011858 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859 {
11860 if (put_folds(fd, wp) == FAIL)
11861 return FAIL;
11862 }
11863#endif
11864
11865 /*
11866 * Set the cursor after creating folds, since that moves the cursor.
11867 */
11868 if (do_cursor)
11869 {
11870
11871 /* Restore the cursor line in the file and relatively in the
11872 * window. Don't use "G", it changes the jumplist. */
11873 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11874 (long)wp->w_cursor.lnum,
11875 (long)(wp->w_cursor.lnum - wp->w_topline),
11876 (long)wp->w_height / 2, (long)wp->w_height) < 0
11877 || put_eol(fd) == FAIL
11878 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11879 || put_line(fd, "exe s:l") == FAIL
11880 || put_line(fd, "normal! zt") == FAIL
11881 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11882 || put_eol(fd) == FAIL)
11883 return FAIL;
11884 /* Restore the cursor column and left offset when not wrapping. */
11885 if (wp->w_cursor.col == 0)
11886 {
11887 if (put_line(fd, "normal! 0") == FAIL)
11888 return FAIL;
11889 }
11890 else
11891 {
11892 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11893 {
11894 if (fprintf(fd,
11895 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011896 (long)wp->w_virtcol + 1,
11897 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898 (long)wp->w_width / 2, (long)wp->w_width) < 0
11899 || put_eol(fd) == FAIL
11900 || put_line(fd, "if s:c > 0") == FAIL
11901 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011902 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11903 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904 || put_eol(fd) == FAIL
11905 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011906 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907 || put_line(fd, "endif") == FAIL)
11908 return FAIL;
11909 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011910 else if (put_view_curpos(fd, wp, "") == FAIL)
11911 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912 }
11913 }
11914
11915 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011916 * Local directory, if the current flag is not view options or the "curdir"
11917 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011919 if (wp->w_localdir != NULL
11920 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 {
11922 if (fputs("lcd ", fd) < 0
11923 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11924 || put_eol(fd) == FAIL)
11925 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011926 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 }
11928
11929 return OK;
11930}
11931
11932/*
11933 * Write an argument list to the session file.
11934 * Returns FAIL if writing fails.
11935 */
11936 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011937ses_arglist(
11938 FILE *fd,
11939 char *cmd,
11940 garray_T *gap,
11941 int fullname, /* TRUE: use full path name */
11942 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943{
11944 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011945 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946 char_u *s;
11947
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011948 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11949 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010011950 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951 return FAIL;
11952 for (i = 0; i < gap->ga_len; ++i)
11953 {
11954 /* NULL file names are skipped (only happens when out of memory). */
11955 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11956 if (s != NULL)
11957 {
11958 if (fullname)
11959 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011960 buf = alloc(MAXPATHL);
11961 if (buf != NULL)
11962 {
11963 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11964 s = buf;
11965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011967 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011968 || ses_put_fname(fd, s, flagp) == FAIL
11969 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011970 {
11971 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011973 }
11974 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 }
11976 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011977 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978}
11979
11980/*
11981 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011982 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983 * Returns FAIL if writing fails.
11984 */
11985 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011986ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987{
11988 char_u *name;
11989
11990 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011991 * the session file will be sourced.
11992 * Don't do this for ":mkview", we don't know the current directory.
11993 * Don't do this after ":lcd", we don't keep track of what the current
11994 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995 if (buf->b_sfname != NULL
11996 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011997 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011998#ifdef FEAT_AUTOCHDIR
11999 && !p_acd
12000#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012001 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002 name = buf->b_sfname;
12003 else
12004 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012005 if (ses_put_fname(fd, name, flagp) == FAIL
12006 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007 return FAIL;
12008 return OK;
12009}
12010
12011/*
12012 * Write a file name to the session file.
12013 * Takes care of the "slash" option in 'sessionoptions' and escapes special
12014 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012015 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016 */
12017 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012018ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019{
12020 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012021 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023
12024 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012025 if (sname == NULL)
12026 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012028 if (*flagp & SSOP_SLASH)
12029 {
12030 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010012031 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012032 if (*p == '\\')
12033 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012035
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020012036 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012037 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012039 if (p == NULL)
12040 return FAIL;
12041
12042 /* write the result */
12043 if (fputs((char *)p, fd) < 0)
12044 retval = FAIL;
12045
12046 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047 return retval;
12048}
12049
12050/*
12051 * ":loadview [nr]"
12052 */
12053 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012054ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055{
12056 char_u *fname;
12057
12058 fname = get_view_file(*eap->arg);
12059 if (fname != NULL)
12060 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012061 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062 vim_free(fname);
12063 }
12064}
12065
12066/*
12067 * Get the name of the view file for the current buffer.
12068 */
12069 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012070get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071{
12072 int len = 0;
12073 char_u *p, *s;
12074 char_u *retval;
12075 char_u *sname;
12076
12077 if (curbuf->b_ffname == NULL)
12078 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012079 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080 return NULL;
12081 }
12082 sname = home_replace_save(NULL, curbuf->b_ffname);
12083 if (sname == NULL)
12084 return NULL;
12085
12086 /*
12087 * We want a file name without separators, because we're not going to make
12088 * a directory.
12089 * "normal" path separator -> "=+"
12090 * "=" -> "=="
12091 * ":" path separator -> "=-"
12092 */
12093 for (p = sname; *p; ++p)
12094 if (*p == '=' || vim_ispathsep(*p))
12095 ++len;
12096 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
12097 if (retval != NULL)
12098 {
12099 STRCPY(retval, p_vdir);
12100 add_pathsep(retval);
12101 s = retval + STRLEN(retval);
12102 for (p = sname; *p; ++p)
12103 {
12104 if (*p == '=')
12105 {
12106 *s++ = '=';
12107 *s++ = '=';
12108 }
12109 else if (vim_ispathsep(*p))
12110 {
12111 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020012112#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113 if (*p == ':')
12114 *s++ = '-';
12115 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012117 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118 }
12119 else
12120 *s++ = *p;
12121 }
12122 *s++ = '=';
12123 *s++ = c;
12124 STRCPY(s, ".vim");
12125 }
12126
12127 vim_free(sname);
12128 return retval;
12129}
12130
12131#endif /* FEAT_SESSION */
12132
12133/*
12134 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12135 * Return FAIL for a write error.
12136 */
12137 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012138put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139{
12140 if (
12141#ifdef USE_CRNL
12142 (
12143# ifdef MKSESSION_NL
12144 !mksession_nl &&
12145# endif
12146 (putc('\r', fd) < 0)) ||
12147#endif
12148 (putc('\n', fd) < 0))
12149 return FAIL;
12150 return OK;
12151}
12152
12153/*
12154 * Write a line to "fd".
12155 * Return FAIL for a write error.
12156 */
12157 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012158put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159{
12160 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12161 return FAIL;
12162 return OK;
12163}
12164
12165#ifdef FEAT_VIMINFO
12166/*
12167 * ":rviminfo" and ":wviminfo".
12168 */
12169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012170ex_viminfo(
12171 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172{
12173 char_u *save_viminfo;
12174
12175 save_viminfo = p_viminfo;
12176 if (*p_viminfo == NUL)
12177 p_viminfo = (char_u *)"'100";
12178 if (eap->cmdidx == CMD_rviminfo)
12179 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012180 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12181 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012182 emsg(_("E195: Cannot open viminfo file for reading"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183 }
12184 else
12185 write_viminfo(eap->arg, eap->forceit);
12186 p_viminfo = save_viminfo;
12187}
12188#endif
12189
12190#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012191/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012192 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012193 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012196dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198 if (fname == NULL)
12199 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012200 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201}
12202#endif
12203
12204/*
12205 * ":behave {mswin,xterm}"
12206 */
12207 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012208ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209{
12210 if (STRCMP(eap->arg, "mswin") == 0)
12211 {
12212 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12213 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12214 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12215 set_option_value((char_u *)"keymodel", 0L,
12216 (char_u *)"startsel,stopsel", 0);
12217 }
12218 else if (STRCMP(eap->arg, "xterm") == 0)
12219 {
12220 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12221 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12222 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12223 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12224 }
12225 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012226 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227}
12228
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012229#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12230/*
12231 * Function given to ExpandGeneric() to obtain the possible arguments of the
12232 * ":behave {mswin,xterm}" command.
12233 */
12234 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012235get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012236{
12237 if (idx == 0)
12238 return (char_u *)"mswin";
12239 if (idx == 1)
12240 return (char_u *)"xterm";
12241 return NULL;
12242}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012243
12244/*
12245 * Function given to ExpandGeneric() to obtain the possible arguments of the
12246 * ":messages {clear}" command.
12247 */
12248 char_u *
12249get_messages_arg(expand_T *xp UNUSED, int idx)
12250{
12251 if (idx == 0)
12252 return (char_u *)"clear";
12253 return NULL;
12254}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012255#endif
12256
Bram Moolenaar113e1072019-01-20 15:30:40 +010012257#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012258 char_u *
12259get_mapclear_arg(expand_T *xp UNUSED, int idx)
12260{
12261 if (idx == 0)
12262 return (char_u *)"<buffer>";
12263 return NULL;
12264}
Bram Moolenaar113e1072019-01-20 15:30:40 +010012265#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012266
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267static int filetype_detect = FALSE;
12268static int filetype_plugin = FALSE;
12269static int filetype_indent = FALSE;
12270
12271/*
12272 * ":filetype [plugin] [indent] {on,off,detect}"
12273 * on: Load the filetype.vim file to install autocommands for file types.
12274 * off: Load the ftoff.vim file to remove all autocommands for file types.
12275 * plugin on: load filetype.vim and ftplugin.vim
12276 * plugin off: load ftplugof.vim
12277 * indent on: load filetype.vim and indent.vim
12278 * indent off: load indoff.vim
12279 */
12280 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012281ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282{
12283 char_u *arg = eap->arg;
12284 int plugin = FALSE;
12285 int indent = FALSE;
12286
12287 if (*eap->arg == NUL)
12288 {
12289 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012290 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291 filetype_detect ? "ON" : "OFF",
12292 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12293 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12294 return;
12295 }
12296
12297 /* Accept "plugin" and "indent" in any order. */
12298 for (;;)
12299 {
12300 if (STRNCMP(arg, "plugin", 6) == 0)
12301 {
12302 plugin = TRUE;
12303 arg = skipwhite(arg + 6);
12304 continue;
12305 }
12306 if (STRNCMP(arg, "indent", 6) == 0)
12307 {
12308 indent = TRUE;
12309 arg = skipwhite(arg + 6);
12310 continue;
12311 }
12312 break;
12313 }
12314 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12315 {
12316 if (*arg == 'o' || !filetype_detect)
12317 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012318 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012319 filetype_detect = TRUE;
12320 if (plugin)
12321 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012322 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323 filetype_plugin = TRUE;
12324 }
12325 if (indent)
12326 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012327 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328 filetype_indent = TRUE;
12329 }
12330 }
12331 if (*arg == 'd')
12332 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012333 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012334 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335 }
12336 }
12337 else if (STRCMP(arg, "off") == 0)
12338 {
12339 if (plugin || indent)
12340 {
12341 if (plugin)
12342 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012343 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 filetype_plugin = FALSE;
12345 }
12346 if (indent)
12347 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012348 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349 filetype_indent = FALSE;
12350 }
12351 }
12352 else
12353 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012354 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355 filetype_detect = FALSE;
12356 }
12357 }
12358 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012359 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360}
12361
12362/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012363 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364 */
12365 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012366ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367{
12368 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012369 {
12370 char_u *arg = eap->arg;
12371
12372 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12373 arg += 9;
12374
12375 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12376 if (arg != eap->arg)
12377 did_filetype = FALSE;
12378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012382ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383{
12384#ifdef FEAT_DIGRAPHS
12385 if (*eap->arg != NUL)
12386 putdigraph(eap->arg);
12387 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010012388 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012390 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012391#endif
12392}
12393
12394 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012395ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396{
12397 int flags = 0;
12398
12399 if (eap->cmdidx == CMD_setlocal)
12400 flags = OPT_LOCAL;
12401 else if (eap->cmdidx == CMD_setglobal)
12402 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012403#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404 if (cmdmod.browse && flags == 0)
12405 ex_options(eap);
12406 else
12407#endif
12408 (void)do_set(eap->arg, flags);
12409}
12410
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012411#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
12412 void
12413set_no_hlsearch(int flag)
12414{
12415 no_hlsearch = flag;
12416# ifdef FEAT_EVAL
12417 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
12418# endif
12419}
12420
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421/*
12422 * ":nohlsearch"
12423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012425ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012427 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012428 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429}
12430
12431/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012432 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433 * Sets nextcmd to the start of the next command, if any. Also called when
12434 * skipping commands to find the next command.
12435 */
12436 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012437ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438{
12439 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012440 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441 char_u *end;
12442 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012443 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012444
12445 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012446 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012447 else
12448 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012449 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012450 return;
12451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012452
12453 /* First clear any old pattern. */
12454 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012455 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012456
12457 if (ends_excmd(*eap->arg))
12458 end = eap->arg;
12459 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012460 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461 end = eap->arg + 4;
12462 else
12463 {
12464 p = skiptowhite(eap->arg);
12465 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012466 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467 p = skipwhite(p);
12468 if (*p == NUL)
12469 {
12470 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012471 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012472 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473 return;
12474 }
12475 end = skip_regexp(p + 1, *p, TRUE, NULL);
12476 if (!eap->skip)
12477 {
12478 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12479 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012480 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481 eap->errmsg = e_trailing;
12482 return;
12483 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012484 if (*end != *p)
12485 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012486 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012487 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012488 return;
12489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490
12491 c = *end;
12492 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012493 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012494 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012495 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012496 }
12497 }
12498 eap->nextcmd = find_nextcmd(end);
12499}
12500#endif
12501
12502#ifdef FEAT_CRYPT
12503/*
12504 * ":X": Get crypt key
12505 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012507ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012509 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012510 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511}
12512#endif
12513
12514#ifdef FEAT_FOLDING
12515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012516ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012517{
12518 if (foldManualAllowed(TRUE))
12519 foldCreate(eap->line1, eap->line2);
12520}
12521
12522 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012523ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524{
12525 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12526 eap->forceit, FALSE);
12527}
12528
12529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012530ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012531{
12532 linenr_T lnum;
12533
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012534#ifdef FEAT_CLIPBOARD
12535 start_global_changes();
12536#endif
12537
Bram Moolenaar071d4272004-06-13 20:20:40 +000012538 /* First set the marks for all lines closed/open. */
12539 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12540 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12541 ml_setmarked(lnum);
12542
12543 /* Execute the command on the marked lines. */
12544 global_exe(eap->arg);
12545 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012546#ifdef FEAT_CLIPBOARD
12547 end_global_changes();
12548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012549}
12550#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012551
Bram Moolenaar39665952018-08-15 20:59:48 +020012552#ifdef FEAT_QUICKFIX
12553/*
12554 * Returns TRUE if the supplied Ex cmdidx is for a location list command
12555 * instead of a quickfix command.
12556 */
12557 int
12558is_loclist_cmd(int cmdidx)
12559{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020012560 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020012561 return FALSE;
12562 return cmdnames[cmdidx].cmd_name[0] == 'l';
12563}
12564#endif
12565
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012566# if defined(FEAT_TIMERS) || defined(PROTO)
12567 int
12568get_pressedreturn(void)
12569{
12570 return ex_pressedreturn;
12571}
12572
12573 void
12574set_pressedreturn(int val)
12575{
12576 ex_pressedreturn = val;
12577}
12578#endif